diff --git a/src/xatu/BUILD b/src/xatu/BUILD index 8a1f8fb..a1ed5a8 100644 --- a/src/xatu/BUILD +++ b/src/xatu/BUILD @@ -28,6 +28,10 @@ rust_test( rust_test( name = "xatu_test", srcs = ["xatu_test.rs"], + data = [ + "testdata/clean.nix", + "testdata/empty_inherit.nix", + ], deps = [ ":xatu_lib", "//src/status:status", diff --git a/src/xatu/testdata/clean.nix b/src/xatu/testdata/clean.nix new file mode 100644 index 0000000..596f5b2 --- /dev/null +++ b/src/xatu/testdata/clean.nix @@ -0,0 +1,4 @@ +{ + path = ./. + "/foo"; + inherit foo; +} diff --git a/src/xatu/testdata/empty_inherit.nix b/src/xatu/testdata/empty_inherit.nix new file mode 100644 index 0000000..dc31afc --- /dev/null +++ b/src/xatu/testdata/empty_inherit.nix @@ -0,0 +1,3 @@ +{ + inherit; +} diff --git a/src/xatu/xatu_test.rs b/src/xatu/xatu_test.rs index 54d022f..010428d 100644 --- a/src/xatu/xatu_test.rs +++ b/src/xatu/xatu_test.rs @@ -1,4 +1,4 @@ -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use status::Code; use xatu::SourceFile; @@ -10,17 +10,28 @@ fn source_file(path: &str, source: &str) -> SourceFile { } } +fn testdata_source_file(path: &str) -> SourceFile { + source_file( + path, + &std::fs::read_to_string(testdata_path(path)).expect("fixture should be readable"), + ) +} + +fn testdata_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) +} + #[test] fn future_sight_reports_clean_sources_as_clean() { - let report = xatu::future_sight(vec![source_file( - "clean.nix", - r#"{ - path = ./. + "/foo"; - inherit foo; -} -"#, - )]) - .expect("fixture should parse"); + let report = xatu::future_sight(vec![testdata_source_file("testdata/clean.nix")]) + .expect("fixture should parse"); assert!(report.is_clean()); } @@ -71,18 +82,15 @@ fn future_sight_reports_deprecated_to_path_select_findings() { #[test] fn future_sight_reports_empty_inherit_findings() { - let report = xatu::future_sight(vec![source_file( - "empty_inherit.nix", - "{\n inherit;\n}\n", - )]) - .expect("fixture should parse"); + let report = xatu::future_sight(vec![testdata_source_file("testdata/empty_inherit.nix")]) + .expect("fixture should parse"); assert_eq!(report.diagnostics.len(), 1); let diagnostic = &report.diagnostics[0]; assert_eq!( report.files.get(diagnostic.file_id).path, - PathBuf::from("empty_inherit.nix"), + PathBuf::from("testdata/empty_inherit.nix"), ); assert_eq!(diagnostic.finding.rule_id, "empty_inherit"); }