Skip to content

Commit 678c239

Browse files
Resolve todo and add unit test (#488)
* Resolve todo and add unit test * Comment: mark as test helper function
1 parent 0c83dce commit 678c239

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

internal/installation/util.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ import (
2727
// ListInstalledPlugins returns a list of all install plugins in a
2828
// name:version format based on the install receipts at the specified dir.
2929
func ListInstalledPlugins(receiptsDir string) (map[string]string, error) {
30-
// TODO(ahmetb): Write unit tests for this method. Currently blocked by
31-
// lack of an in-memory recipt object (issue#270) that we can use to save
32-
// receipts to a tempdir that can be read from unit tests.
33-
3430
matches, err := filepath.Glob(filepath.Join(receiptsDir, "*"+constants.ManifestExtension))
3531
if err != nil {
3632
return nil, errors.Wrapf(err, "failed to grab receipts directory (%s) for manifests", receiptsDir)

internal/installation/util_test.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019 The Kubernetes Authors.
1+
// Copyright 2020 The Kubernetes Authors.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -17,6 +17,12 @@ package installation
1717
import (
1818
"path/filepath"
1919
"testing"
20+
21+
"github.com/google/go-cmp/cmp"
22+
23+
"sigs.k8s.io/krew/internal/testutil"
24+
"sigs.k8s.io/krew/pkg/constants"
25+
"sigs.k8s.io/krew/pkg/index"
2026
)
2127

2228
func testdataPath(t *testing.T) string {
@@ -26,3 +32,49 @@ func testdataPath(t *testing.T) string {
2632
}
2733
return filepath.Join(pwd, "testdata")
2834
}
35+
36+
func TestListInstalledPlugins(t *testing.T) {
37+
tests := []struct {
38+
name string
39+
plugins []index.Plugin
40+
expected map[string]string
41+
}{
42+
{
43+
name: "single plugin",
44+
plugins: []index.Plugin{testutil.NewPlugin().WithName("test").WithVersion("v0.0.1").V()},
45+
expected: map[string]string{"test": "v0.0.1"},
46+
},
47+
{
48+
name: "multiple plugins",
49+
plugins: []index.Plugin{
50+
testutil.NewPlugin().WithName("plugin-a").WithVersion("v0.0.1").V(),
51+
testutil.NewPlugin().WithName("plugin-b").WithVersion("v0.1.0").V(),
52+
testutil.NewPlugin().WithName("plugin-c").WithVersion("v1.0.0").V(),
53+
},
54+
expected: map[string]string{
55+
"plugin-a": "v0.0.1",
56+
"plugin-b": "v0.1.0",
57+
"plugin-c": "v1.0.0",
58+
},
59+
},
60+
}
61+
62+
for _, test := range tests {
63+
t.Run(test.name, func(t *testing.T) {
64+
tempDir, cleanup := testutil.NewTempDir(t)
65+
defer cleanup()
66+
67+
for _, plugin := range test.plugins {
68+
tempDir.WritePlugin(plugin.Name+constants.ManifestExtension, plugin)
69+
}
70+
71+
actual, err := ListInstalledPlugins(tempDir.Root())
72+
if err != nil {
73+
t.Fatal(err)
74+
}
75+
if diff := cmp.Diff(test.expected, actual); diff != "" {
76+
t.Error(diff)
77+
}
78+
})
79+
}
80+
}

internal/testutil/tempdir.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ import (
2121
"path/filepath"
2222
"strings"
2323
"testing"
24+
25+
"sigs.k8s.io/yaml"
26+
27+
"sigs.k8s.io/krew/pkg/index"
2428
)
2529

2630
type TempDir struct {
@@ -72,3 +76,12 @@ func (td *TempDir) Write(file string, content []byte) *TempDir {
7276
}
7377
return td
7478
}
79+
80+
func (td *TempDir) WritePlugin(file string, plugin index.Plugin) *TempDir {
81+
td.t.Helper()
82+
content, err := yaml.Marshal(plugin)
83+
if err != nil {
84+
td.t.Fatalf("cannot marshal plugin: %s", err)
85+
}
86+
return td.Write(file, content)
87+
}

0 commit comments

Comments
 (0)