Skip to content

Commit d75da9d

Browse files
committed
Run shellcheck on installation hook tests
1 parent d8c02f0 commit d75da9d

File tree

2 files changed

+56
-75
lines changed

2 files changed

+56
-75
lines changed

modules/pre-commit.nix

+27-53
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
let
33
inherit (lib)
44
boolToString
5+
concatMapStrings
56
concatStringsSep
6-
compare
77
filterAttrs
88
literalExample
99
mapAttrsToList
1010
mkOption
11+
optionalString
1112
types
1213
remove
1314
;
@@ -62,28 +63,26 @@ let
6263
nativeBuildInputs = enabledExtraPackages
6364
++ lib.optional (config.settings.rust.check.cargoDeps != null) cargoSetupHook;
6465
cargoDeps = config.settings.rust.check.cargoDeps;
65-
buildPhase = ''
66-
set +e
67-
HOME=$PWD
68-
ln -fs ${configFile} .pre-commit-config.yaml
69-
git init -q
70-
git add .
71-
git config --global user.email "[email protected]"
72-
git config --global user.name "Your Name"
73-
git commit -m "init" -q
74-
if [[ ${toString (compare install_stages [ "manual" ])} -eq 0 ]]
75-
then
76-
echo "Running: $ pre-commit run --hook-stage manual --all-files"
77-
${cfg.package}/bin/pre-commit run --hook-stage manual --all-files
78-
else
79-
echo "Running: $ pre-commit run --all-files"
80-
${cfg.package}/bin/pre-commit run --all-files
81-
fi
82-
exitcode=$?
83-
git --no-pager diff --color
84-
mkdir $out
85-
[ $? -eq 0 ] && exit $exitcode
86-
'';
66+
buildPhase =
67+
let
68+
cmd = "pre-commit run${optionalString (install_stages == [ "manual" ]) " --hook-stage manual"} --all-files";
69+
in
70+
''
71+
set +e
72+
HOME=$PWD
73+
ln -fs ${configFile} .pre-commit-config.yaml
74+
git init -q
75+
git add .
76+
git config --global user.email "[email protected]"
77+
git config --global user.name "Your Name"
78+
git commit -m "init" -q
79+
echo "Running: $ ${cmd}"
80+
${cfg.package}/bin/${cmd}
81+
exitcode=$?
82+
git --no-pager diff --color
83+
mkdir $out
84+
[ $? -eq 0 ] && exit $exitcode
85+
'';
8786
};
8887

