From 326282afc13f724c3207359772c6f05549d4adb1 Mon Sep 17 00:00:00 2001 From: David Elliott Date: Thu, 15 Nov 2018 08:12:22 -0600 Subject: [PATCH] fix(version check): Only parse the last line on stdout when checking for version of cli tools --- install/installer.go | 5 +++-- install/installer_test.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/install/installer.go b/install/installer.go index c568d52f5..69bd9c1f4 100644 --- a/install/installer.go +++ b/install/installer.go @@ -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 diff --git a/install/installer_test.go b/install/installer_test.go index ed1956b70..11c89e85a 100644 --- a/install/installer_test.go +++ b/install/installer_test.go @@ -2,6 +2,7 @@ package install import ( "errors" + "fmt" "reflect" "testing" ) @@ -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")