tests.nixpkgs-check-by-name: Intermediate PackageNixDir error

This commit is contained in:
Silvan Mosberger 2023-10-19 23:54:34 +02:00
parent 4f17b9367d
commit e3979d14cd
5 changed files with 41 additions and 27 deletions

View file

@ -1,3 +1,4 @@
use crate::utils::PACKAGE_NIX_FILENAME;
use crate::ErrorWriter;
use itertools::{Either, Itertools};
use rnix::parser::ParseError;
@ -6,6 +7,9 @@ use std::io;
use std::path::PathBuf;
pub enum CheckError {
PackageNixDir {
relative_package_dir: PathBuf,
},
UndefinedAttr {
relative_package_file: PathBuf,
package_name: String,
@ -68,6 +72,12 @@ impl CheckError {
impl fmt::Display for CheckError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
CheckError::PackageNixDir { relative_package_dir } =>
write!(
f,
"{}: \"{PACKAGE_NIX_FILENAME}\" must be a file.",
relative_package_dir.display(),
),
CheckError::UndefinedAttr { relative_package_file, package_name } =>
write!(
f,

View file

@ -1,13 +1,11 @@
use crate::check_result::{pass, write_check_result, CheckError};
use crate::check_result::{flatten_check_results, pass, CheckError, CheckResult};
use crate::structure;
use crate::utils::ErrorWriter;
use crate::Version;
use std::path::Path;
use anyhow::Context;
use serde::Deserialize;
use std::collections::HashMap;
use std::io;
use std::path::PathBuf;
use std::process;
use tempfile::NamedTempFile;
@ -41,12 +39,11 @@ const EXPR: &str = include_str!("eval.nix");
/// Check that the Nixpkgs attribute values corresponding to the packages in pkgs/by-name are
/// of the form `callPackage <package_file> { ... }`.
/// See the `eval.nix` file for how this is achieved on the Nix side
pub fn check_values<W: io::Write>(
pub fn check_values(
version: Version,
error_writer: &mut ErrorWriter<W>,
nixpkgs: &structure::Nixpkgs,
eval_accessible_paths: Vec<&Path>,
) -> anyhow::Result<()> {
) -> CheckResult<()> {
// Write the list of packages we need to check into a temporary JSON file.
// This can then get read by the Nix evaluation.
let attrs_file = NamedTempFile::new().context("Failed to create a temporary file")?;
@ -112,11 +109,11 @@ pub fn check_values<W: io::Write>(
String::from_utf8_lossy(&result.stdout)
))?;
for package_name in &nixpkgs.package_names {
let check_results = nixpkgs.package_names.iter().map(|package_name| {
let relative_package_file = structure::Nixpkgs::relative_file_for_package(package_name);
let absolute_package_file = nixpkgs.path.join(&relative_package_file);
let check_result = if let Some(attribute_info) = actual_files.get(package_name) {
if let Some(attribute_info) = actual_files.get(package_name) {
let valid = match &attribute_info.variant {
AttributeVariant::AutoCalled => true,
AttributeVariant::CallPackage { path, empty_arg } => {
@ -158,8 +155,7 @@ pub fn check_values<W: io::Write>(
package_name: package_name.clone(),
}
.into_result()
};
write_check_result(error_writer, check_result)?;
}
Ok(())
}
});
flatten_check_results(check_results, |_| ())
}

View file

@ -5,7 +5,7 @@ mod structure;
mod utils;
use anyhow::Context;
use check_result::write_check_result;
use check_result::{flatten_check_results, write_check_result};
use clap::{Parser, ValueEnum};
use colored::Colorize;
use std::io;
@ -82,18 +82,24 @@ pub fn check_nixpkgs<W: io::Write>(
// 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(structure::BASE_SUBPATH).exists() {
if !nixpkgs_path.join(utils::BASE_SUBPATH).exists() {
eprintln!(
"Given Nixpkgs path does not contain a {} subdirectory, no check necessary.",
structure::BASE_SUBPATH
utils::BASE_SUBPATH
);
} else {
let nixpkgs = Nixpkgs::new(&nixpkgs_path, &mut error_writer)?;
if error_writer.empty {
// Only if we could successfully parse the structure, we do the semantic checks
eval::check_values(version, &mut error_writer, &nixpkgs, eval_accessible_paths)?;
write_check_result(&mut error_writer, references::check_references(&nixpkgs))?;
let check_result = flatten_check_results(
[
eval::check_values(version, &nixpkgs, eval_accessible_paths),
references::check_references(&nixpkgs),
],
|_| (),
);
write_check_result(&mut error_writer, check_result)?;
}
}
Ok(error_writer.empty)
@ -102,7 +108,7 @@ pub fn check_nixpkgs<W: io::Write>(
#[cfg(test)]
mod tests {
use crate::check_nixpkgs;
use crate::structure;
use crate::utils;
use crate::Version;
use anyhow::Context;
use std::fs;
@ -147,7 +153,7 @@ mod tests {
return Ok(());
}
let base = path.join(structure::BASE_SUBPATH);
let base = path.join(utils::BASE_SUBPATH);
fs::create_dir_all(base.join("fo/foo"))?;
fs::write(base.join("fo/foo/package.nix"), "{ someDrv }: someDrv")?;

View file

@ -1,14 +1,12 @@
use crate::check_result::{write_check_result, CheckError};
use crate::utils;
use crate::utils::ErrorWriter;
use crate::utils::{ErrorWriter, BASE_SUBPATH, PACKAGE_NIX_FILENAME};
use lazy_static::lazy_static;
use regex::Regex;
use std::collections::HashMap;
use std::io;
use std::path::{Path, PathBuf};
pub const BASE_SUBPATH: &str = "pkgs/by-name";
pub const PACKAGE_NIX_FILENAME: &str = "package.nix";
lazy_static! {
static ref SHARD_NAME_REGEX: Regex = Regex::new(r"^[a-z0-9_-]{1,2}$").unwrap();
static ref PACKAGE_NAME_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9_-]+$").unwrap();
@ -134,10 +132,11 @@ impl Nixpkgs {
relative_package_dir.display(),
))?;
} else if package_nix_path.is_dir() {
error_writer.write(&format!(
"{}: \"{PACKAGE_NIX_FILENAME}\" must be a file.",
relative_package_dir.display(),
))?;
let check_result = CheckError::PackageNixDir {
relative_package_dir: relative_package_dir.clone(),
}
.into_result::<()>();
write_check_result(error_writer, check_result)?;
}
package_names.push(package_name.clone());

View file

@ -4,6 +4,9 @@ use std::fs;
use std::io;
use std::path::Path;
pub const BASE_SUBPATH: &str = "pkgs/by-name";
pub const PACKAGE_NIX_FILENAME: &str = "package.nix";
/// Deterministic file listing so that tests are reproducible
pub fn read_dir_sorted(base_dir: &Path) -> anyhow::Result<Vec<fs::DirEntry>> {
let listing = base_dir