Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1{ resholve
2, lib
3, stdenv
4, fetchFromGitHub
5, bash
6, coreutils
7, gnugrep
8, ncurses
9, findutils
10, hostname
11, parallel
12, flock
13, procps
14, bats
15, lsof
16, callPackages
17, symlinkJoin
18, makeWrapper
19, runCommand
20, doInstallCheck ? true
21}:
22
23resholve.mkDerivation rec {
24 pname = "bats";
25 version = "1.9.0";
26
27 src = fetchFromGitHub {
28 owner = "bats-core";
29 repo = "bats-core";
30 rev = "v${version}";
31 sha256 = "sha256-nKBNbqJYRd/3tO85E6KrOh32yOaNKpLXxz5gQ5Uvmcc=";
32 };
33
34 patchPhase = ''
35 patchShebangs .
36 '';
37
38 installPhase = ''
39 ./install.sh $out
40 '';
41
42 solutions = {
43 bats = {
44 scripts = [
45 "bin/bats"
46 "libexec/bats-core/*"
47 "lib/bats-core/*"
48 ];
49 interpreter = "${bash}/bin/bash";
50 inputs = [
51 bash
52 coreutils
53 gnugrep
54 ncurses
55 findutils
56 hostname
57 parallel
58 flock
59 "lib/bats-core"
60 "libexec/bats-core"
61 procps
62 ];
63 fake = {
64 external = [
65 "greadlink"
66 "shlock"
67 "pkill" # procps doesn't supply this on darwin
68 ];
69 };
70 fix = {
71 "$BATS_ROOT" = [ "${placeholder "out"}" ];
72 "$BATS_LIBEXEC" = [ "${placeholder "out"}/libexec/bats-core" ];
73 };
74 keep = {
75 "${placeholder "out"}/libexec/bats-core/bats" = true;
76 source = [
77 "${placeholder "out"}/lib/bats-core/validator.bash"
78 "${placeholder "out"}/lib/bats-core/preprocessing.bash"
79 "$BATS_TEST_SOURCE"
80 "${placeholder "out"}/lib/bats-core/tracing.bash"
81 "${placeholder "out"}/lib/bats-core/test_functions.bash"
82 "$library_load_path"
83 "${placeholder "out"}/lib/bats-core/common.bash"
84 "${placeholder "out"}/lib/bats-core/semaphore.bash"
85 "${placeholder "out"}/lib/bats-core/formatter.bash"
86 "${placeholder "out"}/lib/bats-core/warnings.bash"
87 "$setup_suite_file" # via cli arg
88 ];
89 "$interpolated_report_formatter" = true;
90 "$interpolated_formatter" = true;
91 "$pre_command" = true;
92 "$BATS_TEST_NAME" = true;
93 "${placeholder "out"}/libexec/bats-core/bats-exec-test" = true;
94 "$BATS_LINE_REFERENCE_FORMAT" = "comma_line";
95 "$BATS_LOCKING_IMPLEMENTATION" = "${flock}/bin/flock";
96 };
97 execer = [
98 /*
99 both blatant lies for expedience; these can certainly exec args
100 they may be safe here, because they may always run things that
101 are ultimately in libexec?
102 TODO: handle parallel and flock in binlore/resholve
103 */
104 "cannot:${parallel}/bin/parallel"
105 "cannot:${flock}/bin/flock"
106
107 "cannot:libexec/bats-core/bats-preprocess"
108
109 # these do exec, but other internal files
110 "cannot:libexec/bats-core/bats-exec-file"
111 "cannot:libexec/bats-core/bats-exec-suite"
112 ];
113 };
114 };
115
116 passthru.libraries = callPackages ./libraries.nix {};
117
118 passthru.withLibraries = selector:
119 symlinkJoin {
120 name = "bats-with-libraries-${bats.version}";
121
122 paths = [
123 bats
124 ] ++ selector bats.libraries;
125
126 nativeBuildInputs = [
127 makeWrapper
128 ];
129
130 postBuild = ''
131 wrapProgram "$out/bin/bats" \
132 --suffix BATS_LIB_PATH : "$out/share/bats"
133 '';
134 };
135
136 passthru.tests.libraries = runCommand "${bats.name}-with-libraries-test" {
137 testScript = ''
138 setup() {
139 bats_load_library bats-support
140 bats_load_library bats-assert
141 bats_load_library bats-file
142
143 bats_require_minimum_version 1.5.0
144
145 TEST_TEMP_DIR="$(temp_make --prefix 'nixpkgs-bats-test')"
146 }
147
148 teardown() {
149 temp_del "$TEST_TEMP_DIR"
150 }
151
152 @test echo_hi {
153 run -0 echo hi
154 assert_output "hi"
155 }
156
157 @test cp_failure {
158 run ! cp
159 assert_line --index 0 "cp: missing file operand"
160 assert_line --index 1 "Try 'cp --help' for more information."
161 }
162
163 @test file_exists {
164 echo "hi" > "$TEST_TEMP_DIR/hello.txt"
165 assert_file_exist "$TEST_TEMP_DIR/hello.txt"
166 run cat "$TEST_TEMP_DIR/hello.txt"
167 assert_output "hi"
168 }
169 '';
170 passAsFile = [ "testScript" ];
171 } ''
172 ${bats.withLibraries (p: [ p.bats-support p.bats-assert p.bats-file ])}/bin/bats "$testScriptPath"
173 touch "$out"
174 '';
175
176 passthru.tests.upstream = bats.unresholved.overrideAttrs (old: {
177 name = "${bats.name}-tests";
178 dontInstall = true; # just need the build directory
179 nativeInstallCheckInputs = [
180 ncurses
181 parallel # skips some tests if it can't detect
182 flock # skips some tests if it can't detect
183 procps
184 ] ++ lib.optionals stdenv.isDarwin [ lsof ];
185 inherit doInstallCheck;
186 installCheckPhase = ''
187 # TODO: cut if https://github.com/bats-core/bats-core/issues/418 allows
188 sed -i '/test works even if PATH is reset/a skip "disabled for nix build"' test/bats.bats
189
190 # skip tests that assume bats `install.sh` will be in BATS_ROOT
191 rm test/root.bats
192
193 '' + (lib.optionalString stdenv.hostPlatform.isDarwin ''
194 # skip new timeout tests which are failing on macOS for unclear reasons
195 # This might relate to procps not having a pkill?
196 rm test/timeout.bats
197 '') + ''
198
199 # test generates file with absolute shebang dynamically
200 substituteInPlace test/install.bats --replace \
201 "/usr/bin/env bash" "${bash}/bin/bash"
202
203 ${bats}/bin/bats test
204 touch $out
205 '';
206 });
207
208 meta = with lib; {
209 homepage = "https://github.com/bats-core/bats-core";
210 description = "Bash Automated Testing System";
211 maintainers = with maintainers; [ abathur ];
212 license = licenses.mit;
213 platforms = platforms.unix;
214 };
215}