Skip to content

Commit

Permalink
fix (test/extended/util) : shell doesn't parse command output ending …
Browse files Browse the repository at this point in the history
…without newline (#4416)

ScanPipe should not assume entire line to contain only the exit code
token. It should extract exit code token and write remainder output to
output buffer.

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Oct 28, 2024
1 parent 9b7772d commit fa728a4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
14 changes: 12 additions & 2 deletions test/extended/util/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,19 @@ func (shell *ShellInstance) ScanPipe(scanner *bufio.Scanner, buffer *bytes.Buffe
}

if strings.Contains(str, exitCodeIdentifier) && !strings.Contains(str, shell.checkExitCodeCmd) {
exitCode := strings.Split(str, "=")[1]
lastCommandExitCodeStr := str
exitCodeIdentifierIndex := strings.Index(lastCommandExitCodeStr, exitCodeIdentifier)
if exitCodeIdentifierIndex > 0 {
lastCommandExitCodeStr = lastCommandExitCodeStr[exitCodeIdentifierIndex:]
str = str[0:exitCodeIdentifierIndex]
} else {
str = ""
}

exitCode := strings.Split(lastCommandExitCodeStr, "=")[1]
shell.exitCodeChannel <- exitCode
} else {
}
if len(str) > 0 {
buffer.WriteString(str + "\n")
}
}
Expand Down
40 changes: 40 additions & 0 deletions test/extended/util/shell_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package util

import (
"bufio"
"bytes"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

var testArguments = map[string]struct {
commandOutput string
expectedParsedOutput string
expectedParsedExitCode string
}{
"When command output line contains additional output along with exit code, then parse output and exit code": {
"remainderOutputexitCodeOfLastCommandInShell=0", "remainderOutput\n", "0",
},
"When command output line contains only exit code, then parse exit code": {
"exitCodeOfLastCommandInShell=1", "", "1",
},
}

func TestScanPipeShouldCorrectlyParseOutputNotEndingWithNewLine(t *testing.T) {
for name, test := range testArguments {
t.Run(name, func(t *testing.T) {
// Given
shell.ConfigureTypeOfShell("bash")
shell.exitCodeChannel = make(chan string, 2)
scanner := bufio.NewScanner(strings.NewReader(test.commandOutput))
b := &bytes.Buffer{}
// When
shell.ScanPipe(scanner, b, "stdout")
// Then
assert.Equal(t, test.expectedParsedOutput, b.String())
assert.Equal(t, test.expectedParsedExitCode, <-shell.exitCodeChannel)
})
}
}

0 comments on commit fa728a4

Please sign in to comment.