test(xatu): load source fixtures by path
This commit is contained in:
@@ -27,7 +27,7 @@ rust_test(
|
|||||||
rust_test(
|
rust_test(
|
||||||
name = "xatu_test",
|
name = "xatu_test",
|
||||||
srcs = ["xatu_test.rs"],
|
srcs = ["xatu_test.rs"],
|
||||||
compile_data = [
|
data = [
|
||||||
"testdata/clean.nix",
|
"testdata/clean.nix",
|
||||||
"testdata/empty_inherit.nix",
|
"testdata/empty_inherit.nix",
|
||||||
"testdata/invalid.nix",
|
"testdata/invalid.nix",
|
||||||
|
|||||||
@@ -1,21 +1,29 @@
|
|||||||
use std::path::PathBuf;
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use status::Code;
|
use status::Code;
|
||||||
use xatu::SourceFile;
|
use xatu::SourceFile;
|
||||||
|
|
||||||
fn source_file(path: &str, source: &str) -> SourceFile {
|
fn source_file(path: &str) -> SourceFile {
|
||||||
SourceFile {
|
SourceFile {
|
||||||
path: PathBuf::from(path),
|
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]
|
#[test]
|
||||||
fn future_sight_reports_clean_sources_as_clean() {
|
fn future_sight_reports_clean_sources_as_clean() {
|
||||||
let report = xatu::future_sight(vec![source_file(
|
let report = xatu::future_sight(vec![source_file("testdata/clean.nix")])
|
||||||
"clean.nix",
|
|
||||||
include_str!("testdata/clean.nix"),
|
|
||||||
)])
|
|
||||||
.expect("fixture should parse");
|
.expect("fixture should parse");
|
||||||
|
|
||||||
assert!(report.is_clean());
|
assert!(report.is_clean());
|
||||||
@@ -23,10 +31,7 @@ fn future_sight_reports_clean_sources_as_clean() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn future_sight_rejects_parse_errors() {
|
fn future_sight_rejects_parse_errors() {
|
||||||
let error = xatu::future_sight(vec![source_file(
|
let error = xatu::future_sight(vec![source_file("testdata/invalid.nix")])
|
||||||
"invalid.nix",
|
|
||||||
include_str!("testdata/invalid.nix"),
|
|
||||||
)])
|
|
||||||
.expect_err("fixture should fail to parse");
|
.expect_err("fixture should fail to parse");
|
||||||
|
|
||||||
assert_eq!(Code::FailedPrecondition, error.code());
|
assert_eq!(Code::FailedPrecondition, error.code());
|
||||||
@@ -34,17 +39,14 @@ fn future_sight_rejects_parse_errors() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn future_sight_reports_empty_inherit_findings() {
|
fn future_sight_reports_empty_inherit_findings() {
|
||||||
let report = xatu::future_sight(vec![source_file(
|
let report = xatu::future_sight(vec![source_file("testdata/empty_inherit.nix")])
|
||||||
"empty_inherit.nix",
|
|
||||||
include_str!("testdata/empty_inherit.nix"),
|
|
||||||
)])
|
|
||||||
.expect("fixture should parse");
|
.expect("fixture should parse");
|
||||||
|
|
||||||
assert_eq!(1, report.diagnostics.len());
|
assert_eq!(1, report.diagnostics.len());
|
||||||
|
|
||||||
let diagnostic = &report.diagnostics[0];
|
let diagnostic = &report.diagnostics[0];
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
PathBuf::from("empty_inherit.nix"),
|
PathBuf::from("testdata/empty_inherit.nix"),
|
||||||
report.files.get(diagnostic.file_id).path,
|
report.files.get(diagnostic.file_id).path,
|
||||||
);
|
);
|
||||||
assert_eq!("empty_inherit", diagnostic.finding.rule_id);
|
assert_eq!("empty_inherit", diagnostic.finding.rule_id);
|
||||||
|
|||||||
Reference in New Issue
Block a user