diff --git a/test-results/pkg/cli/cli.go b/test-results/pkg/cli/cli.go index 18c00e5d..173eed3f 100644 --- a/test-results/pkg/cli/cli.go +++ b/test-results/pkg/cli/cli.go @@ -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") diff --git a/test-results/pkg/cli/cli_test.go b/test-results/pkg/cli/cli_test.go index 623ceb0f..45b8f9e2 100644 --- a/test-results/pkg/cli/cli_test.go +++ b/test-results/pkg/cli/cli_test.go @@ -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) {