184 lines
5.1 KiB
Rust
184 lines
5.1 KiB
Rust
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 fixture_source(path: &str) -> googletest::Result<SourceFile> {
|
|
Ok(SourceFile {
|
|
path: PathBuf::from(path),
|
|
source: std::fs::read_to_string(fixture_path(path)?).or_fail()?,
|
|
})
|
|
}
|
|
|
|
fn fixture_path(path: &str) -> googletest::Result<PathBuf> {
|
|
if Path::new(path).exists() {
|
|
return Ok(PathBuf::from(path));
|
|
}
|
|
|
|
let runfiles = PathBuf::from(std::env::var("TEST_SRCDIR").or_fail()?);
|
|
let workspace = std::env::var("TEST_WORKSPACE").or_fail()?;
|
|
|
|
Ok(runfiles.join(workspace).join("src/xatu").join(path))
|
|
}
|
|
|
|
#[gtest]
|
|
fn xatu_accepts_clean_source() -> googletest::Result<()> {
|
|
let report = xatu::future_sight(vec![fixture_source("testdata/clean.nix")?]).or_fail()?;
|
|
|
|
expect_that!(report.is_clean(), is_true());
|
|
Ok(())
|
|
}
|
|
|
|
#[gtest]
|
|
fn xatu_rejects_invalid_source() -> googletest::Result<()> {
|
|
let error = xatu::future_sight(vec![fixture_source("testdata/invalid.nix")?])
|
|
.err()
|
|
.or_fail()?;
|
|
|
|
expect_that!(error.code(), eq(Code::FailedPrecondition));
|
|
Ok(())
|
|
}
|
|
|
|
#[gtest]
|
|
fn xatu_reports_deprecated_builtin_to_path() -> 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"),
|
|
"deprecated_to_path"
|
|
)))
|
|
);
|
|
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(())
|
|
}
|
|
|
|
#[gtest]
|
|
fn xatu_reports_empty_let_in() -> googletest::Result<()> {
|
|
let source = fixture_source("testdata/lint_findings.nix")?;
|
|
let range_start = source.source.find("let in src").or_fail()?;
|
|
let range_end = range_start + "let in src".len();
|
|
let report = xatu::future_sight(vec![source]).or_fail()?;
|
|
|
|
let finding = report
|
|
.diagnostics
|
|
.iter()
|
|
.map(|diagnostic| &diagnostic.finding)
|
|
.find(|finding| finding.rule_id == "empty_let_in")
|
|
.or_fail()?;
|
|
let expected_range = format!("{range_start}..{range_end}");
|
|
expect_that!(finding.range.to_string(), eq(&expected_range));
|
|
Ok(())
|
|
}
|
|
|
|
#[gtest]
|
|
fn xatu_reports_commented_empty_let_in() -> googletest::Result<()> {
|
|
let report = xatu::future_sight(vec![fixture_source("testdata/lint_variants.nix")?]).or_fail()?;
|
|
|
|
let findings = report
|
|
.diagnostics
|
|
.iter()
|
|
.map(|diagnostic| diagnostic.finding.rule_id)
|
|
.collect::<Vec<_>>();
|
|
expect_that!(findings, contains(eq(&"empty_let_in")));
|
|
Ok(())
|
|
}
|
|
|
|
#[gtest]
|
|
fn xatu_reports_empty_let_with_empty_inherit() -> googletest::Result<()> {
|
|
let report =
|
|
xatu::future_sight(vec![fixture_source("testdata/lint_interactions.nix")?]).or_fail()?;
|
|
|
|
let findings = report
|
|
.diagnostics
|
|
.iter()
|
|
.map(|diagnostic| diagnostic.finding.rule_id)
|
|
.collect::<Vec<_>>();
|
|
expect_that!(findings, eq(&vec!["empty_let_in", "empty_inherit"]));
|
|
Ok(())
|
|
}
|
|
|
|
#[gtest]
|
|
fn xatu_reports_unquoted_uri() -> 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"),
|
|
"unquoted_uri"
|
|
)))
|
|
);
|
|
Ok(())
|
|
}
|