diff --git a/internal/install/execution/go_task_recipe_executor_test.go b/internal/install/execution/go_task_recipe_executor_test.go index d64cf454c..56c1064de 100644 --- a/internal/install/execution/go_task_recipe_executor_test.go +++ b/internal/install/execution/go_task_recipe_executor_test.go @@ -42,7 +42,7 @@ tasks: func TestExecute_HandleRecipeLastError(t *testing.T) { v := types.RecipeVars{ - "TEST_VAR": "testValue", + "TEST_VAR": "testValue\n \n", } r := types.OpenInstallationRecipe{ Name: "test-recipe", diff --git a/internal/install/execution/line_capture_buffer.go b/internal/install/execution/line_capture_buffer.go index 874afcb30..945218cc0 100644 --- a/internal/install/execution/line_capture_buffer.go +++ b/internal/install/execution/line_capture_buffer.go @@ -1,9 +1,9 @@ package execution import ( - "io" - log "github.com/sirupsen/logrus" + "io" + "strings" ) type LineCaptureBuffer struct { @@ -28,7 +28,7 @@ func (c *LineCaptureBuffer) Write(p []byte) (n int, err error) { s := string(c.current) c.fullRecipeOutput = append(c.fullRecipeOutput, s) - if s != "" { + if strings.TrimSpace(s) != "" { log.Debugf(s) c.LastFullLine = s } diff --git a/internal/install/execution/line_capture_buffer_test.go b/internal/install/execution/line_capture_buffer_test.go index 46b4a8585..f3c5e6b73 100644 --- a/internal/install/execution/line_capture_buffer_test.go +++ b/internal/install/execution/line_capture_buffer_test.go @@ -9,35 +9,72 @@ import ( ) func TestLineCaptureBuffer(t *testing.T) { - w := bytes.NewBufferString("") - b := NewLineCaptureBuffer(w) - _, err := b.Write([]byte("abc\n123\ndef")) - assert.NoError(t, err) + tests := []struct { + name string + inputs []string // Multiple writes if needed + expectedLast string // Expected LastFullLine + expectedCurr string // Expected Current() + expectedOutput string // Expected final buffer content + }{ + { + name: "Basic input with multiple lines", + inputs: []string{"abc\n123\ndef"}, + expectedLast: "123", + expectedCurr: "def", + expectedOutput: "abc\n123\ndef", + }, + { + name: "Input with trailing empty line", + inputs: []string{"abc\n123\ndef\n \n"}, + expectedLast: "def", + expectedCurr: "", + expectedOutput: "abc\n123\ndef\n \n", + }, + { + name: "Multiple write operations", + inputs: []string{"abc\n", "123\n", "def\n", "nope"}, + expectedLast: "def", + expectedCurr: "nope", + expectedOutput: "abc\n123\ndef\nnope", + }, + { + name: "Empty input", + inputs: []string{""}, + expectedLast: "", + expectedCurr: "", + expectedOutput: "", + }, + { + name: "Single line without newline", + inputs: []string{"single"}, + expectedLast: "", + expectedCurr: "single", + expectedOutput: "single", + }, + { + name: "Multiple empty lines", + inputs: []string{"\n\n\n"}, + expectedLast: "", + expectedCurr: "", + expectedOutput: "\n\n\n", + }, + } - require.Equal(t, "123", b.LastFullLine) - require.Equal(t, "def", b.Current()) - require.Equal(t, "abc\n123\ndef", w.String()) -} - -func TestLineCaptureBufferCapturesEntireOutput(t *testing.T) { - w := bytes.NewBufferString("") - - b := NewLineCaptureBuffer(w) - _, err := b.Write([]byte("abc\n")) - assert.NoError(t, err) - - _, err = b.Write([]byte("123\n")) - assert.NoError(t, err) - - _, err = b.Write([]byte("def\n")) - assert.NoError(t, err) - - _, err = b.Write([]byte("nope")) - assert.NoError(t, err) - - require.Equal(t, "def", b.LastFullLine) - require.Equal(t, "nope", b.Current()) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + w := bytes.NewBufferString("") + b := NewLineCaptureBuffer(w) - require.Equal(t, len(b.fullRecipeOutput), 3) + // Process all inputs + for _, input := range tt.inputs { + _, err := b.Write([]byte(input)) + assert.NoError(t, err, "Write operation should not fail") + } + // Verify the results + require.Equal(t, tt.expectedLast, b.LastFullLine, "LastFullLine mismatch") + require.Equal(t, tt.expectedCurr, b.Current(), "Current() mismatch") + require.Equal(t, tt.expectedOutput, w.String(), "Buffer content mismatch") + }) + } }