181 lines
6.9 KiB
Rust
181 lines
6.9 KiB
Rust
use std::{fs, path::PathBuf, process::Command};
|
|
|
|
use serde_json::Value;
|
|
|
|
fn lint() -> Command {
|
|
Command::new(env!("CARGO_BIN_EXE_marqdo-lint"))
|
|
}
|
|
|
|
fn fixtures() -> PathBuf {
|
|
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures")
|
|
}
|
|
|
|
#[test]
|
|
fn rule_commands_do_not_require_the_core() {
|
|
let output = lint()
|
|
.env("MARQDO_BIN", "no-such-core")
|
|
.args(["rules", "--format", "json"])
|
|
.output()
|
|
.unwrap();
|
|
assert!(output.status.success());
|
|
let rules: Vec<Value> = serde_json::from_slice(&output.stdout).unwrap();
|
|
assert_eq!(rules.len(), 18);
|
|
|
|
let output = lint()
|
|
.env("MARQDO_BIN", "no-such-core")
|
|
.args(["explain", "MQD-BRANCH-001", "--format", "json"])
|
|
.output()
|
|
.unwrap();
|
|
assert!(output.status.success());
|
|
let rule: Value = serde_json::from_slice(&output.stdout).unwrap();
|
|
assert_eq!(rule["fix_safety"], "manual");
|
|
assert!(rule["evidence"].is_string());
|
|
}
|
|
|
|
#[test]
|
|
fn reports_and_exit_codes_distinguish_style_from_unavailable_analysis() {
|
|
if std::env::var_os("MARQDO_BIN").is_none() {
|
|
return;
|
|
}
|
|
let temp = tempfile::tempdir().unwrap();
|
|
let source = temp.path().join("index.mq.md");
|
|
fs::write(&source, "## @exec small\n\n*return 1*\n").unwrap();
|
|
let report = |args: &[&str]| {
|
|
let output = lint()
|
|
.current_dir(temp.path())
|
|
.args(["index.mq.md", "--format", "json"])
|
|
.args(args)
|
|
.output()
|
|
.unwrap();
|
|
assert!(output.stderr.is_empty(), "{:?}", output);
|
|
(output.status.code().unwrap(), serde_json::from_slice::<Value>(&output.stdout).unwrap())
|
|
};
|
|
let (code, result) = report(&[]);
|
|
assert_eq!(code, 1, "{result}");
|
|
assert_eq!(result["analysis_status"], "complete");
|
|
assert_eq!(result["diagnostics"][0]["id"], "MQD-DOC-001");
|
|
assert!(result["diagnostics"].as_array().unwrap().iter().any(|diagnostic| {
|
|
diagnostic["id"] == "MQD-DOC-003" && diagnostic["severity"] == "info"
|
|
}));
|
|
assert_eq!(report(&["--fail-on", "error"]).0, 0);
|
|
|
|
fs::write(temp.path().join("marqdo-lint.toml"), r#"
|
|
[[suppress]]
|
|
path = "index.mq.md"
|
|
rule = "MQD-DOC-001"
|
|
reason = "Fixture for a deliberately short helper"
|
|
"#).unwrap();
|
|
assert_eq!(report(&[]).0, 0);
|
|
|
|
fs::copy(fixtures().join("golden/invalid-control.mq.md"), source).unwrap();
|
|
let (code, result) = report(&["--fail-on", "never"]);
|
|
assert_eq!(code, 2, "{result}");
|
|
assert_eq!(result["analysis_status"], "analysis_incomplete");
|
|
assert!(result["metrics"]["narrative_ratio"].is_null());
|
|
assert!(result["core_output"].as_str().unwrap().contains("sem.unclosed_control"));
|
|
}
|
|
|
|
#[test]
|
|
fn multi_module_run_reports_each_source_once() {
|
|
if std::env::var_os("MARQDO_BIN").is_none() {
|
|
return;
|
|
}
|
|
let output = lint()
|
|
.current_dir(fixtures().join("multi"))
|
|
.args(["index.mq.md", "geometry.mq.md", ".", "--format", "json", "--fail-on", "never"])
|
|
.output()
|
|
.unwrap();
|
|
assert!(output.status.success(), "{:?}", output);
|
|
let report: Value = serde_json::from_slice(&output.stdout).unwrap();
|
|
assert_eq!(report["summary"]["files"], 2);
|
|
assert_eq!(report["reports"][0]["source"]["path"], "geometry.mq.md");
|
|
assert_eq!(report["reports"][1]["source"]["path"], "index.mq.md");
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
#[test]
|
|
fn t30_frozen_core_installs_and_dispatches_lint() {
|
|
let Some(core) = std::env::var_os("MARQDO_BIN") else {
|
|
return;
|
|
};
|
|
let temp = tempfile::tempdir().unwrap();
|
|
let bundle = temp.path().join("bundle");
|
|
fs::create_dir_all(bundle.join("bin")).unwrap();
|
|
fs::copy(env!("CARGO_BIN_EXE_marqdo-lint"), bundle.join("bin/marqdo-lint")).unwrap();
|
|
let manifest = bundle.join("tool.json");
|
|
fs::write(&manifest, include_bytes!("../tool.json")).unwrap();
|
|
let marqdo = || {
|
|
let mut command = Command::new(&core);
|
|
command.env("MARQDO_TOOL_HOME", temp.path().join("registry"))
|
|
.env_remove("MARQDO_BIN");
|
|
command
|
|
};
|
|
let install = marqdo().args(["tool", "install"]).arg(manifest).output().unwrap();
|
|
assert!(install.status.success(), "{:?}", install);
|
|
let output = marqdo()
|
|
.current_dir(fixtures().join("golden"))
|
|
.args(["lint", "branches.mq.md", "--format", "json", "--fail-on", "never"])
|
|
.output().unwrap();
|
|
assert!(output.status.success(), "{:?}", output);
|
|
let report: Value = serde_json::from_slice(&output.stdout).unwrap();
|
|
assert_eq!(report["analysis_status"], "complete");
|
|
}
|
|
|
|
#[test]
|
|
fn safe_fix_previews_then_persists_verified_whitespace_in_a_manifest_project() {
|
|
if std::env::var_os("MARQDO_BIN").is_none() {
|
|
return;
|
|
}
|
|
let temp = tempfile::tempdir().unwrap();
|
|
let source = temp.path().join("index.mq.md");
|
|
let multi = fixtures().join("multi");
|
|
fs::copy(multi.join("marqdo.yaml"), temp.path().join("marqdo.yaml")).unwrap();
|
|
fs::copy(multi.join("geometry.mq.md"), temp.path().join("geometry.mq.md")).unwrap();
|
|
let clean = fs::read_to_string(multi.join("index.mq.md")).unwrap();
|
|
let original = clean.replace("## @exec main\n\n", "## @exec main\n \t\n");
|
|
assert_ne!(original, clean);
|
|
fs::write(&source, &original).unwrap();
|
|
let fix = |args: &[&str]| {
|
|
lint()
|
|
.current_dir(temp.path())
|
|
.args(["index.mq.md", "--fix", "safe", "--format", "json", "--fail-on", "never"])
|
|
.args(args)
|
|
.output()
|
|
.unwrap()
|
|
};
|
|
let preview = fix(&["--dry-run"]);
|
|
assert!(preview.status.success(), "{:?}", preview);
|
|
assert!(String::from_utf8_lossy(&preview.stderr).contains("@@"));
|
|
assert_eq!(fs::read_to_string(&source).unwrap(), original);
|
|
let applied = fix(&[]);
|
|
assert!(applied.status.success(), "{:?}", applied);
|
|
assert_eq!(fs::read_to_string(&source).unwrap(), clean);
|
|
let repeated = fix(&[]);
|
|
assert!(repeated.status.success() && repeated.stderr.is_empty(), "{:?}", repeated);
|
|
}
|
|
|
|
#[test]
|
|
fn safe_fix_rejects_changes_to_inert_payload_and_unavailable_analysis() {
|
|
if std::env::var_os("MARQDO_BIN").is_none() {
|
|
return;
|
|
}
|
|
let temp = tempfile::tempdir().unwrap();
|
|
let source = temp.path().join("index.mq.md");
|
|
for (original, code) in [
|
|
("## @exec example\n\n```text\n \t\n```\n\n*return 1*\n", 1),
|
|
("## @exec example\n\n`if true`\n\n*return 1*", 2),
|
|
] {
|
|
fs::write(&source, original).unwrap();
|
|
let output = lint()
|
|
.current_dir(temp.path())
|
|
.args(["index.mq.md", "--fix", "safe", "--format", "json", "--fail-on", "never"])
|
|
.output().unwrap();
|
|
let report: Value = serde_json::from_slice(&output.stdout).unwrap();
|
|
assert_eq!(output.status.code(), Some(code), "{report}");
|
|
assert!(report["diagnostics"].as_array().unwrap().iter().any(|diagnostic| {
|
|
diagnostic["id"] == "MQD-SAFETY-001"
|
|
}), "{report}");
|
|
assert_eq!(fs::read_to_string(&source).unwrap(), original);
|
|
}
|
|
}
|