8988
failedAssertions = builtins.map (x: x.message) (builtins.filter (x: !x.assertion) config.assertions);
@@ -361,7 +360,7 @@ in
361360
elif ! ${cfg.gitPackage}/bin/git rev-parse --git-dir &> /dev/null; then
362361
echo 1>&2 "WARNING: git-hooks.nix: .git not found; skipping installation."
363362
else
364-
GIT_WC=`${cfg.gitPackage}/bin/git rev-parse --show-toplevel`
363+
GIT_WC=$(${cfg.gitPackage}/bin/git rev-parse --show-toplevel)
365364
366365
# These update procedures compare before they write, to avoid
367366
# filesystem churn. This improves performance with watch tools like lorri
@@ -385,41 +384,16 @@ in
385384
ln -fs ${configFile} "''${GIT_WC}/.pre-commit-config.yaml"
386385
fi
387386
# Remove any previously installed hooks (since pre-commit itself has no convergent design)
388-
hooks="${concatStringsSep " " (remove "manual" supportedHooksLib.supportedHooks )}"
389-
for hook in $hooks; do
390-
pre-commit uninstall -t $hook
391-
done
387+
pre-commit uninstall${concatMapStrings (hook: " --hook-type ${hook}") (remove "manual" supportedHooksLib.supportedHooks)}
392388
${cfg.gitPackage}/bin/git config --local core.hooksPath ""
393-
# Add hooks for configured stages (only) ...
394-
if [ ! -z "${concatStringsSep " " install_stages}" ]; then
395-
for stage in ${concatStringsSep " " install_stages}; do
396-
case $stage in
397-
manual)
398-
;;
399-
# if you amend these switches please also review $hooks above
400-
commit | merge-commit | push)
401-
stage="pre-"$stage
402-
pre-commit install -t $stage
403-
;;
404-
${concatStringsSep "|" supportedHooksLib.supportedHooks})
405-
pre-commit install -t $stage
406-
;;
407-
*)
408-
echo 1>&2 "ERROR: git-hooks.nix: either $stage is not a valid stage or git-hooks.nix doesn't yet support it."
409-
exit 1
410-
;;
411-
esac
412-
done
413-
# ... or default 'pre-commit' hook
414-
else
415-
pre-commit install
416-
fi
389+
# Add hooks for configured stages
390+
pre-commit install${concatMapStrings (stage: " --hook-type ${if builtins.elem stage [ "commit" "merge-commit" "push" ] then "pre-${stage}" else stage}") (remove "manual" install_stages)}
417391
418392
# Fetch the absolute path to the git common directory. This will normally point to $GIT_WC/.git.
419393
common_dir=''$(${cfg.gitPackage}/bin/git rev-parse --path-format=absolute --git-common-dir)
420394
421395
# Convert the absolute path to a path relative to the toplevel working directory.
422-
common_dir=''${common_dir#''$GIT_WC/}
396+
common_dir=''${common_dir#"''$GIT_WC"/}
423397
424398
${cfg.gitPackage}/bin/git config --local core.hooksPath "''$common_dir/hooks"
425399
fi

nix/installation-test.nix

+29-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Test that checks whether the correct hooks are created in the hooks folder.
2-
{ git, perl, coreutils, runCommand, run, lib, mktemp }:
2+
{ git, perl, coreutils, runCommand, run, lib, mktemp, writeShellApplication }:
33
let
44
tests = {
55
basic-test = {
@@ -65,35 +65,42 @@ let
6565
in ''
6666
rm -f ~/.git/hooks/*
6767
${runDerivation.shellHook}
68-
actualHooks=(`find ~/.git/hooks -type f -printf "%f "`)
69-
read -a expectedHooks <<< "${builtins.toString test.expectedHooks}"
68+
read -r -a actualHooks <<< "$(find ~/.git/hooks -type f -printf "%f ")"
69+
expectedHooks=(${builtins.toString test.expectedHooks})
7070
if ! assertArraysEqual actualHooks expectedHooks; then
71-
echo "${name} failed: Expected hooks '${builtins.toString test.expectedHooks}' but found '$actualHooks'."
71+
echo "${name} failed: Expected hooks \"''${expectedHooks[*]}\" but found \"''${actualHooks[*]}\"."
7272
return 1
7373
fi
7474
'')
7575
tests;
76-
in
77-
runCommand "installation-test" { nativeBuildInputs = [ git perl coreutils mktemp ]; } ''
78-
set -eoux
7976

80-
HOME=$(mktemp -d)
81-
cd $HOME
82-
git init
77+
testScript = writeShellApplication {
78+
name = "installation-test";
79+
runtimeInputs = [ git perl coreutils mktemp ];
80+
bashOptions = [ "errexit" "nounset" "xtrace" ];
81+
text = ''
82+
HOME=$(mktemp -d)
83+
cd "$HOME"
84+
git init
8385
84-
assertArraysEqual() {
85-
local -n _array_one=$1
86-
local -n _array_two=$2
87-
diffArray=(`echo ''${_array_one[@]} ''${_array_two[@]} | tr ' ' '\n' | sort | uniq -u`)
88-
if [ ''${#diffArray[@]} -eq 0 ]
89-
then
90-
return 0
91-
else
92-
return 1
93-
fi
94-
}
86+
assertArraysEqual() {
87+
local -n _array_one=$1
88+
local -n _array_two=$2
89+
read -r -a diffArray <<< "$( echo "''${_array_one[*]} ''${_array_two[*]}" | tr ' ' '\n' | sort | uniq -u )"
90+
if [ ''${#diffArray[@]} -eq 0 ]
91+
then
92+
return 0
93+
else
94+
return 1
95+
fi
96+
}
9597
96-
${lib.concatStrings executeTest}
98+
${lib.concatStrings executeTest}
99+
'';
100+
};
101+
in
102+
runCommand "run-installation-test" { } ''
103+
${lib.getExe testScript}
97104
98105
echo "success" > $out
99106
''

0 commit comments

Comments
 (0)