Skip to content

Commit

Permalink
Merge pull request helm#10666 from Juneezee/test/t.TempDir
Browse files Browse the repository at this point in the history
test: use `T.TempDir` to create temporary test directory
  • Loading branch information
hickeyma authored Feb 17, 2022
2 parents bc2f1e0 + 2e3e22a commit 9bf3a14
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 193 deletions.
7 changes: 1 addition & 6 deletions cmd/helm/root_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package main
import (
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -50,11 +49,7 @@ func checkPermsStderr() (string, error) {
}

func TestCheckPerms(t *testing.T) {
tdir, err := ioutil.TempDir("", "helmtest")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tdir)
tdir := t.TempDir()
tfile := filepath.Join(tdir, "testconfig")
fh, err := os.OpenFile(tfile, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0440)
if err != nil {
Expand Down
8 changes: 2 additions & 6 deletions internal/fileutil/fileutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,14 @@ import (
)

func TestAtomicWriteFile(t *testing.T) {
dir, err := ioutil.TempDir("", "helm-tmp")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

testpath := filepath.Join(dir, "test")
stringContent := "Test content"
reader := bytes.NewReader([]byte(stringContent))
mode := os.FileMode(0644)

err = AtomicWriteFile(testpath, reader, mode)
err := AtomicWriteFile(testpath, reader, mode)
if err != nil {
t.Errorf("AtomicWriteFile error: %s", err)
}
Expand Down
99 changes: 24 additions & 75 deletions internal/third_party/dep/fs/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,9 @@ var (
)

func TestRenameWithFallback(t *testing.T) {
dir, err := ioutil.TempDir("", "helm-tmp")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

if err = RenameWithFallback(filepath.Join(dir, "does_not_exists"), filepath.Join(dir, "dst")); err == nil {
if err := RenameWithFallback(filepath.Join(dir, "does_not_exists"), filepath.Join(dir, "dst")); err == nil {
t.Fatal("expected an error for non existing file, but got nil")
}

Expand All @@ -64,31 +60,27 @@ func TestRenameWithFallback(t *testing.T) {
srcf.Close()
}

if err = RenameWithFallback(srcpath, filepath.Join(dir, "dst")); err != nil {
if err := RenameWithFallback(srcpath, filepath.Join(dir, "dst")); err != nil {
t.Fatal(err)
}

srcpath = filepath.Join(dir, "a")
if err = os.MkdirAll(srcpath, 0777); err != nil {
if err := os.MkdirAll(srcpath, 0777); err != nil {
t.Fatal(err)
}

dstpath := filepath.Join(dir, "b")
if err = os.MkdirAll(dstpath, 0777); err != nil {
if err := os.MkdirAll(dstpath, 0777); err != nil {
t.Fatal(err)
}

if err = RenameWithFallback(srcpath, dstpath); err == nil {
if err := RenameWithFallback(srcpath, dstpath); err == nil {
t.Fatal("expected an error if dst is an existing directory, but got nil")
}
}

func TestCopyDir(t *testing.T) {
dir, err := ioutil.TempDir("", "helm-tmp")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

srcdir := filepath.Join(dir, "src")
if err := os.MkdirAll(srcdir, 0755); err != nil {
Expand All @@ -108,7 +100,7 @@ func TestCopyDir(t *testing.T) {
for i, file := range files {
fn := filepath.Join(srcdir, file.path)
dn := filepath.Dir(fn)
if err = os.MkdirAll(dn, 0755); err != nil {
if err := os.MkdirAll(dn, 0755); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -189,14 +181,10 @@ func TestCopyDirFail_SrcInaccessible(t *testing.T) {
})
defer cleanup()

dir, err := ioutil.TempDir("", "helm-tmp")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

dstdir = filepath.Join(dir, "dst")
if err = CopyDir(srcdir, dstdir); err == nil {
if err := CopyDir(srcdir, dstdir); err == nil {
t.Fatalf("expected error for CopyDir(%s, %s), got none", srcdir, dstdir)
}
}
Expand All @@ -218,14 +206,10 @@ func TestCopyDirFail_DstInaccessible(t *testing.T) {

var srcdir, dstdir string

dir, err := ioutil.TempDir("", "helm-tmp")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

srcdir = filepath.Join(dir, "src")
if err = os.MkdirAll(srcdir, 0755); err != nil {
if err := os.MkdirAll(srcdir, 0755); err != nil {
t.Fatal(err)
}

Expand All @@ -242,12 +226,9 @@ func TestCopyDirFail_DstInaccessible(t *testing.T) {

func TestCopyDirFail_SrcIsNotDir(t *testing.T) {
var srcdir, dstdir string
var err error

dir, err := ioutil.TempDir("", "helm-tmp")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

srcdir = filepath.Join(dir, "src")
if _, err = os.Create(srcdir); err != nil {
Expand All @@ -268,12 +249,9 @@ func TestCopyDirFail_SrcIsNotDir(t *testing.T) {

func TestCopyDirFail_DstExists(t *testing.T) {
var srcdir, dstdir string
var err error

dir, err := ioutil.TempDir("", "helm-tmp")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

srcdir = filepath.Join(dir, "src")
if err = os.MkdirAll(srcdir, 0755); err != nil {
Expand Down Expand Up @@ -314,14 +292,10 @@ func TestCopyDirFailOpen(t *testing.T) {

var srcdir, dstdir string

dir, err := ioutil.TempDir("", "helm-tmp")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

srcdir = filepath.Join(dir, "src")
if err = os.MkdirAll(srcdir, 0755); err != nil {
if err := os.MkdirAll(srcdir, 0755); err != nil {
t.Fatal(err)
}

Expand All @@ -345,11 +319,7 @@ func TestCopyDirFailOpen(t *testing.T) {
}

func TestCopyFile(t *testing.T) {
dir, err := ioutil.TempDir("", "helm-tmp")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

srcf, err := os.Create(filepath.Join(dir, "srcfile"))
if err != nil {
Expand Down Expand Up @@ -405,13 +375,7 @@ func cleanUpDir(dir string) {
}

func TestCopyFileSymlink(t *testing.T) {
var tempdir, err = ioutil.TempDir("", "gotest")

if err != nil {
t.Fatalf("failed to create directory: %s", err)
}

defer cleanUpDir(tempdir)
tempdir := t.TempDir()

testcases := map[string]string{
filepath.Join("./testdata/symlinks/file-symlink"): filepath.Join(tempdir, "dst-file"),
Expand Down Expand Up @@ -477,11 +441,7 @@ func TestCopyFileFail(t *testing.T) {
t.Skip("Skipping for root user")
}

dir, err := ioutil.TempDir("", "helm-tmp")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

srcf, err := os.Create(filepath.Join(dir, "srcfile"))
if err != nil {
Expand Down Expand Up @@ -517,21 +477,14 @@ func TestCopyFileFail(t *testing.T) {
// files this function creates. It is the caller's responsibility to call
// this function before the test is done running, whether there's an error or not.
func setupInaccessibleDir(t *testing.T, op func(dir string) error) func() {
dir, err := ioutil.TempDir("", "helm-tmp")
if err != nil {
t.Fatal(err)
return nil // keep compiler happy
}
dir := t.TempDir()

subdir := filepath.Join(dir, "dir")

cleanup := func() {
if err := os.Chmod(subdir, 0777); err != nil {
t.Error(err)
}
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}

if err := os.Mkdir(subdir, 0777); err != nil {
Expand Down Expand Up @@ -617,14 +570,10 @@ func TestIsSymlink(t *testing.T) {
t.Skip("Skipping for root user")
}

dir, err := ioutil.TempDir("", "helm-tmp")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

dirPath := filepath.Join(dir, "directory")
if err = os.MkdirAll(dirPath, 0777); err != nil {
if err := os.MkdirAll(dirPath, 0777); err != nil {
t.Fatal(err)
}

Expand Down
8 changes: 0 additions & 8 deletions pkg/action/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package action
import (
"flag"
"io/ioutil"
"os"
"testing"

fakeclientset "k8s.io/client-go/kubernetes/fake"
Expand All @@ -38,13 +37,6 @@ var verbose = flag.Bool("test.log", false, "enable test logging")
func actionConfigFixture(t *testing.T) *Configuration {
t.Helper()

tdir, err := ioutil.TempDir("", "helm-action-test")
if err != nil {
t.Fatal(err)
}

t.Cleanup(func() { os.RemoveAll(tdir) })

registryClient, err := registry.NewClient()
if err != nil {
t.Fatal(err)
Expand Down
15 changes: 3 additions & 12 deletions pkg/action/dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package action

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -68,11 +67,7 @@ func TestList(t *testing.T) {
// chart names do not cause resolution problems.
func TestDependencyStatus_Dashes(t *testing.T) {
// Make a temp dir
dir, err := ioutil.TempDir("", "helmtest-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

chartpath := filepath.Join(dir, "charts")
if err := os.MkdirAll(chartpath, 0700); err != nil {
Expand All @@ -81,7 +76,7 @@ func TestDependencyStatus_Dashes(t *testing.T) {

// Add some fake charts
first := buildChart(withName("first-chart"))
_, err = chartutil.Save(first, chartpath)
_, err := chartutil.Save(first, chartpath)
if err != nil {
t.Fatal(err)
}
Expand All @@ -106,11 +101,7 @@ func TestDependencyStatus_Dashes(t *testing.T) {

func TestStatArchiveForStatus(t *testing.T) {
// Make a temp dir
dir, err := ioutil.TempDir("", "helmtest-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

chartpath := filepath.Join(dir, "charts")
if err := os.MkdirAll(chartpath, 0700); err != nil {
Expand Down
17 changes: 4 additions & 13 deletions pkg/action/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -552,15 +551,11 @@ func TestInstallReleaseOutputDir(t *testing.T) {
instAction := installAction(t)
vals := map[string]interface{}{}

dir, err := ioutil.TempDir("", "output-dir")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

instAction.OutputDir = dir

_, err = instAction.Run(buildChart(withSampleTemplates(), withMultipleManifestTemplate()), vals)
_, err := instAction.Run(buildChart(withSampleTemplates(), withMultipleManifestTemplate()), vals)
if err != nil {
t.Fatalf("Failed install: %s", err)
}
Expand Down Expand Up @@ -588,19 +583,15 @@ func TestInstallOutputDirWithReleaseName(t *testing.T) {
instAction := installAction(t)
vals := map[string]interface{}{}

dir, err := ioutil.TempDir("", "output-dir")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

instAction.OutputDir = dir
instAction.UseReleaseName = true
instAction.ReleaseName = "madra"

newDir := filepath.Join(dir, instAction.ReleaseName)

_, err = instAction.Run(buildChart(withSampleTemplates(), withMultipleManifestTemplate()), vals)
_, err := instAction.Run(buildChart(withSampleTemplates(), withMultipleManifestTemplate()), vals)
if err != nil {
t.Fatalf("Failed install: %s", err)
}
Expand Down
Loading

0 comments on commit 9bf3a14

Please sign in to comment.