test(xatu): simplify future sight tests
This commit is contained in:
@@ -3,21 +3,14 @@ use std::path::{Path, PathBuf};
|
||||
use status::Code;
|
||||
use xatu::SourceFile;
|
||||
|
||||
fn source_file(path: &str, source: &str) -> SourceFile {
|
||||
fn fixture_source(path: &str) -> SourceFile {
|
||||
SourceFile {
|
||||
path: PathBuf::from(path),
|
||||
source: source.to_owned(),
|
||||
source: std::fs::read_to_string(fixture_path(path)).expect("fixture should be readable"),
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
fn fixture_path(path: &str) -> PathBuf {
|
||||
if Path::new(path).exists() {
|
||||
return PathBuf::from(path);
|
||||
}
|
||||
@@ -28,9 +21,14 @@ fn testdata_path(path: &str) -> PathBuf {
|
||||
runfiles.join(workspace).join("src/xatu").join(path)
|
||||
}
|
||||
|
||||
fn only_diagnostic(report: &xatu::Report) -> &xatu::Diagnostic {
|
||||
assert_eq!(report.diagnostics.len(), 1);
|
||||
&report.diagnostics[0]
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn future_sight_reports_clean_sources_as_clean() {
|
||||
let report = xatu::future_sight(vec![testdata_source_file("testdata/clean.nix")])
|
||||
let report = xatu::future_sight(vec![fixture_source("testdata/clean.nix")])
|
||||
.expect("fixture should parse");
|
||||
|
||||
assert!(report.is_clean());
|
||||
@@ -38,59 +36,47 @@ 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", "{\n")])
|
||||
.expect_err("fixture should fail to parse");
|
||||
let error = xatu::future_sight(vec![SourceFile {
|
||||
path: PathBuf::from("invalid.nix"),
|
||||
source: "{\n".to_owned(),
|
||||
}])
|
||||
.expect_err("fixture should fail to parse");
|
||||
|
||||
assert_eq!(error.code(), Code::FailedPrecondition);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn future_sight_reports_deprecated_to_path_ident_findings() {
|
||||
let report = xatu::future_sight(vec![source_file(
|
||||
"deprecated_to_path_ident.nix",
|
||||
"toPath x\n",
|
||||
)])
|
||||
let report = xatu::future_sight(vec![SourceFile {
|
||||
path: PathBuf::from("deprecated_to_path_ident.nix"),
|
||||
source: "toPath x\n".to_owned(),
|
||||
}])
|
||||
.expect("fixture should parse");
|
||||
|
||||
assert_eq!(report.diagnostics.len(), 1);
|
||||
let diagnostic = only_diagnostic(&report);
|
||||
|
||||
let diagnostic = &report.diagnostics[0];
|
||||
assert_eq!(
|
||||
report.files.get(diagnostic.file_id).path,
|
||||
PathBuf::from("deprecated_to_path_ident.nix"),
|
||||
);
|
||||
assert_eq!(diagnostic.finding.rule_id, "deprecated_to_path");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn future_sight_reports_deprecated_to_path_select_findings() {
|
||||
let report = xatu::future_sight(vec![source_file(
|
||||
"deprecated_to_path_select.nix",
|
||||
"builtins.toPath x\n",
|
||||
)])
|
||||
let report = xatu::future_sight(vec![SourceFile {
|
||||
path: PathBuf::from("deprecated_to_path_select.nix"),
|
||||
source: "builtins.toPath x\n".to_owned(),
|
||||
}])
|
||||
.expect("fixture should parse");
|
||||
|
||||
assert_eq!(report.diagnostics.len(), 1);
|
||||
let diagnostic = only_diagnostic(&report);
|
||||
|
||||
let diagnostic = &report.diagnostics[0];
|
||||
assert_eq!(
|
||||
report.files.get(diagnostic.file_id).path,
|
||||
PathBuf::from("deprecated_to_path_select.nix"),
|
||||
);
|
||||
assert_eq!(diagnostic.finding.rule_id, "deprecated_to_path");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn future_sight_reports_empty_inherit_findings() {
|
||||
let report = xatu::future_sight(vec![testdata_source_file("testdata/empty_inherit.nix")])
|
||||
let report = xatu::future_sight(vec![fixture_source("testdata/empty_inherit.nix")])
|
||||
.expect("fixture should parse");
|
||||
|
||||
assert_eq!(report.diagnostics.len(), 1);
|
||||
let diagnostic = only_diagnostic(&report);
|
||||
|
||||
let diagnostic = &report.diagnostics[0];
|
||||
assert_eq!(
|
||||
report.files.get(diagnostic.file_id).path,
|
||||
PathBuf::from("testdata/empty_inherit.nix"),
|
||||
);
|
||||
assert_eq!(diagnostic.finding.rule_id, "empty_inherit");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user