versions-le 0.1.4

Find where one dependency is constrained differently across a repository's manifests
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! The exit codes and the stdout contract, driven against the built
//! binary.
//!
//! These are the API — a shell branches on them — so they are pinned by
//! tests that drive the real binary against a real tree. No network, no
//! privileged operation, so they run everywhere on every push.
//!
//! **A new refusal adds its case here.**

use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::atomic::{AtomicUsize, Ordering};

const BINARY: &str = env!("CARGO_BIN_EXE_versions-le");
static COUNTER: AtomicUsize = AtomicUsize::new(0);

struct Tree {
    root: PathBuf,
}

impl Tree {
    fn new(name: &str) -> Self {
        let unique = COUNTER.fetch_add(1, Ordering::Relaxed);
        let root = std::env::temp_dir().join(format!(
            "versions-le-contract-{name}-{}-{unique}",
            std::process::id()
        ));
        let _ = std::fs::remove_dir_all(&root);
        std::fs::create_dir_all(&root).expect("a temporary directory");
        Self {
            root: std::fs::canonicalize(&root).expect("a canonical directory"),
        }
    }

    fn path(&self) -> &Path {
        &self.root
    }

    fn write(&self, relative: &str, contents: &str) -> PathBuf {
        let target = self.root.join(relative);
        if let Some(parent) = target.parent() {
            std::fs::create_dir_all(parent).expect("a parent directory");
        }
        std::fs::write(&target, contents).expect("a file");
        target
    }
}

impl Drop for Tree {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.root);
    }
}

struct Run {
    code: i32,
    stdout: String,
    stderr: String,
}

fn run(args: &[&str]) -> Run {
    let output = Command::new(BINARY)
        .args(args)
        .output()
        .expect("the binary runs");
    Run {
        code: output.status.code().expect("an exit code"),
        stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
        stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
    }
}

/// The whole report, parsed. Doubles as the assertion that stdout is one
/// JSON object and nothing else.
fn report(run: &Run) -> serde_json::Value {
    serde_json::from_str(run.stdout.trim()).expect("stdout carries one JSON report")
}

fn codes(report: &serde_json::Value) -> Vec<String> {
    report["findings"]
        .as_array()
        .expect("a list")
        .iter()
        .map(|finding| finding["code"].as_str().expect("a code").to_string())
        .collect()
}

/// Two crates that disagree about `serde`, and a workflow whose
/// toolchain is older than the declared minimum.
fn drifted(name: &str) -> Tree {
    let tree = Tree::new(name);
    tree.write(
        "api/Cargo.toml",
        "[package]\nname = \"api\"\nrust-version = \"1.88\"\n\n[dependencies]\nserde = \"1.0.200\"\n",
    );
    tree.write(
        "web/Cargo.toml",
        "[package]\nname = \"web\"\n\n[dependencies]\nserde = \"1\"\n",
    );
    tree.write(
        ".github/workflows/ci.yml",
        "jobs:\n  test:\n    steps:\n      - uses: dtolnay/rust-toolchain@1.80\n",
    );
    tree.write("README.md", "not a manifest\n");
    tree
}

#[test]
fn manifests_that_agree_exit_zero() {
    let tree = Tree::new("agree");
    tree.write("a/Cargo.toml", "[dependencies]\nserde = \"1.0.200\"\n");
    tree.write("b/Cargo.toml", "[dependencies]\nserde = \"1.0.200\"\n");
    let run = run(&[&tree.path().to_string_lossy()]);
    assert_eq!(run.code, 0, "{}", run.stderr);
    assert_eq!(report(&run)["summary"]["findings"], 0);
    assert!(run.stderr.contains("consistent"), "{}", run.stderr);
}

#[test]
fn a_drifted_constraint_exits_one_and_names_both_sites() {
    let tree = drifted("drift");
    let run = run(&[&tree.path().to_string_lossy()]);
    assert_eq!(run.code, 1, "{}", run.stderr);
    let report = report(&run);
    assert!(codes(&report).contains(&"constraint-conflict".to_string()));
    assert!(run.stderr.contains("api/Cargo.toml"), "{}", run.stderr);
    assert!(run.stderr.contains("web/Cargo.toml"), "{}", run.stderr);
}

/// The strongest finding, and the one that must stay distinguishable
/// from a merely different constraint.
#[test]
fn an_unsatisfiable_pair_is_reported_as_disjoint_and_not_as_a_conflict() {
    let tree = Tree::new("disjoint");
    tree.write("a/Cargo.toml", "[dependencies]\nserde = \"1\"\n");
    tree.write("b/Cargo.toml", "[dependencies]\nserde = \"2\"\n");
    let run = run(&[&tree.path().to_string_lossy()]);
    assert_eq!(run.code, 1);
    assert_eq!(codes(&report(&run)), ["disjoint-constraint"]);
    assert!(run.stderr.contains("error"), "{}", run.stderr);
}

