Skip to content

Commit

Permalink
tests.nixpkgs-check-by-name: Basic info for non-by-name attributes
Browse files Browse the repository at this point in the history
In a future commit this will be extended
  • Loading branch information
infinisil committed Jan 10, 2024
1 parent da3e72b commit 69fc71a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
27 changes: 19 additions & 8 deletions pkgs/test/nixpkgs-check-by-name/src/eval.nix
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,26 @@ let
};
};

attrInfos = map (name: [
byNameAttrs = map (name: [
name
(
if ! pkgs ? ${name} then
{ Missing = null; }
else
{ Existing = attrInfo name pkgs.${name}; }
)
{
ByName =
if ! pkgs ? ${name} then
{ Missing = null; }
else
{ Existing = attrInfo name pkgs.${name}; };
}
]) attrs;

# Information on all attributes that exist but are not in pkgs/by-name.
# We need this to enforce pkgs/by-name for new packages
nonByNameAttrs = map (name:
[
name
{
NonByName = null;
}
]
) (builtins.attrNames (builtins.removeAttrs pkgs attrs));
in
attrInfos
byNameAttrs ++ nonByNameAttrs
26 changes: 19 additions & 7 deletions pkgs/test/nixpkgs-check-by-name/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ use std::process;
use tempfile::NamedTempFile;

/// Attribute set of this structure is returned by eval.nix
#[derive(Deserialize)]
enum Attribute {
/// An attribute that should be defined via pkgs/by-name
ByName(ByNameAttribute),
/// An attribute not defined via pkgs/by-name
NonByName,
}

#[derive(Deserialize)]
enum ByNameAttribute {
/// The attribute doesn't exist at all
Expand Down Expand Up @@ -120,7 +128,7 @@ pub fn check_values(
anyhow::bail!("Failed to run command {command:?}");
}
// Parse the resulting JSON value
let attributes: Vec<(String, ByNameAttribute)> = serde_json::from_slice(&result.stdout)
let attributes: Vec<(String, Attribute)> = serde_json::from_slice(&result.stdout)
.with_context(|| {
format!(
"Failed to deserialise {}",
Expand All @@ -133,30 +141,34 @@ pub fn check_values(
let relative_package_file = structure::relative_file_for_package(&attribute_name);

use ratchet::RatchetState::*;
use Attribute::*;
use AttributeInfo::*;
use ByNameAttribute::*;
use CallPackageVariant::*;

let check_result = match attribute_value {
Missing => NixpkgsProblem::UndefinedAttr {
NonByName => Success(ratchet::Package {
empty_non_auto_called: Tight,
}),
ByName(Missing) => NixpkgsProblem::UndefinedAttr {
relative_package_file: relative_package_file.clone(),
package_name: attribute_name.clone(),
}
.into(),
Existing(NonAttributeSet) => NixpkgsProblem::NonDerivation {
ByName(Existing(NonAttributeSet)) => NixpkgsProblem::NonDerivation {
relative_package_file: relative_package_file.clone(),
package_name: attribute_name.clone(),
}
.into(),
Existing(NonCallPackage) => NixpkgsProblem::WrongCallPackage {
ByName(Existing(NonCallPackage)) => NixpkgsProblem::WrongCallPackage {
relative_package_file: relative_package_file.clone(),
package_name: attribute_name.clone(),
}
.into(),
Existing(CallPackage(CallPackageInfo {
ByName(Existing(CallPackage(CallPackageInfo {
is_derivation,
call_package_variant,
})) => {
}))) => {
let check_result = if !is_derivation {
NixpkgsProblem::NonDerivation {
relative_package_file: relative_package_file.clone(),
Expand Down Expand Up @@ -203,7 +215,7 @@ pub fn check_values(
));

Ok(check_result.map(|elems| ratchet::Nixpkgs {
package_names,
package_names: elems.iter().map(|(name, _)| name.to_owned()).collect(),
package_map: elems.into_iter().collect(),
}))
}
6 changes: 3 additions & 3 deletions pkgs/test/nixpkgs-check-by-name/src/ratchet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::collections::HashMap;
/// The ratchet value for the entirety of Nixpkgs.
#[derive(Default)]
pub struct Nixpkgs {
/// Sorted list of attributes in package_map
/// Sorted list of packages in package_map
pub package_names: Vec<String>,
/// The ratchet values for all packages
pub package_map: HashMap<String, Package>,
Expand All @@ -29,14 +29,14 @@ impl Nixpkgs {
}
}

/// The ratchet value for a single package in `pkgs/by-name`
/// The ratchet value for a top-level package
pub struct Package {
/// The ratchet value for the check for non-auto-called empty arguments
pub empty_non_auto_called: RatchetState<EmptyNonAutoCalled>,
}

impl Package {
/// Validates the ratchet checks for a single package defined in `pkgs/by-name`
/// Validates the ratchet checks for a top-level package
pub fn compare(name: &str, optional_from: Option<&Self>, to: &Self) -> Validation<()> {
RatchetState::<EmptyNonAutoCalled>::compare(
name,
Expand Down

0 comments on commit 69fc71a

Please sign in to comment.