|
| 1 | +//go:build !integration |
| 2 | + |
| 3 | +package workflowcontract |
| 4 | + |
| 5 | +import ( |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +func TestCLIConsistencyCheckerHelpCollection(t *testing.T) { |
| 15 | + content, err := os.ReadFile(filepath.Join("..", "..", ".github", "workflows", "cli-consistency-checker.md")) |
| 16 | + if err != nil { |
| 17 | + t.Fatal(err) |
| 18 | + } |
| 19 | + source := string(content) |
| 20 | + start := strings.Index(source, ` for i in "${!command_paths[@]}"; do`) |
| 21 | + if start < 0 { |
| 22 | + t.Fatal("help collection loop not found") |
| 23 | + } |
| 24 | + end := strings.Index(source[start:], "\n done") |
| 25 | + if end < 0 { |
| 26 | + t.Fatal("end of help collection loop not found") |
| 27 | + } |
| 28 | + loop := source[start : start+end+len("\n done")] |
| 29 | + loop = strings.ReplaceAll(loop, "./gh-aw", "fake_gh_aw") |
| 30 | + |
| 31 | + run := func(t *testing.T, paths ...string) (string, error) { |
| 32 | + t.Helper() |
| 33 | + dir := t.TempDir() |
| 34 | + script := `set -euo pipefail |
| 35 | +output_dir="$1" |
| 36 | +shift |
| 37 | +command_paths=("$@") |
| 38 | +fake_gh_aw() { printf '%s\n' "$*"; } |
| 39 | +` + loop |
| 40 | + args := append([]string{"-c", script, "help-collector", dir}, paths...) |
| 41 | + err := exec.Command("bash", args...).Run() |
| 42 | + return dir, err |
| 43 | + } |
| 44 | + |
| 45 | + dir, err := run(t, "foo-bar", "foo bar", "main") |
| 46 | + if err != nil { |
| 47 | + t.Fatal(err) |
| 48 | + } |
| 49 | + for i, want := range []string{"foo-bar --help\n", "foo bar --help\n", "main --help\n"} { |
| 50 | + name := filepath.Join(dir, "command-"+strconv.Itoa(i)+".txt") |
| 51 | + got, err := os.ReadFile(name) |
| 52 | + if err != nil || string(got) != want { |
| 53 | + t.Errorf("%s: got %q, err %v; want %q", name, got, err, want) |
| 54 | + } |
| 55 | + } |
| 56 | + for _, path := range []string{"", "../escape", "-flag", "valid /invalid", "valid invalid"} { |
| 57 | + t.Run(path, func(t *testing.T) { |
| 58 | + dir, err := run(t, path) |
| 59 | + if err == nil { |
| 60 | + t.Fatalf("accepted invalid command path %q", path) |
| 61 | + } |
| 62 | + entries, err := os.ReadDir(dir) |
| 63 | + if err != nil || len(entries) != 0 { |
| 64 | + t.Fatalf("invalid path wrote help files: %v, %v", entries, err) |
| 65 | + } |
| 66 | + }) |
| 67 | + } |
| 68 | +} |
0 commit comments