monochromatic-git-policy-cli 0.1.4

Repository-owned Git policy wrapper: forwards Git commands and checks commits against configured policies.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! What:
//!  Controls for the executable's linter:
//!  lookup,
//!  arguments,
//!  configuration file,
//!       bounds and every failure,
//!  with stand-in programs,
//!  and the real linter's contract.
//! Why:
//!  The stand-ins reach each branch quickly;
//!  the real linter,
//!  built from this
//!      repository and named by `GIT_POLICY_NATIVE_TEST_LINTER`,
//!  proves the contract the
//!      policy relies on.
//!  A test that needs it fails when the variable is unset,
//!  so an
//!      absent linter can never read as a pass.
//!
//! In TS you'd write (pseudocode):
//! ```ts
//! // const run = processLinter(env, limits, scratch).lint({ topLevel, options, path, source });
//! ```

/// The linter under test and its private parts.
use super::{
    LINTER_LIMITS, ProcessLinter, child_failure_reason, executable_linter, process_linter,
};
use crate::bounded_child::{ChildFailure, ChildLimits};
use crate::config_schema::{MarkdownAutofixOptions, PolicyConfig};
use crate::markdown_linter_output::{LintRun, RemainingFinding};
use crate::policy_markdown::{LintRequest, MarkdownLinter};
use crate::test_support::{executable, fixture, remove};
use std::ffi::OsString;
use std::os::unix::ffi::{OsStrExt, OsStringExt};
use std::path::{Path, PathBuf};
use std::time::Duration;

/// The variable naming the linter built from this repository,
///  set in the gate image.
const LINTER_VARIABLE: &str = "GIT_POLICY_NATIVE_TEST_LINTER";

/// The default options:
///  the LFS rule and no exclusion.
fn default_options() -> MarkdownAutofixOptions {
    return PolicyConfig::defaults().markdown_autofix;
}

/// An environment whose PATH is `bin` and system programs,
///  with `extra` pairs.
fn environment(bin: &Path, extra: &[(&str, &Path)]) -> Vec<(OsString, OsString)> {
    let mut path: OsString = bin.as_os_str().to_os_string();
    path.push(":/usr/bin:/bin");
    let mut pairs: Vec<(OsString, OsString)> = vec![(OsString::from("PATH"), path)];
    for (name, value) in extra {
        pairs.push((OsString::from(name), value.as_os_str().to_os_string()));
    }
    return pairs;
}

/// Write a stand-in named `monochromatic-lint` into `<root>/bin` and return that directory.
fn stand_in(root: &Path, script: &str) -> PathBuf {
    let bin: PathBuf = root.join("bin");
    std::fs::create_dir_all(&bin).expect("bin");
    executable(
        bin.join("monochromatic-lint").as_path(),
        format!("#!/bin/sh\n{script}\n").as_bytes(),
    );
    return bin;
}

/// Lint `source` as `path` from `top_level` with `linter` and the default options.
fn lint(
    linter: &mut ProcessLinter,
    top_level: &Path,
    path: &[u8],
    source: &[u8],
) -> Result<LintRun, String> {
    let options: MarkdownAutofixOptions = default_options();
    return linter.lint(&LintRequest {
        top_level,
        options: &options,
        path,
        source,
    });
}

/// The bounds of the executable's linter and its scratch directory.
#[test]
fn the_executable_linter_uses_the_measured_bounds() {
    assert_eq!(
        LINTER_LIMITS,
        ChildLimits {
            timeout: Duration::from_secs(60),
            max_stdout: 67_108_864,
            max_stderr: 4_194_304,
        }
    );
    let linter: ProcessLinter = executable_linter(&[]);
    assert_eq!(linter.limits, LINTER_LIMITS);
    assert!(linter.scratch.is_absolute(), "{:?}", linter.scratch);
    assert_eq!(linter.environment, Vec::<(OsString, OsString)>::new());
}