/// **The refusal spine, end to end.** A grammar the tool does not model
/// is named and excluded — never turned into a conflict against a real
/// constraint.
#[test]
fn an_unmodelled_grammar_is_a_refusal_and_never_a_finding() {
    let tree = Tree::new("refusal");
    tree.write("a/Cargo.toml", "[dependencies]\nserde = \"1.0.200\"\n");
    tree.write(
        "b/Cargo.toml",
        "[dependencies]\nserde = { workspace = true }\n",
    );
    let run = run(&[&tree.path().to_string_lossy()]);
    assert_eq!(run.code, 0, "{}", run.stderr);
    let report = report(&run);
    assert!(codes(&report).is_empty(), "{:?}", codes(&report));
    assert_eq!(report["refusals"][0]["reason"], "unknown_grammar");
    assert!(run.stderr.contains("refused"), "{}", run.stderr);
}

/// The same word in two ecosystems is a coincidence, and the tool says
/// so instead of comparing two different questions.
#[test]
fn a_name_in_two_ecosystems_is_refused_rather_than_compared() {
    let tree = Tree::new("cross");
    tree.write("Cargo.toml", "[dependencies]\nsemver = \"1.0.28\"\n");
    tree.write(
        "package.json",
        r#"{ "dependencies": { "semver": "^7.6.0" } }"#,
    );
    let root = tree.path().to_string_lossy().to_string();
    let relaxed = run(&[&root]);
    assert_eq!(relaxed.code, 0, "{}", relaxed.stderr);
    let report = report(&relaxed);
    assert!(codes(&report).is_empty());
    assert_eq!(report["refusals"][0]["reason"], "cross_ecosystem");
    // It is the answer, not a failure to answer, so --strict leaves it
    // alone.
    assert_eq!(run(&["--strict", &root]).code, 0);
}

/// **A CI tool version belongs to the job that installs it.** Testing on
/// the oldest supported interpreter and publishing on the newest is
/// correct and common; calling it a `disjoint-constraint` failed a build
/// over a workflow that had done nothing wrong. Like `cross_ecosystem`,
/// this refusal is the answer rather than a failure to answer, so
/// `--strict` leaves it alone too.
#[test]
fn two_jobs_pinning_one_tool_differently_is_refused_and_exits_zero() {
    let tree = Tree::new("per-job");
    tree.write(
        ".github/workflows/ci.yml",
        "jobs:\n  test:\n    steps:\n      - uses: actions/setup-python@v5\n        with:\n          python-version: 3.9\n",
    );
    tree.write(
        ".github/workflows/publish.yml",
        "jobs:\n  publish:\n    steps:\n      - uses: actions/setup-python@v5\n        with:\n          python-version: 3.12\n",
    );
    let root = tree.path().to_string_lossy().to_string();

    let relaxed = run(&[&root]);
    assert_eq!(relaxed.code, 0, "{}", relaxed.stderr);
    let report = report(&relaxed);
    assert!(codes(&report).is_empty(), "{:?}", codes(&report));
    assert_eq!(report["refusals"][0]["reason"], "per_job_tool_version");
    assert_eq!(report["refusals"][0]["name"], "python");
    assert!(relaxed.stderr.contains("refused"), "{}", relaxed.stderr);
    assert_eq!(run(&["--strict", &root]).code, 0);
    assert_eq!(run(&["--fail-on", "any", &root]).code, 0);
}

/// A `<tool>-version:` key is a naming convention, not a schema, so a
/// value that is not evidently a version is refused rather than read.
#[test]
fn a_matrix_expression_is_an_ambiguous_version_string() {
    let tree = Tree::new("ambiguous");
    tree.write(
        ".github/workflows/ci.yml",
        "jobs:\n  test:\n    steps:\n      - uses: actions/setup-node@v4\n        with:\n          node-version: ${{ matrix.node }}\n",
    );
    let root = tree.path().to_string_lossy().to_string();
    let relaxed = run(&[&root]);
    assert_eq!(
        report(&relaxed)["refusals"][0]["reason"],
        "ambiguous_version_string"
    );
    assert_eq!(relaxed.code, 0);
    assert_eq!(run(&["--strict", &root]).code, 2);
}

/// A repository with no manifests is not in conflict — failing a build
/// over it would be the tool inventing a problem.
#[test]
fn no_manifests_exits_zero() {
    let tree = Tree::new("none");
    tree.write("README.md", "nothing here\n");
    let run = run(&[&tree.path().to_string_lossy()]);
    assert_eq!(run.code, 0);
    assert_eq!(report(&run)["summary"]["manifests"], 0);
    assert!(run.stderr.contains("no manifests"), "{}", run.stderr);
}

