Skip to content

Commit

Permalink
Run shellcheck on installation hook tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicknovitski committed Dec 6, 2024
1 parent 6f4e2a2 commit 25a0759
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 61 deletions.
55 changes: 16 additions & 39 deletions modules/pre-commit.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
let
inherit (lib)
boolToString
concatMapStrings
concatStringsSep
compare
filterAttrs
literalExample
mapAttrsToList
mkOption
optionalString
types
remove
;
Expand Down Expand Up @@ -66,14 +67,15 @@ let
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
git commit -m "init" -q
if [[ ${toString (compare install_stages [ "manual" ])} -eq 0 ]]
then
echo "Running: $ pre-commit run --hook-stage manual --all-files"
${cfg.package}/bin/pre-commit run --hook-stage manual --all-files
else
echo "Running: $ pre-commit run --all-files"
${cfg.package}/bin/pre-commit run --all-files
fi
${
let
cmd = "pre-commit run${optionalString (install_stages == [ "manual" ]) " --hook-stage manual"} --all-files";
in
''
echo "Running: $ ${cmd}"
${cfg.package}/bin/${cmd}
''
}
exitcode=$?
git --no-pager diff --color
mkdir $out
Expand Down Expand Up @@ -355,7 +357,7 @@ in
elif ! ${cfg.gitPackage}/bin/git rev-parse --git-dir &> /dev/null; then
echo 1>&2 "WARNING: git-hooks.nix: .git not found; skipping installation."
else
GIT_WC=`${cfg.gitPackage}/bin/git rev-parse --show-toplevel`
GIT_WC=$(${cfg.gitPackage}/bin/git rev-parse --show-toplevel)
# These update procedures compare before they write, to avoid
# filesystem churn. This improves performance with watch tools like lorri
Expand All @@ -379,41 +381,16 @@ in
ln -fs ${configFile} "''${GIT_WC}/.pre-commit-config.yaml"
fi
# Remove any previously installed hooks (since pre-commit itself has no convergent design)
hooks="${concatStringsSep " " (remove "manual" supportedHooksLib.supportedHooks )}"
for hook in $hooks; do
pre-commit uninstall -t $hook
done
pre-commit uninstall${concatMapStrings (hook: " --hook-type ${hook}") (remove "manual" supportedHooksLib.supportedHooks)}
${cfg.gitPackage}/bin/git config --local core.hooksPath ""
# Add hooks for configured stages (only) ...
if [ ! -z "${concatStringsSep " " install_stages}" ]; then
for stage in ${concatStringsSep " " install_stages}; do
case $stage in
manual)
;;
# if you amend these switches please also review $hooks above
commit | merge-commit | push)
stage="pre-"$stage
pre-commit install -t $stage
;;
${concatStringsSep "|" supportedHooksLib.supportedHooks})
pre-commit install -t $stage
;;
*)
echo 1>&2 "ERROR: git-hooks.nix: either $stage is not a valid stage or git-hooks.nix doesn't yet support it."
exit 1
;;
esac
done
# ... or default 'pre-commit' hook
else
pre-commit install
fi
# Add hooks for configured stages
pre-commit install${concatMapStrings (stage: " --hook-type ${if builtins.elem stage [ "commit" "merge-commit" "push" ] then "pre-${stage}" else stage}") (remove "manual" install_stages)}
# Fetch the absolute path to the git common directory. This will normally point to $GIT_WC/.git.
common_dir=''$(${cfg.gitPackage}/bin/git rev-parse --path-format=absolute --git-common-dir)
# Convert the absolute path to a path relative to the toplevel working directory.
common_dir=''${common_dir#''$GIT_WC/}
common_dir=''${common_dir#"''$GIT_WC"/}
${cfg.gitPackage}/bin/git config --local core.hooksPath "''$common_dir/hooks"
fi
Expand Down
51 changes: 29 additions & 22 deletions nix/installation-test.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Test that checks whether the correct hooks are created in the hooks folder.
{ git, perl, coreutils, runCommand, run, lib, mktemp }:
{ git, perl, coreutils, runCommand, run, lib, mktemp, writeShellApplication }:
let
tests = {
basic-test = {
Expand Down Expand Up @@ -65,35 +65,42 @@ let
in ''
rm -f ~/.git/hooks/*
${runDerivation.shellHook}
actualHooks=(`find ~/.git/hooks -type f -printf "%f "`)
read -a expectedHooks <<< "${builtins.toString test.expectedHooks}"
read -r -a actualHooks <<< "$(find ~/.git/hooks -type f -printf "%f ")"
expectedHooks=(${builtins.toString test.expectedHooks})
if ! assertArraysEqual actualHooks expectedHooks; then
echo "${name} failed: Expected hooks '${builtins.toString test.expectedHooks}' but found '$actualHooks'."
echo "${name} failed: Expected hooks \"''${expectedHooks[*]}\" but found \"''${actualHooks[*]}\"."
return 1
fi
'')
tests;
in
runCommand "installation-test" { nativeBuildInputs = [ git perl coreutils mktemp ]; } ''
set -eoux

HOME=$(mktemp -d)
cd $HOME
git init
testScript = writeShellApplication {
name = "installation-test";
runtimeInputs = [ git perl coreutils mktemp ];
bashOptions = [ "errexit" "nounset" "xtrace" ];
text = ''
HOME=$(mktemp -d)
cd "$HOME"
git init
assertArraysEqual() {
local -n _array_one=$1
local -n _array_two=$2
diffArray=(`echo ''${_array_one[@]} ''${_array_two[@]} | tr ' ' '\n' | sort | uniq -u`)
if [ ''${#diffArray[@]} -eq 0 ]
then
return 0
else
return 1
fi
}
assertArraysEqual() {
local -n _array_one=$1
local -n _array_two=$2
read -r -a diffArray <<< "$( echo "''${_array_one[*]} ''${_array_two[*]}" | tr ' ' '\n' | sort | uniq -u )"
if [ ''${#diffArray[@]} -eq 0 ]
then
return 0
else
return 1
fi
}
${lib.concatStrings executeTest}
${lib.concatStrings executeTest}
'';
};
in
runCommand "run-installation-test" { } ''
${lib.getExe testScript}
echo "success" > $out
''

0 comments on commit 25a0759

Please sign in to comment.