@@ -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+
104122func 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