summary refs log tree commit diff
path: root/pkgs/test
diff options
context:
space:
mode:
authorSilvan Mosberger <silvan.mosberger@tweag.io>2023-10-20 02:45:54 +0200
committerSilvan Mosberger <silvan.mosberger@tweag.io>2023-10-24 01:18:13 +0200
commit3d60440799721601b04ff39e83374aa684a5300b (patch)
tree1cd14454409f7b0b269eb02e5ab3795621dca6b2 /pkgs/test
parente58bc75444d5d722a995cf350b0d6c298aeb3808 (diff)
downloadnixpkgs-3d60440799721601b04ff39e83374aa684a5300b.tar
nixpkgs-3d60440799721601b04ff39e83374aa684a5300b.tar.gz
nixpkgs-3d60440799721601b04ff39e83374aa684a5300b.tar.bz2
nixpkgs-3d60440799721601b04ff39e83374aa684a5300b.tar.lz
nixpkgs-3d60440799721601b04ff39e83374aa684a5300b.tar.xz
nixpkgs-3d60440799721601b04ff39e83374aa684a5300b.tar.zst
nixpkgs-3d60440799721601b04ff39e83374aa684a5300b.zip
tests.nixpkgs-check-by-name: Remove error writer
Diffstat (limited to 'pkgs/test')
-rw-r--r--pkgs/test/nixpkgs-check-by-name/src/check_result.rs16
-rw-r--r--pkgs/test/nixpkgs-check-by-name/src/main.rs35
-rw-r--r--pkgs/test/nixpkgs-check-by-name/src/utils.rs24
3 files changed, 21 insertions, 54 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 818ba1a9afd..e5f14152d5a 100644
--- a/pkgs/test/nixpkgs-check-by-name/src/check_result.rs
+++ b/pkgs/test/nixpkgs-check-by-name/src/check_result.rs
@@ -1,5 +1,4 @@
 use crate::utils::PACKAGE_NIX_FILENAME;
-use crate::ErrorWriter;
 use itertools::concat;
 use itertools::{Either, Itertools};
 use rnix::parser::ParseError;
@@ -225,21 +224,6 @@ impl fmt::Display for CheckError {
     }
 }
 
-pub fn write_check_result<A, W: io::Write>(
-    error_writer: &mut ErrorWriter<W>,
-    check_result: CheckResult<A>,
-) -> anyhow::Result<Option<A>> {
-    match check_result? {
-        Either::Left(errors) => {
-            for error in errors {
-                error_writer.write(&error.to_string())?
-            }
-            Ok(None)
-        }
-        Either::Right(value) => Ok(Some(value)),
-    }
-}
-
 pub fn pass<A>(value: A) -> CheckResult<A> {
     Ok(Either::Right(value))
 }
diff --git a/pkgs/test/nixpkgs-check-by-name/src/main.rs b/pkgs/test/nixpkgs-check-by-name/src/main.rs
index 67f6055e733..537276a177d 100644
--- a/pkgs/test/nixpkgs-check-by-name/src/main.rs
+++ b/pkgs/test/nixpkgs-check-by-name/src/main.rs
@@ -6,13 +6,14 @@ mod utils;
 
 use crate::structure::check_structure;
 use anyhow::Context;
-use check_result::write_check_result;
+use check_result::pass;
 use clap::{Parser, ValueEnum};
 use colored::Colorize;
+use itertools::Either;
+use itertools::Either::{Left, Right};
 use std::io;
 use std::path::{Path, PathBuf};
 use std::process::ExitCode;
-use utils::ErrorWriter;
 
 /// Program to check the validity of pkgs/by-name
 #[derive(Parser, Debug)]
@@ -78,26 +79,32 @@ pub fn check_nixpkgs<W: io::Write>(
         nixpkgs_path.display()
     ))?;
 
-    // Wraps the error_writer to print everything in red, and tracks whether anything was printed
-    // 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(utils::BASE_SUBPATH).exists() {
+    let check_result = if !nixpkgs_path.join(utils::BASE_SUBPATH).exists() {
         eprintln!(
             "Given Nixpkgs path does not contain a {} subdirectory, no check necessary.",
             utils::BASE_SUBPATH
         );
+        pass(())
     } else {
-        let check_result = check_structure(&nixpkgs_path);
-
-        if let Some(package_names) = write_check_result(&mut error_writer, check_result)? {
+        match check_structure(&nixpkgs_path)? {
+            Left(errors) => Ok(Left(errors)),
+            Right(package_names) =>
             // Only if we could successfully parse the structure, we do the evaluation checks
-            let check_result =
-                eval::check_values(version, &nixpkgs_path, package_names, eval_accessible_paths);
-            write_check_result(&mut error_writer, check_result)?;
+            {
+                eval::check_values(version, &nixpkgs_path, package_names, eval_accessible_paths)
+            }
+        }
+    };
+
+    match check_result? {
+        Either::Left(errors) => {
+            for error in errors {
+                writeln!(error_writer, "{}", error.to_string().red())?
+            }
+            Ok(false)
         }
+        Either::Right(_) => Ok(true),
     }
-    Ok(error_writer.empty)
 }
 
 #[cfg(test)]
diff --git a/pkgs/test/nixpkgs-check-by-name/src/utils.rs b/pkgs/test/nixpkgs-check-by-name/src/utils.rs
index 728e50e7233..5cc4a0863ba 100644
--- a/pkgs/test/nixpkgs-check-by-name/src/utils.rs
+++ b/pkgs/test/nixpkgs-check-by-name/src/utils.rs
@@ -1,5 +1,4 @@
 use anyhow::Context;
-use colored::Colorize;
 use std::fs;
 use std::io;
 use std::path::Path;
@@ -50,26 +49,3 @@ impl LineIndex {
         }
     }
 }
-
-/// A small wrapper around a generic io::Write specifically for errors:
-/// - Print everything in red to signal it's an error
-/// - Keep track of whether anything was printed at all, so that
-///   it can be queried whether any errors were encountered at all
-pub struct ErrorWriter<W> {
-    pub writer: W,
-    pub empty: bool,
-}
-
-impl<W: io::Write> ErrorWriter<W> {
-    pub fn new(writer: W) -> ErrorWriter<W> {
-        ErrorWriter {
-            writer,
-            empty: true,
-        }
-    }
-
-    pub fn write(&mut self, string: &str) -> io::Result<()> {
-        self.empty = false;
-        writeln!(self.writer, "{}", string.red())
-    }
-}