Skip to content

Commit 25a0759

Browse files
committed
Run shellcheck on installation hook tests
1 parent 6f4e2a2 commit 25a0759

File tree

2 files changed

+45
-61
lines changed

2 files changed

+45
-61
lines changed

modules/pre-commit.nix

+16-39
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
;
@@ -66,14 +67,15 @@ let
6667
git config --global user.email "[email protected]"
6768
git config --global user.name "Your Name"
6869
git commit -m "init" -q
69-
if [[ ${toString (compare install_stages [ "manual" ])} -eq 0 ]]
70-
then
71-
echo "Running: $ pre-commit run --hook-stage manual --all-files"
72-
${cfg.package}/bin/pre-commit run --hook-stage manual --all-files
73-
else
74-
echo "Running: $ pre-commit run --all-files"
75-
${cfg.package}/bin/pre-commit run --all-files
76-
fi
70+
${
71+
let
72+
cmd = "pre-commit run${optionalString (install_stages == [ "manual" ]) " --hook-stage manual"} --all-files";
73+
in
74+
''
75+
echo "Running: $ ${cmd}"
76+
${cfg.package}/bin/${cmd}
77+
''
78+
}
7779
exitcode=$?
7880
git --no-pager diff --color
7981
mkdir $out
@@ -355,7 +357,7 @@ in
355357
elif ! ${cfg.gitPackage}/bin/git rev-parse --git-dir &> /dev/null; then
356358
echo 1>&2 "WARNING: git-hooks.nix: .git not found; skipping installation."
357359
else
358-
GIT_WC=`${cfg.gitPackage}/bin/git rev-parse --show-toplevel`
360+
GIT_WC=$(${cfg.gitPackage}/bin/git rev-parse --show-toplevel)
359361
360362
# These update procedures compare before they write, to avoid
361363
# filesystem churn. This improves performance with watch tools like lorri
@@ -379,41 +381,16 @@ in
379381
ln -fs ${configFile} "''${GIT_WC}/.pre-commit-config.yaml"
380382
fi
381383
# Remove any previously installed hooks (since pre-commit itself has no convergent design)
382-
hooks="${concatStringsSep " " (remove "manual" supportedHooksLib.supportedHooks )}"
383-
for hook in $hooks; do
384-
pre-commit uninstall -t $hook
385-
done
384+
pre-commit uninstall${concatMapStrings (hook: " --hook-type ${hook}") (remove "manual" supportedHooksLib.supportedHooks)}
386385
${cfg.gitPackage}/bin/git config --local core.hooksPath ""
387-
# Add hooks for configured stages (only) ...
388-
if [ ! -z "${concatStringsSep " " install_stages}" ]; then
389-
for stage in ${concatStringsSep " " install_stages}; do
390-
case $stage in
391-
manual)
392-
;;
393-
# if you amend these switches please also review $hooks above
394-
commit | merge-commit | push)
395-
stage="pre-"$stage
396-
pre-commit install -t $stage
397-
;;
398-
${concatStringsSep "|" supportedHooksLib.supportedHooks})
399-
pre-commit install -t $stage
400-
;;
401-
*)
402-
echo 1>&2 "ERROR: git-hooks.nix: either $stage is not a valid stage or git-hooks.nix doesn't yet support it."
403-
exit 1
404-
;;
405-
esac
406-
done
407-
# ... or default 'pre-commit' hook
408-
else
409-
pre-commit install
410-
fi
386+
# Add hooks for configured stages
387+
pre-commit install${concatMapStrings (stage: " --hook-type ${if builtins.elem stage [ "commit" "merge-commit" "push" ] then "pre-${stage}" else stage}") (remove "manual" install_stages)}
411388
412389
# Fetch the absolute path to the git common directory. This will normally point to $GIT_WC/.git.
413390
common_dir=''$(${cfg.gitPackage}/bin/git rev-parse --path-format=absolute --git-common-dir)
414391
415392
# Convert the absolute path to a path relative to the toplevel working directory.
416-
common_dir=''${common_dir#''$GIT_WC/}
393+
common_dir=''${common_dir#"''$GIT_WC"/}
417394
418395
${cfg.gitPackage}/bin/git config --local core.hooksPath "''$common_dir/hooks"
419396
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)