Skip to content

Commit 2a9c8db

Browse files
committed
fix(shell): harden add hook status handling in zsh/fish
1 parent bec8896 commit 2a9c8db

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

cmd/wtp/hook.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ wtp() {
107107
108108
local target_dir
109109
target_dir=$(command wtp "$@" --quiet)
110-
local status=$?
111-
if [[ $status -eq 0 && -n "$target_dir" ]]; then
110+
local wtp_status=$?
111+
if [[ $wtp_status -eq 0 && -n "$target_dir" ]]; then
112112
cd "$target_dir" || return $?
113113
fi
114-
return $status
114+
return $wtp_status
115115
else
116116
command wtp "$@"
117117
fi
@@ -160,11 +160,11 @@ wtp() {
160160
161161
local target_dir
162162
target_dir=$(command wtp "$@" --quiet)
163-
local status=$?
164-
if [[ $status -eq 0 && -n "$target_dir" ]]; then
163+
local wtp_status=$?
164+
if [[ $wtp_status -eq 0 && -n "$target_dir" ]]; then
165165
cd "$target_dir" || return $?
166166
fi
167-
return $status
167+
return $wtp_status
168168
else
169169
command wtp "$@"
170170
fi
@@ -200,7 +200,7 @@ function wtp
200200
end
201201
else if test "$argv[1]" = "add"
202202
for arg in $argv
203-
if test "$arg" = "--help" -o "$arg" = "-h"
203+
if test "$arg" = "--help"; or test "$arg" = "-h"
204204
command wtp $argv
205205
return $status
206206
end
@@ -212,12 +212,12 @@ function wtp
212212
end
213213
214214
set -l target_dir (command wtp $argv --quiet)
215-
set -l status $status
216-
if test $status -eq 0 -a -n "$target_dir"
215+
set -l wtp_status $status
216+
if test $wtp_status -eq 0 -a -n "$target_dir"
217217
cd "$target_dir"
218218
or return $status
219219
end
220-
return $status
220+
return $wtp_status
221221
else
222222
command wtp $argv
223223
end

cmd/wtp/hook_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,9 @@ func TestHookScripts_AutoCdAfterAdd(t *testing.T) {
206206
"if [[ \"$arg\" == \"--help\" || \"$arg\" == \"-h\" ]]; then",
207207
"if [[ ! -t 1 ]]; then",
208208
"target_dir=$(command wtp \"$@\" --quiet)",
209+
"local wtp_status=$?",
209210
"cd \"$target_dir\" || return $?",
210-
"return $status",
211+
"return $wtp_status",
211212
},
212213
},
213214
{
@@ -218,21 +219,23 @@ func TestHookScripts_AutoCdAfterAdd(t *testing.T) {
218219
"if [[ \"$arg\" == \"--help\" || \"$arg\" == \"-h\" ]]; then",
219220
"if [[ ! -t 1 ]]; then",
220221
"target_dir=$(command wtp \"$@\" --quiet)",
222+
"local wtp_status=$?",
221223
"cd \"$target_dir\" || return $?",
222-
"return $status",
224+
"return $wtp_status",
223225
},
224226
},
225227
{
226228
name: "fish auto cd after add uses quiet and tty guard",
227229
shell: "fish",
228230
contains: []string{
229231
"else if test \"$argv[1]\" = \"add\"",
230-
"if test \"$arg\" = \"--help\" -o \"$arg\" = \"-h\"",
232+
"if test \"$arg\" = \"--help\"; or test \"$arg\" = \"-h\"",
231233
"if not isatty stdout",
232234
"set -l target_dir (command wtp $argv --quiet)",
235+
"set -l wtp_status $status",
233236
"cd \"$target_dir\"",
234237
"or return $status",
235-
"return $status",
238+
"return $wtp_status",
236239
},
237240
},
238241
}

0 commit comments

Comments
 (0)