Skip to content

Commit

Permalink
Use go 1.14 t.Cleanup() for simpler tests (#633)
Browse files Browse the repository at this point in the history
* Use go 1.14 t.Cleanup() for simpler tests

* Update godoc for NewTempDir()
  • Loading branch information
corneliusweig authored Aug 16, 2020
1 parent bf4ad3a commit 60d3f00
Show file tree
Hide file tree
Showing 26 changed files with 108 additions and 205 deletions.
7 changes: 3 additions & 4 deletions cmd/krew/cmd/internal/setup_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import (
)

func TestIsBinDirInPATH_firstRun(t *testing.T) {
tempDir, cleanup := testutil.NewTempDir(t)
defer cleanup()
tempDir := testutil.NewTempDir(t)

paths := environment.NewPaths(tempDir.Path("does-not-exist"))
res := IsBinDirInPATH(paths)
Expand All @@ -35,8 +34,8 @@ func TestIsBinDirInPATH_firstRun(t *testing.T) {
}

func TestIsBinDirInPATH_secondRun(t *testing.T) {
tempDir, cleanup := testutil.NewTempDir(t)
defer cleanup()
tempDir := testutil.NewTempDir(t)

paths := environment.NewPaths(tempDir.Root())
res := IsBinDirInPATH(paths)
if res == true {
Expand Down
3 changes: 1 addition & 2 deletions cmd/validate-krew-manifest/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ func TestValidateManifestFile(t *testing.T) {

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

if test.writeManifest {
content, err := yaml.Marshal(test.plugin)
Expand Down
3 changes: 1 addition & 2 deletions integration_test/commandline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import "testing"
func TestUnknownCommand(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

if _, err := test.Krew("foobar").Run(); err == nil {
t.Errorf("Expected `krew foobar` to fail")
Expand Down
6 changes: 2 additions & 4 deletions integration_test/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import (
func TestKrewHelp(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

test.Krew().RunOrFail() // no args
test.Krew("help").RunOrFail()
Expand All @@ -33,8 +32,7 @@ func TestKrewHelp(t *testing.T) {

func TestRootHelpShowsKubectlPrefix(t *testing.T) {
skipShort(t)
test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

out := string(test.Krew("help").RunOrFailOutput())

Expand Down
42 changes: 14 additions & 28 deletions integration_test/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ import (
func TestKrewIndexAdd(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

test.WithDefaultIndex()
if _, err := test.Krew("index", "add").Run(); err == nil {
Expand All @@ -51,8 +50,7 @@ func TestKrewIndexAdd(t *testing.T) {

func TestKrewIndexAddUnsafe(t *testing.T) {
skipShort(t)
test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)
test.WithDefaultIndex()

cases := []string{"a/b", `a\b`, "../a", `..\a`}
Expand All @@ -71,8 +69,7 @@ func TestKrewIndexAddUnsafe(t *testing.T) {
func TestKrewIndexAddShowsSecurityWarning(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

test.WithDefaultIndex()
index := environment.NewPaths(test.Root()).IndexPath(constants.DefaultIndexName)
Expand All @@ -85,8 +82,7 @@ func TestKrewIndexAddShowsSecurityWarning(t *testing.T) {
func TestKrewIndexList(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

test.WithDefaultIndex()
out := test.Krew("index", "list").RunOrFailOutput()
Expand All @@ -106,8 +102,7 @@ func TestKrewIndexList(t *testing.T) {
func TestKrewIndexList_NoIndexes(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

test.WithDefaultIndex()
index := environment.NewPaths(test.Root()).IndexBase()
Expand All @@ -124,8 +119,7 @@ func TestKrewIndexList_NoIndexes(t *testing.T) {

func TestKrewIndexRemove_nonExisting(t *testing.T) {
skipShort(t)
test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

_, err := test.Krew("index", "remove", "non-existing").Run()
if err == nil {
Expand All @@ -135,18 +129,16 @@ func TestKrewIndexRemove_nonExisting(t *testing.T) {

func TestKrewIndexRemove_ok(t *testing.T) {
skipShort(t)
test, cleanup := NewTest(t)
test := NewTest(t)
test.WithDefaultIndex().WithCustomIndexFromDefault("foo")
defer cleanup()

test.Krew("index", "remove", "foo").RunOrFail()
}

func TestKrewIndexRemove_unsafe(t *testing.T) {
skipShort(t)
test, cleanup := NewTest(t)
test := NewTest(t)
test.WithDefaultIndex()
defer cleanup()

expected := "invalid index name"
cases := []string{"a/b", `a\b`, "../a", `..\a`}
Expand All @@ -162,9 +154,8 @@ func TestKrewIndexRemove_unsafe(t *testing.T) {

func TestKrewIndexRemoveFailsWhenPluginsInstalled(t *testing.T) {
skipShort(t)
test, cleanup := NewTest(t)
test := NewTest(t)
test.WithDefaultIndex()
defer cleanup()

test.Krew("install", validPlugin).RunOrFailOutput()
if _, err := test.Krew("index", "remove", "default").Run(); err == nil {
Expand All @@ -177,17 +168,15 @@ func TestKrewIndexRemoveFailsWhenPluginsInstalled(t *testing.T) {

func TestKrewIndexRemoveForce_nonExisting(t *testing.T) {
skipShort(t)
test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

// --force returns success for non-existing indexes
test.Krew("index", "remove", "--force", "non-existing").RunOrFail()
}

func TestKrewDefaultIndex_notAutomaticallyAdded(t *testing.T) {
skipShort(t)
test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

test.Krew("help").RunOrFail()
out, err := test.Krew("search").Run()
Expand All @@ -202,26 +191,23 @@ func TestKrewDefaultIndex_notAutomaticallyAdded(t *testing.T) {

func TestKrewDefaultIndex_AutoAddedOnInstall(t *testing.T) {
skipShort(t)
test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

test.Krew("install", validPlugin).RunOrFail()
ensureIndexListHasDefaultIndex(t, string(test.Krew("index", "list").RunOrFailOutput()))
}

func TestKrewDefaultIndex_AutoAddedOnUpdate(t *testing.T) {
skipShort(t)
test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

test.Krew("update").RunOrFail()
ensureIndexListHasDefaultIndex(t, string(test.Krew("index", "list").RunOrFailOutput()))
}

func TestKrewDefaultIndex_AutoAddedOnUpgrade(t *testing.T) {
skipShort(t)
test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

test.Krew("upgrade").RunOrFail()
ensureIndexListHasDefaultIndex(t, string(test.Krew("index", "list").RunOrFailOutput()))
Expand Down
9 changes: 3 additions & 6 deletions integration_test/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import (
func TestKrewInfo(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

out := string(test.WithDefaultIndex().Krew("info", validPlugin).RunOrFailOutput())
expected := `INDEX: default`
Expand All @@ -35,8 +34,7 @@ func TestKrewInfo(t *testing.T) {
func TestKrewInfoInvalidPlugin(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

plugin := "invalid-plugin"
_, err := test.WithDefaultIndex().Krew("info", plugin).Run()
Expand All @@ -48,8 +46,7 @@ func TestKrewInfoInvalidPlugin(t *testing.T) {
func TestKrewInfoCustomIndex(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

test = test.WithDefaultIndex().WithCustomIndexFromDefault("foo")
test.Krew("install", "foo/"+validPlugin).RunOrFail()
Expand Down
45 changes: 15 additions & 30 deletions integration_test/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ const (

func TestKrewInstall(t *testing.T) {
skipShort(t)
test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

if err := test.Krew("install", validPlugin); err == nil {
t.Fatal("expected to fail without initializing the index")
Expand All @@ -49,8 +48,7 @@ func TestKrewInstall(t *testing.T) {

func TestKrewInstallReRun(t *testing.T) {
skipShort(t)
test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

test = test.WithDefaultIndex()
test.Krew("install", validPlugin).RunOrFail()
Expand All @@ -60,8 +58,7 @@ func TestKrewInstallReRun(t *testing.T) {

func TestKrewInstallUnsafe(t *testing.T) {
skipShort(t)
test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)
test.WithDefaultIndex()

cases := []string{
Expand All @@ -86,8 +83,7 @@ func TestKrewInstallUnsafe(t *testing.T) {
func TestKrewInstall_MultiplePositionalArgs(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

test.WithDefaultIndex().Krew("install", validPlugin, validPlugin2).RunOrFailOutput()
test.AssertExecutableInPATH("kubectl-" + validPlugin)
Expand All @@ -97,8 +93,7 @@ func TestKrewInstall_MultiplePositionalArgs(t *testing.T) {
func TestKrewInstall_Stdin(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

test.WithDefaultIndex().WithStdin(strings.NewReader(validPlugin + "\n" + validPlugin2)).
Krew("install").RunOrFailOutput()
Expand All @@ -110,8 +105,7 @@ func TestKrewInstall_Stdin(t *testing.T) {
func TestKrewInstall_StdinAndPositionalArguments(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

// when stdin is detected, it's ignored in favor of positional arguments
test.WithDefaultIndex().
Expand All @@ -124,8 +118,7 @@ func TestKrewInstall_StdinAndPositionalArguments(t *testing.T) {
func TestKrewInstall_ExplicitDefaultIndex(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

test.Krew("install", "default/"+validPlugin).RunOrFail()
test.AssertExecutableInPATH("kubectl-" + validPlugin)
Expand All @@ -135,8 +128,7 @@ func TestKrewInstall_ExplicitDefaultIndex(t *testing.T) {
func TestKrewInstall_CustomIndex(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

test.WithDefaultIndex().WithCustomIndexFromDefault("foo")
test.Krew("install", "foo/"+validPlugin).RunOrFail()
Expand All @@ -152,8 +144,7 @@ func TestKrewInstall_CustomIndex(t *testing.T) {
func TestKrewInstallNoSecurityWarningForCustomIndex(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

test.WithDefaultIndex().WithCustomIndexFromDefault("foo")
out := string(test.Krew("install", "foo/"+validPlugin).RunOrFailOutput())
Expand All @@ -165,8 +156,7 @@ func TestKrewInstallNoSecurityWarningForCustomIndex(t *testing.T) {
func TestKrewInstall_Manifest(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

test.Krew("install",
"--manifest", filepath.Join("testdata", validPlugin+constants.ManifestExtension)).
Expand All @@ -178,8 +168,7 @@ func TestKrewInstall_Manifest(t *testing.T) {
func TestKrewInstall_ManifestURL(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)
srv, shutdown := localTestServer()
defer shutdown()

Expand All @@ -193,8 +182,7 @@ func TestKrewInstall_ManifestURL(t *testing.T) {
func TestKrewInstall_ManifestAndArchive(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

test.Krew("install",
"--manifest", filepath.Join("testdata", fooPlugin+constants.ManifestExtension),
Expand All @@ -207,8 +195,7 @@ func TestKrewInstall_ManifestAndArchive(t *testing.T) {
func TestKrewInstall_OnlyArchive(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

_, err := test.Krew("install",
"--archive", filepath.Join("testdata", fooPlugin+".tar.gz")).
Expand All @@ -221,8 +208,7 @@ func TestKrewInstall_OnlyArchive(t *testing.T) {
func TestKrewInstall_ManifestArgsAreMutuallyExclusive(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)
srv, shutdown := localTestServer()
defer shutdown()

Expand All @@ -237,8 +223,7 @@ func TestKrewInstall_ManifestArgsAreMutuallyExclusive(t *testing.T) {
func TestKrewInstall_NoManifestArgsWhenPositionalArgsSpecified(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()
test := NewTest(t)

_, err := test.Krew("install", validPlugin,
"--manifest", filepath.Join("testdata", fooPlugin+constants.ManifestExtension),
Expand Down
Loading

0 comments on commit 60d3f00

Please sign in to comment.