#[test]
fn a_floating_pin_fails_only_under_fail_on_any() {
    let tree = Tree::new("floating");
    tree.write(
        "package.json",
        r#"{ "dependencies": { "left-pad": "latest" } }"#,
    );
    let root = tree.path().to_string_lossy().to_string();
    assert_eq!(run(&[&root]).code, 0);
    assert_eq!(codes(&report(&run(&[&root]))), ["floating-pin"]);
    assert_eq!(run(&["--fail-on", "any", &root]).code, 1);
}

/// **The real-world finding, end to end.** A workspace holding its own
/// crates together with `{ path = "…", version = "0.7.7" }` has not
/// pinned them: that is a caret requirement, `[0.7.7, 0.8.0)`. Only
/// `=0.7.7` is the pin. The tool must say so about the first spelling
/// and stay silent about the second.
#[test]
fn a_bare_version_in_a_path_dependency_floats_and_an_equals_pin_does_not() {
    let tree = Tree::new("path-pin");
    tree.write(
        "crates/binary/Cargo.toml",
        "[dependencies]\ncore = { path = \"../core\", version = \"0.7.7\" }\n",
    );
    let root = tree.path().to_string_lossy().to_string();

    let floating = run(&["--fail-on", "any", &root]);
    assert_eq!(
        codes(&report(&floating)),
        ["floating-pin"],
        "{}",
        floating.stderr
    );
    assert_eq!(floating.code, 1, "{}", floating.stderr);

    tree.write(
        "crates/binary/Cargo.toml",
        "[dependencies]\ncore = { path = \"../core\", version = \"=0.7.7\" }\n",
    );
    let pinned = run(&["--fail-on", "any", &root]);
    assert!(codes(&report(&pinned)).is_empty(), "{}", pinned.stderr);
    assert_eq!(pinned.code, 0, "{}", pinned.stderr);
}

/// The one check that reads across ecosystems, and it does so by naming
/// both keys rather than by matching a package name.
#[test]
fn a_ci_toolchain_below_the_declared_msrv_is_a_finding() {
    let tree = drifted("msrv");
    let run = run(&[
        "--ecosystem",
        "cargo",
        "--ecosystem",
        "ci",
        &tree.path().to_string_lossy(),
    ]);
    assert!(codes(&report(&run)).contains(&"msrv-mismatch".to_string()));
    assert!(run.stderr.contains("1.80"), "{}", run.stderr);
}

