Loading pkgs/test/nixpkgs-check-by-name/src/check_result.rs +65 −39 Original line number Diff line number Diff line use crate::utils::PACKAGE_NIX_FILENAME; use itertools::concat; use itertools::{Either, Itertools}; use itertools::{ Either, Either::{Left, Right}, Itertools, }; use rnix::parser::ParseError; use std::ffi::OsString; use std::fmt; use std::io; use std::path::PathBuf; pub enum CheckError { pub enum CheckProblem { ShardNonDir { relative_shard_path: PathBuf, }, Loading Loading @@ -90,97 +94,97 @@ pub enum CheckError { }, } impl CheckError { impl CheckProblem { pub fn into_result<A>(self) -> CheckResult<A> { Ok(Either::Left(vec![self])) Ok(Left(vec![self])) } } impl fmt::Display for CheckError { impl fmt::Display for CheckProblem { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { CheckError::ShardNonDir { relative_shard_path } => CheckProblem::ShardNonDir { relative_shard_path } => write!( f, "{}: This is a file, but it should be a directory.", relative_shard_path.display(), ), CheckError::InvalidShardName { relative_shard_path, shard_name } => CheckProblem::InvalidShardName { relative_shard_path, shard_name } => write!( f, "{}: Invalid directory name \"{shard_name}\", must be at most 2 ASCII characters consisting of a-z, 0-9, \"-\" or \"_\".", relative_shard_path.display() ), CheckError::PackageNonDir { relative_package_dir } => CheckProblem::PackageNonDir { relative_package_dir } => write!( f, "{}: This path is a file, but it should be a directory.", relative_package_dir.display(), ), CheckError::CaseSensitiveDuplicate { relative_shard_path, first, second } => CheckProblem::CaseSensitiveDuplicate { relative_shard_path, first, second } => write!( f, "{}: Duplicate case-sensitive package directories {first:?} and {second:?}.", relative_shard_path.display(), ), CheckError::InvalidPackageName { relative_package_dir, package_name } => CheckProblem::InvalidPackageName { relative_package_dir, package_name } => write!( f, "{}: Invalid package directory name \"{package_name}\", must be ASCII characters consisting of a-z, A-Z, 0-9, \"-\" or \"_\".", relative_package_dir.display(), ), CheckError::IncorrectShard { relative_package_dir, correct_relative_package_dir } => CheckProblem::IncorrectShard { relative_package_dir, correct_relative_package_dir } => write!( f, "{}: Incorrect directory location, should be {} instead.", relative_package_dir.display(), correct_relative_package_dir.display(), ), CheckError::PackageNixNonExistent { relative_package_dir } => CheckProblem::PackageNixNonExistent { relative_package_dir } => write!( f, "{}: Missing required \"{PACKAGE_NIX_FILENAME}\" file.", relative_package_dir.display(), ), CheckError::PackageNixDir { relative_package_dir } => CheckProblem::PackageNixDir { relative_package_dir } => write!( f, "{}: \"{PACKAGE_NIX_FILENAME}\" must be a file.", relative_package_dir.display(), ), CheckError::UndefinedAttr { relative_package_file, package_name } => CheckProblem::UndefinedAttr { relative_package_file, package_name } => write!( f, "pkgs.{package_name}: This attribute is not defined but it should be defined automatically as {}", relative_package_file.display() ), CheckError::WrongCallPackage { relative_package_file, package_name } => CheckProblem::WrongCallPackage { relative_package_file, package_name } => write!( f, "pkgs.{package_name}: This attribute is manually defined (most likely in pkgs/top-level/all-packages.nix), which is only allowed if the definition is of the form `pkgs.callPackage {} {{ ... }}` with a non-empty second argument.", relative_package_file.display() ), CheckError::NonDerivation { relative_package_file, package_name } => CheckProblem::NonDerivation { relative_package_file, package_name } => write!( f, "pkgs.{package_name}: This attribute defined by {} is not a derivation", relative_package_file.display() ), CheckError::OutsideSymlink { relative_package_dir, subpath } => CheckProblem::OutsideSymlink { relative_package_dir, subpath } => write!( f, "{}: Path {} is a symlink pointing to a path outside the directory of that package.", relative_package_dir.display(), subpath.display(), ), CheckError::UnresolvableSymlink { relative_package_dir, subpath, io_error } => CheckProblem::UnresolvableSymlink { relative_package_dir, subpath, io_error } => write!( f, "{}: Path {} is a symlink which cannot be resolved: {io_error}.", relative_package_dir.display(), subpath.display(), ), CheckError::CouldNotParseNix { relative_package_dir, subpath, error } => CheckProblem::CouldNotParseNix { relative_package_dir, subpath, error } => write!( f, "{}: File {} could not be parsed by rnix: {}", Loading @@ -188,7 +192,7 @@ impl fmt::Display for CheckError { subpath.display(), error, ), CheckError::PathInterpolation { relative_package_dir, subpath, line, text } => CheckProblem::PathInterpolation { relative_package_dir, subpath, line, text } => write!( f, "{}: File {} at line {line} contains the path expression \"{}\", which is not yet supported and may point outside the directory of that package.", Loading @@ -196,7 +200,7 @@ impl fmt::Display for CheckError { subpath.display(), text ), CheckError::SearchPath { relative_package_dir, subpath, line, text } => CheckProblem::SearchPath { relative_package_dir, subpath, line, text } => write!( f, "{}: File {} at line {line} contains the nix search path expression \"{}\" which may point outside the directory of that package.", Loading @@ -204,7 +208,7 @@ impl fmt::Display for CheckError { subpath.display(), text ), CheckError::OutsidePathReference { relative_package_dir, subpath, line, text } => CheckProblem::OutsidePathReference { relative_package_dir, subpath, line, text } => write!( f, "{}: File {} at line {line} contains the path expression \"{}\" which may point outside the directory of that package.", Loading @@ -212,7 +216,7 @@ impl fmt::Display for CheckError { subpath.display(), text, ), CheckError::UnresolvablePathReference { relative_package_dir, subpath, line, text, io_error } => CheckProblem::UnresolvablePathReference { relative_package_dir, subpath, line, text, io_error } => write!( f, "{}: File {} at line {line} contains the path expression \"{}\" which cannot be resolved: {io_error}.", Loading @@ -224,27 +228,44 @@ impl fmt::Display for CheckError { } } pub fn pass<A>(value: A) -> CheckResult<A> { Ok(Either::Right(value)) /// A type alias representing the result of a check, either: /// - Err(anyhow::Error): A fatal failure, typically I/O errors. /// Such failures are not caused by the files in Nixpkgs. /// This hints at a bug in the code or a problem with the deployment. /// - Ok(Left(Vec<CheckProblem>)): A non-fatal problem with the Nixpkgs files. /// Further checks can be run even with this result type. /// Such problems can be fixed by changing the Nixpkgs files. /// - Ok(Right(A)): A successful (potentially intermediate) result with an arbitrary value. /// No fatal errors have occurred and no problems have been found with Nixpkgs. pub type CheckResult<A> = anyhow::Result<Either<Vec<CheckProblem>, A>>; /// Map a `CheckResult<I>` to a `CheckResult<O>` by applying a function to the /// potentially contained value in case of success. pub fn map<I, O>(check_result: CheckResult<I>, f: impl FnOnce(I) -> O) -> CheckResult<O> { Ok(check_result?.map_right(f)) } pub type CheckResult<A> = anyhow::Result<Either<Vec<CheckError>, A>>; /// Create a successfull `CheckResult<A>` from a value `A`. pub fn ok<A>(value: A) -> CheckResult<A> { Ok(Right(value)) } pub fn sequence_check_results<A>(first: CheckResult<()>, second: CheckResult<A>) -> CheckResult<A> { /// Combine two check results, both of which need to be successful for the return value to be successful. /// The `CheckProblem`s of both sides are returned concatenated. pub fn and<A>(first: CheckResult<()>, second: CheckResult<A>) -> CheckResult<A> { match (first?, second?) { (Either::Right(_), Either::Right(right_value)) => pass(right_value), (Either::Left(errors), Either::Right(_)) => Ok(Either::Left(errors)), (Either::Right(_), Either::Left(errors)) => Ok(Either::Left(errors)), (Either::Left(errors_l), Either::Left(errors_r)) => { Ok(Either::Left(concat([errors_l, errors_r]))) } (Right(_), Right(right_value)) => Ok(Right(right_value)), (Left(errors), Right(_)) => Ok(Left(errors)), (Right(_), Left(errors)) => Ok(Left(errors)), (Left(errors_l), Left(errors_r)) => Ok(Left(concat([errors_l, errors_r]))), } } pub fn flatten_check_results<I, O>( check_results: impl IntoIterator<Item = CheckResult<I>>, value_transform: impl Fn(Vec<I>) -> O, ) -> CheckResult<O> { /// Combine many checks results into a single one. /// All given check results need to be successful in order for the returned check result to be successful, /// in which case the returned check result value contains each a `Vec` of each individual result value. /// The `CheckProblem`s of all results are returned concatenated. pub fn sequence<A>(check_results: impl IntoIterator<Item = CheckResult<A>>) -> CheckResult<Vec<A>> { let (errors, values): (Vec<_>, Vec<_>) = check_results .into_iter() .collect::<anyhow::Result<Vec<_>>>()? Loading @@ -256,8 +277,13 @@ pub fn flatten_check_results<I, O>( let flattened_errors = errors.into_iter().flatten().collect::<Vec<_>>(); if flattened_errors.is_empty() { Ok(Either::Right(value_transform(values))) Ok(Right(values)) } else { Ok(Either::Left(flattened_errors)) Ok(Left(flattened_errors)) } } /// Like `sequence`, but replace the resulting value with () pub fn sequence_<A>(check_results: impl IntoIterator<Item = CheckResult<A>>) -> CheckResult<()> { map(sequence(check_results), |_| ()) } pkgs/test/nixpkgs-check-by-name/src/eval.rs +8 −8 Original line number Diff line number Diff line use crate::check_result::{flatten_check_results, pass, CheckError, CheckResult}; use crate::check_result; use crate::check_result::{CheckProblem, CheckResult}; use crate::structure; use crate::Version; use std::path::Path; Loading Loading @@ -110,7 +111,7 @@ pub fn check_values( String::from_utf8_lossy(&result.stdout) ))?; let check_results = package_names.iter().map(|package_name| { check_result::sequence_(package_names.iter().map(|package_name| { let relative_package_file = structure::relative_file_for_package(package_name); let absolute_package_file = nixpkgs_path.join(&relative_package_file); Loading @@ -136,27 +137,26 @@ pub fn check_values( }; if !valid { CheckError::WrongCallPackage { CheckProblem::WrongCallPackage { relative_package_file: relative_package_file.clone(), package_name: package_name.clone(), } .into_result() } else if !attribute_info.is_derivation { CheckError::NonDerivation { CheckProblem::NonDerivation { relative_package_file: relative_package_file.clone(), package_name: package_name.clone(), } .into_result() } else { pass(()) check_result::ok(()) } } else { CheckError::UndefinedAttr { CheckProblem::UndefinedAttr { relative_package_file: relative_package_file.clone(), package_name: package_name.clone(), } .into_result() } }); flatten_check_results(check_results, |_| ()) })) } pkgs/test/nixpkgs-check-by-name/src/main.rs +5 −4 Original line number Diff line number Diff line Loading @@ -6,11 +6,12 @@ mod utils; use crate::structure::check_structure; use anyhow::Context; use check_result::pass; use clap::{Parser, ValueEnum}; use colored::Colorize; use itertools::Either; use itertools::Either::{Left, Right}; use itertools::{ Either, Either::{Left, Right}, }; use std::io; use std::path::{Path, PathBuf}; use std::process::ExitCode; Loading Loading @@ -84,7 +85,7 @@ pub fn check_nixpkgs<W: io::Write>( "Given Nixpkgs path does not contain a {} subdirectory, no check necessary.", utils::BASE_SUBPATH ); pass(()) check_result::ok(()) } else { match check_structure(&nixpkgs_path)? { Left(errors) => Ok(Left(errors)), Loading pkgs/test/nixpkgs-check-by-name/src/references.rs +18 −19 Original line number Diff line number Diff line use crate::check_result::{flatten_check_results, pass, CheckError, CheckResult}; use crate::check_result; use crate::check_result::{CheckProblem, CheckResult}; use crate::utils; use crate::utils::LineIndex; Loading Loading @@ -46,16 +47,16 @@ fn check_path(context: &PackageContext, subpath: &Path) -> CheckResult<()> { // No need to handle the case of it being inside the directory, since we scan through the // entire directory recursively anyways if let Err(_prefix_error) = target.strip_prefix(context.absolute_package_dir) { CheckError::OutsideSymlink { CheckProblem::OutsideSymlink { relative_package_dir: context.relative_package_dir.clone(), subpath: subpath.to_path_buf(), } .into_result() } else { pass(()) check_result::ok(()) } } Err(io_error) => CheckError::UnresolvableSymlink { Err(io_error) => CheckProblem::UnresolvableSymlink { relative_package_dir: context.relative_package_dir.clone(), subpath: subpath.to_path_buf(), io_error, Loading @@ -64,12 +65,11 @@ fn check_path(context: &PackageContext, subpath: &Path) -> CheckResult<()> { } } else if path.is_dir() { // Recursively check each entry let check_results = utils::read_dir_sorted(&path)?.into_iter().map(|entry| { check_result::sequence_(utils::read_dir_sorted(&path)?.into_iter().map(|entry| { let entry_subpath = subpath.join(entry.file_name()); check_path(context, &entry_subpath) .context(format!("Error while recursing into {}", subpath.display())) }); flatten_check_results(check_results, |_| ()) })) } else if path.is_file() { // Only check Nix files if let Some(ext) = path.extension() { Loading @@ -79,10 +79,10 @@ fn check_path(context: &PackageContext, subpath: &Path) -> CheckResult<()> { subpath.display() )) } else { pass(()) check_result::ok(()) } } else { pass(()) check_result::ok(()) } } else { // This should never happen, git doesn't support other file types Loading @@ -104,7 +104,7 @@ fn check_nix_file(context: &PackageContext, subpath: &Path) -> CheckResult<()> { let root = Root::parse(&contents); if let Some(error) = root.errors().first() { return CheckError::CouldNotParseNix { return CheckProblem::CouldNotParseNix { relative_package_dir: context.relative_package_dir.clone(), subpath: subpath.to_path_buf(), error: error.clone(), Loading @@ -114,17 +114,17 @@ fn check_nix_file(context: &PackageContext, subpath: &Path) -> CheckResult<()> { let line_index = LineIndex::new(&contents); let check_results = root.syntax().descendants().map(|node| { check_result::sequence_(root.syntax().descendants().map(|node| { let text = node.text().to_string(); let line = line_index.line(node.text_range().start().into()); if node.kind() != NODE_PATH { // We're only interested in Path expressions pass(()) check_result::ok(()) } else if node.children().count() != 0 { // Filters out ./foo/${bar}/baz // TODO: We can just check ./foo CheckError::PathInterpolation { CheckProblem::PathInterpolation { relative_package_dir: context.relative_package_dir.clone(), subpath: subpath.to_path_buf(), line, Loading @@ -133,7 +133,7 @@ fn check_nix_file(context: &PackageContext, subpath: &Path) -> CheckResult<()> { .into_result() } else if text.starts_with('<') { // Filters out search paths like <nixpkgs> CheckError::SearchPath { CheckProblem::SearchPath { relative_package_dir: context.relative_package_dir.clone(), subpath: subpath.to_path_buf(), line, Loading @@ -149,7 +149,7 @@ fn check_nix_file(context: &PackageContext, subpath: &Path) -> CheckResult<()> { // No need to handle the case of it being inside the directory, since we scan through the // entire directory recursively anyways if let Err(_prefix_error) = target.strip_prefix(context.absolute_package_dir) { CheckError::OutsidePathReference { CheckProblem::OutsidePathReference { relative_package_dir: context.relative_package_dir.clone(), subpath: subpath.to_path_buf(), line, Loading @@ -157,10 +157,10 @@ fn check_nix_file(context: &PackageContext, subpath: &Path) -> CheckResult<()> { } .into_result() } else { pass(()) check_result::ok(()) } } Err(e) => CheckError::UnresolvablePathReference { Err(e) => CheckProblem::UnresolvablePathReference { relative_package_dir: context.relative_package_dir.clone(), subpath: subpath.to_path_buf(), line, Loading @@ -170,6 +170,5 @@ fn check_nix_file(context: &PackageContext, subpath: &Path) -> CheckResult<()> { .into_result(), } } }); flatten_check_results(check_results, |_| ()) })) } pkgs/test/nixpkgs-check-by-name/src/structure.rs +48 −52 File changed.Preview size limit exceeded, changes collapsed. Show changes Loading
pkgs/test/nixpkgs-check-by-name/src/check_result.rs +65 −39 Original line number Diff line number Diff line use crate::utils::PACKAGE_NIX_FILENAME; use itertools::concat; use itertools::{Either, Itertools}; use itertools::{ Either, Either::{Left, Right}, Itertools, }; use rnix::parser::ParseError; use std::ffi::OsString; use std::fmt; use std::io; use std::path::PathBuf; pub enum CheckError { pub enum CheckProblem { ShardNonDir { relative_shard_path: PathBuf, }, Loading Loading @@ -90,97 +94,97 @@ pub enum CheckError { }, } impl CheckError { impl CheckProblem { pub fn into_result<A>(self) -> CheckResult<A> { Ok(Either::Left(vec![self])) Ok(Left(vec![self])) } } impl fmt::Display for CheckError { impl fmt::Display for CheckProblem { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { CheckError::ShardNonDir { relative_shard_path } => CheckProblem::ShardNonDir { relative_shard_path } => write!( f, "{}: This is a file, but it should be a directory.", relative_shard_path.display(), ), CheckError::InvalidShardName { relative_shard_path, shard_name } => CheckProblem::InvalidShardName { relative_shard_path, shard_name } => write!( f, "{}: Invalid directory name \"{shard_name}\", must be at most 2 ASCII characters consisting of a-z, 0-9, \"-\" or \"_\".", relative_shard_path.display() ), CheckError::PackageNonDir { relative_package_dir } => CheckProblem::PackageNonDir { relative_package_dir } => write!( f, "{}: This path is a file, but it should be a directory.", relative_package_dir.display(), ), CheckError::CaseSensitiveDuplicate { relative_shard_path, first, second } => CheckProblem::CaseSensitiveDuplicate { relative_shard_path, first, second } => write!( f, "{}: Duplicate case-sensitive package directories {first:?} and {second:?}.", relative_shard_path.display(), ), CheckError::InvalidPackageName { relative_package_dir, package_name } => CheckProblem::InvalidPackageName { relative_package_dir, package_name } => write!( f, "{}: Invalid package directory name \"{package_name}\", must be ASCII characters consisting of a-z, A-Z, 0-9, \"-\" or \"_\".", relative_package_dir.display(), ), CheckError::IncorrectShard { relative_package_dir, correct_relative_package_dir } => CheckProblem::IncorrectShard { relative_package_dir, correct_relative_package_dir } => write!( f, "{}: Incorrect directory location, should be {} instead.", relative_package_dir.display(), correct_relative_package_dir.display(), ), CheckError::PackageNixNonExistent { relative_package_dir } => CheckProblem::PackageNixNonExistent { relative_package_dir } => write!( f, "{}: Missing required \"{PACKAGE_NIX_FILENAME}\" file.", relative_package_dir.display(), ), CheckError::PackageNixDir { relative_package_dir } => CheckProblem::PackageNixDir { relative_package_dir } => write!( f, "{}: \"{PACKAGE_NIX_FILENAME}\" must be a file.", relative_package_dir.display(), ), CheckError::UndefinedAttr { relative_package_file, package_name } => CheckProblem::UndefinedAttr { relative_package_file, package_name } => write!( f, "pkgs.{package_name}: This attribute is not defined but it should be defined automatically as {}", relative_package_file.display() ), CheckError::WrongCallPackage { relative_package_file, package_name } => CheckProblem::WrongCallPackage { relative_package_file, package_name } => write!( f, "pkgs.{package_name}: This attribute is manually defined (most likely in pkgs/top-level/all-packages.nix), which is only allowed if the definition is of the form `pkgs.callPackage {} {{ ... }}` with a non-empty second argument.", relative_package_file.display() ), CheckError::NonDerivation { relative_package_file, package_name } => CheckProblem::NonDerivation { relative_package_file, package_name } => write!( f, "pkgs.{package_name}: This attribute defined by {} is not a derivation", relative_package_file.display() ), CheckError::OutsideSymlink { relative_package_dir, subpath } => CheckProblem::OutsideSymlink { relative_package_dir, subpath } => write!( f, "{}: Path {} is a symlink pointing to a path outside the directory of that package.", relative_package_dir.display(), subpath.display(), ), CheckError::UnresolvableSymlink { relative_package_dir, subpath, io_error } => CheckProblem::UnresolvableSymlink { relative_package_dir, subpath, io_error } => write!( f, "{}: Path {} is a symlink which cannot be resolved: {io_error}.", relative_package_dir.display(), subpath.display(), ), CheckError::CouldNotParseNix { relative_package_dir, subpath, error } => CheckProblem::CouldNotParseNix { relative_package_dir, subpath, error } => write!( f, "{}: File {} could not be parsed by rnix: {}", Loading @@ -188,7 +192,7 @@ impl fmt::Display for CheckError { subpath.display(), error, ), CheckError::PathInterpolation { relative_package_dir, subpath, line, text } => CheckProblem::PathInterpolation { relative_package_dir, subpath, line, text } => write!( f, "{}: File {} at line {line} contains the path expression \"{}\", which is not yet supported and may point outside the directory of that package.", Loading @@ -196,7 +200,7 @@ impl fmt::Display for CheckError { subpath.display(), text ), CheckError::SearchPath { relative_package_dir, subpath, line, text } => CheckProblem::SearchPath { relative_package_dir, subpath, line, text } => write!( f, "{}: File {} at line {line} contains the nix search path expression \"{}\" which may point outside the directory of that package.", Loading @@ -204,7 +208,7 @@ impl fmt::Display for CheckError { subpath.display(), text ), CheckError::OutsidePathReference { relative_package_dir, subpath, line, text } => CheckProblem::OutsidePathReference { relative_package_dir, subpath, line, text } => write!( f, "{}: File {} at line {line} contains the path expression \"{}\" which may point outside the directory of that package.", Loading @@ -212,7 +216,7 @@ impl fmt::Display for CheckError { subpath.display(), text, ), CheckError::UnresolvablePathReference { relative_package_dir, subpath, line, text, io_error } => CheckProblem::UnresolvablePathReference { relative_package_dir, subpath, line, text, io_error } => write!( f, "{}: File {} at line {line} contains the path expression \"{}\" which cannot be resolved: {io_error}.", Loading @@ -224,27 +228,44 @@ impl fmt::Display for CheckError { } } pub fn pass<A>(value: A) -> CheckResult<A> { Ok(Either::Right(value)) /// A type alias representing the result of a check, either: /// - Err(anyhow::Error): A fatal failure, typically I/O errors. /// Such failures are not caused by the files in Nixpkgs. /// This hints at a bug in the code or a problem with the deployment. /// - Ok(Left(Vec<CheckProblem>)): A non-fatal problem with the Nixpkgs files. /// Further checks can be run even with this result type. /// Such problems can be fixed by changing the Nixpkgs files. /// - Ok(Right(A)): A successful (potentially intermediate) result with an arbitrary value. /// No fatal errors have occurred and no problems have been found with Nixpkgs. pub type CheckResult<A> = anyhow::Result<Either<Vec<CheckProblem>, A>>; /// Map a `CheckResult<I>` to a `CheckResult<O>` by applying a function to the /// potentially contained value in case of success. pub fn map<I, O>(check_result: CheckResult<I>, f: impl FnOnce(I) -> O) -> CheckResult<O> { Ok(check_result?.map_right(f)) } pub type CheckResult<A> = anyhow::Result<Either<Vec<CheckError>, A>>; /// Create a successfull `CheckResult<A>` from a value `A`. pub fn ok<A>(value: A) -> CheckResult<A> { Ok(Right(value)) } pub fn sequence_check_results<A>(first: CheckResult<()>, second: CheckResult<A>) -> CheckResult<A> { /// Combine two check results, both of which need to be successful for the return value to be successful. /// The `CheckProblem`s of both sides are returned concatenated. pub fn and<A>(first: CheckResult<()>, second: CheckResult<A>) -> CheckResult<A> { match (first?, second?) { (Either::Right(_), Either::Right(right_value)) => pass(right_value), (Either::Left(errors), Either::Right(_)) => Ok(Either::Left(errors)), (Either::Right(_), Either::Left(errors)) => Ok(Either::Left(errors)), (Either::Left(errors_l), Either::Left(errors_r)) => { Ok(Either::Left(concat([errors_l, errors_r]))) } (Right(_), Right(right_value)) => Ok(Right(right_value)), (Left(errors), Right(_)) => Ok(Left(errors)), (Right(_), Left(errors)) => Ok(Left(errors)), (Left(errors_l), Left(errors_r)) => Ok(Left(concat([errors_l, errors_r]))), } } pub fn flatten_check_results<I, O>( check_results: impl IntoIterator<Item = CheckResult<I>>, value_transform: impl Fn(Vec<I>) -> O, ) -> CheckResult<O> { /// Combine many checks results into a single one. /// All given check results need to be successful in order for the returned check result to be successful, /// in which case the returned check result value contains each a `Vec` of each individual result value. /// The `CheckProblem`s of all results are returned concatenated. pub fn sequence<A>(check_results: impl IntoIterator<Item = CheckResult<A>>) -> CheckResult<Vec<A>> { let (errors, values): (Vec<_>, Vec<_>) = check_results .into_iter() .collect::<anyhow::Result<Vec<_>>>()? Loading @@ -256,8 +277,13 @@ pub fn flatten_check_results<I, O>( let flattened_errors = errors.into_iter().flatten().collect::<Vec<_>>(); if flattened_errors.is_empty() { Ok(Either::Right(value_transform(values))) Ok(Right(values)) } else { Ok(Either::Left(flattened_errors)) Ok(Left(flattened_errors)) } } /// Like `sequence`, but replace the resulting value with () pub fn sequence_<A>(check_results: impl IntoIterator<Item = CheckResult<A>>) -> CheckResult<()> { map(sequence(check_results), |_| ()) }
pkgs/test/nixpkgs-check-by-name/src/eval.rs +8 −8 Original line number Diff line number Diff line use crate::check_result::{flatten_check_results, pass, CheckError, CheckResult}; use crate::check_result; use crate::check_result::{CheckProblem, CheckResult}; use crate::structure; use crate::Version; use std::path::Path; Loading Loading @@ -110,7 +111,7 @@ pub fn check_values( String::from_utf8_lossy(&result.stdout) ))?; let check_results = package_names.iter().map(|package_name| { check_result::sequence_(package_names.iter().map(|package_name| { let relative_package_file = structure::relative_file_for_package(package_name); let absolute_package_file = nixpkgs_path.join(&relative_package_file); Loading @@ -136,27 +137,26 @@ pub fn check_values( }; if !valid { CheckError::WrongCallPackage { CheckProblem::WrongCallPackage { relative_package_file: relative_package_file.clone(), package_name: package_name.clone(), } .into_result() } else if !attribute_info.is_derivation { CheckError::NonDerivation { CheckProblem::NonDerivation { relative_package_file: relative_package_file.clone(), package_name: package_name.clone(), } .into_result() } else { pass(()) check_result::ok(()) } } else { CheckError::UndefinedAttr { CheckProblem::UndefinedAttr { relative_package_file: relative_package_file.clone(), package_name: package_name.clone(), } .into_result() } }); flatten_check_results(check_results, |_| ()) })) }
pkgs/test/nixpkgs-check-by-name/src/main.rs +5 −4 Original line number Diff line number Diff line Loading @@ -6,11 +6,12 @@ mod utils; use crate::structure::check_structure; use anyhow::Context; use check_result::pass; use clap::{Parser, ValueEnum}; use colored::Colorize; use itertools::Either; use itertools::Either::{Left, Right}; use itertools::{ Either, Either::{Left, Right}, }; use std::io; use std::path::{Path, PathBuf}; use std::process::ExitCode; Loading Loading @@ -84,7 +85,7 @@ pub fn check_nixpkgs<W: io::Write>( "Given Nixpkgs path does not contain a {} subdirectory, no check necessary.", utils::BASE_SUBPATH ); pass(()) check_result::ok(()) } else { match check_structure(&nixpkgs_path)? { Left(errors) => Ok(Left(errors)), Loading
pkgs/test/nixpkgs-check-by-name/src/references.rs +18 −19 Original line number Diff line number Diff line use crate::check_result::{flatten_check_results, pass, CheckError, CheckResult}; use crate::check_result; use crate::check_result::{CheckProblem, CheckResult}; use crate::utils; use crate::utils::LineIndex; Loading Loading @@ -46,16 +47,16 @@ fn check_path(context: &PackageContext, subpath: &Path) -> CheckResult<()> { // No need to handle the case of it being inside the directory, since we scan through the // entire directory recursively anyways if let Err(_prefix_error) = target.strip_prefix(context.absolute_package_dir) { CheckError::OutsideSymlink { CheckProblem::OutsideSymlink { relative_package_dir: context.relative_package_dir.clone(), subpath: subpath.to_path_buf(), } .into_result() } else { pass(()) check_result::ok(()) } } Err(io_error) => CheckError::UnresolvableSymlink { Err(io_error) => CheckProblem::UnresolvableSymlink { relative_package_dir: context.relative_package_dir.clone(), subpath: subpath.to_path_buf(), io_error, Loading @@ -64,12 +65,11 @@ fn check_path(context: &PackageContext, subpath: &Path) -> CheckResult<()> { } } else if path.is_dir() { // Recursively check each entry let check_results = utils::read_dir_sorted(&path)?.into_iter().map(|entry| { check_result::sequence_(utils::read_dir_sorted(&path)?.into_iter().map(|entry| { let entry_subpath = subpath.join(entry.file_name()); check_path(context, &entry_subpath) .context(format!("Error while recursing into {}", subpath.display())) }); flatten_check_results(check_results, |_| ()) })) } else if path.is_file() { // Only check Nix files if let Some(ext) = path.extension() { Loading @@ -79,10 +79,10 @@ fn check_path(context: &PackageContext, subpath: &Path) -> CheckResult<()> { subpath.display() )) } else { pass(()) check_result::ok(()) } } else { pass(()) check_result::ok(()) } } else { // This should never happen, git doesn't support other file types Loading @@ -104,7 +104,7 @@ fn check_nix_file(context: &PackageContext, subpath: &Path) -> CheckResult<()> { let root = Root::parse(&contents); if let Some(error) = root.errors().first() { return CheckError::CouldNotParseNix { return CheckProblem::CouldNotParseNix { relative_package_dir: context.relative_package_dir.clone(), subpath: subpath.to_path_buf(), error: error.clone(), Loading @@ -114,17 +114,17 @@ fn check_nix_file(context: &PackageContext, subpath: &Path) -> CheckResult<()> { let line_index = LineIndex::new(&contents); let check_results = root.syntax().descendants().map(|node| { check_result::sequence_(root.syntax().descendants().map(|node| { let text = node.text().to_string(); let line = line_index.line(node.text_range().start().into()); if node.kind() != NODE_PATH { // We're only interested in Path expressions pass(()) check_result::ok(()) } else if node.children().count() != 0 { // Filters out ./foo/${bar}/baz // TODO: We can just check ./foo CheckError::PathInterpolation { CheckProblem::PathInterpolation { relative_package_dir: context.relative_package_dir.clone(), subpath: subpath.to_path_buf(), line, Loading @@ -133,7 +133,7 @@ fn check_nix_file(context: &PackageContext, subpath: &Path) -> CheckResult<()> { .into_result() } else if text.starts_with('<') { // Filters out search paths like <nixpkgs> CheckError::SearchPath { CheckProblem::SearchPath { relative_package_dir: context.relative_package_dir.clone(), subpath: subpath.to_path_buf(), line, Loading @@ -149,7 +149,7 @@ fn check_nix_file(context: &PackageContext, subpath: &Path) -> CheckResult<()> { // No need to handle the case of it being inside the directory, since we scan through the // entire directory recursively anyways if let Err(_prefix_error) = target.strip_prefix(context.absolute_package_dir) { CheckError::OutsidePathReference { CheckProblem::OutsidePathReference { relative_package_dir: context.relative_package_dir.clone(), subpath: subpath.to_path_buf(), line, Loading @@ -157,10 +157,10 @@ fn check_nix_file(context: &PackageContext, subpath: &Path) -> CheckResult<()> { } .into_result() } else { pass(()) check_result::ok(()) } } Err(e) => CheckError::UnresolvablePathReference { Err(e) => CheckProblem::UnresolvablePathReference { relative_package_dir: context.relative_package_dir.clone(), subpath: subpath.to_path_buf(), line, Loading @@ -170,6 +170,5 @@ fn check_nix_file(context: &PackageContext, subpath: &Path) -> CheckResult<()> { .into_result(), } } }); flatten_check_results(check_results, |_| ()) })) }
pkgs/test/nixpkgs-check-by-name/src/structure.rs +48 −52 File changed.Preview size limit exceeded, changes collapsed. Show changes