mod common;
use common::{init_project, normalize_output, run_commands, today};
use std::fs;
fn enable_source_scan(dir: &std::path::Path) -> Result<(), Box<dyn std::error::Error>> {
let config_path = dir.join("gov/config.toml");
let content = fs::read_to_string(&config_path)?;
let mut doc: toml::Table = toml::from_str(&content)?;
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)?)?;
Ok(())
}
#[test]
fn test_scan_no_references() -> common::TestResult {
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)?);
Ok(())
}
#[test]
fn test_scan_valid_rfc_reference() -> common::TestResult {
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"))?;
fs::write(
temp_dir.path().join("src/main.rs"),
"// Implements [[RFC-0001]]\nfn main() {}\n",
)?;
let output = run_commands(temp_dir.path(), &[&["check"]])?;
insta::assert_snapshot!(normalize_output(&output, temp_dir.path(), &date)?);
Ok(())
}
#[test]
fn test_scan_valid_clause_reference() -> common::TestResult {
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"))?;
fs::write(
temp_dir.path().join("src/main.rs"),
"// Implements [[RFC-0001:C-TEST]]\nfn main() {}\n",
)?;
let output = run_commands(temp_dir.path(), &[&["check"]])?;
insta::assert_snapshot!(normalize_output(&output, temp_dir.path(), &date)?);
Ok(())
}
#[test]
fn test_scan_unknown_rfc_reference() -> common::TestResult {
let temp_dir = init_project()?;
enable_source_scan(temp_dir.path())?;
let date = today();
fs::create_dir_all(temp_dir.path().join("src"))?;
fs::write(
temp_dir.path().join("src/main.rs"),
"// Implements [[RFC-9999]]\nfn main() {}\n",
)?;
let output = run_commands(temp_dir.path(), &[&["check"]])?;
insta::assert_snapshot!(normalize_output(&output, temp_dir.path(), &date)?);
Ok(())
}
#[test]
fn test_scan_unknown_clause_reference() -> common::TestResult {
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"))?;
fs::write(
temp_dir.path().join("src/main.rs"),
"// Implements [[RFC-0001:C-NONEXISTENT]]\nfn main() {}\n",
)?;
let output = run_commands(temp_dir.path(), &[&["check"]])?;
insta::assert_snapshot!(normalize_output(&output, temp_dir.path(), &date)?);
Ok(())
}
#[test]
fn test_scan_deprecated_rfc_reference() -> common::TestResult {
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"))?;
fs::write(
temp_dir.path().join("src/main.rs"),
"// Implements [[RFC-0001]]\nfn main() {}\n",
)?;
let output = run_commands(temp_dir.path(), &[&["check"]])?;
insta::assert_snapshot!(normalize_output(&output, temp_dir.path(), &date)?);
Ok(())
}
#[test]
fn test_scan_valid_adr_reference() -> common::TestResult {
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"))?;
fs::write(
temp_dir.path().join("src/main.rs"),
"// Follows [[ADR-0001]]\nfn main() {}\n",
)?;
let output = run_commands(temp_dir.path(), &[&["check"]])?;
insta::assert_snapshot!(normalize_output(&output, temp_dir.path(), &date)?);
Ok(())
}
#[test]
fn test_scan_valid_work_item_reference() -> common::TestResult {
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}")?
.find(&wi_output)
.ok_or("No work item ID found")?
.as_str()
.to_string();
fs::create_dir_all(temp_dir.path().join("src"))?;
fs::write(
temp_dir.path().join("src/main.rs"),
format!("// Implements [[{}]]\nfn main() {{}}\n", wi_id),
)?;
let output = run_commands(temp_dir.path(), &[&["check"]])?;
insta::assert_snapshot!(normalize_output(&output, temp_dir.path(), &date)?);
Ok(())
}
#[test]
fn test_scan_multiple_references_in_file() -> common::TestResult {
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"))?;
fs::write(
temp_dir.path().join("src/main.rs"),
"// Implements [[RFC-0001]] and [[RFC-0002]]\nfn main() {}\n",
)?;
let output = run_commands(temp_dir.path(), &[&["check"]])?;
insta::assert_snapshot!(normalize_output(&output, temp_dir.path(), &date)?);
Ok(())
}
#[test]
fn test_scan_mixed_valid_invalid_references() -> common::TestResult {
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"))?;
fs::write(
temp_dir.path().join("src/main.rs"),
"// Implements [[RFC-0001]] and [[RFC-9999]]\nfn main() {}\n",
)?;
let output = run_commands(temp_dir.path(), &[&["check"]])?;
insta::assert_snapshot!(normalize_output(&output, temp_dir.path(), &date)?);
Ok(())
}