/// Without the linter on PATH every candidate fails with the same reason.
#[test]
fn a_missing_linter_is_reported() {
    let root: PathBuf = fixture("linter-missing");
    let reason: &str = "cli-git could not find monochromatic-lint in any absolute directory on PATH, and the policy markdown/autofix runs it for every Markdown candidate; install it so that it is on PATH";
    let mut unset: ProcessLinter = process_linter(&[], LINTER_LIMITS, root.clone());
    let mut empty: ProcessLinter = process_linter(
        environment(root.as_path(), &[]).as_slice(),
        LINTER_LIMITS,
        root.clone(),
    );
    for linter in [&mut unset, &mut empty] {
        for _ in 0..2 {
            assert_eq!(
                lint(linter, root.as_path(), b"a.md", b"a\n"),
                Err(String::from(reason))
            );
        }
    }
    remove(root.as_path());
}

/// Whether one byte ends a line,
///  for splitting the recorded arguments.
fn is_line_feed(byte: &u8) -> bool {
    return *byte == b'\n';
}

/// The stand-in records its arguments,
///  directory and configuration,
///  then echoes its input.
const RECORDING: &str = "printf '%s\\n' \"$@\" > \"$RECORD/arguments\"\npwd > \"$RECORD/directory\"\ncat \"${1#--config=}\" > \"$RECORD/configuration\"\ncat";

/// The linter is started from the top level with one `--name=value` argument per option,
/// the candidate's exact name and the one-rule configuration,
///  written once for the
/// invocation and removed with it;
///  its input comes back as the fixed source.
#[test]
fn the_linter_gets_the_contract_arguments() {
    let root: PathBuf = fixture("linter-arguments");
    let bin: PathBuf = stand_in(root.as_path(), RECORDING);
    let record: PathBuf = root.join("record");
    let top: PathBuf = root.join("top");
    std::fs::create_dir(&record).expect("record");
    std::fs::create_dir(&top).expect("top");
    let mut linter: ProcessLinter = process_linter(
        environment(bin.as_path(), &[("RECORD", record.as_path())]).as_slice(),
        LINTER_LIMITS,
        root.clone(),
    );
    let mut configurations: Vec<Vec<u8>> = Vec::new();
    for name in [&b"-x.md"[..], b"dir/caf\xe9 =.md"] {
        assert_eq!(
            lint(&mut linter, top.as_path(), name, b"source\n"),
            Ok(LintRun {
                fixed: b"source\n".to_vec(),
                remaining: Vec::new(),
            })
        );
        let arguments: Vec<u8> = std::fs::read(record.join("arguments")).expect("arguments");
        let lines: Vec<&[u8]> = arguments.split(is_line_feed).collect();
        assert_eq!(lines.len(), 5, "{lines:?}");
        configurations.push(lines[0].to_vec());
        assert_eq!(lines[1], b"--stdin");
        assert_eq!(
            lines[2],
            [&b"--stdin-filename="[..], name].concat().as_slice()
        );
        assert_eq!((lines[3], lines[4]), (&b"--fix"[..], &b""[..]));
        assert_eq!(
            std::fs::read_to_string(record.join("directory")).expect("directory"),
            format!("{}\n", top.display())
        );
    }
    assert_eq!(
        configurations[0], configurations[1],
        "one configuration per invocation"
    );
    let configuration: PathBuf = PathBuf::from(OsString::from_vec(
        configurations[0]["--config=".len()..].to_vec(),
    ));
    assert_eq!(configuration.parent(), Some(root.as_path()));
    assert_eq!(
        std::fs::read_to_string(record.join("configuration")).expect("copy"),
        crate::markdown_linter_config::one_rule_configuration(
            default_options().rules.as_slice(),
            &[]
        )
    );
    assert!(configuration.exists());
    drop(linter);
    assert!(
        !configuration.exists(),
        "the configuration leaves with the invocation"
    );
    remove(root.as_path());
}

/// The program found first is kept for the invocation:
///  once it is gone,
///  the next start
/// fails instead of searching again.
#[test]
fn the_program_is_looked_up_once() {
    let root: PathBuf = fixture("linter-once");
    let bin: PathBuf = stand_in(root.as_path(), "cat");
    let mut linter: ProcessLinter = process_linter(
        environment(bin.as_path(), &[]).as_slice(),
        LINTER_LIMITS,
        root.clone(),
    );
    assert!(lint(&mut linter, root.as_path(), b"a.md", b"a\n").is_ok());
    std::fs::remove_file(bin.join("monochromatic-lint")).expect("remove the program");
    let started: String = lint(&mut linter, root.as_path(), b"a.md", b"a\n").expect_err("gone");
    assert!(
        started.starts_with(
            format!(
                "cli-git could not start monochromatic-lint at {}/monochromatic-lint: ",
                bin.display()
            )
            .as_str()
        ),
        "{started}"
    );
    remove(root.as_path());
}

