Skip to content

Commit

Permalink
Addressed review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
soumeh01 committed Jul 15, 2024
1 parent 00617a3 commit b1fa332
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
13 changes: 13 additions & 0 deletions pkg/utils/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package utils

import (
"bytes"
"os/exec"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -38,3 +39,15 @@ func (r Runner) ExecuteCommand(program string, quiet bool, args ...string) (stri
err := cmd.Run()
return string(r.outBytes), err
}

// This exclusive function returns the standard output and standard error as strings
func ExecuteCommand(program string, args ...string) (string, string, error) {
cmd := exec.Command(program, args...)

var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()

return stdout.String(), stderr.String(), err
}
17 changes: 17 additions & 0 deletions pkg/utils/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,20 @@ func TestExecuteCommand(t *testing.T) {
assert.Nil(err)
})
}

func TestExecuteCommandEx(t *testing.T) {
assert := assert.New(t)
t.Run("execute command normal verbosity", func(t *testing.T) {
outStr, errStr, err := ExecuteCommand("go", "version")
assert.Nil(err)
assert.Empty(errStr)
assert.Regexp("(go\\sversion\\sgo([\\d.]+).*)", outStr)
})

t.Run("execute invalid command", func(t *testing.T) {
outStr, errStr, err := ExecuteCommand("go", "invalid")
assert.Error(err)
assert.Empty(outStr)
assert.Equal("go invalid: unknown command\nRun 'go help' for usage.\n", errStr)
})
}
13 changes: 0 additions & 13 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package utils

import (
"bytes"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -398,15 +397,3 @@ func isFileSystemCaseInsensitive() bool {
// On Linux, file systems are typically case sensitive
return filepath.Separator == '\\' || strings.Contains(strings.ToLower(os.Getenv("OS")), "darwin")
}

// This exclusive function returns the standard output and standard error as strings
func ExecuteCommand(program string, args ...string) (string, string, error) {
cmd := exec.Command(program, args...)

var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()

return stdout.String(), stderr.String(), err
}
17 changes: 0 additions & 17 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,20 +441,3 @@ func TestComparePaths(t *testing.T) {
}
})
}

func TestExecuteCommandEx(t *testing.T) {
assert := assert.New(t)
t.Run("execute command normal verbosity", func(t *testing.T) {
outStr, errStr, err := ExecuteCommand("go", "version")
assert.Nil(err)
assert.Empty(errStr)
assert.Regexp("(go\\sversion\\sgo([\\d.]+).*)", outStr)
})

t.Run("execute invalid command", func(t *testing.T) {
outStr, errStr, err := ExecuteCommand("go", "invalid")
assert.Error(err)
assert.Empty(outStr)
assert.Equal("go invalid: unknown command\nRun 'go help' for usage.\n", errStr)
})
}

0 comments on commit b1fa332

Please sign in to comment.