Skip to content

Commit

Permalink
test: replace ensure.TempDir with t.TempDir
Browse files Browse the repository at this point in the history
This commit replaces `ensure.TempDir` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.

Prior to this commit, temporary directory created using `ensure.TempDir`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
	defer func() {
		if err := os.RemoveAll(dir); err != nil {
			t.Fatal(err)
		}
	}
is also tedious, but `t.TempDir` handles this for us nicely.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
  • Loading branch information
Juneezee committed Jul 29, 2023
1 parent 3433898 commit 2ceebff
Show file tree
Hide file tree
Showing 21 changed files with 58 additions and 110 deletions.
8 changes: 4 additions & 4 deletions cmd/helm/create_test.go
Expand Up @@ -30,9 +30,9 @@ import (
)

func TestCreateCmd(t *testing.T) {
defer ensure.HelmHome(t)()
ensure.HelmHome(t)
cname := "testchart"
dir := ensure.TempDir(t)
dir := t.TempDir()
defer testChdir(t, dir)()

// Run a create
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestCreateCmd(t *testing.T) {
}

func TestCreateStarterCmd(t *testing.T) {
defer ensure.HelmHome(t)()
ensure.HelmHome(t)
cname := "testchart"
defer resetEnv()()
os.MkdirAll(helmpath.CachePath(), 0755)
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestCreateStarterCmd(t *testing.T) {

func TestCreateStarterAbsoluteCmd(t *testing.T) {
defer resetEnv()()
defer ensure.HelmHome(t)()
ensure.HelmHome(t)
cname := "testchart"

// Create a starter.
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/dependency_update_test.go
Expand Up @@ -149,7 +149,7 @@ func TestDependencyUpdateCmd(t *testing.T) {

func TestDependencyUpdateCmd_DoNotDeleteOldChartsOnError(t *testing.T) {
defer resetEnv()()
defer ensure.HelmHome(t)()
ensure.HelmHome(t)

srv, err := repotest.NewTempServerWithCleanup(t, "testdata/testcharts/*.tgz")
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions cmd/helm/package_test.go
Expand Up @@ -23,7 +23,6 @@ import (
"strings"
"testing"

"helm.sh/helm/v3/internal/test/ensure"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
)
Expand Down Expand Up @@ -111,7 +110,7 @@ func TestPackage(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cachePath := ensure.TempDir(t)
cachePath := t.TempDir()
defer testChdir(t, cachePath)()

if err := os.MkdirAll("toot", 0777); err != nil {
Expand Down Expand Up @@ -170,7 +169,7 @@ func TestSetAppVersion(t *testing.T) {
var ch *chart.Chart
expectedAppVersion := "app-version-foo"
chartToPackage := "testdata/testcharts/alpine"
dir := ensure.TempDir(t)
dir := t.TempDir()
cmd := fmt.Sprintf("package %s --destination=%s --app-version=%s", chartToPackage, dir, expectedAppVersion)
_, output, err := executeActionCommand(cmd)
if err != nil {
Expand Down
19 changes: 9 additions & 10 deletions cmd/helm/repo_add_test.go
Expand Up @@ -27,7 +27,6 @@ import (

"sigs.k8s.io/yaml"

"helm.sh/helm/v3/internal/test/ensure"
"helm.sh/helm/v3/pkg/helmpath"
"helm.sh/helm/v3/pkg/helmpath/xdg"
"helm.sh/helm/v3/pkg/repo"
Expand All @@ -48,7 +47,7 @@ func TestRepoAddCmd(t *testing.T) {
}
defer srv2.Stop()

tmpdir := filepath.Join(ensure.TempDir(t), "path-component.yaml/data")
tmpdir := filepath.Join(t.TempDir(), "path-component.yaml/data")
err = os.MkdirAll(tmpdir, 0777)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -88,7 +87,7 @@ func TestRepoAdd(t *testing.T) {
}
defer ts.Stop()

rootDir := ensure.TempDir(t)
rootDir := t.TempDir()
repoFile := filepath.Join(rootDir, "repositories.yaml")

const testRepoName = "test-name"
Expand Down Expand Up @@ -145,8 +144,8 @@ func TestRepoAddCheckLegalName(t *testing.T) {

const testRepoName = "test-hub/test-name"

rootDir := ensure.TempDir(t)
repoFile := filepath.Join(ensure.TempDir(t), "repositories.yaml")
rootDir := t.TempDir()
repoFile := filepath.Join(t.TempDir(), "repositories.yaml")

o := &repoAddOptions{
name: testRepoName,
Expand All @@ -170,25 +169,25 @@ func TestRepoAddCheckLegalName(t *testing.T) {

func TestRepoAddConcurrentGoRoutines(t *testing.T) {
const testName = "test-name"
repoFile := filepath.Join(ensure.TempDir(t), "repositories.yaml")
repoFile := filepath.Join(t.TempDir(), "repositories.yaml")
repoAddConcurrent(t, testName, repoFile)
}

func TestRepoAddConcurrentDirNotExist(t *testing.T) {
const testName = "test-name-2"
repoFile := filepath.Join(ensure.TempDir(t), "foo", "repositories.yaml")
repoFile := filepath.Join(t.TempDir(), "foo", "repositories.yaml")
repoAddConcurrent(t, testName, repoFile)
}

func TestRepoAddConcurrentNoFileExtension(t *testing.T) {
const testName = "test-name-3"
repoFile := filepath.Join(ensure.TempDir(t), "repositories")
repoFile := filepath.Join(t.TempDir(), "repositories")
repoAddConcurrent(t, testName, repoFile)
}

func TestRepoAddConcurrentHiddenFile(t *testing.T) {
const testName = "test-name-4"
repoFile := filepath.Join(ensure.TempDir(t), ".repositories")
repoFile := filepath.Join(t.TempDir(), ".repositories")
repoAddConcurrent(t, testName, repoFile)
}

Expand Down Expand Up @@ -254,7 +253,7 @@ func TestRepoAddWithPasswordFromStdin(t *testing.T) {
t.Errorf("unexpected error, got '%v'", err)
}

tmpdir := ensure.TempDir(t)
tmpdir := t.TempDir()
repoFile := filepath.Join(tmpdir, "repositories.yaml")

store := storageFixture()
Expand Down
3 changes: 1 addition & 2 deletions cmd/helm/repo_index_test.go
Expand Up @@ -23,13 +23,12 @@ import (
"path/filepath"
"testing"

"helm.sh/helm/v3/internal/test/ensure"
"helm.sh/helm/v3/pkg/repo"
)

func TestRepoIndexCmd(t *testing.T) {

dir := ensure.TempDir(t)
dir := t.TempDir()

comp := filepath.Join(dir, "compressedchart-0.1.0.tgz")
if err := linkOrCopy("testdata/testcharts/compressedchart-0.1.0.tgz", comp); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions cmd/helm/repo_remove_test.go
Expand Up @@ -24,7 +24,6 @@ import (
"strings"
"testing"

"helm.sh/helm/v3/internal/test/ensure"
"helm.sh/helm/v3/pkg/helmpath"
"helm.sh/helm/v3/pkg/repo"
"helm.sh/helm/v3/pkg/repo/repotest"
Expand All @@ -37,7 +36,7 @@ func TestRepoRemove(t *testing.T) {
}
defer ts.Stop()

rootDir := ensure.TempDir(t)
rootDir := t.TempDir()
repoFile := filepath.Join(rootDir, "repositories.yaml")

const testRepoName = "test-name"
Expand Down Expand Up @@ -169,7 +168,7 @@ func TestRepoRemoveCompletion(t *testing.T) {
}
defer ts.Stop()

rootDir := ensure.TempDir(t)
rootDir := t.TempDir()
repoFile := filepath.Join(rootDir, "repositories.yaml")
repoCache := filepath.Join(rootDir, "cache/")

Expand Down
9 changes: 4 additions & 5 deletions cmd/helm/repo_update_test.go
Expand Up @@ -102,10 +102,9 @@ func TestUpdateCmdInvalid(t *testing.T) {
}

func TestUpdateCustomCacheCmd(t *testing.T) {
rootDir := ensure.TempDir(t)
rootDir := t.TempDir()
cachePath := filepath.Join(rootDir, "updcustomcache")
os.Mkdir(cachePath, os.ModePerm)
defer os.RemoveAll(cachePath)

ts, err := repotest.NewTempServerWithCleanup(t, "testdata/testserver/*.*")
if err != nil {
Expand All @@ -129,7 +128,7 @@ func TestUpdateCustomCacheCmd(t *testing.T) {

func TestUpdateCharts(t *testing.T) {
defer resetEnv()()
defer ensure.HelmHome(t)()
ensure.HelmHome(t)

ts, err := repotest.NewTempServerWithCleanup(t, "testdata/testserver/*.*")
if err != nil {
Expand Down Expand Up @@ -164,7 +163,7 @@ func TestRepoUpdateFileCompletion(t *testing.T) {

func TestUpdateChartsFail(t *testing.T) {
defer resetEnv()()
defer ensure.HelmHome(t)()
ensure.HelmHome(t)

ts, err := repotest.NewTempServerWithCleanup(t, "testdata/testserver/*.*")
if err != nil {
Expand Down Expand Up @@ -197,7 +196,7 @@ func TestUpdateChartsFail(t *testing.T) {

func TestUpdateChartsFailWithError(t *testing.T) {
defer resetEnv()()
defer ensure.HelmHome(t)()
ensure.HelmHome(t)

ts, err := repotest.NewTempServerWithCleanup(t, "testdata/testserver/*.*")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/root_test.go
Expand Up @@ -77,7 +77,7 @@ func TestRootCmd(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer ensure.HelmHome(t)()
ensure.HelmHome(t)

for k, v := range tt.envvars {
os.Setenv(k, v)
Expand Down
5 changes: 2 additions & 3 deletions cmd/helm/upgrade_test.go
Expand Up @@ -23,7 +23,6 @@ import (
"strings"
"testing"

"helm.sh/helm/v3/internal/test/ensure"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
Expand All @@ -32,7 +31,7 @@ import (

func TestUpgradeCmd(t *testing.T) {

tmpChart := ensure.TempDir(t)
tmpChart := t.TempDir()
cfile := &chart.Chart{
Metadata: &chart.Metadata{
APIVersion: chart.APIVersionV1,
Expand Down Expand Up @@ -357,7 +356,7 @@ func TestUpgradeInstallWithValuesFromStdin(t *testing.T) {
}

func prepareMockRelease(releaseName string, t *testing.T) (func(n string, v int, ch *chart.Chart) *release.Release, *chart.Chart, string) {
tmpChart := ensure.TempDir(t)
tmpChart := t.TempDir()
configmapData, err := os.ReadFile("testdata/testcharts/upgradetest/templates/configmap.yaml")
if err != nil {
t.Fatalf("Error loading template yaml %v", err)
Expand Down
22 changes: 4 additions & 18 deletions internal/test/ensure/ensure.go
Expand Up @@ -26,41 +26,27 @@ import (
)

// HelmHome sets up a Helm Home in a temp dir.
func HelmHome(t *testing.T) func() {
func HelmHome(t *testing.T) {
t.Helper()
base := TempDir(t)
base := t.TempDir()
os.Setenv(xdg.CacheHomeEnvVar, base)
os.Setenv(xdg.ConfigHomeEnvVar, base)
os.Setenv(xdg.DataHomeEnvVar, base)
os.Setenv(helmpath.CacheHomeEnvVar, "")
os.Setenv(helmpath.ConfigHomeEnvVar, "")
os.Setenv(helmpath.DataHomeEnvVar, "")
return func() {
os.RemoveAll(base)
}
}

// TempDir ensures a scratch test directory for unit testing purposes.
func TempDir(t *testing.T) string {
t.Helper()
d, err := os.MkdirTemp("", "helm")
if err != nil {
t.Fatal(err)
}
return d
}

// TempFile ensures a temp file for unit testing purposes.
//
// It returns the path to the directory (to which you will still need to join the filename)
//
// You must clean up the directory that is returned.
// The returned directory is automatically removed when the test and all its subtests complete.
//
// tempdir := TempFile(t, "foo", []byte("bar"))
// defer os.RemoveAll(tempdir)
// filename := filepath.Join(tempdir, "foo")
func TempFile(t *testing.T, name string, data []byte) string {
path := TempDir(t)
path := t.TempDir()
filename := filepath.Join(path, name)
if err := os.WriteFile(filename, data, 0755); err != nil {
t.Fatal(err)
Expand Down
5 changes: 1 addition & 4 deletions pkg/action/package_test.go
Expand Up @@ -29,7 +29,6 @@ import (
func TestPassphraseFileFetcher(t *testing.T) {
secret := "secret"
directory := ensure.TempFile(t, "passphrase-file", []byte(secret))
defer os.RemoveAll(directory)

fetcher, err := passphraseFileFetcher(path.Join(directory, "passphrase-file"), nil)
if err != nil {
Expand All @@ -49,7 +48,6 @@ func TestPassphraseFileFetcher(t *testing.T) {
func TestPassphraseFileFetcher_WithLineBreak(t *testing.T) {
secret := "secret"
directory := ensure.TempFile(t, "passphrase-file", []byte(secret+"\n\n."))
defer os.RemoveAll(directory)

fetcher, err := passphraseFileFetcher(path.Join(directory, "passphrase-file"), nil)
if err != nil {
Expand All @@ -67,8 +65,7 @@ func TestPassphraseFileFetcher_WithLineBreak(t *testing.T) {
}

func TestPassphraseFileFetcher_WithInvalidStdin(t *testing.T) {
directory := ensure.TempDir(t)
defer os.RemoveAll(directory)
directory := t.TempDir()

stdin, err := os.CreateTemp(directory, "non-existing")
if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions pkg/chartutil/save_test.go
Expand Up @@ -29,14 +29,12 @@ import (
"testing"
"time"

"helm.sh/helm/v3/internal/test/ensure"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
)

func TestSave(t *testing.T) {
tmp := ensure.TempDir(t)
defer os.RemoveAll(tmp)
tmp := t.TempDir()

for _, dest := range []string{tmp, filepath.Join(tmp, "newdir")} {
t.Run("outDir="+dest, func(t *testing.T) {
Expand Down
5 changes: 2 additions & 3 deletions pkg/downloader/chart_downloader_test.go
Expand Up @@ -269,10 +269,9 @@ func TestDownloadTo_TLS(t *testing.T) {
}

func TestDownloadTo_VerifyLater(t *testing.T) {
defer ensure.HelmHome(t)()
ensure.HelmHome(t)

dest := ensure.TempDir(t)
defer os.RemoveAll(dest)
dest := t.TempDir()

// Set up a fake repo
srv, err := repotest.NewTempServerWithCleanup(t, "testdata/*.tgz*")
Expand Down
5 changes: 1 addition & 4 deletions pkg/lint/rules/dependencies_test.go
Expand Up @@ -16,11 +16,9 @@ limitations under the License.
package rules

import (
"os"
"path/filepath"
"testing"

"helm.sh/helm/v3/internal/test/ensure"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/lint/support"
Expand Down Expand Up @@ -79,8 +77,7 @@ func TestValidateDependencyInMetadata(t *testing.T) {
}

func TestDependencies(t *testing.T) {
tmp := ensure.TempDir(t)
defer os.RemoveAll(tmp)
tmp := t.TempDir()

c := chartWithBadDependencies()
err := chartutil.SaveDir(&c, tmp)
Expand Down

0 comments on commit 2ceebff

Please sign in to comment.