tests.nixpkgs-check-by-name: Intermediate CouldNotParseNix error

This commit is contained in:
Silvan Mosberger 2023-10-19 02:50:15 +02:00
parent 96f6a350fa
commit 9a3abc4383
2 changed files with 37 additions and 26 deletions

View file

@ -1,10 +1,16 @@
use crate::ErrorWriter;
use itertools::{Either, Itertools};
use rnix::parser::ParseError;
use std::fmt;
use std::io;
use std::path::PathBuf;
pub enum CheckError {
CouldNotParseNix {
relative_package_dir: PathBuf,
subpath: PathBuf,
error: ParseError,
},
PathInterpolation {
relative_package_dir: PathBuf,
subpath: PathBuf,
@ -41,6 +47,14 @@ impl CheckError {
impl fmt::Display for CheckError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
CheckError::CouldNotParseNix { relative_package_dir, subpath, error } =>
write!(
f,
"{}: File {} could not be parsed by rnix: {}",
relative_package_dir.display(),
subpath.display(),
error,
),
CheckError::PathInterpolation { relative_package_dir, subpath, line, text } =>
write!(
f,

View file

@ -1,4 +1,6 @@
use crate::check_result::{pass, write_check_result, CheckError};
use crate::check_result::{
flatten_check_results, pass, write_check_result, CheckError, CheckResult,
};
use crate::structure::Nixpkgs;
use crate::utils;
use crate::utils::{ErrorWriter, LineIndex};
@ -81,10 +83,11 @@ fn check_path<W: io::Write>(context: &mut PackageContext<W>, subpath: &Path) ->
// Only check Nix files
if let Some(ext) = path.extension() {
if ext == OsStr::new("nix") {
check_nix_file(context, subpath).context(format!(
let check_result = check_nix_file(context, subpath).context(format!(
"Error while checking Nix file {}",
subpath.display()
))?
));
write_check_result(context.error_writer, check_result)?;
}
}
} else {
@ -99,7 +102,7 @@ fn check_path<W: io::Write>(context: &mut PackageContext<W>, subpath: &Path) ->
fn check_nix_file<W: io::Write>(
context: &mut PackageContext<W>,
subpath: &Path,
) -> anyhow::Result<()> {
) -> CheckResult<()> {
let path = context.absolute_package_dir.join(subpath);
let parent_dir = path.parent().context(format!(
"Could not get parent of path {}",
@ -111,29 +114,26 @@ fn check_nix_file<W: io::Write>(
let root = Root::parse(&contents);
if let Some(error) = root.errors().first() {
context.error_writer.write(&format!(
"{}: File {} could not be parsed by rnix: {}",
context.relative_package_dir.display(),
subpath.display(),
error,
))?;
return Ok(());
return CheckError::CouldNotParseNix {
relative_package_dir: context.relative_package_dir.clone(),
subpath: subpath.to_path_buf(),
error: error.clone(),
}
.into_result();
}
let line_index = LineIndex::new(&contents);
for node in root.syntax().descendants() {
// We're only interested in Path expressions
if node.kind() != NODE_PATH {
continue;
}
let check_results = root.syntax().descendants().map(|node| {
let text = node.text().to_string();
let line = line_index.line(node.text_range().start().into());
if node.kind() != NODE_PATH {
// We're only interested in Path expressions
pass(())
} else if node.children().count() != 0 {
// Filters out ./foo/${bar}/baz
// TODO: We can just check ./foo
let check_result = if node.children().count() != 0 {
CheckError::PathInterpolation {
relative_package_dir: context.relative_package_dir.clone(),
subpath: subpath.to_path_buf(),
@ -179,10 +179,7 @@ fn check_nix_file<W: io::Write>(
}
.into_result(),
}
};
write_check_result(context.error_writer, check_result)?;
}
Ok(())
});
flatten_check_results(check_results, |_| ())
}