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. # This function tries to capture a common flake pattern.
simpleFlake = import ./simpleFlake.nix { inherit lib; }; simpleFlake = import ./simpleFlake.nix { inherit lib; };
# Helper functions for Nix evaluation
check-utils = import ./check-utils.nix;
lib = { lib = {
inherit inherit
allSystems allSystems
check-utils
defaultSystems defaultSystems
eachDefaultSystem eachDefaultSystem
eachSystem eachSystem
flattenTree
filterPackages filterPackages
flattenTree
mkApp mkApp
simpleFlake 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; }; nameValuePair = name: value: { inherit name value; };
filterAttrs = pred: set: filterAttrs = pred: set:
listToAttrs ( listToAttrs (
concatMap (name: concatMap
let v = set.${name}; in (name:
if pred name v then [(nameValuePair name v)] else [] let v = set.${name}; in
) if pred name v then [ (nameValuePair name v) ] else [ ]
(attrNames set) )
(attrNames set)
); );
# Everything that nix flake check requires for the packages output # Everything that nix flake check requires for the packages output
@ -22,9 +23,9 @@ let
isBroken = meta.broken or false; isBroken = meta.broken or false;
platforms = meta.hydraPlatforms or meta.platforms or allSystems; platforms = meta.hydraPlatforms or meta.platforms or allSystems;
in 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 # flattenTree, which also does filter on derviations
isDerivation v && !isBroken && builtins.elem system platforms isDerivation v && !isBroken && builtins.elem system platforms
; ;
in in
filterAttrs sieve packages filterAttrs sieve packages