feat(xatu): report deprecated toPath calls
This commit was merged in pull request #75.
This commit is contained in:
@@ -1,53 +1,114 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use googletest::matchers::{contains, eq, is_true};
|
||||
use googletest::{OrFail, expect_that, gtest};
|
||||
use status::Code;
|
||||
use xatu::SourceFile;
|
||||
|
||||
fn source_file(path: &str) -> SourceFile {
|
||||
SourceFile {
|
||||
fn fixture_source(path: &str) -> googletest::Result<SourceFile> {
|
||||
Ok(SourceFile {
|
||||
path: PathBuf::from(path),
|
||||
source: std::fs::read_to_string(testdata_path(path)).expect("fixture should be readable"),
|
||||
}
|
||||
source: std::fs::read_to_string(fixture_path(path)?).or_fail()?,
|
||||
})
|
||||
}
|
||||
|
||||
fn testdata_path(path: &str) -> PathBuf {
|
||||
fn fixture_path(path: &str) -> googletest::Result<PathBuf> {
|
||||
if Path::new(path).exists() {
|
||||
return PathBuf::from(path);
|
||||
return Ok(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");
|
||||
let runfiles = PathBuf::from(std::env::var("TEST_SRCDIR").or_fail()?);
|
||||
let workspace = std::env::var("TEST_WORKSPACE").or_fail()?;
|
||||
|
||||
runfiles.join(workspace).join("src/xatu").join(path)
|
||||
Ok(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("testdata/clean.nix")])
|
||||
.expect("fixture should parse");
|
||||
#[gtest]
|
||||
fn xatu_accepts_clean_source() -> googletest::Result<()> {
|
||||
let report = xatu::future_sight(vec![fixture_source("testdata/clean.nix")?]).or_fail()?;
|
||||
|
||||
assert!(report.is_clean());
|
||||
expect_that!(report.is_clean(), is_true());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn future_sight_rejects_parse_errors() {
|
||||
let error = xatu::future_sight(vec![source_file("testdata/invalid.nix")])
|
||||
.expect_err("fixture should fail to parse");
|
||||
#[gtest]
|
||||
fn xatu_rejects_invalid_source() -> googletest::Result<()> {
|
||||
let error = xatu::future_sight(vec![fixture_source("testdata/invalid.nix")?])
|
||||
.err()
|
||||
.or_fail()?;
|
||||
|
||||
assert_eq!(error.code(), Code::FailedPrecondition);
|
||||
expect_that!(error.code(), eq(Code::FailedPrecondition));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn future_sight_reports_empty_inherit_findings() {
|
||||
let report = xatu::future_sight(vec![source_file("testdata/empty_inherit.nix")])
|
||||
.expect("fixture should parse");
|
||||
#[gtest]
|
||||
fn xatu_reports_deprecated_builtin_to_path() -> googletest::Result<()> {
|
||||
let report = xatu::future_sight(vec![fixture_source("testdata/lint_findings.nix")?]).or_fail()?;
|
||||
|
||||
assert_eq!(report.diagnostics.len(), 1);
|
||||
|
||||
let diagnostic = &report.diagnostics[0];
|
||||
assert_eq!(
|
||||
report.files.get(diagnostic.file_id).path,
|
||||
PathBuf::from("testdata/empty_inherit.nix"),
|
||||
let findings = report
|
||||
.diagnostics
|
||||
.iter()
|
||||
.map(|diagnostic| {
|
||||
(
|
||||
report.files.get(diagnostic.file_id).path.clone(),
|
||||
diagnostic.finding.rule_id,
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
expect_that!(
|
||||
findings,
|
||||
contains(eq(&(
|
||||
PathBuf::from("testdata/lint_findings.nix"),
|
||||
"deprecated_to_path"
|
||||
)))
|
||||
);
|
||||
assert_eq!(diagnostic.finding.rule_id, "empty_inherit");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[gtest]
|
||||
fn xatu_reports_deprecated_unqualified_to_path() -> googletest::Result<()> {
|
||||
let report = xatu::future_sight(vec![fixture_source("testdata/lint_variants.nix")?]).or_fail()?;
|
||||
|
||||
let findings = report
|
||||
.diagnostics
|
||||
.iter()
|
||||
.map(|diagnostic| {
|
||||
(
|
||||
report.files.get(diagnostic.file_id).path.clone(),
|
||||
diagnostic.finding.rule_id,
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
expect_that!(
|
||||
findings,
|
||||
contains(eq(&(
|
||||
PathBuf::from("testdata/lint_variants.nix"),
|
||||
"deprecated_to_path"
|
||||
)))
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[gtest]
|
||||
fn xatu_reports_empty_inherit() -> googletest::Result<()> {
|
||||
let report = xatu::future_sight(vec![fixture_source("testdata/lint_findings.nix")?]).or_fail()?;
|
||||
|
||||
let findings = report
|
||||
.diagnostics
|
||||
.iter()
|
||||
.map(|diagnostic| {
|
||||
(
|
||||
report.files.get(diagnostic.file_id).path.clone(),
|
||||
diagnostic.finding.rule_id,
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
expect_that!(
|
||||
findings,
|
||||
contains(eq(&(
|
||||
PathBuf::from("testdata/lint_findings.nix"),
|
||||
"empty_inherit"
|
||||
)))
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user