summaryrefslogtreecommitdiff
path: root/pkgs/test/replace-vars/default.nix
blob: e1cc2bf1c13d8dc80a898f7a29531db2c11628ca (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
{
  replaceVars,
  replaceVarsWith,
  emptyDirectory,
  emptyFile,
  lib,
  runCommand,
  testers,
}:
let
  inherit (testers) testEqualContents testBuildFailure;

  mkTests =
    callReplaceVars: mkExpectation:
    lib.recurseIntoAttrs {
      succeeds = testEqualContents {
        assertion = "replaceVars-succeeds";
        actual = callReplaceVars ./source.txt {
          free = "free";
          "equal in" = "are the same in";
          brotherhood = "shared humanity";
        };

        expected = mkExpectation (
          builtins.toFile "source.txt" ''
            All human beings are born free and are the same in dignity and rights.
            They are endowed with reason and conscience and should act towards
            one another in a spirit of shared humanity.

              -- eroosevelt@humanrights.un.org
          ''
        );
      };

      # There might eventually be a usecase for this, but it's not supported at the moment.
      fails-on-directory =
        runCommand "replaceVars-fails" { failed = testBuildFailure (callReplaceVars emptyDirectory { }); }
          ''
            grep -e "ERROR: file.*empty-directory.*does not exist" $failed/testBuildFailure.log
            touch $out
          '';

      fails-in-build-phase =
        runCommand "replaceVars-fails"
          { failed = testBuildFailure (callReplaceVars emptyFile { not-found = "boo~"; }); }
          ''
            grep -e "ERROR: pattern @not-found@ doesn't match anything in file.*empty-file" $failed/testBuildFailure.log
            touch $out
          '';

      fails-in-check-phase =
        runCommand "replaceVars-fails"
          {
            failed =
              let
                src = builtins.toFile "source.txt" ''
                  Header.
                  before @whatIsThis@ middle @It'sOdd2Me@ after.
                  @cannot detect due to space@
                  Trailer.
                '';
              in
              testBuildFailure (callReplaceVars src { });
          }
          ''
            grep -e "unsubstituted Nix identifiers.*source.txt" $failed/testBuildFailure.log
            grep -F "@whatIsThis@" $failed/testBuildFailure.log
            grep -F "@It'sOdd2Me@" $failed/testBuildFailure.log
            grep -F 'more precise `substitute` function' $failed/testBuildFailure.log

            # Shouldn't see irrelevant details.
            ! grep -q -E -e "Header|before|middle|after|Trailer" $failed/testBuildFailure.log

            # Shouldn't see the "cannot detect" version.
            ! grep -q -F "cannot detect due to space" $failed/testBuildFailure.log

            touch $out
          '';

      succeeds-with-exemption = testEqualContents {
        assertion = "replaceVars-succeeds";
        actual = callReplaceVars ./source.txt {
          free = "free";
          "equal in" = "are the same in";
          brotherhood = null;
        };

        expected = mkExpectation (
          builtins.toFile "source.txt" ''
            All human beings are born free and are the same in dignity and rights.
            They are endowed with reason and conscience and should act towards
            one another in a spirit of @brotherhood@.

              -- eroosevelt@humanrights.un.org
          ''
        );
      };

      fails-in-check-phase-with-exemption =
        runCommand "replaceVars-fails"
          {
            failed =
              let
                src = builtins.toFile "source.txt" ''
                  @a@
                  @b@
                  @c@
                '';
              in
              testBuildFailure (
                callReplaceVars src {
                  a = "a";
                  b = null;
                }
              );
          }
          ''
            grep -e "unsubstituted Nix identifiers.*source.txt" $failed/testBuildFailure.log
            grep -F "@c@" $failed/testBuildFailure.log
            ! grep -F "@b@" $failed/testBuildFailure.log

            touch $out
          '';

      fails-in-check-phase-with-bad-exemption =
        runCommand "replaceVars-fails"
          {
            failed =
              let
                src = builtins.toFile "source.txt" ''
                  @a@
                  @b@
                '';
              in
              testBuildFailure (
                callReplaceVars src {
                  a = "a";
                  b = null;
                  c = null;
                }
              );
          }
          ''
            grep -e "ERROR: pattern @c@ doesn't match anything in file.*source.txt" $failed/testBuildFailure.log

            touch $out
          '';
    };
in
{
  replaceVars = mkTests replaceVars lib.id;
  replaceVarsWith =
    mkTests
      (
        src: replacements:
        replaceVarsWith {
          inherit src replacements;
          dir = "bin";
          isExecutable = true;
        }
      )
      (
        file:
        runCommand "expected" { inherit file; } ''
          install -Dm755 "$file" "$out/bin/$(stripHash "$file")"
        ''
      );
}