diff options
author | Silvan Mosberger <silvan.mosberger@tweag.io> | 2023-10-20 01:26:22 +0200 |
---|---|---|
committer | Silvan Mosberger <silvan.mosberger@tweag.io> | 2023-10-24 01:15:58 +0200 |
commit | bb89ca72dfd79d88f7e3de2460c96a0255ebac11 (patch) | |
tree | 61dd8bc0b578adbfb21f7abde26a0b7f8674351c /pkgs/test | |
parent | 571eaed155dced7e29b4e2c6c40f63b0ce521a99 (diff) |
tests.nixpkgs-check-by-name: Refactor
Diffstat (limited to 'pkgs/test')
-rw-r--r-- | pkgs/test/nixpkgs-check-by-name/src/structure.rs | 226 |
1 files changed, 116 insertions, 110 deletions
diff --git a/pkgs/test/nixpkgs-check-by-name/src/structure.rs b/pkgs/test/nixpkgs-check-by-name/src/structure.rs index 17eece94d61c8..2f225ecd2ec31 100644 --- a/pkgs/test/nixpkgs-check-by-name/src/structure.rs +++ b/pkgs/test/nixpkgs-check-by-name/src/structure.rs @@ -49,139 +49,145 @@ impl Nixpkgs { ) -> anyhow::Result<Nixpkgs> { let base_dir = path.join(BASE_SUBPATH); - let mut package_names = Vec::new(); - - for shard_entry in utils::read_dir_sorted(&base_dir)? { - let shard_path = shard_entry.path(); - let shard_name = shard_entry.file_name().to_string_lossy().into_owned(); - let relative_shard_path = Nixpkgs::relative_dir_for_shard(&shard_name); - - if shard_name == "README.md" { - // README.md is allowed to be a file and not checked - continue; - } - - let check_result = if !shard_path.is_dir() { - CheckError::ShardNonDir { - relative_shard_path: relative_shard_path.clone(), - } - .into_result() - // we can't check for any other errors if it's a file, since there's no subdirectories to check - } else { - let shard_name_valid = SHARD_NAME_REGEX.is_match(&shard_name); - let shard_name_valid_check_result = if !shard_name_valid { - CheckError::InvalidShardName { + let check_results = utils::read_dir_sorted(&base_dir)? + .into_iter() + .map(|shard_entry| { + let shard_path = shard_entry.path(); + let shard_name = shard_entry.file_name().to_string_lossy().into_owned(); + let relative_shard_path = Nixpkgs::relative_dir_for_shard(&shard_name); + + if shard_name == "README.md" { + // README.md is allowed to be a file and not checked + pass(vec![]) + } else if !shard_path.is_dir() { + CheckError::ShardNonDir { relative_shard_path: relative_shard_path.clone(), - shard_name: shard_name.clone(), } .into_result() + // we can't check for any other errors if it's a file, since there's no subdirectories to check } else { - pass(()) - }; - - write_check_result(error_writer, shard_name_valid_check_result)?; - - let entries = utils::read_dir_sorted(&shard_path)?; - - let duplicate_check_results = entries - .iter() - .zip(entries.iter().skip(1)) - .filter(|(l, r)| { - l.file_name().to_ascii_lowercase() == r.file_name().to_ascii_lowercase() - }) - .map(|(l, r)| { - CheckError::CaseSensitiveDuplicate { + let shard_name_valid = SHARD_NAME_REGEX.is_match(&shard_name); + let shard_name_valid_check_result = if !shard_name_valid { + CheckError::InvalidShardName { relative_shard_path: relative_shard_path.clone(), - first: l.file_name(), - second: r.file_name(), + shard_name: shard_name.clone(), } - .into_result::<()>() - }); + .into_result() + } else { + pass(()) + }; + + write_check_result(error_writer, shard_name_valid_check_result)?; + + let entries = utils::read_dir_sorted(&shard_path)?; + + let duplicate_check_results = entries + .iter() + .zip(entries.iter().skip(1)) + .filter(|(l, r)| { + l.file_name().to_ascii_lowercase() == r.file_name().to_ascii_lowercase() + }) + .map(|(l, r)| { + CheckError::CaseSensitiveDuplicate { + relative_shard_path: relative_shard_path.clone(), + first: l.file_name(), + second: r.file_name(), + } + .into_result::<()>() + }); - let duplicate_check_result = flatten_check_results(duplicate_check_results, |_| ()); + let duplicate_check_result = + flatten_check_results(duplicate_check_results, |_| ()); - write_check_result(error_writer, duplicate_check_result)?; + write_check_result(error_writer, duplicate_check_result)?; - let check_results = entries.into_iter().map(|package_entry| { - let package_path = package_entry.path(); - let package_name = package_entry.file_name().to_string_lossy().into_owned(); - let relative_package_dir = - PathBuf::from(format!("{BASE_SUBPATH}/{shard_name}/{package_name}")); + let check_results = entries.into_iter().map(|package_entry| { + let package_path = package_entry.path(); + let package_name = package_entry.file_name().to_string_lossy().into_owned(); + let relative_package_dir = + PathBuf::from(format!("{BASE_SUBPATH}/{shard_name}/{package_name}")); - if !package_path.is_dir() { - CheckError::PackageNonDir { - relative_package_dir: relative_package_dir.clone(), - } - .into_result() - } else { - let package_name_valid = PACKAGE_NAME_REGEX.is_match(&package_name); - let name_check_result = if !package_name_valid { - CheckError::InvalidPackageName { + if !package_path.is_dir() { + CheckError::PackageNonDir { relative_package_dir: relative_package_dir.clone(), - package_name: package_name.clone(), } .into_result() } else { - pass(()) - }; - - let correct_relative_package_dir = - Nixpkgs::relative_dir_for_package(&package_name); - let shard_check_result = - if relative_package_dir != correct_relative_package_dir { - // Only show this error if we have a valid shard and package name - // Because if one of those is wrong, you should fix that first - if shard_name_valid && package_name_valid { - CheckError::IncorrectShard { - relative_package_dir: relative_package_dir.clone(), - correct_relative_package_dir: correct_relative_package_dir - .clone(), + let package_name_valid = PACKAGE_NAME_REGEX.is_match(&package_name); + let name_check_result = if !package_name_valid { + CheckError::InvalidPackageName { + relative_package_dir: relative_package_dir.clone(), + package_name: package_name.clone(), + } + .into_result() + } else { + pass(()) + }; + + let correct_relative_package_dir = + Nixpkgs::relative_dir_for_package(&package_name); + let shard_check_result = + if relative_package_dir != correct_relative_package_dir { + // Only show this error if we have a valid shard and package name + // Because if one of those is wrong, you should fix that first + if shard_name_valid && package_name_valid { + CheckError::IncorrectShard { + relative_package_dir: relative_package_dir.clone(), + correct_relative_package_dir: + correct_relative_package_dir.clone(), + } + .into_result() + } else { + pass(()) } - .into_result() } else { pass(()) + }; + + let package_nix_path = package_path.join(PACKAGE_NIX_FILENAME); + let package_nix_check_result = if !package_nix_path.exists() { + CheckError::PackageNixNonExistent { + relative_package_dir: relative_package_dir.clone(), } + .into_result() + } else if package_nix_path.is_dir() { + CheckError::PackageNixDir { + relative_package_dir: relative_package_dir.clone(), + } + .into_result() } else { pass(()) }; - let package_nix_path = package_path.join(PACKAGE_NIX_FILENAME); - let package_nix_check_result = if !package_nix_path.exists() { - CheckError::PackageNixNonExistent { - relative_package_dir: relative_package_dir.clone(), - } - .into_result() - } else if package_nix_path.is_dir() { - CheckError::PackageNixDir { - relative_package_dir: relative_package_dir.clone(), - } - .into_result() - } else { - pass(()) - }; - - flatten_check_results( - [ - name_check_result, - shard_check_result, - package_nix_check_result, - ], - |_| package_name.clone(), - ) - } - }); - - flatten_check_results(check_results, |x| x) - }; + flatten_check_results( + [ + name_check_result, + shard_check_result, + package_nix_check_result, + ], + |_| package_name.clone(), + ) + } + }); - if let Some(shard_package_names) = write_check_result(error_writer, check_result)? { - package_names.extend(shard_package_names) - } + flatten_check_results(check_results, |x| x) + } + }); + + let check_result = flatten_check_results(check_results, |x| { + x.into_iter().flatten().collect::<Vec<_>>() + }); + + if let Some(package_names) = write_check_result(error_writer, check_result)? { + Ok(Nixpkgs { + path: path.to_owned(), + package_names, + }) + } else { + Ok(Nixpkgs { + path: path.to_owned(), + package_names: vec![], + }) } - - Ok(Nixpkgs { - path: path.to_owned(), - package_names, - }) } } |