Skip to content

Commit e9095d3

Browse files
authored
Merge pull request #65 from yoshiori/fix_fish_shell_setting
fix(hook): declare fish target_dir variable outside if/else block to fix scoping
2 parents 3afdc10 + e856f76 commit e9095d3

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

cmd/wtp/hook.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,11 @@ function wtp
143143
end
144144
end
145145
if test "$argv[1]" = "cd"
146+
set -l target_dir
146147
if test -z "$argv[2]"
147-
set -l target_dir (command wtp cd 2>/dev/null)
148+
set target_dir (command wtp cd 2>/dev/null)
148149
else
149-
set -l target_dir (command wtp cd $argv[2] 2>/dev/null)
150+
set target_dir (command wtp cd $argv[2] 2>/dev/null)
150151
end
151152
if test $status -eq 0 -a -n "$target_dir"
152153
cd "$target_dir"

cmd/wtp/hook_test.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,24 @@ func TestHookCommand_GeneratesValidShellScripts(t *testing.T) {
101101
}
102102

103103
// Test the core business logic that matters most
104+
// Test fish variable scoping: target_dir must be declared outside if/else block
105+
func TestFishHook_VariableScoping(t *testing.T) {
106+
var buf bytes.Buffer
107+
require.NoError(t, printFishHook(&buf))
108+
output := buf.String()
109+
110+
// target_dir must be declared BEFORE the if/else block (outside block scope)
111+
// Correct: "set -l target_dir" on its own line, then "set target_dir ..." inside blocks
112+
assert.Contains(t, output, "set -l target_dir\n",
113+
"target_dir must be declared outside if/else block for proper fish scoping")
114+
115+
// Inside if/else blocks, assignment should NOT use -l flag
116+
assert.Contains(t, output, "set target_dir (command wtp cd 2>/dev/null)",
117+
"target_dir assignment in if block should not use -l flag")
118+
assert.Contains(t, output, "set target_dir (command wtp cd $argv[2] 2>/dev/null)",
119+
"target_dir assignment in else block should not use -l flag")
120+
}
121+
104122
func TestHookScripts_HandleEdgeCases(t *testing.T) {
105123
tests := []struct {
106124
name string
@@ -138,10 +156,10 @@ func TestHookScripts_HandleEdgeCases(t *testing.T) {
138156
name: "fish hook supports no-arg cd",
139157
shell: "fish",
140158
requiredLogic: []string{
141-
"if test -z \"$argv[2]\"", // No-arg branch
142-
"set -l target_dir (command wtp cd", // Uses `wtp cd` default behavior
143-
"command wtp cd $argv[2]", // Uses explicit worktree name when present
144-
"cd \"$target_dir\"", // Handles spaces safely
159+
"if test -z \"$argv[2]\"", // No-arg branch
160+
"set target_dir (command wtp", // Uses `wtp cd` (no -l inside block)
161+
"command wtp cd $argv[2]", // Uses explicit worktree name when present
162+
"cd \"$target_dir\"", // Handles spaces safely
145163
},
146164
notContains: []string{
147165
"Usage: wtp cd <worktree>",

0 commit comments

Comments
 (0)