use super::super::fixtures::*;
use super::*;
#[test]
fn lens_runs_at_every_depth_and_the_depths_differ() {
for d in ["rule", "why", "all"] {
assert_eq!(
run_args(argv(&["lens", ".", "--depth", d])),
ExitCode::SUCCESS,
"lens --depth {d}"
);
}
let root = repo_root();
let p = |d| lens::pack(&root, &root, d).map(|p| p.text.len());
let (Ok(rule), Ok(all)) = (p(lens::Depth::Rule), p(lens::Depth::All))
else {
panic!("the root pack must be readable at both depths")
};
assert!(rule < all, "B8: `rule` must SELECT, not render everything");
}
#[test]
fn a_chain_over_its_ceiling_exits_one_and_a_generous_one_exits_zero() {
assert_eq!(over_ceiling_is_caught(), Ok(()));
}
const FAT_SPEC: &str = "# SPEC\n\n## \u{a7}G GOAL\n\nsomething long \
enough to cost more than one token, several times over, so the \
ceiling below is genuinely exceeded rather than merely equalled\n";
fn over_ceiling_is_caught() -> Result<(), String> {
let r = crate::testrepo::TestRepo::new("cli-budget-over")?;
r.write("SPEC.md", FAT_SPEC)?;
r.write(".context-limits", "SPEC.md 1\n")?;
r.commit("a node over its ceiling")?;
let root = r.path().to_path_buf();
assert_eq!(
budget(r.path(), root.clone()),
ExitCode::from(1),
"B7: a chain over its ceiling must FAIL, not merely print"
);
r.write(".context-limits", "SPEC.md 100000\n")?;
r.commit("raise it")?;
assert_eq!(budget(r.path(), root), ExitCode::SUCCESS);
Ok(())
}
#[test]
fn absence_is_reported_and_is_not_a_failure() -> Result<(), String> {
let r = crate::testrepo::TestRepo::new("cli-absence")?;
r.write("SPEC.md", "# SPEC\n\n## \u{a7}G GOAL\n\nnothing here\n")?;
r.commit("a repo with no §F and no slice registry")?;
assert_eq!(slice_cmd(r.path(), "--list"), ExitCode::SUCCESS);
assert_eq!(slice_cmd(r.path(), "--check"), ExitCode::SUCCESS);
assert_eq!(fed_cmd(r.path()), ExitCode::SUCCESS);
assert_eq!(fed_cmd(&r.path().join("nowhere")), ExitCode::from(2));
Ok(())
}
#[test]
fn the_debt_verb_checks_records_and_refuses() -> Result<(), String> {
let r = crate::testrepo::TestRepo::new("cli-debt")?;
r.write("SPEC.md", "# SPEC\n\n## \u{a7}G GOAL\n\ndebt\n")?;
r.write("src/a.rs", &"fn f() {}\n".repeat(100))?;
r.write(
".lint-debt",
"# the reason\ndensity 20.0\nshape 25.0\ncount 0\nexcess 0\nloc 0\n",
)?;
r.commit("a tree with a ratchet")?;
let fake = r.path().join("fake-cargo");
let fake = crate::testrepo::write_script(
&fake,
"#!/bin/sh\necho 'src/a.rs:1:1: warning: too many lines (35/15)' >&2\nexit 0\n",
)?;
assert_eq!(
debt_cmd(r.path(), Some("--check"), &fake),
ExitCode::SUCCESS
);
assert_eq!(
debt_cmd(r.path(), Some("--record"), &fake),
ExitCode::SUCCESS
);
let after = std::fs::read_to_string(r.path().join(".lint-debt"))
.unwrap_or_default();
assert!(after.contains("density 10.0"), "{after}");
assert!(after.contains("shape 20.0"), "{after}");
assert!(after.contains("# the reason"), "the reason survives");
assert_eq!(
debt_cmd(r.path(), Some("--check"), &fake),
ExitCode::SUCCESS
);
Ok(())
}
#[test]
fn a_tree_with_no_ratchet_is_a_usage_error() -> Result<(), String> {
let r = crate::testrepo::TestRepo::new("cli-debt-none")?;
r.write("SPEC.md", "# SPEC\n\n## \u{a7}G GOAL\n\nnone\n")?;
r.write("src/a.rs", "fn f() {}\n")?;
r.commit("no ratchet here")?;
assert_eq!(
debt_cmd(r.path(), Some("--check"), "true"),
ExitCode::from(2)
);
r.write(".lint-debt", "# a comment and nothing else\n")?;
assert_eq!(
debt_cmd(r.path(), Some("--check"), "true"),
ExitCode::from(2)
);
Ok(())
}
#[test]
fn the_coverage_verb_checks_records_and_refuses() -> Result<(), String> {
let r = crate::testrepo::TestRepo::new("cli-cov")?;
r.write("SPEC.md", "# SPEC\n\n## \u{a7}G GOAL\n\ncov\n")?;
r.write(".coverage", "# the reason\nlines 90.00\n")?;
r.commit("a tree with a floor")?;
let fake = r.path().join("fake-cargo");
let fake = crate::testrepo::write_script(
&fake,
"#!/bin/sh\necho 'TOTAL 1 2 3.00% 4 5 6.00% 7 8 92.50% 0 0 -'\nexit 0\n",
)?;
assert_eq!(
coverage_cmd(r.path(), Some("--check"), &fake),
ExitCode::SUCCESS
);
assert_eq!(
coverage_cmd(r.path(), Some("--record"), &fake),
ExitCode::SUCCESS
);
let after =
std::fs::read_to_string(r.path().join(".coverage")).unwrap_or_default();
assert!(after.contains("lines 92.50"), "{after}");
assert!(after.contains("# the reason"), "the reason survives");
assert_eq!(
coverage_cmd(r.path(), Some("--check"), "definitely-not-a-cargo"),
ExitCode::from(2)
);
let _ = std::fs::remove_file(r.path().join(".coverage"));
assert_eq!(
coverage_cmd(r.path(), Some("--check"), &fake),
ExitCode::from(2)
);
Ok(())
}
#[test]
fn the_coverage_verb_checks_the_default_build_when_it_has_a_floor()
-> Result<(), String> {
let r = crate::testrepo::TestRepo::new("cli-cov-default")?;
let fake =
crate::debt::coverage_tests::two_builds(r.path(), "90.89", "94.06")?;
r.write(".coverage", "lines 90.89\nlines-default 94.10\n")?;
assert_eq!(
coverage_cmd(r.path(), Some("--check"), &fake),
ExitCode::from(1),
"94.06 under a 94.10 floor fails though `lines` holds"
);
r.write(".coverage", "lines 90.00\nlines-default 94.00\n")?;
assert_eq!(
coverage_cmd(r.path(), Some("--check"), &fake),
ExitCode::SUCCESS
);
assert_eq!(
coverage_cmd(r.path(), Some("--record"), &fake),
ExitCode::SUCCESS
);
let after = std::fs::read_to_string(r.path().join(".coverage"))
.map_err(|e| e.to_string())?;
assert_eq!(after, "lines 90.89\nlines-default 94.06\n");
Ok(())
}
#[test]
fn route_reports_a_hit_a_miss_and_an_ambiguity_by_exit_code() {
let repo = routing_fixture("cli-route");
let root = repo.path();
assert_eq!(route_cmd(root, "sprockets"), ExitCode::SUCCESS);
assert_eq!(route_cmd(root, "wombat"), ExitCode::from(2));
assert_eq!(route_cmd(root, "widgets gizmos"), ExitCode::from(3));
}
fn scripted(dir: &Path, stdout: &str, stderr: &str) -> Result<String, String> {
let p = dir.join("scripted-cargo");
crate::testrepo::write_script(
&p,
&format!("#!/bin/sh\necho '{stdout}'\necho '{stderr}' >&2\nexit 0\n"),
)
}
#[test]
fn the_ratchet_verbs_refuse_the_wrong_direction() -> Result<(), String> {
let r = crate::testrepo::TestRepo::new("cli-ratchet-refuse")?;
r.write("src/a.rs", &"fn f() {}\n".repeat(100))?;
r.write(".lint-debt", "density 5.0\nshape 25.0\n")?;
r.write(".coverage", "lines 95.00\n")?;
let lint = scripted(
r.path(),
"",
"src/a.rs:1:1: warning: too many lines (35/15)",
)?;
assert_eq!(
debt_cmd(r.path(), Some("--check"), &lint),
ExitCode::from(1)
);
assert_eq!(
debt_cmd(r.path(), Some("--record"), &lint),
ExitCode::from(1)
);
let cov =
scripted(r.path(), "TOTAL 1 2 3.00% 4 5 6.00% 7 8 92.50% 0 0 -", "")?;
assert_eq!(
coverage_cmd(r.path(), Some("--record"), &cov),
ExitCode::from(1),
"95.00 -> 92.50 is a drop"
);
let after = std::fs::read_to_string(r.path().join(".coverage"))
.map_err(|e| e.to_string())?;
assert_eq!(after, "lines 95.00\n", "the refused drop was not written");
Ok(())
}
#[test]
fn budget_fails_on_what_it_cannot_measure() -> Result<(), String> {
let r = routing_fixture("cli-budget-fail");
r.write(".context-limits", "not a ceiling row\n")?;
assert_eq!(budget(r.path(), r.path().to_path_buf()), ExitCode::from(1));
let r = routing_fixture("cli-budget-unread");
std::fs::write(r.path().join("alpha/SPEC.md"), b"\xff\xfe")
.map_err(|e| e.to_string())?;
assert_eq!(budget(r.path(), r.path().to_path_buf()), ExitCode::from(1));
Ok(())
}
#[test]
fn route_on_a_single_node_repo_is_a_miss() -> Result<(), String> {
let r = crate::testrepo::TestRepo::new("cli-single-node")?;
assert_eq!(route_cmd(r.path(), "anything"), ExitCode::from(2));
Ok(())
}