From 9a3abc4383da1a430525902b87db02ddcfc279ee Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Thu, 19 Oct 2023 02:50:15 +0200 Subject: [PATCH] tests.nixpkgs-check-by-name: Intermediate CouldNotParseNix error --- .../nixpkgs-check-by-name/src/check_result.rs | 14 ++++++ .../nixpkgs-check-by-name/src/references.rs | 49 +++++++++---------- 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/pkgs/test/nixpkgs-check-by-name/src/check_result.rs b/pkgs/test/nixpkgs-check-by-name/src/check_result.rs index ed00f383f193..42468a29e809 100644 --- a/pkgs/test/nixpkgs-check-by-name/src/check_result.rs +++ b/pkgs/test/nixpkgs-check-by-name/src/check_result.rs @@ -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, diff --git a/pkgs/test/nixpkgs-check-by-name/src/references.rs b/pkgs/test/nixpkgs-check-by-name/src/references.rs index 6cc933e721a3..9c88507ff99b 100644 --- a/pkgs/test/nixpkgs-check-by-name/src/references.rs +++ b/pkgs/test/nixpkgs-check-by-name/src/references.rs @@ -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(context: &mut PackageContext, 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(context: &mut PackageContext, subpath: &Path) -> fn check_nix_file( context: &mut PackageContext, 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( 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()); - // Filters out ./foo/${bar}/baz - // TODO: We can just check ./foo - let check_result = if node.children().count() != 0 { + 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 CheckError::PathInterpolation { relative_package_dir: context.relative_package_dir.clone(), subpath: subpath.to_path_buf(), @@ -179,10 +179,7 @@ fn check_nix_file( } .into_result(), } - }; - - write_check_result(context.error_writer, check_result)?; - } - - Ok(()) + } + }); + flatten_check_results(check_results, |_| ()) }