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:24:29 +0200
committerSilvan Mosberger <silvan.mosberger@tweag.io>2023-10-24 01:11:57 +0200
commit37f8f6681c761a5788b633f1da8f1f8a940bfabc (patch)
tree8e15d707332144955c099fda5d571775fad08a61 /pkgs/test
parent72e453f6ce9dc95f089c47a40034b647c5097553 (diff)
downloadnixpkgs-37f8f6681c761a5788b633f1da8f1f8a940bfabc.tar
nixpkgs-37f8f6681c761a5788b633f1da8f1f8a940bfabc.tar.gz
nixpkgs-37f8f6681c761a5788b633f1da8f1f8a940bfabc.tar.bz2
nixpkgs-37f8f6681c761a5788b633f1da8f1f8a940bfabc.tar.lz
nixpkgs-37f8f6681c761a5788b633f1da8f1f8a940bfabc.tar.xz
nixpkgs-37f8f6681c761a5788b633f1da8f1f8a940bfabc.tar.zst
nixpkgs-37f8f6681c761a5788b633f1da8f1f8a940bfabc.zip
tests.nixpkgs-check-by-name: Intermediate error type refactoring prep
Currently the tool prints problems right as it is checking the code
without an intermediate error representation. However for various reasons
it would be beneficial to have an intermediate error type:
- It makes the code cleaner, having all errors in one place
- It allows printing the error in different ways, e.g. for a future
  --json mode

This commit prepares for an incremental refactoring for an intermediate
error/problem representation. Most notable is that we want to be able to collect
multiple errors/problems and not just exit on the first one.

We introduce the type alias CheckResult and CheckError (later renamed to
NixpkgsProblem), where CheckError allows collecting multiple
CheckErrors using the utility function flatten_check_results (later
renamed to check_result::sequence)

The write_check_result function is only temporarily introduced to help
refactoring, it's removed again in later commits.
Diffstat (limited to 'pkgs/test')
-rw-r--r--pkgs/test/nixpkgs-check-by-name/Cargo.lock16
-rw-r--r--pkgs/test/nixpkgs-check-by-name/Cargo.toml1
-rw-r--r--pkgs/test/nixpkgs-check-by-name/src/check_result.rs61
-rw-r--r--pkgs/test/nixpkgs-check-by-name/src/main.rs1
4 files changed, 79 insertions, 0 deletions
diff --git a/pkgs/test/nixpkgs-check-by-name/Cargo.lock b/pkgs/test/nixpkgs-check-by-name/Cargo.lock
index aa4459c7cff..fc3aeb9fd79 100644
--- a/pkgs/test/nixpkgs-check-by-name/Cargo.lock
+++ b/pkgs/test/nixpkgs-check-by-name/Cargo.lock
@@ -163,6 +163,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "7704b5fdd17b18ae31c4c1da5a2e0305a2bf17b5249300a9ee9ed7b72114c636"
 
 [[package]]
+name = "either"
+version = "1.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
+
+[[package]]
 name = "errno"
 version = "0.3.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -219,6 +225,15 @@ dependencies = [
 ]
 
 [[package]]
+name = "itertools"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57"
+dependencies = [
+ "either",
+]
+
+[[package]]
 name = "itoa"
 version = "1.0.9"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -274,6 +289,7 @@ dependencies = [
  "anyhow",
  "clap",
  "colored",
+ "itertools",
  "lazy_static",
  "regex",
  "rnix",
diff --git a/pkgs/test/nixpkgs-check-by-name/Cargo.toml b/pkgs/test/nixpkgs-check-by-name/Cargo.toml
index 70b44d04820..1e6eaa1106d 100644
--- a/pkgs/test/nixpkgs-check-by-name/Cargo.toml
+++ b/pkgs/test/nixpkgs-check-by-name/Cargo.toml
@@ -13,6 +13,7 @@ serde = { version = "1.0.185", features = ["derive"] }
 anyhow = "1.0"
 lazy_static = "1.4.0"
 colored = "2.0.4"
+itertools = "0.11.0"
 
 [dev-dependencies]
 temp-env = "0.3.5"
diff --git a/pkgs/test/nixpkgs-check-by-name/src/check_result.rs b/pkgs/test/nixpkgs-check-by-name/src/check_result.rs
new file mode 100644
index 00000000000..df894df45c7
--- /dev/null
+++ b/pkgs/test/nixpkgs-check-by-name/src/check_result.rs
@@ -0,0 +1,61 @@
+use crate::ErrorWriter;
+use itertools::{Either, Itertools};
+use std::fmt;
+use std::io;
+use std::path::PathBuf;
+
+pub enum CheckError {}
+
+impl CheckError {
+    pub fn into_result<A>(self) -> CheckResult<A> {
+        Ok(Either::Left(vec![self]))
+    }
+}
+
+impl fmt::Display for CheckError {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        match *self {}
+    }
+}
+
+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))
+}
+
+pub type CheckResult<A> = anyhow::Result<Either<Vec<CheckError>, A>>;
+
+pub fn flatten_check_results<I, O>(
+    check_results: impl IntoIterator<Item = CheckResult<I>>,
+    value_transform: impl Fn(Vec<I>) -> O,
+) -> CheckResult<O> {
+    let (errors, values): (Vec<_>, Vec<_>) = check_results
+        .into_iter()
+        .collect::<anyhow::Result<Vec<_>>>()?
+        .into_iter()
+        .partition_map(|r| r);
+
+    // To combine the errors from the results we flatten all the error Vec's into a new Vec
+    // This is not very efficient, but doesn't matter because generally we should have no errors
+    let flattened_errors = errors.into_iter().flatten().collect::<Vec<_>>();
+
+    if flattened_errors.is_empty() {
+        Ok(Either::Right(value_transform(values)))
+    } else {
+        Ok(Either::Left(flattened_errors))
+    }
+}
diff --git a/pkgs/test/nixpkgs-check-by-name/src/main.rs b/pkgs/test/nixpkgs-check-by-name/src/main.rs
index 5d28077ae4a..ee98c5a1a89 100644
--- a/pkgs/test/nixpkgs-check-by-name/src/main.rs
+++ b/pkgs/test/nixpkgs-check-by-name/src/main.rs
@@ -1,3 +1,4 @@
+mod check_result;
 mod eval;
 mod references;
 mod structure;