use std::path::{Path, PathBuf}; use status::Code; use xatu::SourceFile; fn fixture_source(path: &str) -> SourceFile { SourceFile { path: PathBuf::from(path), source: std::fs::read_to_string(fixture_path(path)).expect("fixture should be readable"), } } fn fixture_path(path: &str) -> PathBuf { if Path::new(path).exists() { return PathBuf::from(path); } let runfiles = PathBuf::from(std::env::var("TEST_SRCDIR").expect("TEST_SRCDIR should be set")); let workspace = std::env::var("TEST_WORKSPACE").expect("TEST_WORKSPACE should be set"); runfiles.join(workspace).join("src/xatu").join(path) } fn fixture_report(path: &str) -> xatu::Report { xatu::future_sight(vec![fixture_source(path)]).expect("fixture should parse") } fn has_finding(report: &xatu::Report, path: &str, rule_id: &str) -> bool { report.diagnostics.iter().any(|diagnostic| { report.files.get(diagnostic.file_id).path.as_path() == Path::new(path) && diagnostic.finding.rule_id == rule_id }) } #[test] fn future_sight_reports_clean_sources_as_clean() { let report = fixture_report("testdata/clean.nix"); assert!(report.is_clean()); } #[test] fn future_sight_rejects_parse_errors() { let error = xatu::future_sight(vec![fixture_source("testdata/invalid.nix")]) .expect_err("fixture should fail to parse"); assert_eq!(error.code(), Code::FailedPrecondition); } #[test] fn future_sight_reports_deprecated_to_path_qualified_findings() { let report = fixture_report("testdata/lint_findings.nix"); assert!(has_finding( &report, "testdata/lint_findings.nix", "deprecated_to_path", )); } #[test] fn future_sight_reports_deprecated_to_path_unqualified_findings() { let report = fixture_report("testdata/lint_variants.nix"); assert!(has_finding( &report, "testdata/lint_variants.nix", "deprecated_to_path", )); } #[test] fn future_sight_reports_empty_inherit_findings() { let report = fixture_report("testdata/lint_findings.nix"); assert!(has_finding( &report, "testdata/lint_findings.nix", "empty_inherit", )); }