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 23:54:34 +0200
committerSilvan Mosberger <silvan.mosberger@tweag.io>2023-10-24 01:15:57 +0200
commite3979d14cda13b4a6cb15ebccf6052cc67c0db4c (patch)
tree07a16af0b194787a19f8be68e148d31a6148949f /pkgs/test
parent4f17b9367d7535fc5308557b92e1b306662b3d3d (diff)
downloadnixlib-e3979d14cda13b4a6cb15ebccf6052cc67c0db4c.tar
nixlib-e3979d14cda13b4a6cb15ebccf6052cc67c0db4c.tar.gz
nixlib-e3979d14cda13b4a6cb15ebccf6052cc67c0db4c.tar.bz2
nixlib-e3979d14cda13b4a6cb15ebccf6052cc67c0db4c.tar.lz
nixlib-e3979d14cda13b4a6cb15ebccf6052cc67c0db4c.tar.xz
nixlib-e3979d14cda13b4a6cb15ebccf6052cc67c0db4c.tar.zst
nixlib-e3979d14cda13b4a6cb15ebccf6052cc67c0db4c.zip
tests.nixpkgs-check-by-name: Intermediate PackageNixDir error
Diffstat (limited to 'pkgs/test')
-rw-r--r--pkgs/test/nixpkgs-check-by-name/src/check_result.rs10
-rw-r--r--pkgs/test/nixpkgs-check-by-name/src/eval.rs20
-rw-r--r--pkgs/test/nixpkgs-check-by-name/src/main.rs20
-rw-r--r--pkgs/test/nixpkgs-check-by-name/src/structure.rs15
-rw-r--r--pkgs/test/nixpkgs-check-by-name/src/utils.rs3
5 files changed, 41 insertions, 27 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 f546b1285450..13d780cfd56e 100644
--- a/pkgs/test/nixpkgs-check-by-name/src/check_result.rs
+++ b/pkgs/test/nixpkgs-check-by-name/src/check_result.rs
@@ -1,3 +1,4 @@
+use crate::utils::PACKAGE_NIX_FILENAME;
 use crate::ErrorWriter;
 use itertools::{Either, Itertools};
 use rnix::parser::ParseError;
