TestWriteToPager tests the writeToPager function
(t *testing.T)
| 13 | |
| 14 | // TestWriteToPager tests the writeToPager function |
| 15 | func TestWriteToPager(t *testing.T) { |
| 16 | // Skip these tests in CI/CD environments where interactive commands might not work |
| 17 | if os.Getenv("CI") != "" { |
| 18 | t.Skip("Skipping pager tests in CI environment") |
| 19 | } |
| 20 | |
| 21 | // Note: We can't easily test os.Exit calls, so we focus on testing writeToPager |
| 22 | // which contains the core logic |
| 23 | |
| 24 | t.Run("successful pager execution", func(t *testing.T) { |
| 25 | // Save original stdout |
| 26 | oldStdout := os.Stdout |
| 27 | defer func() { |
| 28 | os.Stdout = oldStdout |
| 29 | }() |
| 30 | |
| 31 | // Create pipe for capturing output |
| 32 | r, w, _ := os.Pipe() |
| 33 | os.Stdout = w |
| 34 | |
| 35 | // Use 'cat' as a simple pager that just outputs input |
| 36 | conf := config.Config{ |
| 37 | Pager: "cat", |
| 38 | } |
| 39 | |
| 40 | // This will call os.Exit on error, so we need to be careful |
| 41 | // We're using 'cat' which should always succeed |
| 42 | input := "Test output\n" |
| 43 | |
| 44 | // Run in a goroutine to avoid blocking |
| 45 | done := make(chan bool) |
| 46 | go func() { |
| 47 | writeToPager(input, conf) |
| 48 | done <- true |
| 49 | }() |
| 50 | |
| 51 | // Wait for completion or timeout |
| 52 | select { |
| 53 | case <-done: |
| 54 | // Success |
| 55 | } |
| 56 | |
| 57 | // Close write end and read output |
| 58 | w.Close() |
| 59 | var buf bytes.Buffer |
| 60 | io.Copy(&buf, r) |
| 61 | |
| 62 | // Verify output |
| 63 | if buf.String() != input { |
| 64 | t.Errorf("expected output %q, got %q", input, buf.String()) |
| 65 | } |
| 66 | }) |
| 67 | |
| 68 | t.Run("pager with arguments", func(t *testing.T) { |
| 69 | // Save original stdout |
| 70 | oldStdout := os.Stdout |
| 71 | defer func() { |
| 72 | os.Stdout = oldStdout |
nothing calls this directly
no test coverage detected