Skip to content

Commit

Permalink
chore: Resolved go task formatting inconsistency (#1665)
Browse files Browse the repository at this point in the history
  • Loading branch information
Avinash-Sanpala authored Oct 28, 2024
1 parent 9abb031 commit 97af6b7
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 32 deletions.
2 changes: 1 addition & 1 deletion internal/install/execution/go_task_recipe_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions internal/install/execution/line_capture_buffer.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package execution

import (
"io"

log "github.com/sirupsen/logrus"
"io"
"strings"
)

type LineCaptureBuffer struct {
Expand All @@ -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
}
Expand Down
93 changes: 65 additions & 28 deletions internal/install/execution/line_capture_buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
}
}

0 comments on commit 97af6b7

Please sign in to comment.