Skip to content

Commit 58a5184

Browse files
authored
Add OS/Arch information in the krew version CLI output to help with diagnostics. (#484)
1 parent 7842e57 commit 58a5184

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

cmd/krew/cmd/version.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/spf13/cobra"
2121

22+
"sigs.k8s.io/krew/internal/installation"
2223
"sigs.k8s.io/krew/internal/version"
2324
"sigs.k8s.io/krew/pkg/constants"
2425
)
@@ -46,6 +47,7 @@ Remarks:
4647
{"IndexPath", paths.IndexPath()},
4748
{"InstallPath", paths.InstallPath()},
4849
{"BinPath", paths.BinPath()},
50+
{"DetectedPlatform", installation.OSArch().String()},
4951
}
5052
return printTable(os.Stdout, []string{"OPTION", "VALUE"}, conf)
5153
},

integration_test/testutil_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ func NewTest(t *testing.T) (*ITest, func()) {
6868
env: []string{
6969
fmt.Sprintf("KREW_ROOT=%s", tempDir.Root()),
7070
fmt.Sprintf("PATH=%s", augmentPATH(t, binDir)),
71+
"KREW_OS=linux",
72+
"KREW_ARCH=amd64",
7173
},
7274
tempDir: tempDir,
7375
}, cleanup

integration_test/version_test.go

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
package integrationtest
1616

1717
import (
18-
"fmt"
18+
"path"
1919
"regexp"
20+
"strings"
2021
"testing"
22+
23+
"github.com/google/go-cmp/cmp"
2124
)
2225

2326
func TestKrewVersion(t *testing.T) {
@@ -26,21 +29,34 @@ func TestKrewVersion(t *testing.T) {
2629
test, cleanup := NewTest(t)
2730
defer cleanup()
2831

29-
output := test.Krew("version").RunOrFailOutput()
32+
stdOut := string(test.Krew("version").RunOrFailOutput())
33+
34+
lineSplit := regexp.MustCompile(`\s+`)
35+
actual := make(map[string]string)
36+
for _, line := range strings.Split(stdOut, "\n") {
37+
if line == "" {
38+
continue
39+
}
40+
optionValue := lineSplit.Split(line, 2)
41+
if len(optionValue) < 2 {
42+
t.Errorf("Expect `krew version` to output `OPTION VALUE` pair separated by spaces, got: %v", optionValue)
43+
}
44+
actual[optionValue[0]] = optionValue[1]
45+
}
3046

31-
requiredSubstrings := []string{
32-
fmt.Sprintf(`BasePath\s+%s`, test.Root()),
33-
"GitTag",
34-
"GitCommit",
35-
`IndexURI\s+https://github.com/kubernetes-sigs/krew-index.git`,
36-
"IndexPath",
37-
"InstallPath",
38-
"BinPath",
47+
requiredSubstrings := map[string]string{
48+
"OPTION": "VALUE",
49+
"BasePath": test.Root(),
50+
"GitTag": "unknown",
51+
"GitCommit": "unknown",
52+
"IndexURI": "https://github.com/kubernetes-sigs/krew-index.git",
53+
"IndexPath": path.Join(test.Root(), "index"),
54+
"InstallPath": path.Join(test.Root(), "store"),
55+
"BinPath": path.Join(test.Root(), "bin"),
56+
"DetectedPlatform": "linux/amd64",
3957
}
4058

41-
for _, p := range requiredSubstrings {
42-
if regexp.MustCompile(p).FindSubmatchIndex(output) == nil {
43-
t.Errorf("Expected to find %q in output of `krew version`", p)
44-
}
59+
if diff := cmp.Diff(actual, requiredSubstrings); diff != "" {
60+
t.Errorf("`krew version` output mismatch (-got, +want):\n%s", diff)
4561
}
4662
}

0 commit comments

Comments
 (0)