1
0
Fork 0
mirror of https://github.com/numtide/flake-utils.git synced 2024-12-15 17:50:53 +00:00

Add checks-utils (#36)

* Add checks-utils overlay

Add checks-utils overlay

Fix example flake inputs

Fix string expected exception

* Update check-utils implementation

* nixpkgs-fmt
This commit is contained in:
Gytis Ivaskevicius 2021-06-14 11:47:39 +03:00 committed by GitHub
parent 7d706970d9
commit 98c8d36b18
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 8 deletions

22
check-utils.nix Normal file
View file

@ -0,0 +1,22 @@
systemOrPkgs:
let
str = it: if it == null then "null" else (toString it);
system = systemOrPkgs.system or systemOrPkgs;
test = name: command: derivation {
inherit name system;
builder = "/bin/sh";
args = [ "-c" command ];
};
in
{
isEqual = a: b:
if a == b
then test "SUCCESS__${str a}__IS_EQUAL__${str b}" "echo success > $out"
else test "FAILURE__${str a}__NOT_EQUAL__${str b}" "exit 1";
hasKey = attrset: key:
if attrset ? ${str key}
then test "SUCCESS__${str key}__EXISTS_IN_ATTRSET" "echo success > $out"
else test "FAILURE__${str key}__DOES_NOT_EXISTS_IN_ATTRSET_SIZE_${str(builtins.length (builtins.attrNames attrset))}" "exit 1";
}

View file

@ -135,14 +135,18 @@ let
# This function tries to capture a common flake pattern.
simpleFlake = import ./simpleFlake.nix { inherit lib; };
# Helper functions for Nix evaluation
check-utils = import ./check-utils.nix;
lib = {
inherit
allSystems
check-utils
defaultSystems
eachDefaultSystem
eachSystem
flattenTree
filterPackages
flattenTree
mkApp
simpleFlake
;

View file

@ -0,0 +1,29 @@
{
description = "Flake utils demo";
inputs.flake-utils.url = "github:numtide/flake-utils";
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
inherit (flake-utils.lib.check-utils system) isEqual hasKey;
testDataset = { key1 = "value1"; key2 = "value2"; key3 = "value3"; };
in
rec {
checks = {
valid_key1 = isEqual testDataset.key1 "value1";
contains_key2 = hasKey testDataset "key2";
failing_valid_key1 = isEqual testDataset.key1 "failing-data";
failing_contains_key2 = hasKey testDataset "failing-data";
number_formatting_isEqual = isEqual testDataset.key1 123;
number_formatting_hasKey = hasKey testDataset 123;
null_formatting_key1 = isEqual testDataset.key1 null;
null_formatting_hasKey = hasKey testDataset null;
};
}
);
}

View file

@ -6,11 +6,12 @@ let
nameValuePair = name: value: { inherit name value; };
filterAttrs = pred: set:
listToAttrs (
concatMap (name:
let v = set.${name}; in
if pred name v then [(nameValuePair name v)] else []
)
(attrNames set)
concatMap
(name:
let v = set.${name}; in
if pred name v then [ (nameValuePair name v) ] else [ ]
)
(attrNames set)
);
# Everything that nix flake check requires for the packages output
@ -22,9 +23,9 @@ let
isBroken = meta.broken or false;
platforms = meta.hydraPlatforms or meta.platforms or allSystems;
in
# check for isDerivation, so this is independently useful of
# check for isDerivation, so this is independently useful of
# flattenTree, which also does filter on derviations
isDerivation v && !isBroken && builtins.elem system platforms
isDerivation v && !isBroken && builtins.elem system platforms
;
in
filterAttrs sieve packages