|
8 | 8 | "testing" |
9 | 9 |
|
10 | 10 | "github.com/semaphoreci/toolbox/test-results/pkg/parser" |
| 11 | + "github.com/spf13/cobra" |
11 | 12 |
|
12 | 13 | "github.com/semaphoreci/toolbox/test-results/pkg/cli" |
13 | 14 | "github.com/stretchr/testify/assert" |
@@ -151,6 +152,168 @@ func TestWriteToTmpFile(t *testing.T) { |
151 | 152 | }) |
152 | 153 | } |
153 | 154 |
|
| 155 | +func TestApplyOutputTrimming(t *testing.T) { |
| 156 | + longText := func(n int) string { |
| 157 | + s := "" |
| 158 | + for i := 0; i < n; i++ { |
| 159 | + s += "x" |
| 160 | + } |
| 161 | + return s |
| 162 | + } |
| 163 | + |
| 164 | + createTestResult := func(text string) *parser.Result { |
| 165 | + return &parser.Result{ |
| 166 | + TestResults: []parser.TestResults{ |
| 167 | + { |
| 168 | + ID: "test-1", |
| 169 | + Name: "Test", |
| 170 | + Suites: []parser.Suite{ |
| 171 | + { |
| 172 | + Name: "Suite1", |
| 173 | + SystemOut: text, |
| 174 | + SystemErr: text, |
| 175 | + Tests: []parser.Test{ |
| 176 | + { |
| 177 | + Name: "Test1", |
| 178 | + SystemOut: text, |
| 179 | + SystemErr: text, |
| 180 | + Failure: &parser.Failure{ |
| 181 | + Message: text, |
| 182 | + Body: text, |
| 183 | + }, |
| 184 | + }, |
| 185 | + }, |
| 186 | + }, |
| 187 | + }, |
| 188 | + }, |
| 189 | + }, |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + createCmd := func(trimTo int, noTrim bool) *cobra.Command { |
| 194 | + cmd := &cobra.Command{} |
| 195 | + cmd.Flags().Int("trim-output-to", trimTo, "") |
| 196 | + cmd.Flags().Bool("no-trim-output", noTrim, "") |
| 197 | + return cmd |
| 198 | + } |
| 199 | + |
| 200 | + t.Run("default trimming to 1000 characters", func(t *testing.T) { |
| 201 | + result := createTestResult(longText(2000)) |
| 202 | + cmd := createCmd(1000, false) |
| 203 | + |
| 204 | + cli.ApplyOutputTrimming(result, cmd) |
| 205 | + |
| 206 | + suite := result.TestResults[0].Suites[0] |
| 207 | + assert.True(t, len(suite.SystemOut) <= 1000+len("...[truncated]...\n")) |
| 208 | + assert.True(t, len(suite.SystemErr) <= 1000+len("...[truncated]...\n")) |
| 209 | + assert.Contains(t, suite.SystemOut, "...[truncated]...") |
| 210 | + }) |
| 211 | + |
| 212 | + t.Run("custom trim length", func(t *testing.T) { |
| 213 | + result := createTestResult(longText(5000)) |
| 214 | + cmd := createCmd(3000, false) |
| 215 | + |
| 216 | + cli.ApplyOutputTrimming(result, cmd) |
| 217 | + |
| 218 | + suite := result.TestResults[0].Suites[0] |
| 219 | + assert.True(t, len(suite.SystemOut) <= 3000+len("...[truncated]...\n")) |
| 220 | + assert.Contains(t, suite.SystemOut, "...[truncated]...") |
| 221 | + }) |
| 222 | + |
| 223 | + t.Run("no trimming when --no-trim-output is set", func(t *testing.T) { |
| 224 | + originalText := longText(5000) |
| 225 | + result := createTestResult(originalText) |
| 226 | + cmd := createCmd(1000, true) |
| 227 | + |
| 228 | + cli.ApplyOutputTrimming(result, cmd) |
| 229 | + |
| 230 | + suite := result.TestResults[0].Suites[0] |
| 231 | + assert.Equal(t, originalText, suite.SystemOut) |
| 232 | + assert.Equal(t, originalText, suite.SystemErr) |
| 233 | + assert.Equal(t, originalText, suite.Tests[0].SystemOut) |
| 234 | + assert.Equal(t, originalText, suite.Tests[0].Failure.Message) |
| 235 | + }) |
| 236 | + |
| 237 | + t.Run("no trimming when --trim-output-to is 0", func(t *testing.T) { |
| 238 | + originalText := longText(5000) |
| 239 | + result := createTestResult(originalText) |
| 240 | + cmd := createCmd(0, false) |
| 241 | + |
| 242 | + cli.ApplyOutputTrimming(result, cmd) |
| 243 | + |
| 244 | + suite := result.TestResults[0].Suites[0] |
| 245 | + assert.Equal(t, originalText, suite.SystemOut) |
| 246 | + assert.Equal(t, originalText, suite.SystemErr) |
| 247 | + }) |
| 248 | + |
| 249 | + t.Run("no trimming when --trim-output-to is negative", func(t *testing.T) { |
| 250 | + originalText := longText(5000) |
| 251 | + result := createTestResult(originalText) |
| 252 | + cmd := createCmd(-1, false) |
| 253 | + |
| 254 | + cli.ApplyOutputTrimming(result, cmd) |
| 255 | + |
| 256 | + suite := result.TestResults[0].Suites[0] |
| 257 | + assert.Equal(t, originalText, suite.SystemOut) |
| 258 | + }) |
| 259 | + |
| 260 | + t.Run("text shorter than trim limit is not modified", func(t *testing.T) { |
| 261 | + originalText := longText(500) |
| 262 | + result := createTestResult(originalText) |
| 263 | + cmd := createCmd(1000, false) |
| 264 | + |
| 265 | + cli.ApplyOutputTrimming(result, cmd) |
| 266 | + |
| 267 | + suite := result.TestResults[0].Suites[0] |
| 268 | + assert.Equal(t, originalText, suite.SystemOut) |
| 269 | + assert.NotContains(t, suite.SystemOut, "...[truncated]...") |
| 270 | + }) |
| 271 | + |
| 272 | + t.Run("nil result does not panic", func(t *testing.T) { |
| 273 | + cmd := createCmd(1000, false) |
| 274 | + assert.NotPanics(t, func() { |
| 275 | + cli.ApplyOutputTrimming(nil, cmd) |
| 276 | + }) |
| 277 | + }) |
| 278 | + |
| 279 | + t.Run("trims failure and error fields", func(t *testing.T) { |
| 280 | + result := &parser.Result{ |
| 281 | + TestResults: []parser.TestResults{ |
| 282 | + { |
| 283 | + ID: "test-1", |
| 284 | + Suites: []parser.Suite{ |
| 285 | + { |
| 286 | + Tests: []parser.Test{ |
| 287 | + { |
| 288 | + Failure: &parser.Failure{ |
| 289 | + Message: longText(2000), |
| 290 | + Type: longText(2000), |
| 291 | + Body: longText(2000), |
| 292 | + }, |
| 293 | + Error: &parser.Error{ |
| 294 | + Message: longText(2000), |
| 295 | + Type: longText(2000), |
| 296 | + Body: longText(2000), |
| 297 | + }, |
| 298 | + }, |
| 299 | + }, |
| 300 | + }, |
| 301 | + }, |
| 302 | + }, |
| 303 | + }, |
| 304 | + } |
| 305 | + cmd := createCmd(1000, false) |
| 306 | + |
| 307 | + cli.ApplyOutputTrimming(result, cmd) |
| 308 | + |
| 309 | + test := result.TestResults[0].Suites[0].Tests[0] |
| 310 | + assert.Contains(t, test.Failure.Message, "...[truncated]...") |
| 311 | + assert.Contains(t, test.Failure.Body, "...[truncated]...") |
| 312 | + assert.Contains(t, test.Error.Message, "...[truncated]...") |
| 313 | + assert.Contains(t, test.Error.Body, "...[truncated]...") |
| 314 | + }) |
| 315 | +} |
| 316 | + |
154 | 317 | func TestWriteToFilePath(t *testing.T) { |
155 | 318 | tr := parser.TestResults{ |
156 | 319 | ID: "1234", |
|
0 commit comments