@@ -6,6 +7,9 @@ use std::io;
 use std::path::PathBuf;
 
 pub enum CheckError {
+    PackageNixDir {
+        relative_package_dir: PathBuf,
+    },
     UndefinedAttr {
         relative_package_file: PathBuf,
         package_name: String,
@@ -68,6 +72,12 @@ impl CheckError {
 impl fmt::Display for CheckError {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         match self {
+            CheckError::PackageNixDir { relative_package_dir } =>
+                write!(
+                    f,
+                    "{}: \"{PACKAGE_NIX_FILENAME}\" must be a file.",
+                    relative_package_dir.display(),
+                ),
             CheckError::UndefinedAttr { relative_package_file, package_name } =>
                 write!(
                     f,
diff --git a/pkgs/test/nixpkgs-check-by-name/src/eval.rs b/pkgs/test/nixpkgs-check-by-name/src/eval.rs
index 81cf43a381ed..eb90a295a577 100644
--- a/pkgs/test/nixpkgs-check-by-name/src/eval.rs
+++ b/pkgs/test/nixpkgs-check-by-name/src/eval.rs
@@ -1,13 +1,11 @@
-use crate::check_result::{pass, write_check_result, CheckError};
+use crate::check_result::{flatten_check_results, pass, CheckError, CheckResult};
 use crate::structure;
-use crate::utils::ErrorWriter;
 use crate::Version;
 use std::path::Path;
 
 use anyhow::Context;
 use serde::Deserialize;
 use std::collections::HashMap;
-use std::io;
 use std::path::PathBuf;
 use std::process;
 use tempfile::NamedTempFile;
@@ -41,12 +39,11 @@ const EXPR: &str = include_str!("eval.nix");
 /// Check that the Nixpkgs attribute values corresponding to the packages in pkgs/by-name are
 /// of the form `callPackage <package_file> { ... }`.
 /// See the `eval.nix` file for how this is achieved on the Nix side
-pub fn check_values<W: io::Write>(
+pub fn check_values(
     version: Version,
-    error_writer: &mut ErrorWriter<W>,
     nixpkgs: &structure::Nixpkgs,
     eval_accessible_paths: Vec<&Path>,
-) -> anyhow::Result<()> {
+) -> CheckResult<()> {
     // Write the list of packages we need to check into a temporary JSON file.
     // This can then get read by the Nix evaluation.
     let attrs_file = NamedTempFile::new().context("Failed to create a temporary file")?;
@@ -112,11 +109,11 @@ pub fn check_values<W: io::Write>(
             String::from_utf8_lossy(&result.stdout)
         ))?;
 
-    for package_name in &nixpkgs.package_names {
+    let check_results = nixpkgs.package_names.iter().map(|package_name| {
         let relative_package_file = structure::Nixpkgs::relative_file_for_package(package_name);
         let absolute_package_file = nixpkgs.path.join(&relative_package_file);
 
-        let check_result = if let Some(attribute_info) = actual_files.get(package_name) {
+        if let Some(attribute_info) = actual_files.get(package_name) {
             let valid = match &attribute_info.variant {
                 AttributeVariant::AutoCalled => true,
                 AttributeVariant::CallPackage { path, empty_arg } => {
@@ -158,8 +155,7 @@ pub fn check_values<W: io::Write>(
                 package_name: package_name.clone(),
             }
             .into_result()
-        };
-        write_check_result(error_writer, check_result)?;
-    }
-    Ok(())
+        }
+    });
+    flatten_check_results(check_results, |_| ())
 }
diff --git a/pkgs/test/nixpkgs-check-by-name/src/main.rs b/pkgs/test/nixpkgs-check-by-name/src/main.rs
index 80ed29646f72..7de6eadf6564 100644
--- a/pkgs/test/nixpkgs-check-by-name/src/main.rs
+++ b/pkgs/test/nixpkgs-check-by-name/src/main.rs
@@ -5,7 +5,7 @@ mod structure;
 mod utils;
 
 use anyhow::Context;
-use check_result::write_check_result;
+use check_result::{flatten_check_results, write_check_result};
 use clap::{Parser, ValueEnum};
 use colored::Colorize;
 use std::io;
@@ -82,18 +82,24 @@ pub fn check_nixpkgs<W: io::Write>(
     // at all. Later used to figure out if the structure was valid or not.
     let mut error_writer = ErrorWriter::new(error_writer);
 
-    if !nixpkgs_path.join(structure::BASE_SUBPATH).exists() {
+    if !nixpkgs_path.join(utils::BASE_SUBPATH).exists() {
         eprintln!(
             "Given Nixpkgs path does not contain a {} subdirectory, no check necessary.",
-            structure::BASE_SUBPATH
+            utils::BASE_SUBPATH
         );
     } else {
         let nixpkgs = Nixpkgs::new(&nixpkgs_path, &mut error_writer)?;
 
         if error_writer.empty {
             // Only if we could successfully parse the structure, we do the semantic checks
-            eval::check_values(version, &mut error_writer, &nixpkgs, eval_accessible_paths)?;
-            write_check_result(&mut error_writer, references::check_references(&nixpkgs))?;
+            let check_result = flatten_check_results(
+                [
+                    eval::check_values(version, &nixpkgs, eval_accessible_paths),
+                    references::check_references(&nixpkgs),
+                ],
+                |_| (),
+            );
+            write_check_result(&mut error_writer, check_result)?;
         }
     }
     Ok(error_writer.empty)
@@ -102,7 +108,7 @@ pub fn check_nixpkgs<W: io::Write>(
 #[cfg(test)]
 mod tests {
     use crate::check_nixpkgs;
-    use crate::structure;
+    use crate::utils;
     use crate::Version;
     use anyhow::Context;
     use std::fs;
@@ -147,7 +153,7 @@ mod tests {
             return Ok(());
         }
 
-        let base = path.join(structure::BASE_SUBPATH);
+        let base = path.join(utils::BASE_SUBPATH);
 
         fs::create_dir_all(base.join("fo/foo"))?;
         fs::write(base.join("fo/foo/package.nix"), "{ someDrv }: someDrv")?;
diff --git a/pkgs/test/nixpkgs-check-by-name/src/structure.rs b/pkgs/test/nixpkgs-check-by-name/src/structure.rs
index ea80128e487a..928d5ea088ec 100644
--- a/pkgs/test/nixpkgs-check-by-name/src/structure.rs
+++ b/pkgs/test/nixpkgs-check-by-name/src/structure.rs
@@ -1,14 +1,12 @@
+use crate::check_result::{write_check_result, CheckError};
 use crate::utils;
-use crate::utils::ErrorWriter;
+use crate::utils::{ErrorWriter, BASE_SUBPATH, PACKAGE_NIX_FILENAME};
 use lazy_static::lazy_static;
 use regex::Regex;
 use std::collections::HashMap;
 use std::io;
 use std::path::{Path, PathBuf};
 
-pub const BASE_SUBPATH: &str = "pkgs/by-name";
-pub const PACKAGE_NIX_FILENAME: &str = "package.nix";
-
 lazy_static! {
     static ref SHARD_NAME_REGEX: Regex = Regex::new(r"^[a-z0-9_-]{1,2}$").unwrap();
     static ref PACKAGE_NAME_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9_-]+$").unwrap();
@@ -134,10 +132,11 @@ impl Nixpkgs {
                         relative_package_dir.display(),
                     ))?;
                 } else if package_nix_path.is_dir() {
-                    error_writer.write(&format!(
-                        "{}: \"{PACKAGE_NIX_FILENAME}\" must be a file.",
-                        relative_package_dir.display(),
-                    ))?;
+                    let check_result = CheckError::PackageNixDir {
+                        relative_package_dir: relative_package_dir.clone(),
+                    }
+                    .into_result::<()>();
+                    write_check_result(error_writer, check_result)?;
                 }
 
                 package_names.push(package_name.clone());
diff --git a/pkgs/test/nixpkgs-check-by-name/src/utils.rs b/pkgs/test/nixpkgs-check-by-name/src/utils.rs
index 325c736eca98..728e50e72336 100644
--- a/pkgs/test/nixpkgs-check-by-name/src/utils.rs
+++ b/pkgs/test/nixpkgs-check-by-name/src/utils.rs
@@ -4,6 +4,9 @@ use std::fs;
 use std::io;
 use std::path::Path;
 
+pub const BASE_SUBPATH: &str = "pkgs/by-name";
+pub const PACKAGE_NIX_FILENAME: &str = "package.nix";
+
 /// Deterministic file listing so that tests are reproducible
 pub fn read_dir_sorted(base_dir: &Path) -> anyhow::Result<Vec<fs::DirEntry>> {
     let listing = base_dir