Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)

testers.shellcheck: init

Needed for testing upcoming commit.

+122
+49
doc/build-helpers/testers.chapter.md
··· 116 117 : The `lychee` package to use. 118 119 ## `testVersion` {#tester-testVersion} 120 121 Checks that the output from running a command contains the specified version string in it as a whole word.
··· 116 117 : The `lychee` package to use. 118 119 + ## `shellcheck` {#tester-shellcheck} 120 + 121 + Runs files through `shellcheck`, a static analysis tool for shell scripts. 122 + 123 + :::{.example #ex-shellcheck} 124 + # Run `testers.shellcheck` 125 + 126 + A single script 127 + 128 + ```nix 129 + testers.shellcheck { 130 + name = "shellcheck"; 131 + src = ./script.sh; 132 + } 133 + ``` 134 + 135 + Multiple files 136 + 137 + ```nix 138 + let 139 + inherit (lib) fileset; 140 + in 141 + testers.shellcheck { 142 + name = "shellcheck"; 143 + src = fileset.toSource { 144 + root = ./.; 145 + fileset = fileset.unions [ 146 + ./lib.sh 147 + ./nixbsd-activate 148 + ]; 149 + }; 150 + } 151 + ``` 152 + 153 + ::: 154 + 155 + ### Inputs {#tester-shellcheck-inputs} 156 + 157 + [`src` (path or string)]{#tester-shellcheck-param-src} 158 + 159 + : The path to the shell script(s) to check. 160 + This can be a single file or a directory containing shell files. 161 + All files in `src` will be checked, so you may want to provide `fileset`-based source instead of a whole directory. 162 + 163 + ### Return value {#tester-shellcheck-return} 164 + 165 + A derivation that runs `shellcheck` on the given script(s). 166 + The build will fail if `shellcheck` finds any issues. 167 + 168 ## `testVersion` {#tester-testVersion} 169 170 Checks that the output from running a command contains the specified version string in it as a whole word.
+2
pkgs/build-support/testers/default.nix
··· 151 hasPkgConfigModules = callPackage ./hasPkgConfigModules/tester.nix { }; 152 153 testMetaPkgConfig = callPackage ./testMetaPkgConfig/tester.nix { }; 154 }
··· 151 hasPkgConfigModules = callPackage ./hasPkgConfigModules/tester.nix { }; 152 153 testMetaPkgConfig = callPackage ./testMetaPkgConfig/tester.nix { }; 154 + 155 + shellcheck = callPackage ./shellcheck/tester.nix { }; 156 }
+3
pkgs/build-support/testers/shellcheck/example.sh
···
··· 1 + #!/usr/bin/env bash 2 + 3 + echo $@
+28
pkgs/build-support/testers/shellcheck/tester.nix
···
··· 1 + # Dependencies (callPackage) 2 + { lib, stdenv, shellcheck }: 3 + 4 + # testers.shellcheck function 5 + # Docs: doc/build-helpers/testers.chapter.md 6 + # Tests: ./tests.nix 7 + { src }: 8 + let 9 + inherit (lib) fileset pathType isPath; 10 + in 11 + stdenv.mkDerivation { 12 + name = "run-shellcheck"; 13 + src = 14 + if isPath src && pathType src == "regular" # note that for strings this would have been IFD, which we prefer to avoid 15 + then fileset.toSource { root = dirOf src; fileset = src; } 16 + else src; 17 + nativeBuildInputs = [ shellcheck ]; 18 + doCheck = true; 19 + dontConfigure = true; 20 + dontBuild = true; 21 + checkPhase = '' 22 + find . -type f -print0 \ 23 + | xargs -0 shellcheck 24 + ''; 25 + installPhase = '' 26 + touch $out 27 + ''; 28 + }
+38
pkgs/build-support/testers/shellcheck/tests.nix
···
··· 1 + # Run: 2 + # nix-build -A tests.testers.shellcheck 3 + 4 + { lib, testers, runCommand }: 5 + let 6 + inherit (lib) fileset; 7 + in 8 + lib.recurseIntoAttrs { 9 + 10 + example-dir = runCommand "test-testers-shellcheck-example-dir" { 11 + failure = testers.testBuildFailure 12 + (testers.shellcheck { 13 + src = fileset.toSource { 14 + root = ./.; 15 + fileset = fileset.unions [ 16 + ./example.sh 17 + ]; 18 + }; 19 + }); 20 + } '' 21 + log="$failure/testBuildFailure.log" 22 + echo "Checking $log" 23 + grep SC2068 "$log" 24 + touch $out 25 + ''; 26 + 27 + example-file = runCommand "test-testers-shellcheck-example-file" { 28 + failure = testers.testBuildFailure 29 + (testers.shellcheck { 30 + src = ./example.sh; 31 + }); 32 + } '' 33 + log="$failure/testBuildFailure.log" 34 + echo "Checking $log" 35 + grep SC2068 "$log" 36 + touch $out 37 + ''; 38 + }
+2
pkgs/build-support/testers/test/default.nix
··· 16 17 hasPkgConfigModules = pkgs.callPackage ../hasPkgConfigModules/tests.nix { }; 18 19 runNixOSTest-example = pkgs-with-overlay.testers.runNixOSTest ({ lib, ... }: { 20 name = "runNixOSTest-test"; 21 nodes.machine = { pkgs, ... }: {
··· 16 17 hasPkgConfigModules = pkgs.callPackage ../hasPkgConfigModules/tests.nix { }; 18 19 + shellcheck = pkgs.callPackage ../shellcheck/tests.nix { }; 20 + 21 runNixOSTest-example = pkgs-with-overlay.testers.runNixOSTest ({ lib, ... }: { 22 name = "runNixOSTest-test"; 23 nodes.machine = { pkgs, ... }: {