summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/windows/polyglot-hooks.md14
-rwxr-xr-xhooks/session-start6
-rwxr-xr-xtests/hooks/test-session-start.sh31
3 files changed, 34 insertions, 17 deletions
diff --git a/docs/windows/polyglot-hooks.md b/docs/windows/polyglot-hooks.md
index 0379037..d6970e1 100644
--- a/docs/windows/polyglot-hooks.md
+++ b/docs/windows/polyglot-hooks.md
@@ -176,7 +176,12 @@ Your hook logic goes in the extensionless script file. A few portable patterns:
nl='
'
tab=' '
-cr=$(printf '\r')
+if [ -n "${BASH_VERSION:-}" ]; then
+ # shellcheck disable=SC3045 # bash builtin avoids a subshell fork (#2081)
+ printf -v cr '\r'
+else
+ cr=$(printf '\r' 2>/dev/null || true)
+fi
replace_all() {
rest=$1
@@ -202,8 +207,13 @@ escape_for_json() {
[ -z "$cr" ] || replace_all "$escaped" "$cr" '\r'
replace_all "$escaped" "$nl" '\n'
replace_all "$escaped" "$tab" '\t'
- printf '%s' "$escaped"
+ using_superpowers_escaped=$escaped
}
+
+# The result is left in a variable instead of printed, because capturing
+# output with $(...) forks. Call the function, then read the variable:
+# escape_for_json "$content"
+# json=$using_superpowers_escaped
```
## Troubleshooting
diff --git a/hooks/session-start b/hooks/session-start
index 2ad81f0..fc71eb8 100755
--- a/hooks/session-start
+++ b/hooks/session-start
@@ -29,8 +29,10 @@ else
cr=$(printf '\r' 2>/dev/null || true)
fi
-# Read using-superpowers content via POSIX read (no cat, no $(< file) subshell),
-# dropping trailing blank lines as $(< file) did.
+# Read using-superpowers content with the read builtin (no cat, for the same
+# PATH-independence reason, and no $(< file) subshell). read returns 1 at end
+# of file, hence || [ -n "$line" ]; the loop drops trailing newlines as
+# $(< file) did.
skill_md="${PLUGIN_ROOT}/skills/using-superpowers/SKILL.md"
using_superpowers_content="Error reading using-superpowers skill"
if [ -r "$skill_md" ]; then
diff --git a/tests/hooks/test-session-start.sh b/tests/hooks/test-session-start.sh
index 9332f42..79aefb7 100755
--- a/tests/hooks/test-session-start.sh
+++ b/tests/hooks/test-session-start.sh
@@ -352,15 +352,22 @@ if [[ -n "$posix_sh" ]]; then
CLAUDE_PLUGIN_ROOT="$REPO_ROOT" \
"$posix_sh" "$WRAPPER_UNDER_TEST" session-start
fi
+else
+ echo " [SKIP] run-hook.cmd/session-start sh-only tests: no non-bash/non-zsh POSIX sh found"
+fi
- # Adversarial escaping test under POSIX sh (backslashes, quotes, tabs, globs, CRLF)
- tricky_root="$TEST_ROOT/tricky-plugin"
- mkdir -p "$tricky_root/hooks" "$tricky_root/skills/using-superpowers"
- cp "$HOOK_UNDER_TEST" "$tricky_root/hooks/session-start"
- printf 'Line1: C:\\Users\\new\\tab"quote\\*?[foo]\\bar\r\nLine2:\twith\ttabs and ""quotes""\n\n' > "$tricky_root/skills/using-superpowers/SKILL.md"
- tricky_json="$(env -i PATH="" HOME="$(make_home sh-tricky)" CLAUDE_PLUGIN_ROOT="$tricky_root" \
- "$posix_sh" "$tricky_root/hooks/session-start")"
- if node -e '
+# Adversarial escaping test (backslashes, quotes, tabs, globs, CRLF) under
+# every shell the wrapper can pick. bash gets CR through printf -v, the
+# others through a command substitution.
+tricky_root="$TEST_ROOT/tricky-plugin"
+mkdir -p "$tricky_root/hooks" "$tricky_root/skills/using-superpowers"
+cp "$HOOK_UNDER_TEST" "$tricky_root/hooks/session-start"
+printf 'Line1: C:\\Users\\new\\tab"quote\\*?[foo]\\bar\r\nLine2:\twith\ttabs and ""quotes""\n\n' > "$tricky_root/skills/using-superpowers/SKILL.md"
+for tricky_sh in ${posix_sh:+"$posix_sh"} "$BASH" ${zsh_bin:+"$zsh_bin"}; do
+ tricky_rc=0
+ tricky_json="$(env -i PATH="" HOME="$(make_home "tricky-${tricky_sh##*/}")" CLAUDE_PLUGIN_ROOT="$tricky_root" \
+ "$tricky_sh" "$tricky_root/hooks/session-start")" || tricky_rc=$?
+ if [[ "$tricky_rc" -eq 0 ]] && node -e '
const fs = require("fs");
const raw = fs.readFileSync(process.argv[1], "utf8").replace(/\n+$/, "");
const parsed = JSON.parse(process.argv[2]);
@@ -370,13 +377,11 @@ if [[ -n "$posix_sh" ]]; then
process.exit(1);
}
' "$tricky_root/skills/using-superpowers/SKILL.md" "$tricky_json"; then
- pass "POSIX sh (${posix_sh##*/}) round-trips backslashes, quotes, tabs, globs, and CRLF in JSON"
+ pass "${tricky_sh##*/} round-trips backslashes, quotes, tabs, globs, and CRLF in JSON"
else
- fail "POSIX sh (${posix_sh##*/}) round-trips backslashes, quotes, tabs, globs, and CRLF in JSON"
+ fail "${tricky_sh##*/} round-trips backslashes, quotes, tabs, globs, and CRLF in JSON (rc=$tricky_rc)"
fi
-else
- echo " [SKIP] run-hook.cmd/session-start sh-only tests: no non-bash/non-zsh POSIX sh found"
-fi
+done
# Run by bare filename from inside hooks/ ($0 has no slash), the plugin root
# must still resolve.