|
| 1 | +package shellshape |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestEmitPositional(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + name string |
| 11 | + start []string |
| 12 | + tok string |
| 13 | + placeholder string |
| 14 | + want []string |
| 15 | + }{ |
| 16 | + {"plain token", nil, "foo", "<val>", []string{"<val>"}}, |
| 17 | + {"preserves prefix", []string{"pre"}, "foo", "<path>", []string{"pre", "<path>"}}, |
| 18 | + {"subshell preserved", nil, "$(date)", "<val>", []string{"$(date)"}}, |
| 19 | + {"subshell in middle", []string{"pre"}, "$(pwd)", "<path>", []string{"pre", "$(pwd)"}}, |
| 20 | + } |
| 21 | + for _, tt := range tests { |
| 22 | + t.Run(tt.name, func(t *testing.T) { |
| 23 | + got := EmitPositional(tt.start, tt.tok, tt.placeholder) |
| 24 | + if !reflect.DeepEqual(got, tt.want) { |
| 25 | + t.Errorf("EmitPositional(%v, %q, %q) = %v, want %v", tt.start, tt.tok, tt.placeholder, got, tt.want) |
| 26 | + } |
| 27 | + }) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func TestConsumeUntil(t *testing.T) { |
| 32 | + terminators := map[string]bool{";": true, "+": true} |
| 33 | + |
| 34 | + tests := []struct { |
| 35 | + name string |
| 36 | + args []string |
| 37 | + i int |
| 38 | + wantTerminator string |
| 39 | + wantNext int |
| 40 | + }{ |
| 41 | + {"semicolon terminator", []string{"grep", "-l", "foo", "{}", ";"}, 0, ";", 5}, |
| 42 | + {"plus terminator", []string{"rm", "{}", "+"}, 0, "+", 3}, |
| 43 | + {"mid-stream start", []string{"a", "b", "c", ";", "d"}, 2, ";", 4}, |
| 44 | + {"no terminator", []string{"grep", "-l", "foo"}, 0, "", 3}, |
| 45 | + {"empty slice", nil, 0, "", 0}, |
| 46 | + {"start past end", []string{"a"}, 5, "", 1}, |
| 47 | + {"terminator immediately", []string{";", "a"}, 0, ";", 1}, |
| 48 | + } |
| 49 | + for _, tt := range tests { |
| 50 | + t.Run(tt.name, func(t *testing.T) { |
| 51 | + term, next := ConsumeUntil(tt.args, tt.i, terminators) |
| 52 | + if term != tt.wantTerminator || next != tt.wantNext { |
| 53 | + t.Errorf("ConsumeUntil(%v, %d) = (%q, %d), want (%q, %d)", |
| 54 | + tt.args, tt.i, term, next, tt.wantTerminator, tt.wantNext) |
| 55 | + } |
| 56 | + }) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestRepairLeadingFlagSubcommand(t *testing.T) { |
| 61 | + // Typical setup: a handler with --filter (val), -C (path), and some |
| 62 | + // boolean flags. Mimics the pnpm/systemctl family. |
| 63 | + valFlags := map[string]bool{"--filter": true, "-F": true} |
| 64 | + pathFlags := map[string]bool{"-C": true, "--config": true} |
| 65 | + categories := []FlagCategory{ |
| 66 | + {Flags: valFlags, Placeholder: "<val>"}, |
| 67 | + {Flags: pathFlags, Placeholder: "<path>"}, |
| 68 | + } |
| 69 | + verbatimFlags := map[string]bool{"-t": true, "--type": true} |
| 70 | + |
| 71 | + tests := []struct { |
| 72 | + name string |
| 73 | + subcommand string |
| 74 | + args []string |
| 75 | + startResult []string |
| 76 | + categories []FlagCategory |
| 77 | + verbatim map[string]bool |
| 78 | + wantResult []string |
| 79 | + wantRealSubcommand string |
| 80 | + wantNextI int |
| 81 | + }{ |
| 82 | + { |
| 83 | + name: "not a flag: no-op", |
| 84 | + subcommand: "install", |
| 85 | + args: []string{"react"}, |
| 86 | + startResult: []string{"exe", "install"}, |
| 87 | + categories: categories, |
| 88 | + wantResult: []string{"exe", "install"}, |
| 89 | + wantRealSubcommand: "install", |
| 90 | + wantNextI: 0, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "leading val flag then real subcommand", |
| 94 | + subcommand: "--filter", |
| 95 | + args: []string{"infra", "exec", "tsgo", "--noEmit"}, |
| 96 | + startResult: []string{"exe", "--filter"}, |
| 97 | + categories: categories, |
| 98 | + wantResult: []string{"exe", "--filter", "<val>", "exec"}, |
| 99 | + wantRealSubcommand: "exec", |
| 100 | + wantNextI: 2, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "leading path flag then real subcommand", |
| 104 | + subcommand: "-C", |
| 105 | + args: []string{"/app", "add", "react"}, |
| 106 | + startResult: []string{"exe", "-C"}, |
| 107 | + categories: categories, |
| 108 | + wantResult: []string{"exe", "-C", "<path>", "add"}, |
| 109 | + wantRealSubcommand: "add", |
| 110 | + wantNextI: 2, |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "two leading flags before subcommand", |
| 114 | + subcommand: "--filter", |
| 115 | + args: []string{"infra", "-C", "/app", "add"}, |
| 116 | + startResult: []string{"exe", "--filter"}, |
| 117 | + categories: categories, |
| 118 | + wantResult: []string{"exe", "--filter", "<val>", "-C", "<path>", "add"}, |
| 119 | + wantRealSubcommand: "add", |
| 120 | + wantNextI: 4, |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "boolean flag leaked (no value)", |
| 124 | + subcommand: "--dry-run", |
| 125 | + args: []string{"enable"}, |
| 126 | + startResult: []string{"exe", "--dry-run"}, |
| 127 | + categories: nil, |
| 128 | + wantResult: []string{"exe", "--dry-run", "enable"}, |
| 129 | + wantRealSubcommand: "enable", |
| 130 | + wantNextI: 1, |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "subshell as leading flag value preserved", |
| 134 | + subcommand: "--filter", |
| 135 | + args: []string{"$(pick-workspace)", "exec", "tsgo"}, |
| 136 | + startResult: []string{"exe", "--filter"}, |
| 137 | + categories: categories, |
| 138 | + wantResult: []string{"exe", "--filter", "$(pick-workspace)", "exec"}, |
| 139 | + wantRealSubcommand: "exec", |
| 140 | + wantNextI: 2, |
| 141 | + }, |
| 142 | + { |
| 143 | + name: "verbatim-value flag leaked", |
| 144 | + subcommand: "-t", |
| 145 | + args: []string{"service", "list-units"}, |
| 146 | + startResult: []string{"exe", "-t"}, |
| 147 | + categories: nil, |
| 148 | + verbatim: verbatimFlags, |
| 149 | + wantResult: []string{"exe", "-t", "service", "list-units"}, |
| 150 | + wantRealSubcommand: "list-units", |
| 151 | + wantNextI: 2, |
| 152 | + }, |
| 153 | + { |
| 154 | + name: "fused flag leaked", |
| 155 | + subcommand: "--filter=infra", |
| 156 | + args: []string{"exec", "tsgo"}, |
| 157 | + startResult: []string{"exe", "--filter=infra"}, |
| 158 | + categories: categories, |
| 159 | + wantResult: []string{"exe", "--filter=infra", "exec"}, |
| 160 | + wantRealSubcommand: "exec", |
| 161 | + wantNextI: 1, |
| 162 | + }, |
| 163 | + { |
| 164 | + name: "exhausted without finding subcommand", |
| 165 | + subcommand: "--filter", |
| 166 | + args: []string{"infra"}, |
| 167 | + startResult: []string{"exe", "--filter"}, |
| 168 | + categories: categories, |
| 169 | + wantResult: []string{"exe", "--filter", "<val>"}, |
| 170 | + wantRealSubcommand: "", |
| 171 | + wantNextI: 1, |
| 172 | + }, |
| 173 | + { |
| 174 | + name: "intermediate subshell before subcommand", |
| 175 | + subcommand: "--filter", |
| 176 | + args: []string{"infra", "$(date)", "exec"}, |
| 177 | + startResult: []string{"exe", "--filter"}, |
| 178 | + categories: categories, |
| 179 | + wantResult: []string{"exe", "--filter", "<val>", "$(date)", "exec"}, |
| 180 | + wantRealSubcommand: "exec", |
| 181 | + wantNextI: 3, |
| 182 | + }, |
| 183 | + } |
| 184 | + |
| 185 | + for _, tt := range tests { |
| 186 | + t.Run(tt.name, func(t *testing.T) { |
| 187 | + got, realSub, nextI := RepairLeadingFlagSubcommand( |
| 188 | + tt.subcommand, tt.args, 0, tt.startResult, tt.categories, tt.verbatim, |
| 189 | + ) |
| 190 | + if !reflect.DeepEqual(got, tt.wantResult) { |
| 191 | + t.Errorf("result = %v, want %v", got, tt.wantResult) |
| 192 | + } |
| 193 | + if realSub != tt.wantRealSubcommand { |
| 194 | + t.Errorf("realSubcommand = %q, want %q", realSub, tt.wantRealSubcommand) |
| 195 | + } |
| 196 | + if nextI != tt.wantNextI { |
| 197 | + t.Errorf("nextI = %d, want %d", nextI, tt.wantNextI) |
| 198 | + } |
| 199 | + }) |
| 200 | + } |
| 201 | +} |
0 commit comments