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 content = fs::read_to_string(&config_path).unwrap();
let mut doc: toml::Table = toml::from_str(&content).unwrap();
let mut scan = toml::Table::new();
scan.insert("enabled".into(), toml::Value::Boolean(true));
scan.insert(
"include".into(),
toml::Value::Array(vec![toml::Value::String("src/**/*.rs".into())]),
);
scan.insert("exclude".into(), toml::Value::Array(vec![]));
doc.insert("source_scan".into(), toml::Value::Table(scan));
fs::write(&config_path, toml::to_string_pretty(&doc).unwrap()).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));
}