Skip to content

Commit

Permalink
fix(version check): Only parse the last line on stdout when checking …
Browse files Browse the repository at this point in the history
…for version of cli tools
  • Loading branch information
David Elliott committed Nov 15, 2018
1 parent 2acf846 commit 326282a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 3 additions & 2 deletions install/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ func (i *Installer) GetVersionForBinary(binary string) (version string, err erro
log.Println("[DEBUG] running binary", binary)

content, err := i.commander.Output(binary, "version")
version = string(content)
elements := strings.Split(strings.TrimSpace(string(content)), "\n")
version = strings.TrimSpace(elements[len(elements)-1])

return strings.TrimSpace(version), err
return version, err
}

// commander wraps the exec package, allowing us
Expand Down
18 changes: 18 additions & 0 deletions install/installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package install

import (
"errors"
"fmt"
"reflect"
"testing"
)
Expand Down Expand Up @@ -70,6 +71,23 @@ func TestInstaller_getVersionForBinary(t *testing.T) {
}
}

func TestInstaller_getVersionForBinaryMultilineOutput(t *testing.T) {
version := "1.5.0"
actualOutput := fmt.Sprintf(`
some other characters sent to stdout
%s
`, version)
i := getInstaller(actualOutput, nil)
v, err := i.GetVersionForBinary("pact-mock-service")

if err != nil {
t.Fatal("error:", err)
}
if v != version {
t.Fatal("Want", version, "got", v)
}
}

func TestInstaller_getVersionForBinaryError(t *testing.T) {
i := getInstaller("", errors.New("test error"))
_, err := i.GetVersionForBinary("pact-mock-service")
Expand Down

0 comments on commit 326282a

Please sign in to comment.