Skip to content

Commit

Permalink
Resolve todo and add unit test (#488)
Browse files Browse the repository at this point in the history
* Resolve todo and add unit test

* Comment: mark as test helper function
  • Loading branch information
corneliusweig authored Feb 7, 2020
1 parent 0c83dce commit 678c239
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
4 changes: 0 additions & 4 deletions internal/installation/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ import (
// ListInstalledPlugins returns a list of all install plugins in a
// name:version format based on the install receipts at the specified dir.
func ListInstalledPlugins(receiptsDir string) (map[string]string, error) {
// TODO(ahmetb): Write unit tests for this method. Currently blocked by
// lack of an in-memory recipt object (issue#270) that we can use to save
// receipts to a tempdir that can be read from unit tests.

matches, err := filepath.Glob(filepath.Join(receiptsDir, "*"+constants.ManifestExtension))
if err != nil {
return nil, errors.Wrapf(err, "failed to grab receipts directory (%s) for manifests", receiptsDir)
Expand Down
54 changes: 53 additions & 1 deletion internal/installation/util_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 The Kubernetes Authors.
// Copyright 2020 The Kubernetes Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,12 @@ package installation
import (
"path/filepath"
"testing"

"github.com/google/go-cmp/cmp"

"sigs.k8s.io/krew/internal/testutil"
"sigs.k8s.io/krew/pkg/constants"
"sigs.k8s.io/krew/pkg/index"
)

func testdataPath(t *testing.T) string {
Expand All @@ -26,3 +32,49 @@ func testdataPath(t *testing.T) string {
}
return filepath.Join(pwd, "testdata")
}

func TestListInstalledPlugins(t *testing.T) {
tests := []struct {
name string
plugins []index.Plugin
expected map[string]string
}{
{
name: "single plugin",
plugins: []index.Plugin{testutil.NewPlugin().WithName("test").WithVersion("v0.0.1").V()},
expected: map[string]string{"test": "v0.0.1"},
},
{
name: "multiple plugins",
plugins: []index.Plugin{
testutil.NewPlugin().WithName("plugin-a").WithVersion("v0.0.1").V(),
testutil.NewPlugin().WithName("plugin-b").WithVersion("v0.1.0").V(),
testutil.NewPlugin().WithName("plugin-c").WithVersion("v1.0.0").V(),
},
expected: map[string]string{
"plugin-a": "v0.0.1",
"plugin-b": "v0.1.0",
"plugin-c": "v1.0.0",
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tempDir, cleanup := testutil.NewTempDir(t)
defer cleanup()

for _, plugin := range test.plugins {
tempDir.WritePlugin(plugin.Name+constants.ManifestExtension, plugin)
}

actual, err := ListInstalledPlugins(tempDir.Root())
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(test.expected, actual); diff != "" {
t.Error(diff)
}
})
}
}
13 changes: 13 additions & 0 deletions internal/testutil/tempdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import (
"path/filepath"
"strings"
"testing"

"sigs.k8s.io/yaml"

"sigs.k8s.io/krew/pkg/index"
)

type TempDir struct {
Expand Down Expand Up @@ -72,3 +76,12 @@ func (td *TempDir) Write(file string, content []byte) *TempDir {
}
return td
}

func (td *TempDir) WritePlugin(file string, plugin index.Plugin) *TempDir {
td.t.Helper()
content, err := yaml.Marshal(plugin)
if err != nil {
td.t.Fatalf("cannot marshal plugin: %s", err)
}
return td.Write(file, content)
}

0 comments on commit 678c239

Please sign in to comment.