summaryrefslogtreecommitdiff
path: root/pkgs/test/problems/default.nix
blob: e6560636237633d60a9c085a33bc10c7dc70404c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
{
  lib,
  nix,
  runCommand,
  removeReferencesTo,
  path,
  gitMinimal,
}:
let
  nixpkgs = lib.cleanSource path;
  unitResult = import ./unit.nix { inherit lib; };
in
assert lib.assertMsg (unitResult == [ ])
  "problems/unit.nix failing: ${lib.generators.toPretty { } unitResult}";
lib.mapAttrs (
  name: _:
  let
    nixFile = "${./cases + "/${name}/default.nix"}";
    result = runCommand "test-problems-${name}" { nativeBuildInputs = [ removeReferencesTo ]; } ''
      export NIX_STATE_DIR=$(mktemp -d)
      mkdir $out


      command=()
      ${lib.optionalString (builtins.pathExists (./cases + "/${name}/env.nix")) ''
        command+=(
          env
          ${lib.concatMapAttrsStringSep "\n" (name: value: "${name}=${toString value}") (
            import (./cases + "/${name}/env.nix")
          )}
        )
      ''}
      command+=(
        # FIXME: Using this version because it doesn't print a trace by default
        # Probably should have some regex-style error matching instead
        "${lib.getBin nix}/bin/nix-instantiate"
        ${nixFile}
        # Readonly mode because we don't need to write anything to the store
        "--readonly-mode"
        "--arg"
        "nixpkgs"
        "${nixpkgs}"
      )

      echo "''${command[*]@Q}" > $out/command
      echo "Running ''${command[*]@Q}"
      set +e
      "''${command[@]}" >$out/stdout 2>$out/stderr
      set +e
      echo "$?" > $out/code
      echo "Command exited with code $(<$out/code)"
      remove-references-to -t ${nixFile} $out/stderr
      if grep '(stack trace truncated.*)' $out/stderr; then
        sed -i -n '/(stack trace truncated.*)/,$ p' $out/stderr
      fi
      sed -i 's/^       //' $out/stderr
    '';
    checker = runCommand "test-problems-check-${name}" { } ''
      if ! PAGER=cat ${lib.getExe gitMinimal} diff --no-index ${
        ./cases + "/${name}/expected-stderr"
      } ${result}/stderr ; then
        echo "Output of $(< ${result}/command) does not match what was expected. To adapt:"
        echo "cp ${result}/stderr ${lib.path.removePrefix path ./cases + "/${name}/expected-stderr"}"
        exit 1
      fi
      touch $out
    '';
  in
  checker
) (builtins.readDir ./cases)