summary refs log tree commit diff
path: root/pkgs/development/misc/resholve/test.nix
blob: dd847462420bc6e4c1d08f7091f9210fef033bfe (plain) (blame)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
{ lib
, stdenv
, callPackage
, resholve
, resholvePackage
, resholveScript
, resholveScriptBin
, shunit2
, coreutils
, gnused
, gnugrep
, findutils
, jq
, bash
, bats
, libressl
, openssl
, python27
, file
, gettext
, rSrc
, runDemo ? false
, binlore
}:

let

in
rec {
  re_shunit2 = with shunit2;
    resholvePackage {
      inherit pname src version installPhase;
      solutions = {
        shunit = {
          interpreter = "none";
          scripts = [ "bin/shunit2" ];
          inputs = [ coreutils gnused gnugrep findutils ];
          # resholve's Nix API is analogous to the CLI flags
          # documented in 'man resholve'
          fake = {
            # "missing" functions shunit2 expects the user to declare
            function = [
              "oneTimeSetUp"
              "oneTimeTearDown"
              "setUp"
              "tearDown"
              "suite"
              "noexec"
            ];
            # shunit2 is both bash and zsh compatible, and in
            # some zsh-specific code it uses this non-bash builtin
            builtin = [ "setopt" ];
          };
          fix = {
            # stray absolute path; make it resolve from coreutils
            "/usr/bin/od" = true;
          };
          keep = {
            # dynamically defined in shunit2:_shunit_mktempFunc
            eval = [ "shunit_condition_" "_shunit_test_" "_shunit_prepForSourcing" ];

            # variables invoked as commands; long-term goal is to
            # resolve the *variable*, but that is complexish, so
            # this is where we are...
            "$__SHUNIT_CMD_ECHO_ESC" = true;
            "$_SHUNIT_LINENO_" = true;
            "$SHUNIT_CMD_TPUT" = true;
          };
        };
      };
    };
  module1 = resholvePackage {
    pname = "testmod1";
    version = "unreleased";

    src = rSrc;
    setSourceRoot = "sourceRoot=$(echo */tests/nix/libressl)";

    installPhase = ''
      mkdir -p $out/{bin,submodule}
      install libressl.sh $out/bin/libressl.sh
      install submodule/helper.sh $out/submodule/helper.sh
    '';

    solutions = {
      libressl = {
        # submodule to demonstrate
        scripts = [ "bin/libressl.sh" "submodule/helper.sh" ];
        interpreter = "none";
        inputs = [ jq module2 libressl.bin ];
      };
    };

    is_it_okay_with_arbitrary_envs = "shonuff";
  };
  module2 = resholvePackage {
    pname = "testmod2";
    version = "unreleased";

    src = rSrc;
    setSourceRoot = "sourceRoot=$(echo */tests/nix/openssl)";

    installPhase = ''
      mkdir -p $out/bin
      install openssl.sh $out/bin/openssl.sh
      install profile $out/profile
    '';

    solutions = {
      openssl = {
        fix = {
          aliases = true;
        };
        scripts = [ "bin/openssl.sh" ];
        interpreter = "none";
        inputs = [ re_shunit2 openssl.bin ];
        execer = [
          /*
            This is the same verdict binlore will
            come up with. It's a no-op just to demo
            how to fiddle lore via the Nix API.
          */
          "cannot:${openssl.bin}/bin/openssl"
          # different verdict, but not used
          "can:${openssl.bin}/bin/c_rehash"
        ];
      };
      profile = {
        scripts = [ "profile" ];
        interpreter = "none";
        inputs = [ ];
      };
    };
  };
  module3 = resholvePackage {
    pname = "testmod3";
    version = "unreleased";

    src = rSrc;
    setSourceRoot = "sourceRoot=$(echo */tests/nix/future_perfect_tense)";

    installPhase = ''
      mkdir -p $out/bin
      install conjure.sh $out/bin/conjure.sh
    '';

    solutions = {
      conjure = {
        scripts = [ "bin/conjure.sh" ];
        interpreter = "${bash}/bin/bash";
        inputs = [ module1 ];
      };
    };
  };

  cli = stdenv.mkDerivation {
    name = "resholve-test";
    src = rSrc;
    installPhase = ''
      mkdir $out
      cp *.ansi $out/
    '';
    doCheck = true;
    buildInputs = [ resholve ];
    checkInputs = [ coreutils bats python27 ];
    # LOGLEVEL="DEBUG";

    # default path
    RESHOLVE_PATH = "${lib.makeBinPath [ bash file findutils gettext ]}";
    # but separate packages for combining as needed
    PKG_FILE = "${lib.makeBinPath [ file ]}";
    PKG_FINDUTILS = "${lib.makeBinPath [ findutils ]}";
    PKG_GETTEXT = "${lib.makeBinPath [ gettext ]}";
    PKG_COREUTILS = "${lib.makeBinPath [ coreutils ]}";
    RESHOLVE_LORE = "${binlore.collect { drvs = [ bash file findutils gettext coreutils ]; } }";

    # explicit interpreter for demo suite; maybe some better way...
    INTERP = "${bash}/bin/bash";

    checkPhase = ''
      patchShebangs .
      mkdir empty_lore
      touch empty_lore/{execers,wrappers}
      export EMPTY_LORE=$PWD/empty_lore
      printf "\033[33m============================= resholve test suite ===================================\033[0m\n" > test.ansi
      if ./test.sh &>> test.ansi; then
        cat test.ansi
      else
        cat test.ansi && exit 1
      fi
    '' + lib.optionalString runDemo ''
      printf "\033[33m============================= resholve demo ===================================\033[0m\n" > demo.ansi
      if ./demo &>> demo.ansi; then
        cat demo.ansi
      else
        cat demo.ansi && exit 1
      fi
    '';
  };

  # Caution: ci.nix asserts the equality of both of these w/ diff
  resholvedScript = resholveScript "resholved-script" {
    inputs = [ file ];
    interpreter = "${bash}/bin/bash";
  } ''
    echo "Hello"
    file .
  '';
  resholvedScriptBin = resholveScriptBin "resholved-script-bin" {
    inputs = [ file ];
    interpreter = "${bash}/bin/bash";
  } ''
    echo "Hello"
    file .
  '';
}