/// A run past its bound or its cap,
///  a run that could not finish,
///  a configuration that
/// cannot be written and a name that cannot be spelled each come back with their reason.
#[test]
fn each_failure_comes_back_with_its_reason() {
    let root: PathBuf = fixture("linter-failures");
    let small: ChildLimits = ChildLimits {
        timeout: Duration::from_millis(300),
        max_stdout: 10,
        max_stderr: 1 << 20,
    };
    let sleeper: PathBuf = stand_in(&root.join("sleeper"), "exec sleep 30");
    let mut slow: ProcessLinter = process_linter(
        environment(sleeper.as_path(), &[]).as_slice(),
        small,
        root.clone(),
    );
    assert_eq!(
        lint(&mut slow, root.as_path(), b"a.md", b"a\n"),
        Err(String::from(
            "monochromatic-lint did not finish within 300 milliseconds and was stopped"
        ))
    );
    let flood: PathBuf = stand_in(&root.join("flood"), "exec head -c 1000 /dev/zero");
    let mut loud: ProcessLinter = process_linter(
        environment(flood.as_path(), &[]).as_slice(),
        small,
        root.clone(),
    );
    assert_eq!(
        lint(&mut loud, root.as_path(), b"a.md", b"a\n"),
        Err(String::from(
            "monochromatic-lint wrote more than 10 bytes to its standard output and was stopped"
        ))
    );
    let broken: PathBuf = stand_in(
        &root.join("broken"),
        "echo 'monochromatic-lint: Cannot read standard input.' >&2\nexit 2",
    );
    let mut failing: ProcessLinter = process_linter(
        environment(broken.as_path(), &[]).as_slice(),
        LINTER_LIMITS,
        root.clone(),
    );
    assert_eq!(
        lint(&mut failing, root.as_path(), b"a.md", b"a\n"),
        Err(String::from(
            "monochromatic-lint could not finish (exit status 2): Cannot read standard input."
        ))
    );
    assert_eq!(
        lint(&mut failing, root.as_path(), b"", b"a\n"),
        Err(String::from(
            "cli-git cannot spell this candidate's pathname for monochromatic-lint on this platform"
        ))
    );
    let nowhere: PathBuf = root.join("no-scratch");
    let mut homeless: ProcessLinter = process_linter(
        environment(broken.as_path(), &[]).as_slice(),
        LINTER_LIMITS,
        nowhere.clone(),
    );
    for _ in 0..2 {
        let reason: String =
            lint(&mut homeless, root.as_path(), b"a.md", b"a\n").expect_err("no file");
        assert!(
            reason.starts_with(
                format!(
                    "cli-git could not create the temporary linter configuration in {}: ",
                    nowhere.display()
                )
                .as_str()
            ),
            "{reason}"
        );
    }
    remove(root.as_path());
}

/// Every child failure has its own sentence.
#[test]
fn child_failures_read_as_sentences() {
    let program: &Path = Path::new("/opt/lint");
    for (failure, sentence) in [
        (
            ChildFailure::Files(String::from("disk full")),
            "cli-git could not create the temporary files for monochromatic-lint's input and output: disk full",
        ),
        (
            ChildFailure::Start(String::from("denied")),
            "cli-git could not start monochromatic-lint at /opt/lint: denied",
        ),
        (
            ChildFailure::Wait(String::from("interrupted")),
            "cli-git could not wait for monochromatic-lint: interrupted",
        ),
        (
            ChildFailure::TimedOut(Duration::from_secs(60)),
            "monochromatic-lint did not finish within 60000 milliseconds and was stopped",
        ),
        (
            ChildFailure::TooLarge {
                stream: "standard error",
                limit: 7,
            },
            "monochromatic-lint wrote more than 7 bytes to its standard error and was stopped",
        ),
    ] {
        assert_eq!(child_failure_reason(&failure, program), sentence);
    }
}

