Skip to content

Commit 164357c

Browse files
committed
Run shellcheck on installation hook tests
1 parent dcf5072 commit 164357c

File tree

2 files changed

+58
-77
lines changed

2 files changed

+58
-77
lines changed

modules/pre-commit.nix

+29-55
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
;
@@ -91,30 +92,28 @@ let
9192
nativeBuildInputs = enabledExtraPackages
9293
++ lib.optional (config.settings.rust.check.cargoDeps != null) cargoSetupHook;
9394
cargoDeps = config.settings.rust.check.cargoDeps;
94-
buildPhase = ''
95-
set +e
96-
# Set HOME to a temporary directory for pre-commit to create its cache files in.
97-
HOME=$(mktemp -d)
98-
ln -fs ${cfg.configFile} ${cfg.configPath}
99-
git init -q
100-
git add .
101-
git config --global user.email "[email protected]"
102-
git config --global user.name "Your Name"
103-
git commit -m "init" -q
104-
if [[ ${toString (compare cfg.installStages [ "manual" ])} -eq 0 ]]
105-
then
106-
echo "Running: $ pre-commit run --hook-stage manual --all-files"
107-
pre-commit run -c ${cfg.configPath} --hook-stage manual --all-files
108-
else
109-
echo "Running: $ pre-commit run --all-files"
110-
pre-commit run -c ${cfg.configPath} --all-files
111-
fi
112-
exitcode=$?
113-
git --no-pager diff --color
114-
# Derivations must produce an output
115-
mkdir $out
116-
[ $? -eq 0 ] && exit $exitcode
117-
'';
95+
buildPhase =
96+
let
97+
cmd = "pre-commit run -c ${cfg.configPath}${optionalString (install_stages == [ "manual" ]) " --hook-stage manual"} --all-files";
98+
in
99+
''
100+
set +e
101+
# Set HOME to a temporary directory for pre-commit to create its cache files in.
102+
HOME=$(mktemp -d)
103+
ln -fs ${configFile} ${cfg.configPath}
104+
git init -q
105+
git add .
106+
git config --global user.email "[email protected]"
107+
git config --global user.name "Your Name"
108+
git commit -m "init" -q
109+
echo "Running: $ ${cmd}"
110+
${cfg.package}/bin/${cmd}
111+
exitcode=$?
112+
# Derivations must produce an output
113+
git --no-pager diff --color
114+
mkdir $out
115+
[ $? -eq 0 ] && exit $exitcode
116+
'';
118117
};
119118

120119
failedAssertions = builtins.map (x: x.message) (builtins.filter (x: !x.assertion) config.assertions);
@@ -423,7 +422,7 @@ in
423422
if ! ${cfg.gitPackage}/bin/git rev-parse --git-dir &> /dev/null; then
424423
echo 1>&2 "WARNING: git-hooks.nix: .git not found; skipping installation."
425424
else
426-
GIT_WC=`${cfg.gitPackage}/bin/git rev-parse --show-toplevel`
425+
GIT_WC=$(${cfg.gitPackage}/bin/git rev-parse --show-toplevel)
427426
428427
# These update procedures compare before they write, to avoid
429428
# filesystem churn. This improves performance with watch tools like lorri
@@ -449,41 +448,16 @@ in
449448
ln -fs ${cfg.configFile} "''${GIT_WC}/${cfg.configPath}"
450449
fi
451450
# Remove any previously installed hooks (since pre-commit itself has no convergent design)
452-
hooks="${concatStringsSep " " (remove "manual" supportedHooksLib.supportedHooks )}"
453-
for hook in $hooks; do
454-
pre-commit uninstall -t $hook
455-
done
451+
pre-commit uninstall${concatMapStrings (hook: " --hook-type ${hook}") (remove "manual" supportedHooksLib.supportedHooks)}
456452
${cfg.gitPackage}/bin/git config --local core.hooksPath ""
457-
# Add hooks for configured stages (only) ...
458-
if [ ! -z "${concatStringsSep " " install_stages}" ]; then
459-
for stage in ${concatStringsSep " " install_stages}; do
460-
case $stage in
461-
manual)
462-
;;
463-
# if you amend these switches please also review $hooks above
464-
commit | merge-commit | push)
465-
stage="pre-"$stage
466-
pre-commit install -c ${cfg.configPath} -t $stage
467-
;;
468-
${concatStringsSep "|" supportedHooksLib.supportedHooks})
469-
pre-commit install -c ${cfg.configPath} -t $stage
470-
;;
471-
*)
472-
echo 1>&2 "ERROR: git-hooks.nix: either $stage is not a valid stage or git-hooks.nix doesn't yet support it."
473-
exit 1
474-
;;
475-
esac
476-
done
477-
# ... or default 'pre-commit' hook
478-
else
479-
pre-commit install -c ${cfg.configPath}
480-
fi
453+
# Add hooks for configured stages
454+
pre-commit install -c ${cfg.configPath}${concatMapStrings (stage: " --hook-type ${if builtins.elem stage [ "commit" "merge-commit" "push" ] then "pre-${stage}" else stage}") (remove "manual" install_stages)}
481455
482456
# Fetch the absolute path to the git common directory. This will normally point to $GIT_WC/.git.
483457
common_dir=''$(${cfg.gitPackage}/bin/git rev-parse --path-format=absolute --git-common-dir)
484458
485459
# Convert the absolute path to a path relative to the toplevel working directory.
486-
common_dir=''${common_dir#''$GIT_WC/}
460+
common_dir=''${common_dir#"''$GIT_WC"/}
487461
488462
${cfg.gitPackage}/bin/git config --local core.hooksPath "''$common_dir/hooks"
489463
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)