TestBriefFlagIntegration exercises the -b/--brief flag end-to-end.
(t *testing.T)
| 12 | |
| 13 | // TestBriefFlagIntegration exercises the -b/--brief flag end-to-end. |
| 14 | func TestBriefFlagIntegration(t *testing.T) { |
| 15 | if runtime.GOOS == "windows" { |
| 16 | t.Skip("integration test uses Unix-specific env vars") |
| 17 | } |
| 18 | |
| 19 | // Build the cheat binary once for all sub-tests. |
| 20 | binPath := filepath.Join(t.TempDir(), "cheat_test") |
| 21 | build := exec.Command("go", "build", "-o", binPath, "./cmd/cheat") |
| 22 | build.Dir = repoRoot(t) |
| 23 | if output, err := build.CombinedOutput(); err != nil { |
| 24 | t.Fatalf("failed to build cheat: %v\nOutput: %s", err, output) |
| 25 | } |
| 26 | |
| 27 | // Set up a temp environment with some cheatsheets. |
| 28 | root := t.TempDir() |
| 29 | sheetsDir := filepath.Join(root, "sheets") |
| 30 | os.MkdirAll(sheetsDir, 0755) |
| 31 | |
| 32 | os.WriteFile( |
| 33 | filepath.Join(sheetsDir, "tar"), |
| 34 | []byte("---\nsyntax: bash\ntags: [ compression ]\n---\ntar xf archive.tar\n"), |
| 35 | 0644, |
| 36 | ) |
| 37 | os.WriteFile( |
| 38 | filepath.Join(sheetsDir, "curl"), |
| 39 | []byte("---\nsyntax: bash\ntags: [ networking, http ]\n---\ncurl https://example.com\n"), |
| 40 | 0644, |
| 41 | ) |
| 42 | |
| 43 | confPath := filepath.Join(root, "conf.yml") |
| 44 | conf := fmt.Sprintf("---\neditor: vi\ncolorize: false\ncheatpaths:\n - name: test\n path: %s\n readonly: true\n", sheetsDir) |
| 45 | os.WriteFile(confPath, []byte(conf), 0644) |
| 46 | |
| 47 | env := []string{ |
| 48 | "CHEAT_CONFIG_PATH=" + confPath, |
| 49 | "HOME=" + root, |
| 50 | "PATH=" + os.Getenv("PATH"), |
| 51 | "EDITOR=vi", |
| 52 | } |
| 53 | |
| 54 | run := func(t *testing.T, args ...string) string { |
| 55 | t.Helper() |
| 56 | cmd := exec.Command(binPath, args...) |
| 57 | cmd.Dir = root |
| 58 | cmd.Env = env |
| 59 | output, err := cmd.CombinedOutput() |
| 60 | if err != nil { |
| 61 | t.Fatalf("cheat %v failed: %v\nOutput: %s", args, err, output) |
| 62 | } |
| 63 | return string(output) |
| 64 | } |
| 65 | |
| 66 | t.Run("brief output omits file path column", func(t *testing.T) { |
| 67 | output := run(t, "-b") |
| 68 | lines := strings.Split(strings.TrimSpace(output), "\n") |
| 69 | |
| 70 | // Header should have title and tags but not file |
| 71 | if !strings.Contains(lines[0], "title:") { |