about summary refs log tree commit diff
path: root/pkgs/test
diff options
context:
space:
mode:
authorSilvan Mosberger <silvan.mosberger@tweag.io>2023-10-19 02:50:15 +0200
committerSilvan Mosberger <silvan.mosberger@tweag.io>2023-10-24 01:15:57 +0200
commit9a3abc4383da1a430525902b87db02ddcfc279ee (patch)
treeed7d41bc86f7f18e859a964ba5509baf611df43c /pkgs/test
parent96f6a350fa74e995dbbf750b17cad2d6cbb3186e (diff)
tests.nixpkgs-check-by-name: Intermediate CouldNotParseNix error
Diffstat (limited to 'pkgs/test')
-rw-r--r--pkgs/test/nixpkgs-check-by-name/src/check_result.rs14
-rw-r--r--pkgs/test/nixpkgs-check-by-name/src/references.rs49
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 ed00f383f193c..42468a29e809a 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 6cc933e721a35..9c88507ff99b1 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<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());
 
-        // 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<W: io::Write>(
                 }
                 .into_result(),
             }
-        };
-
-        write_check_result(context.error_writer, check_result)?;
-    }
-
-    Ok(())
+        }
+    });
+    flatten_check_results(check_results, |_| ())
 }