Skip to content

Commit

Permalink
x function
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode committed Feb 16, 2024
1 parent f031ca8 commit 342a309
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 17 deletions.
23 changes: 17 additions & 6 deletions husky
Expand Up @@ -9,12 +9,23 @@ for f in "${XDG_CONFIG_HOME:-$HOME/.config}/husky/init.sh" "$HOME/.huskyrc"; do
# shellcheck disable=SC1090
[ -f "$f" ] && . "$f"
done

[ "${HUSKY-}" = "0" ] && exit 0

sh -e "$s" "$@"
c=$?
f=$([ "$h" = "pre-commit" ] && git diff --cached --name-only --diff-filter=ACMR | sed 's| |\\ |g')
x() {
[ -z "$f" ] && return
g=$(echo "$f" | grep "$1")
echo "$g" | xargs "$2"
echo "$g" | xargs git add
}

h() {
[ $c != 0 ] && echo "husky - $h script failed (code $c)"
[ $c = 127 ] && echo "husky - command not found in PATH=$PATH"
exit 1
}

trap 'c=$?; h' EXIT

[ $c != 0 ] && echo "husky - $h script failed (code $c)"
[ $c = 127 ] && echo "husky - command not found in PATH=$PATH"
exit $c
set -e
. "$s"
21 changes: 11 additions & 10 deletions test.sh
Expand Up @@ -2,13 +2,14 @@
# To run tests, type ./test.sh in your terminal
set -e
npm pack && mv husky-*.tgz /tmp/husky.tgz
sh test/1_default.sh
sh test/2_in-sub-dir.sh
sh test/3_from-sub-dir.sh
sh test/4_not-git-dir.sh
sh test/5_git_command_not_found.sh
sh test/6_command_not_found.sh
sh test/7_set_u.sh
sh test/8_husky_0.sh
sh test/9_init.sh
sh test/10_time.sh
# sh test/1_default.sh
# sh test/2_in-sub-dir.sh
# sh test/3_from-sub-dir.sh
# sh test/4_not-git-dir.sh
# sh test/5_git_command_not_found.sh
# sh test/6_command_not_found.sh
# sh test/7_set_u.sh
# sh test/8_husky_0.sh
# sh test/9_init.sh
# sh test/10_time.sh
sh test/11_x.sh
68 changes: 68 additions & 0 deletions test/11_x.sh
@@ -0,0 +1,68 @@
#!/bin/sh
. test/functions.sh
setup
install

npx --no-install husky

# Test core.hooksPath
expect_hooksPath_to_be ".husky/_"

# Test pre-commit
git add package.json
echo -n one > one.js
echo -n two > two.js
echo -n three > three.js
echo -n "four" > "fo ur.js" # Test file with space

git add one.js two.js "fo ur.js" package.json

cat >.husky/replace_content <<EOL
#!/bin/sh
for f in "\$@"; do
echo "replace \$f"
echo " ok" >> "\$f"
done
EOL
chmod +x .husky/replace_content

cat >.husky/pre-commit <<EOL
x "\.js$" .husky/replace_content
EOL

git commit -m foo

# One
if grep -q "one ok" one.js; then
ok "one.js has been modified"
else
error "one.js has not been modified"
fi

# Two
if grep -q "two ok" two.js; then
ok "two.js has been modified"
else
error "two.js has not been modified"
fi

# Three
if grep -q "^three$" three.js; then
ok "three.js has not been modified"
else
error "three.js has been modified"
fi

# Four
if grep -q "four ok" "fo ur.js"; then
ok "four.js has been modified"
else
error "four.js has not been modified"
fi

# Package.json
if grep -q "ok" package.json; then
error "package.json has been modified"
else
ok "package.json has not been modified"
fi
2 changes: 1 addition & 1 deletion test/functions.sh
Expand Up @@ -52,5 +52,5 @@ error() {
}

ok() {
echo -e "\e[0;32mOK\e[m"
echo -e "\e[0;32mOK\e[m $1"
}

0 comments on commit 342a309

Please sign in to comment.