test(xatu): load source fixtures by path

This commit is contained in:
2026-09-18 23:21:38 -04:00
parent 1040aa258e
commit 1af403cd87
2 changed files with 19 additions and 17 deletions

View File

@@ -27,7 +27,7 @@ rust_test(
rust_test(
name = "xatu_test",
srcs = ["xatu_test.rs"],
compile_data = [
data = [
"testdata/clean.nix",
"testdata/empty_inherit.nix",
"testdata/invalid.nix",

View File

@@ -1,21 +1,29 @@
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use status::Code;
use xatu::SourceFile;
fn source_file(path: &str, source: &str) -> SourceFile {
fn source_file(path: &str) -> SourceFile {
SourceFile {
path: PathBuf::from(path),
source: source.to_owned(),
source: 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",
include_str!("testdata/clean.nix"),
)])
let report = xatu::future_sight(vec![source_file("testdata/clean.nix")])
.expect("fixture should parse");
assert!(report.is_clean());
@@ -23,10 +31,7 @@ fn future_sight_reports_clean_sources_as_clean() {
#[test]
fn future_sight_rejects_parse_errors() {
let error = xatu::future_sight(vec![source_file(
"invalid.nix",
include_str!("testdata/invalid.nix"),
)])
let error = xatu::future_sight(vec![source_file("testdata/invalid.nix")])
.expect_err("fixture should fail to parse");
assert_eq!(Code::FailedPrecondition, error.code());
@@ -34,17 +39,14 @@ fn future_sight_rejects_parse_errors() {
#[test]
fn future_sight_reports_empty_inherit_findings() {
let report = xatu::future_sight(vec![source_file(
"empty_inherit.nix",
include_str!("testdata/empty_inherit.nix"),
)])
let report = xatu::future_sight(vec![source_file("testdata/empty_inherit.nix")])
.expect("fixture should parse");
assert_eq!(1, report.diagnostics.len());
let diagnostic = &report.diagnostics[0];
assert_eq!(
PathBuf::from("empty_inherit.nix"),
PathBuf::from("testdata/empty_inherit.nix"),
report.files.get(diagnostic.file_id).path,
);
assert_eq!("empty_inherit", diagnostic.finding.rule_id);