#[test]
fn an_ecosystem_filter_narrows_the_walk() {
    let tree = Tree::new("filter");
    tree.write("Cargo.toml", "[dependencies]\nserde = \"1\"\n");
    tree.write("package.json", r#"{ "dependencies": { "a": "1" } }"#);
    let root = tree.path().to_string_lossy().to_string();
    assert_eq!(report(&run(&[&root]))["summary"]["manifests"], 2);
    assert_eq!(
        report(&run(&["--ecosystem", "cargo", &root]))["summary"]["manifests"],
        1
    );
}

#[test]
fn an_exclude_pattern_removes_a_manifest_from_the_comparison() {
    let tree = Tree::new("exclude");
    tree.write("Cargo.toml", "[dependencies]\nserde = \"1\"\n");
    tree.write(
        "examples/demo/Cargo.toml",
        "[dependencies]\nserde = \"2\"\n",
    );
    let root = tree.path().to_string_lossy().to_string();
    assert_eq!(run(&[&root]).code, 1);
    let excluded = run(&["--exclude", "examples/**", &root]);
    assert_eq!(excluded.code, 0, "{}", excluded.stderr);
}

/// `.github` is a hidden directory, and a tool that needed a flag to see
/// it would ship with the CI half switched off.
#[test]
fn the_workflow_directory_is_walked_but_other_hidden_directories_are_not() {
    let tree = Tree::new("hidden");
    tree.write(
        ".github/workflows/ci.yml",
        "steps:\n  - uses: actions/checkout@v4\n",
    );
    tree.write(".cache/Cargo.toml", "[dependencies]\nserde = \"1\"\n");
    let root = tree.path().to_string_lossy().to_string();
    assert_eq!(report(&run(&[&root]))["summary"]["manifests"], 1);
    assert_eq!(
        report(&run(&["--hidden", &root]))["summary"]["manifests"],
        2
    );
}

#[test]
fn several_roots_are_analysed_together() {
    let tree = drifted("roots");
    let run = run(&[
        &tree.path().join("api").to_string_lossy(),
        &tree.path().join("web").to_string_lossy(),
    ]);
    assert_eq!(report(&run)["summary"]["manifests"], 2);
    assert_eq!(run.code, 1);
}

#[test]
fn a_named_manifest_is_the_whole_walk() {
    let tree = drifted("named");
    let run = run(&[&tree.path().join("api/Cargo.toml").to_string_lossy()]);
    assert_eq!(report(&run)["summary"]["manifests"], 1);
    assert_eq!(run.code, 0);
}

#[test]
fn an_unreadable_input_exits_two() {
    assert_eq!(run(&["/no/such/place-xyz"]).code, 2);
}

/// A manifest that does not parse is named rather than dropped: a file
/// that vanished from the report reads to whoever ran it as a clean one.
#[test]
fn a_manifest_that_does_not_parse_is_named_and_the_rest_still_answer() {
    let tree = Tree::new("broken");
    tree.write("package.json", "{ not json");
    tree.write("a/Cargo.toml", "[dependencies]\nserde = \"1\"\n");
    tree.write("b/Cargo.toml", "[dependencies]\nserde = \"2\"\n");
    let root = tree.path().to_string_lossy().to_string();
    let relaxed = run(&[&root]);
    assert_eq!(relaxed.code, 1, "the Cargo answer still stands");
    assert!(
        relaxed.stderr.contains("package.json"),
        "{}",
        relaxed.stderr
    );
    assert_eq!(report(&relaxed)["diagnostics"][0]["code"], "parse-error");
    assert_eq!(run(&["--strict", &root]).code, 2);
}

#[test]
fn an_unknown_flag_exits_two_and_names_itself() {
    let tree = drifted("badflag");
    let run = run(&["--stict", &tree.path().to_string_lossy()]);
    assert_eq!(run.code, 2);
    assert!(run.stderr.contains("--stict"), "{}", run.stderr);
    assert!(run.stdout.is_empty(), "a refusal writes no report");
}

#[test]
fn an_unknown_ecosystem_exits_two_rather_than_widening_the_walk() {
    let tree = drifted("badecosystem");
    let run = run(&["--ecosystem", "maven", &tree.path().to_string_lossy()]);
    assert_eq!(run.code, 2);
    assert!(run.stderr.contains("maven"), "{}", run.stderr);
}

/// Nothing here writes, and no flag may suggest otherwise.
#[test]
fn no_flag_offers_to_change_a_manifest() {
    let tree = drifted("nowrite");
    for attempt in ["--fix", "--write", "--update", "--pin", "--json"] {
        assert_eq!(
            run(&[attempt, &tree.path().to_string_lossy()]).code,
            2,
            "{attempt} was accepted"
        );
    }
}

#[test]
fn version_and_help_exit_clear() {
    let version = run(&["--version"]);
    assert_eq!(version.code, 0);
    assert!(version.stdout.contains("versions-le"));
    let help = run(&["--help"]);
    assert_eq!(help.code, 0);
    assert!(help.stdout.contains("usage: versions-le"));
    assert!(help.stdout.contains("never crosses an ecosystem"));
}

/// A report is a thing to diff. One object, no timestamp, and the same
/// bytes for the same tree.
#[test]
fn stdout_is_one_report_with_no_timestamp_and_is_byte_stable() {
    let tree = drifted("streams");
    let first = run(&[&tree.path().to_string_lossy()]);
    let again = run(&[&tree.path().to_string_lossy()]);
    assert_eq!(first.stdout.trim().lines().count(), 1);
    assert_eq!(first.stdout, again.stdout, "two runs, two reports");
    assert!(!first.stderr.contains('{'), "{}", first.stderr);
    assert_eq!(report(&first)["schema"], 1);
    for absent in ["timestamp", "lastChecked", "generatedAt"] {
        assert!(!first.stdout.contains(absent), "{}", first.stdout);
    }
}

/// **The cross-surface contract.** Both surfaces call one entry point,
/// so they must answer identically for the same tree.
#[test]
fn the_cli_and_the_mcp_server_report_the_same_thing() {
    let tree = drifted("agreement");
    let from_cli = report(&run(&[&tree.path().to_string_lossy()]));

    let request = serde_json::json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "tools/call",
        "params": {
            "name": "versions_le_check",
            "arguments": { "path": tree.path().to_string_lossy() },
        },
    });
    let mut child = Command::new(BINARY)
        .arg("mcp")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("the server starts");
    writeln!(child.stdin.as_mut().expect("stdin"), "{request}").expect("written");
    let output = child.wait_with_output().expect("finishes");
    let response: serde_json::Value = serde_json::from_slice(
        output
            .stdout
            .split(|byte| *byte == b'\n')
            .next()
            .expect("a line"),
    )
    .expect("the reply is JSON");

    assert_eq!(
        response["result"]["structuredContent"]["data"], from_cli,
        "the two surfaces disagree"
    );
}