Skip to content

Commit 871cda0

Browse files
authored
testers.shellcheck: refactor, update docs, and simplify tests (NixOS#385940)
2 parents dd5a41a + cd7df19 commit 871cda0

File tree

7 files changed

+55
-58
lines changed

7 files changed

+55
-58
lines changed

doc/build-helpers/testers.chapter.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ It has two modes:
118118

119119
## `shellcheck` {#tester-shellcheck}
120120

121-
Runs files through `shellcheck`, a static analysis tool for shell scripts.
121+
Run files through `shellcheck`, a static analysis tool for shell scripts, failing if there are any issues.
122122

123123
:::{.example #ex-shellcheck}
124124
# Run `testers.shellcheck`
@@ -127,7 +127,7 @@ A single script
127127

128128
```nix
129129
testers.shellcheck {
130-
name = "shellcheck";
130+
name = "script";
131131
src = ./script.sh;
132132
}
133133
```
@@ -139,7 +139,7 @@ let
139139
inherit (lib) fileset;
140140
in
141141
testers.shellcheck {
142-
name = "shellcheck";
142+
name = "nixbsd-activate";
143143
src = fileset.toSource {
144144
root = ./.;
145145
fileset = fileset.unions [
@@ -154,15 +154,20 @@ testers.shellcheck {
154154

155155
### Inputs {#tester-shellcheck-inputs}
156156

157-
[`src` (path or string)]{#tester-shellcheck-param-src}
157+
`name` (string, optional)
158+
: The name of the test.
159+
`name` will be required at a future point because it massively improves traceability of test failures, but is kept optional for now to avoid breaking existing usages.
160+
Defaults to `run-shellcheck`.
161+
The name of the derivation produced by the tester is `shellcheck-${name}` when `name` is supplied.
158162

163+
`src` (path-like)
159164
: The path to the shell script(s) to check.
160165
This can be a single file or a directory containing shell files.
161166
All files in `src` will be checked, so you may want to provide `fileset`-based source instead of a whole directory.
162167

163168
### Return value {#tester-shellcheck-return}
164169

165-
A derivation that runs `shellcheck` on the given script(s).
170+
A derivation that runs `shellcheck` on the given script(s), producing an empty output if no issues are found.
166171
The build will fail if `shellcheck` finds any issues.
167172

168173
## `shfmt` {#tester-shfmt}

doc/redirects.json

-3
Original file line numberDiff line numberDiff line change
@@ -1557,9 +1557,6 @@
15571557
"tester-shellcheck-inputs": [
15581558
"index.html#tester-shellcheck-inputs"
15591559
],
1560-
"tester-shellcheck-param-src": [
1561-
"index.html#tester-shellcheck-param-src"
1562-
],
15631560
"tester-shellcheck-return": [
15641561
"index.html#tester-shellcheck-return"
15651562
],

doc/release-notes/rl-2505.section.md

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
- The hand written `perlPackages.SearchXapian` bindings have been dropped in favor of the (mostly compatible)
1919
`perlPackages.Xapian`.
2020

21+
- [testers.shellcheck](https://nixos.org/manual/nixpkgs/unstable/#tester-shellcheck) now warns when `name` is not provided.
22+
The `name` argument will become mandatory in a future release.
23+
2124
- The `nixLog*` family of functions made available through the standard environment have been rewritten to prefix messages with both the debug level and the function name of the caller.
2225
The `nixLog` function, which logs unconditionally, was also re-introduced and modified to prefix messages with the function name of the caller.
2326
For more information, [see this PR](https://github.com/NixOS/nixpkgs/pull/370742).

nixos/modules/config/nix-channel/test.nix

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ let
55
inherit (lib) fileset;
66

77
runShellcheck = testers.shellcheck {
8+
name = "activation-check";
89
src = fileset.toSource {
910
root = ./.;
1011
fileset = fileset.unions [

nixos/modules/system/activation/lib/test.nix

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ let
3131
};
3232

3333
runShellcheck = testers.shellcheck {
34+
name = "activation-lib";
3435
src = runTests.src;
3536
};
3637

Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
11
# Dependencies (callPackage)
22
{
33
lib,
4-
stdenv,
5-
runCommand,
4+
stdenvNoCC,
65
shellcheck,
76
}:
87

98
# testers.shellcheck function
109
# Docs: doc/build-helpers/testers.chapter.md
1110
# Tests: ./tests.nix
12-
{ src }:
13-
let
14-
inherit (lib) pathType isPath;
15-
in
16-
stdenv.mkDerivation {
17-
name = "run-shellcheck";
18-
src =
19-
if
20-
isPath src && pathType src == "regular" # note that for strings this would have been IFD, which we prefer to avoid
21-
then
22-
runCommand "testers-shellcheck-src" { } ''
23-
mkdir $out
24-
cp ${src} $out
25-
''
11+
{
12+
name ? null,
13+
src,
14+
}:
15+
stdenvNoCC.mkDerivation {
16+
__structuredAttrs = true;
17+
strictDeps = true;
18+
name =
19+
if name == null then
20+
lib.warn "testers.shellcheck: name will be required in a future release, defaulting to run-shellcheck" "run-shellcheck"
2621
else
27-
src;
22+
"shellcheck-${name}";
23+
inherit src;
24+
dontUnpack = true; # Unpack phase tries to extract an archive, which we don't want to do with source trees
2825
nativeBuildInputs = [ shellcheck ];
2926
doCheck = true;
3027
dontConfigure = true;
3128
dontBuild = true;
3229
checkPhase = ''
33-
find . -type f -print0 \
34-
| xargs -0 shellcheck
30+
find "$src" -type f -print0 | xargs -0 shellcheck
3531
'';
3632
installPhase = ''
37-
touch $out
33+
touch "$out"
3834
'';
3935
}

pkgs/build-support/testers/shellcheck/tests.nix

+24-30
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,33 @@
44
{
55
lib,
66
testers,
7-
runCommand,
87
}:
98
lib.recurseIntoAttrs {
10-
11-
example-dir =
12-
runCommand "test-testers-shellcheck-example-dir"
13-
{
14-
failure = testers.testBuildFailure (
15-
testers.shellcheck {
16-
src = ./src;
17-
}
18-
);
19-
}
9+
example-dir = testers.testBuildFailure' {
10+
drv = testers.shellcheck {
11+
name = "example-dir";
12+
src = ./src;
13+
};
14+
expectedBuilderExitCode = 123;
15+
expectedBuilderLogEntries = [
16+
''
17+
echo $@
18+
^-- SC2068 (error): Double quote array expansions to avoid re-splitting elements.
2019
''
21-
log="$failure/testBuildFailure.log"
22-
echo "Checking $log"
23-
grep SC2068 "$log"
24-
touch $out
25-
'';
20+
];
21+
};
2622

27-
example-file =
28-
runCommand "test-testers-shellcheck-example-file"
29-
{
30-
failure = testers.testBuildFailure (
31-
testers.shellcheck {
32-
src = ./src/example.sh;
33-
}
34-
);
35-
}
23+
example-file = testers.testBuildFailure' {
24+
drv = testers.shellcheck {
25+
name = "example-file";
26+
src = ./src/example.sh;
27+
};
28+
expectedBuilderExitCode = 123;
29+
expectedBuilderLogEntries = [
30+
''
31+
echo $@
32+
^-- SC2068 (error): Double quote array expansions to avoid re-splitting elements.
3633
''
37-
log="$failure/testBuildFailure.log"
38-
echo "Checking $log"
39-
grep SC2068 "$log"
40-
touch $out
41-
'';
34+
];
35+
};
4236
}

0 commit comments

Comments
 (0)