Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion test-results/pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,12 +466,23 @@ func ApplyOutputTrimming(result *parser.Result, cmd *cobra.Command) {
return
}

// Check if trimming is disabled via --no-trim-output flag
// Check if trimming is disabled via --no-trim-output flag (highest priority)
noTrim, err := cmd.Flags().GetBool("no-trim-output")
if err == nil && noTrim {
return
}

// Check if trimming is disabled via SEMAPHORE_TEST_RESULTS_NO_TRIM env var
// This is set by the backend when the organization has the feature enabled.
// CLI flags take priority over env var.
if noTrimEnv := os.Getenv("SEMAPHORE_TEST_RESULTS_NO_TRIM"); noTrimEnv == "true" {
// Only apply env var if --trim-output-to was not explicitly set
trimToFlag, _ := cmd.Flags().GetInt("trim-output-to")
if trimToFlag == 1000 { // default value, meaning not explicitly set
return
}
}

trimTo := 1000

trimToFlag, err := cmd.Flags().GetInt("trim-output-to")
Expand Down
55 changes: 55 additions & 0 deletions test-results/pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,61 @@ func TestApplyOutputTrimming(t *testing.T) {
assert.Contains(t, test.Error.Message, "...[truncated]...")
assert.Contains(t, test.Error.Body, "...[truncated]...")
})

t.Run("no trimming when SEMAPHORE_TEST_RESULTS_NO_TRIM env var is true", func(t *testing.T) {
originalText := longText(5000)
result := createTestResult(originalText)
cmd := createCmd(1000, false) // default trim value

os.Setenv("SEMAPHORE_TEST_RESULTS_NO_TRIM", "true")
defer os.Unsetenv("SEMAPHORE_TEST_RESULTS_NO_TRIM")

cli.ApplyOutputTrimming(result, cmd)

suite := result.TestResults[0].Suites[0]
assert.Equal(t, originalText, suite.SystemOut)
assert.Equal(t, originalText, suite.SystemErr)
assert.Equal(t, originalText, suite.Tests[0].SystemOut)
})

t.Run("env var ignored when SEMAPHORE_TEST_RESULTS_NO_TRIM is not 'true'", func(t *testing.T) {
result := createTestResult(longText(2000))
cmd := createCmd(1000, false)

os.Setenv("SEMAPHORE_TEST_RESULTS_NO_TRIM", "false")
defer os.Unsetenv("SEMAPHORE_TEST_RESULTS_NO_TRIM")

cli.ApplyOutputTrimming(result, cmd)

suite := result.TestResults[0].Suites[0]
assert.Contains(t, suite.SystemOut, "...[truncated]...")
})

t.Run("explicit --trim-output-to flag takes priority over env var", func(t *testing.T) {
result := createTestResult(longText(5000))
cmd := createCmd(500, false) // explicit non-default value

os.Setenv("SEMAPHORE_TEST_RESULTS_NO_TRIM", "true")
defer os.Unsetenv("SEMAPHORE_TEST_RESULTS_NO_TRIM")

cli.ApplyOutputTrimming(result, cmd)

suite := result.TestResults[0].Suites[0]
// Should still trim because explicit flag takes priority
assert.True(t, len(suite.SystemOut) <= 500+len("...[truncated]...\n"))
assert.Contains(t, suite.SystemOut, "...[truncated]...")
})

t.Run("--no-trim-output flag takes priority over everything", func(t *testing.T) {
originalText := longText(5000)
result := createTestResult(originalText)
cmd := createCmd(500, true) // noTrim=true

cli.ApplyOutputTrimming(result, cmd)

suite := result.TestResults[0].Suites[0]
assert.Equal(t, originalText, suite.SystemOut)
})
}

func TestWriteToFilePath(t *testing.T) {
Expand Down