1
0
Fork 0

pretty print test error

This commit is contained in:
ibizaman 2024-05-26 23:08:29 -07:00 committed by Pierre Penninckx
parent ebbd19c7fd
commit c18f3f77b0
2 changed files with 53 additions and 4 deletions

View file

@ -14,13 +14,17 @@
outputs = { nixpkgs, nix-flake-tests, flake-utils, nmdsrc, ... }: flake-utils.lib.eachDefaultSystem (system: outputs = { nixpkgs, nix-flake-tests, flake-utils, nmdsrc, ... }: flake-utils.lib.eachDefaultSystem (system:
let let
patches = [
];
originPkgs = nixpkgs.legacyPackages.${system}; originPkgs = nixpkgs.legacyPackages.${system};
patches = [
(originPkgs.fetchpatch {
url = "https://patch-diff.githubusercontent.com/raw/NixOS/nixpkgs/pull/315018.patch";
hash = "sha256-8jcGyO/d+htfv/ZajxXh89S3OiDZAr7/fsWC1JpGczM=";
})
];
patchedNixpkgs = originPkgs.applyPatches { patchedNixpkgs = originPkgs.applyPatches {
name = "nixpkgs-patched"; name = "nixpkgs-patched";
src = nixpkgs; src = nixpkgs;
patches = map (p: originPkgs.writeText "patch" p) patches; inherit patches;
}; };
pkgs = import patchedNixpkgs { pkgs = import patchedNixpkgs {
@ -85,12 +89,14 @@
inherit (pkgs) lib; inherit (pkgs) lib;
} }
); );
shblib = pkgs.callPackage ./lib {};
in (rec { in (rec {
all = mergeTests [ all = mergeTests [
modules modules
]; ];
modules = nix-flake-tests.lib.check { modules = shblib.check {
inherit pkgs; inherit pkgs;
tests = tests =
mergeTests (importFiles [ mergeTests (importFiles [

View file

@ -204,4 +204,47 @@ rec {
(lib.attrsets.filterAttrs (name: v: name == from) attrset) // { (lib.attrsets.filterAttrs (name: v: name == from) attrset) // {
${to} = attrset.${from}; ${to} = attrset.${from};
}; };
# Taken from https://github.com/antifuchs/nix-flake-tests/blob/main/default.nix
# with a nicer diff display function.
check = { pkgs, tests }:
let
system = pkgs.stdenv.targetPlatform.system;
formatValue = val:
if (builtins.isList val || builtins.isAttrs val) then builtins.toJSON val
else builtins.toString val;
resultToString = { name, expected, result }:
pkgs.callPackage (pkgs.runCommand "nix-flake-tests-error" {
expected = formatValue expected;
result = formatValue result;
passAsFile = [ "expected" "result" ];
} ''
echo "${name} failed (- expected, + result)"
cp ''${expectedPath} ''${expectedPath}.json
cp ''${resultPath} ''${resultPath}.json
${pkgs.deepdiff}/bin/deep diff ''${expectedPath}.json ''${resultPath}.json
'') {};
# ''
# ${name} failed: expected ${formatValue expected}, but got ${
# formatValue result
# }
# '';
results = pkgs.lib.runTests tests;
in
if results != [ ] then
builtins.throw (builtins.concatStringsSep "\n" (map resultToString results))
## TODO: The derivation below is preferable but "nix flake check" hangs with it:
## (it's preferable because "examples/many-failures" would then show all errors.)
# pkgs.runCommand "nix-flake-tests-failure" { } ''
# cat <<EOF
# ${builtins.concatStringsSep "\n" (map resultToString results)}
# EOF
# exit 1
# ''
else
pkgs.runCommand "nix-flake-tests-success" { } "echo > $out";
} }