(t *testing.T)
| 155 | } |
| 156 | |
| 157 | func TestNoCmdNameCompletionInGo(t *testing.T) { |
| 158 | rootCmd := &Command{ |
| 159 | Use: "root", |
| 160 | Run: emptyRun, |
| 161 | } |
| 162 | rootCmd.Flags().String("localroot", "", "local root flag") |
| 163 | |
| 164 | childCmd1 := &Command{ |
| 165 | Use: "childCmd1", |
| 166 | Short: "First command", |
| 167 | Args: MinimumNArgs(0), |
| 168 | Run: emptyRun, |
| 169 | } |
| 170 | rootCmd.AddCommand(childCmd1) |
| 171 | childCmd1.PersistentFlags().StringP("persistent", "p", "", "persistent flag") |
| 172 | persistentFlag := childCmd1.PersistentFlags().Lookup("persistent") |
| 173 | childCmd1.Flags().StringP("nonPersistent", "n", "", "non-persistent flag") |
| 174 | nonPersistentFlag := childCmd1.Flags().Lookup("nonPersistent") |
| 175 | |
| 176 | childCmd2 := &Command{ |
| 177 | Use: "childCmd2", |
| 178 | Run: emptyRun, |
| 179 | } |
| 180 | childCmd1.AddCommand(childCmd2) |
| 181 | |
| 182 | // Test that sub-command names are not completed if there is an argument already |
| 183 | output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd1", "arg1", "") |
| 184 | if err != nil { |
| 185 | t.Errorf("Unexpected error: %v", err) |
| 186 | } |
| 187 | |
| 188 | expected := strings.Join([]string{ |
| 189 | ":0", |
| 190 | "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n") |
| 191 | |
| 192 | if output != expected { |
| 193 | t.Errorf("expected: %q, got: %q", expected, output) |
| 194 | } |
| 195 | |
| 196 | // Test that sub-command names are not completed if a local non-persistent flag is present |
| 197 | output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd1", "--nonPersistent", "value", "") |
| 198 | if err != nil { |
| 199 | t.Errorf("Unexpected error: %v", err) |
| 200 | } |
| 201 | // Reset the flag for the next command |
| 202 | nonPersistentFlag.Changed = false |
| 203 | |
| 204 | expected = strings.Join([]string{ |
| 205 | ":0", |
| 206 | "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n") |
| 207 | |
| 208 | if output != expected { |
| 209 | t.Errorf("expected: %q, got: %q", expected, output) |
| 210 | } |
| 211 | |
| 212 | // Test that sub-command names are completed if a local non-persistent flag is present and TraverseChildren is set to true |
| 213 | // set TraverseChildren to true on the root cmd |
| 214 | rootCmd.TraverseChildren = true |
nothing calls this directly
no test coverage detected