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.
This commit is contained in:
Silvan Mosberger 2023-10-19 02:24:29 +02:00
parent 72e453f6ce
commit 37f8f6681c
4 changed files with 79 additions and 0 deletions

View File

@ -162,6 +162,12 @@ version = "3.0.1"
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"
@ -218,6 +224,15 @@ dependencies = [
"windows-sys",
]
[[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"
@ -274,6 +289,7 @@ dependencies = [
"anyhow",
"clap",
"colored",
"itertools",
"lazy_static",
"regex",
"rnix",

View File

@ -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"

View File

@ -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))
}
}

View File

@ -1,3 +1,4 @@
mod check_result;
mod eval;
mod references;
mod structure;