|
5 | 5 | "path/filepath" |
6 | 6 | "strings" |
7 | 7 | "testing" |
| 8 | + |
| 9 | + "github.com/posener/complete" |
8 | 10 | ) |
9 | 11 |
|
10 | 12 | func TestDetectTaskFileFormat(t *testing.T) { |
@@ -174,6 +176,87 @@ func TestResolveTaskFileFromArgsUsesExplicitFlag(t *testing.T) { |
174 | 176 | } |
175 | 177 | } |
176 | 178 |
|
| 179 | +// TestTaskFileAutocompleteMatchesRecipeExtensions guards #340: the previous |
| 180 | +// brace glob "*.{yml,yaml,json,json5}" matched no file through filepath.Glob, |
| 181 | +// so completion only ever offered directories. Every recipe extension must now |
| 182 | +// be offered, a non-recipe file must not, and a directory must appear once |
| 183 | +// (the dedupe, since each per-extension sub-predictor lists it). |
| 184 | +func TestTaskFileAutocompleteMatchesRecipeExtensions(t *testing.T) { |
| 185 | + dir := t.TempDir() |
| 186 | + recipes := []string{"tasks.yml", "config.yaml", "data.json", "recipe.json5"} |
| 187 | + for _, name := range recipes { |
| 188 | + if err := os.WriteFile(filepath.Join(dir, name), []byte("---\n"), 0o644); err != nil { |
| 189 | + t.Fatalf("seed %s: %v", name, err) |
| 190 | + } |
| 191 | + } |
| 192 | + if err := os.WriteFile(filepath.Join(dir, "notes.txt"), []byte("x"), 0o644); err != nil { |
| 193 | + t.Fatalf("seed notes.txt: %v", err) |
| 194 | + } |
| 195 | + if err := os.Mkdir(filepath.Join(dir, "sub"), 0o755); err != nil { |
| 196 | + t.Fatalf("mkdir sub: %v", err) |
| 197 | + } |
| 198 | + |
| 199 | + withCwd(t, dir, func() { |
| 200 | + counts := map[string]int{} |
| 201 | + for _, match := range taskFileAutocomplete().Predict(complete.Args{Last: ""}) { |
| 202 | + counts[match]++ |
| 203 | + } |
| 204 | + for _, name := range recipes { |
| 205 | + if counts[name] == 0 { |
| 206 | + t.Errorf("expected %q to be offered, got %v", name, counts) |
| 207 | + } |
| 208 | + } |
| 209 | + if counts["notes.txt"] != 0 { |
| 210 | + t.Errorf("non-recipe notes.txt must not be offered, got %v", counts) |
| 211 | + } |
| 212 | + if counts["sub/"] != 1 { |
| 213 | + t.Errorf("directory sub/ should be offered exactly once, got %d (%v)", counts["sub/"], counts) |
| 214 | + } |
| 215 | + }) |
| 216 | +} |
| 217 | + |
| 218 | +// TestPredictFilesByExtension proves the completion mechanism is generic and |
| 219 | +// not hard-wired to the recipe extensions. |
| 220 | +func TestPredictFilesByExtension(t *testing.T) { |
| 221 | + dir := t.TempDir() |
| 222 | + for _, name := range []string{"readme.md", "todo.txt", "ignore.yml"} { |
| 223 | + if err := os.WriteFile(filepath.Join(dir, name), []byte("x"), 0o644); err != nil { |
| 224 | + t.Fatalf("seed %s: %v", name, err) |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + withCwd(t, dir, func() { |
| 229 | + got := map[string]bool{} |
| 230 | + for _, match := range predictFilesByExtension([]string{"md", "txt"}).Predict(complete.Args{Last: ""}) { |
| 231 | + got[match] = true |
| 232 | + } |
| 233 | + if !got["readme.md"] { |
| 234 | + t.Errorf("expected readme.md to be offered, got %v", got) |
| 235 | + } |
| 236 | + if !got["todo.txt"] { |
| 237 | + t.Errorf("expected todo.txt to be offered, got %v", got) |
| 238 | + } |
| 239 | + if got["ignore.yml"] { |
| 240 | + t.Errorf("ignore.yml must not be offered for extensions {md,txt}, got %v", got) |
| 241 | + } |
| 242 | + }) |
| 243 | +} |
| 244 | + |
| 245 | +func TestHasTaskFileExtension(t *testing.T) { |
| 246 | + yes := []string{"tasks.yml", "tasks.YAML", "path/to/c.json", "x.json5"} |
| 247 | + no := []string{"notes.txt", "tasks", "archive.tar.gz", ""} |
| 248 | + for _, p := range yes { |
| 249 | + if !hasTaskFileExtension(p) { |
| 250 | + t.Errorf("hasTaskFileExtension(%q) = false, want true", p) |
| 251 | + } |
| 252 | + } |
| 253 | + for _, p := range no { |
| 254 | + if hasTaskFileExtension(p) { |
| 255 | + t.Errorf("hasTaskFileExtension(%q) = true, want false", p) |
| 256 | + } |
| 257 | + } |
| 258 | +} |
| 259 | + |
177 | 260 | // withCwd chdirs to dir for the duration of body and restores the |
178 | 261 | // original cwd afterwards. Centralised so the resolveTaskFilePath |
179 | 262 | // tests do not each handle the t.Cleanup dance. |
|
0 commit comments