mod common;
use common::{init_project, normalize_output, run_commands, today};
use std::fs;
fn enable_source_scan(dir: &std::path::Path) {
let config_path = dir.join("gov/config.toml");
let config = fs::read_to_string(&config_path).unwrap();
if !config.contains("[source_scan]") {
let updated = format!(
"{}\n[source_scan]\nenabled = true\ninclude = [\"src/**/*.rs\"]\nexclude = []\n",
config
);
fs::write(&config_path, updated).unwrap();
}
}
#[test]
fn test_scan_no_references() {
let temp_dir = init_project();
enable_source_scan(temp_dir.path());
let date = today();
let output = run_commands(temp_dir.path(), &[&["check"]]);
insta::assert_snapshot!(normalize_output(&output, temp_dir.path(), &date));
}
#[test]
fn test_scan_valid_rfc_reference() {
let temp_dir = init_project();
enable_source_scan(temp_dir.path());
let date = today();
run_commands(
temp_dir.path(),
&[
&["rfc", "new", "Test RFC"],
&["rfc", "finalize", "RFC-0001", "normative"],
],
);
fs::create_dir_all(temp_dir.path().join("src")).unwrap();
fs::write(
temp_dir.path().join("src/main.rs"),
"// Implements [[RFC-0001]]\nfn main() {}\n",
)
.unwrap();
let output = run_commands(temp_dir.path(), &[&["check"]]);
insta::assert_snapshot!(normalize_output(&output, temp_dir.path(), &date));
}
#[test]
fn test_scan_valid_clause_reference() {
let temp_dir = init_project();
enable_source_scan(temp_dir.path());
let date = today();
run_commands(
temp_dir.path(),
&[
&["rfc", "new", "Test RFC"],
&[
"clause",
"new",
"RFC-0001:C-TEST",
"Test Clause",
"-s",
"Specification",
"-k",
"normative",
],
&["rfc", "finalize", "RFC-0001", "normative"],
],
);
fs::create_dir_all(temp_dir.path().join("src")).unwrap();
fs::write(
temp_dir.path().join("src/main.rs"),
"// Implements [[RFC-0001:C-TEST]]\nfn main() {}\n",
)
.unwrap();
let output = run_commands(temp_dir.path(), &[&["check"]]);
insta::assert_snapshot!(normalize_output(&output, temp_dir.path(), &date));
}
#[test]
fn test_scan_unknown_rfc_reference() {
let temp_dir = init_project();
enable_source_scan(temp_dir.path());
let date = today();
fs::create_dir_all(temp_dir.path().join("src")).unwrap();
fs::write(
temp_dir.path().join("src/main.rs"),
"// Implements [[RFC-9999]]\nfn main() {}\n",
)
.unwrap();
let output = run_commands(temp_dir.path(), &[&["check"]]);
insta::assert_snapshot!(normalize_output(&output, temp_dir.path(), &date));
}
#[test]
fn test_scan_unknown_clause_reference() {
let temp_dir = init_project();
enable_source_scan(temp_dir.path());
let date = today();
run_commands(
temp_dir.path(),
&[
&["rfc", "new", "Test RFC"],
&["rfc", "finalize", "RFC-0001", "normative"],
],
);
fs::create_dir_all(temp_dir.path().join("src")).unwrap();
fs::write(
temp_dir.path().join("src/main.rs"),
"// Implements [[RFC-0001:C-NONEXISTENT]]\nfn main() {}\n",
)
.unwrap();
let output = run_commands(temp_dir.path(), &[&["check"]]);
insta::assert_snapshot!(normalize_output(&output, temp_dir.path(), &date));
}
#[test]
fn test_scan_deprecated_rfc_reference() {
let temp_dir = init_project();
enable_source_scan(temp_dir.path());
let date = today();
run_commands(
temp_dir.path(),
&[
&["rfc", "new", "Old RFC"],
&["rfc", "finalize", "RFC-0001", "normative"],
&["rfc", "deprecate", "RFC-0001", "--force"],
],
);
fs::create_dir_all(temp_dir.path().join("src")).unwrap();
fs::write(
temp_dir.path().join("src/main.rs"),
"// Implements [[RFC-0001]]\nfn main() {}\n",
)
.unwrap();
let output = run_commands(temp_dir.path(), &[&["check"]]);
insta::assert_snapshot!(normalize_output(&output, temp_dir.path(), &date));
}
#[test]
fn test_scan_valid_adr_reference() {
let temp_dir = init_project();
enable_source_scan(temp_dir.path());
let date = today();
run_commands(
temp_dir.path(),
&[
&["adr", "new", "Test Decision"],
&["adr", "accept", "ADR-0001"],
],
);
fs::create_dir_all(temp_dir.path().join("src")).unwrap();
fs::write(
temp_dir.path().join("src/main.rs"),
"// Follows [[ADR-0001]]\nfn main() {}\n",
)
.unwrap();
let output = run_commands(temp_dir.path(), &[&["check"]]);
insta::assert_snapshot!(normalize_output(&output, temp_dir.path(), &date));
}
#[test]
fn test_scan_valid_work_item_reference() {
let temp_dir = init_project();
enable_source_scan(temp_dir.path());
let date = today();
run_commands(temp_dir.path(), &[&["work", "new", "Test task"]]);
let wi_output = run_commands(temp_dir.path(), &[&["work", "list", "all"]]);
let wi_id = regex::Regex::new(r"WI-\d{4}-\d{2}-\d{2}-\d{3}")
.unwrap()
.find(&wi_output)
.map(|m| m.as_str().to_string())
.expect("No work item ID found");
fs::create_dir_all(temp_dir.path().join("src")).unwrap();
fs::write(
temp_dir.path().join("src/main.rs"),
format!("// Implements [[{}]]\nfn main() {{}}\n", wi_id),
)
.unwrap();
let output = run_commands(temp_dir.path(), &[&["check"]]);
insta::assert_snapshot!(normalize_output(&output, temp_dir.path(), &date));
}
#[test]
fn test_scan_multiple_references_in_file() {
let temp_dir = init_project();
enable_source_scan(temp_dir.path());
let date = today();
run_commands(
temp_dir.path(),
&[
&["rfc", "new", "RFC One"],
&["rfc", "finalize", "RFC-0001", "normative"],
&["rfc", "new", "RFC Two"],
&["rfc", "finalize", "RFC-0002", "normative"],
],
);
fs::create_dir_all(temp_dir.path().join("src")).unwrap();
fs::write(
temp_dir.path().join("src/main.rs"),
"// Implements [[RFC-0001]] and [[RFC-0002]]\nfn main() {}\n",
)
.unwrap();
let output = run_commands(temp_dir.path(), &[&["check"]]);
insta::assert_snapshot!(normalize_output(&output, temp_dir.path(), &date));
}
#[test]
fn test_scan_mixed_valid_invalid_references() {
let temp_dir = init_project();
enable_source_scan(temp_dir.path());
let date = today();
run_commands(
temp_dir.path(),
&[
&["rfc", "new", "Valid RFC"],
&["rfc", "finalize", "RFC-0001", "normative"],
],
);
fs::create_dir_all(temp_dir.path().join("src")).unwrap();
fs::write(
temp_dir.path().join("src/main.rs"),
"// Implements [[RFC-0001]] and [[RFC-9999]]\nfn main() {}\n",
)
.unwrap();
let output = run_commands(temp_dir.path(), &[&["check"]]);
insta::assert_snapshot!(normalize_output(&output, temp_dir.path(), &date));
}