Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
yahavi committed Nov 29, 2023
1 parent 96ea182 commit 4dab18c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
15 changes: 9 additions & 6 deletions io/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func RunCmdWithOutputParser(config CmdConfig, prompt bool, regExpStruct ...*CmdO
return
}
errChan := make(chan error)
stdoutBuilder := strings.Builder{}
wg.Add(1)
go func() {
defer wg.Done()
Expand All @@ -97,9 +98,11 @@ func RunCmdWithOutputParser(config CmdConfig, prompt bool, regExpStruct ...*CmdO
if prompt {
fmt.Fprintf(os.Stderr, line+"\n")
}
stdOut += line + "\n"
stdoutBuilder.WriteString(line)
stdoutBuilder.WriteRune('\n')
}
}()
stderrBuilder := strings.Builder{}
wg.Add(1)
go func() {
defer wg.Done()
Expand All @@ -108,7 +111,8 @@ func RunCmdWithOutputParser(config CmdConfig, prompt bool, regExpStruct ...*CmdO
if prompt {
fmt.Fprintf(os.Stderr, line+"\n")
}
errorOut += line + "\n"
stderrBuilder.WriteString(line)
stderrBuilder.WriteRune('\n')
if hasError {
break
}
Expand All @@ -120,12 +124,11 @@ func RunCmdWithOutputParser(config CmdConfig, prompt bool, regExpStruct ...*CmdO
close(errChan)
}()

for channelErr := range errChan {
err = errors.Join(err, channelErr)
}
if err != nil {
for err = range errChan {
return
}
stdOut = stdoutBuilder.String()
errorOut = stderrBuilder.String()

err = cmd.Wait()
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions io/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ var matchAllRegexp = regexp.MustCompile(".*")
var errParsing = errors.New("parsing error")

func TestRunCmdWithOutputParser(t *testing.T) {
config := NewCommand("git", "", []string{"status"})
config := NewCommand("go", "", []string{"version"})
stdout, stderr, exitOk, err := RunCmdWithOutputParser(config, false, &CmdOutputPattern{
RegExp: matchAllRegexp,
ExecFunc: func(pattern *CmdOutputPattern) (string, error) { return pattern.Line, nil },
})
assert.NoError(t, err)
assert.True(t, exitOk)
assert.Contains(t, stdout, "On branch")
assert.Contains(t, stdout, "go version")
assert.Empty(t, stderr)
}

func TestRunCmdWithOutputParserError(t *testing.T) {
config := NewCommand("git", "", []string{"status"})
config := NewCommand("go", "", []string{"version"})
_, _, exitOk, err := RunCmdWithOutputParser(config, false, &CmdOutputPattern{
RegExp: matchAllRegexp,
ExecFunc: func(pattern *CmdOutputPattern) (string, error) { return pattern.Line, errParsing },
})
assert.ErrorContains(t, err, "parsing error\nparsing error")
assert.ErrorContains(t, err, "parsing error")
assert.False(t, exitOk)
}

Expand Down

0 comments on commit 4dab18c

Please sign in to comment.