/// The linter built from this repository,
///  by the variable the gate image sets.
fn real_linter() -> PathBuf {
    let Some(path) = std::env::var_os(LINTER_VARIABLE) else {
        panic!(
            "{LINTER_VARIABLE} is unset: build package/linter/monochromatic-lint and name its executable in \
             {LINTER_VARIABLE}; the gate image sets it to the linter it builds"
        );
    };
    return PathBuf::from(path);
}

/// A repository directory with an LFS endpoint,
///  the PNG rule and one tracked image.
fn lfs_tree(root: &Path) -> PathBuf {
    let top: PathBuf = root.join("top");
    std::fs::create_dir_all(top.join("pkg/asset")).expect("tree");
    std::fs::write(
        top.join(".lfsconfig"),
        b"[lfs]\n\turl = https://lfs.example.com/org/repo\n",
    )
    .expect("lfsconfig");
    std::fs::write(
        top.join(".gitattributes"),
        b"*.png filter=lfs diff=lfs merge=lfs -text\n",
    )
    .expect("attributes");
    std::fs::write(top.join("pkg/asset/shot.png"), b"image bytes").expect("image");
    return top;
}

/// The object URL of `pkg/asset/shot.png`;
///  the digest is `sha256sum` of `image bytes`.
const SHOT_URL: &str = "https://lfs.example.com/org/repo/de7030234493a8bea844dbe1d8676e68a2c1a4b014c721f0425a22b6df66faec/pkg/asset/shot.png";

/// The real linter rewrites a relative image link,
///  keeps an exact one,
///  reports a stale
/// object URL as a remaining finding,
///  honors the exclude option,
///  and lints a name that is
/// not UTF-8 and one that starts with `-`.
#[test]
fn the_real_linter_meets_the_contract() {
    let root: PathBuf = fixture("linter-real");
    let top: PathBuf = lfs_tree(root.as_path());
    let bin: PathBuf = root.join("bin");
    std::fs::create_dir(&bin).expect("bin");
    std::os::unix::fs::symlink(real_linter(), bin.join("monochromatic-lint")).expect("link");
    let mut linter: ProcessLinter = process_linter(
        environment(bin.as_path(), &[]).as_slice(),
        LINTER_LIMITS,
        root.clone(),
    );
    let rewritten: String = format!("# T\n\n![shot]({SHOT_URL})\n");
    for name in [&b"pkg/README.md"[..], b"pkg/caf\xe9.md", b"pkg/-x.mdx"] {
        assert_eq!(
            lint(
                &mut linter,
                top.as_path(),
                name,
                b"# T\n\n![shot](asset/shot.png)\n"
            ),
            Ok(LintRun {
                fixed: rewritten.clone().into_bytes(),
                remaining: Vec::new(),
            }),
            "{:?}",
            std::ffi::OsStr::from_bytes(name)
        );
    }
    assert_eq!(
        lint(
            &mut linter,
            top.as_path(),
            b"pkg/README.md",
            rewritten.as_bytes()
        ),
        Ok(LintRun {
            fixed: rewritten.clone().into_bytes(),
            remaining: Vec::new(),
        })
    );
    let stale: String = format!(
        "![shot](https://lfs.example.com/org/repo/{}/pkg/asset/gone.png)\n",
        "b".repeat(64)
    );
    assert_eq!(
        lint(
            &mut linter,
            top.as_path(),
            b"pkg/README.md",
            stale.as_bytes()
        ),
        Ok(LintRun {
            fixed: stale.clone().into_bytes(),
            remaining: vec![RemainingFinding {
                rule: crate::config_schema::MarkdownRule::LfsImageUrl,
                line: 1,
                column: 1,
                message: String::from(
                    "Object URL names pkg/asset/gone.png, which no longer exists in the repository."
                ),
            }],
        })
    );
    let mut excluding: ProcessLinter = process_linter(
        environment(bin.as_path(), &[]).as_slice(),
        LINTER_LIMITS,
        root.clone(),
    );
    let mut options: MarkdownAutofixOptions = default_options();
    options.exclude = vec![String::from("pkg/")];
    assert_eq!(
        excluding.lint(&LintRequest {
            top_level: top.as_path(),
            options: &options,
            path: b"pkg/README.md",
            source: b"![shot](asset/shot.png)\n",
        }),
        Ok(LintRun {
            fixed: b"![shot](asset/shot.png)\n".to_vec(),
            remaining: Vec::new(),
        })
    );
    remove(root.as_path());
}