|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +// TestLogging_ColorFlagRegistered verifies the --color flag is available |
| 14 | +func TestLogging_ColorFlagRegistered(t *testing.T) { |
| 15 | + stdout, _, _ := runCLI(t, []string{"--help"}, nil, 10*time.Second) |
| 16 | + |
| 17 | + assertLogContains(t, stdout, []string{"--color"}) |
| 18 | + |
| 19 | + if !strings.Contains(stdout, "auto-disabled") { |
| 20 | + t.Logf("Flag description might not mention auto-disable, but flag exists") |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +// TestLogging_ConsoleOutputHasColors verifies console output includes ANSI color codes |
| 25 | +func TestLogging_ConsoleOutputHasColors(t *testing.T) { |
| 26 | + stdout, stderr, exitErr := runCLI(t, []string{"gl", "--help"}, nil, 5*time.Second) |
| 27 | + |
| 28 | + output := stdout + stderr |
| 29 | + assert.Nil(t, exitErr, "Command should succeed") |
| 30 | + |
| 31 | + // ANSI color codes typically start with \x1b[ or \033[ |
| 32 | + hasAnsiCodes := strings.Contains(output, "\x1b[") || strings.Contains(output, "\033[") |
| 33 | + |
| 34 | + t.Logf("Console output contains ANSI codes: %v", hasAnsiCodes) |
| 35 | + t.Logf("Output sample (first 200 chars): %s", truncate(output, 200)) |
| 36 | +} |
| 37 | + |
| 38 | +// TestLogging_FileOutputDisablesColorsAutomatically tests that log files don't contain ANSI codes |
| 39 | +func TestLogging_FileOutputDisablesColorsAutomatically(t *testing.T) { |
| 40 | + tmpDir := t.TempDir() |
| 41 | + logFile := filepath.Join(tmpDir, "test.log") |
| 42 | + |
| 43 | + args := []string{"gl", "enum", "--gitlab", "https://invalid.local", "--token", "test", "--logfile", logFile} |
| 44 | + _, _, _ = runCLI(t, args, nil, 5*time.Second) |
| 45 | + |
| 46 | + content, err := os.ReadFile(logFile) |
| 47 | + if err != nil { |
| 48 | + t.Skipf("Log file not created (command may have exited before logging): %v", err) |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + assert.NotEmpty(t, content, "Log file should have content") |
| 53 | + |
| 54 | + logContent := string(content) |
| 55 | + |
| 56 | + hasAnsiCodes := strings.Contains(logContent, "\x1b[") || strings.Contains(logContent, "\033[") |
| 57 | + assert.False(t, hasAnsiCodes, |
| 58 | + "Log file should not contain ANSI color codes when colors are auto-disabled") |
| 59 | + |
| 60 | + t.Logf("Log file content (first 500 chars):\n%s", truncate(logContent, 500)) |
| 61 | +} |
| 62 | + |
| 63 | +// TestLogging_FileOutputWithExplicitColorEnabled tests manual override |
| 64 | +func TestLogging_FileOutputWithExplicitColorEnabled(t *testing.T) { |
| 65 | + tmpDir := t.TempDir() |
| 66 | + logFile := filepath.Join(tmpDir, "test_color.log") |
| 67 | + |
| 68 | + args := []string{"gl", "enum", "--gitlab", "https://invalid.local", "--token", "test", "--logfile", logFile, "--color=true"} |
| 69 | + _, _, _ = runCLI(t, args, nil, 5*time.Second) |
| 70 | + |
| 71 | + content, err := os.ReadFile(logFile) |
| 72 | + if err != nil { |
| 73 | + t.Skipf("Log file not created: %v", err) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + assert.NotEmpty(t, content, "Log file should have content") |
| 78 | + |
| 79 | + logContent := string(content) |
| 80 | + |
| 81 | + hasAnsiCodes := strings.Contains(logContent, "\x1b[") || strings.Contains(logContent, "\033[") |
| 82 | + assert.True(t, hasAnsiCodes, |
| 83 | + "Log file should contain ANSI color codes when --color=true is explicitly set") |
| 84 | + |
| 85 | + t.Logf("Log file with colors (first 500 chars):\n%s", truncate(logContent, 500)) |
| 86 | +} |
| 87 | + |
| 88 | +// TestLogging_FileOutputWithExplicitColorDisabled tests explicit disable |
| 89 | +func TestLogging_FileOutputWithExplicitColorDisabled(t *testing.T) { |
| 90 | + tmpDir := t.TempDir() |
| 91 | + logFile := filepath.Join(tmpDir, "test_nocolor.log") |
| 92 | + |
| 93 | + args := []string{"gl", "enum", "--gitlab", "https://invalid.local", "--token", "test", "--logfile", logFile, "--color=false"} |
| 94 | + _, _, _ = runCLI(t, args, nil, 5*time.Second) |
| 95 | + |
| 96 | + content, err := os.ReadFile(logFile) |
| 97 | + if err != nil { |
| 98 | + t.Skipf("Log file not created: %v", err) |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + assert.NotEmpty(t, content, "Log file should have content") |
| 103 | + |
| 104 | + logContent := string(content) |
| 105 | + |
| 106 | + hasAnsiCodes := strings.Contains(logContent, "\x1b[") || strings.Contains(logContent, "\033[") |
| 107 | + assert.False(t, hasAnsiCodes, |
| 108 | + "Log file should not contain ANSI color codes when --color=false is set") |
| 109 | + |
| 110 | + t.Logf("Log file without colors (first 500 chars):\n%s", truncate(logContent, 500)) |
| 111 | +} |
| 112 | + |
| 113 | +// TestLogging_ConsoleWithExplicitColorDisabled tests disabling colors for console |
| 114 | +func TestLogging_ConsoleWithExplicitColorDisabled(t *testing.T) { |
| 115 | + args := []string{"gl", "--help", "--color=false"} |
| 116 | + stdout, stderr, exitErr := runCLI(t, args, nil, 5*time.Second) |
| 117 | + |
| 118 | + output := stdout + stderr |
| 119 | + assert.Nil(t, exitErr, "Command should succeed") |
| 120 | + |
| 121 | + hasAnsiCodes := strings.Contains(output, "\x1b[") || strings.Contains(output, "\033[") |
| 122 | + assert.False(t, hasAnsiCodes, |
| 123 | + "Console output should not contain ANSI color codes when --color=false is set") |
| 124 | + |
| 125 | + t.Logf("Console output without colors (first 200 chars): %s", truncate(output, 200)) |
| 126 | +} |
| 127 | + |
| 128 | +// TestLogging_LogFileCreatedSuccessfully verifies log file creation |
| 129 | +func TestLogging_LogFileCreatedSuccessfully(t *testing.T) { |
| 130 | + tmpDir := t.TempDir() |
| 131 | + logFile := filepath.Join(tmpDir, "pipeleak.log") |
| 132 | + |
| 133 | + _, err := os.Stat(logFile) |
| 134 | + assert.True(t, os.IsNotExist(err), "Log file should not exist before command") |
| 135 | + |
| 136 | + args := []string{"gl", "enum", "--gitlab", "https://invalid.local", "--token", "test", "--logfile", logFile} |
| 137 | + _, _, _ = runCLI(t, args, nil, 5*time.Second) |
| 138 | + |
| 139 | + stat, err := os.Stat(logFile) |
| 140 | + if err != nil { |
| 141 | + t.Skipf("Log file not created: %v", err) |
| 142 | + return |
| 143 | + } |
| 144 | + |
| 145 | + assert.Greater(t, stat.Size(), int64(0), "Log file should have content") |
| 146 | + |
| 147 | + t.Logf("Log file created: %s (size: %d bytes)", logFile, stat.Size()) |
| 148 | +} |
| 149 | + |
| 150 | +// TestLogging_LogFileAppendMode verifies log file append behavior |
| 151 | +func TestLogging_LogFileAppendMode(t *testing.T) { |
| 152 | + tmpDir := t.TempDir() |
| 153 | + logFile := filepath.Join(tmpDir, "append.log") |
| 154 | + |
| 155 | + args := []string{"gl", "enum", "--gitlab", "https://invalid.local", "--token", "test", "--logfile", logFile} |
| 156 | + |
| 157 | + _, _, _ = runCLI(t, args, nil, 5*time.Second) |
| 158 | + |
| 159 | + stat1, err := os.Stat(logFile) |
| 160 | + if err != nil { |
| 161 | + t.Skipf("Log file not created on first run: %v", err) |
| 162 | + return |
| 163 | + } |
| 164 | + size1 := stat1.Size() |
| 165 | + |
| 166 | + _, _, _ = runCLI(t, args, nil, 5*time.Second) |
| 167 | + |
| 168 | + stat2, err := os.Stat(logFile) |
| 169 | + assert.NoError(t, err, "Log file should exist after second run") |
| 170 | + size2 := stat2.Size() |
| 171 | + |
| 172 | + assert.Greater(t, size2, size1, |
| 173 | + "Log file should grow on second run (append mode)") |
| 174 | + |
| 175 | + t.Logf("Log file sizes - First: %d, Second: %d (delta: %d)", |
| 176 | + size1, size2, size2-size1) |
| 177 | +} |
| 178 | + |
| 179 | +// Helper function to truncate strings for logging |
| 180 | +func truncate(s string, maxLen int) string { |
| 181 | + if len(s) <= maxLen { |
| 182 | + return s |
| 183 | + } |
| 184 | + return s[:maxLen] + "..." |
| 185 | +} |
0 commit comments