| 1 | package command |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "runtime" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | // TestLoadFollowsSymlinks verifies command discovery follows symlinked |
| 11 | // directories and symlinked .md files (filepath.WalkDir would skip both). |
| 12 | func TestLoadFollowsSymlinks(t *testing.T) { |
| 13 | if runtime.GOOS == "windows" { |
| 14 | t.Skip("symlink creation needs privilege on Windows") |
| 15 | } |
| 16 | cmdDir := t.TempDir() |
| 17 | target := t.TempDir() |
| 18 | |
| 19 | // Real command files living outside the commands dir. |
| 20 | mustWrite(t, filepath.Join(target, "pkg", "deploy.md"), "---\ndescription: d\n---\nrun deploy") |
| 21 | mustWrite(t, filepath.Join(target, "flat.md"), "---\ndescription: f\n---\nflat body") |
| 22 | |
| 23 | // Symlink a directory and a flat file into the commands dir. |
| 24 | if err := os.Symlink(filepath.Join(target, "pkg"), filepath.Join(cmdDir, "pkg")); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | if err := os.Symlink(filepath.Join(target, "flat.md"), filepath.Join(cmdDir, "linked.md")); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | |
| 31 | cmds, err := Load(cmdDir) |
| 32 | if err != nil { |
| 33 | t.Fatalf("load: %v", err) |
| 34 | } |
| 35 | names := map[string]bool{} |
| 36 | for _, c := range cmds { |
| 37 | names[c.Name] = true |
| 38 | } |
| 39 | if !names["pkg:deploy"] { |
| 40 | t.Errorf("command under symlinked directory not discovered; got %v", names) |
| 41 | } |
| 42 | if !names["linked"] { |
| 43 | t.Errorf("symlinked flat command file not discovered; got %v", names) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func mustWrite(t *testing.T, path, body string) { |
| 48 | t.Helper() |
| 49 | if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | if err := os.WriteFile(path, []byte(body), 0o644); err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | } |
| 56 |