Skip to content

Commit

Permalink
test: use t.TempDir to create temporary test directory
Browse files Browse the repository at this point in the history
This commit replaces `ioutil.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 `ioutil.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 <[email protected]>
  • Loading branch information
Juneezee committed Jan 24, 2023
1 parent 95a6b6d commit e2dda16
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 68 deletions.
26 changes: 3 additions & 23 deletions pkg/sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ func TestSyncFiles(t *testing.T) {
testComponentName := "test"

// create a temp dir for the file indexer
directory, e := ioutil.TempDir("", "")
if e != nil {
t.Errorf("TestSyncFiles error: error creating temporary directory for the indexer: %v", e)
}
directory := t.TempDir()

jsFile, e := os.Create(filepath.Join(directory, "red.js"))
if e != nil {
Expand Down Expand Up @@ -188,22 +185,14 @@ func TestSyncFiles(t *testing.T) {
if err != nil {
t.Errorf("TestSyncFiles error: error deleting the temp dir %s, err: %v", directory, err)
}
// Remove the temp dir created for the file indexer
err = os.RemoveAll(directory)
if err != nil {
t.Errorf("TestSyncFiles error: error deleting the temp dir %s, err: %v", directory, err)
}
}

func TestPushLocal(t *testing.T) {

testComponentName := "test"

// create a temp dir for the file indexer
directory, e := ioutil.TempDir("", "")
if e != nil {
t.Errorf("TestPushLocal error: error creating temporary directory for the indexer: %v", e)
}
directory := t.TempDir()

newFilePath := filepath.Join(directory, "foobar.txt")
if err := helper.CreateFileWithContent(newFilePath, "hello world"); err != nil {
Expand Down Expand Up @@ -322,12 +311,6 @@ func TestPushLocal(t *testing.T) {

})
}

// Remove the temp dir created for the file indexer
err := os.RemoveAll(directory)
if err != nil {
t.Errorf("TestPushLocal error: error deleting the temp dir %s", directory)
}
}

func TestUpdateIndexWithWatchChanges(t *testing.T) {
Expand Down Expand Up @@ -360,10 +343,7 @@ func TestUpdateIndexWithWatchChanges(t *testing.T) {
for _, tt := range tests {

// create a temp dir for the fake component
directory, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("TestUpdateIndexWithWatchChangesLocal error: error creating temporary directory for the indexer: %v", err)
}
directory := t.TempDir()

fileIndexPath, err := util.ResolveIndexFilePath(directory)
if err != nil {
Expand Down
10 changes: 2 additions & 8 deletions pkg/util/file_indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,7 @@ func mockDirectoryInfo(create bool, contextDir string, fs filesystem.Filesystem)
func TestCalculateFileDataKeyFromPath(t *testing.T) {

// create a temp dir for the fake component
directory, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("TestUpdateIndexWithWatchChangesLocal error: error creating temporary directory for the indexer: %v", err)
}
directory := t.TempDir()

tests := []struct {
absolutePath string
Expand Down Expand Up @@ -186,10 +183,7 @@ func TestCalculateFileDataKeyFromPath(t *testing.T) {
func TestGenerateNewFileDataEntry(t *testing.T) {

// create a temp dir for the fake component
directory, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("TestUpdateIndexWithWatchChangesLocal error: error creating temporary directory for the indexer: %v", err)
}
directory := t.TempDir()

tests := []struct {
testName string
Expand Down
46 changes: 9 additions & 37 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,11 +518,7 @@ func RemoveContentsFromDir(dir string) error {
}

func TestGetIgnoreRulesFromDirectory(t *testing.T) {
testDir, err := ioutil.TempDir(os.TempDir(), "odo-tests")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(testDir)
testDir := t.TempDir()
tests := []struct {
name string
directoryName string
Expand Down Expand Up @@ -1326,14 +1322,10 @@ func TestUnzip(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir, err := ioutil.TempDir("", "unzip")
if err != nil {
t.Errorf("Error creating temp dir: %s", err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()
t.Logf(dir)

_, err = Unzip(filepath.FromSlash(tt.src), dir, tt.pathToUnzip)
_, err := Unzip(filepath.FromSlash(tt.src), dir, tt.pathToUnzip)
if err != nil {
tt.expectedError = strings.ReplaceAll(tt.expectedError, "/", string(filepath.Separator))
if !strings.HasPrefix(err.Error(), tt.expectedError) {
Expand Down Expand Up @@ -1397,11 +1389,7 @@ func TestIsValidProjectDir(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "valid-project")
if err != nil {
t.Errorf("Error creating temp dir: %s", err)
}
defer os.RemoveAll(tmpDir)
tmpDir := t.TempDir()

for _, f := range tt.filesToCreate {
file := filepath.Join(tmpDir, f)
Expand All @@ -1417,7 +1405,7 @@ func TestIsValidProjectDir(t *testing.T) {
}
}

err = IsValidProjectDir(tmpDir, tt.devfilePath)
err := IsValidProjectDir(tmpDir, tt.devfilePath)
expectedError := tt.expectedError
if expectedError != "" {
expectedError = fmt.Sprintf(expectedError, tmpDir)
Expand Down Expand Up @@ -1573,10 +1561,7 @@ func TestValidateURL(t *testing.T) {

func TestValidateFile(t *testing.T) {
// Create temp dir and temp file
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Errorf("Failed to create temp dir: %s, error: %v", tempDir, err)
}
tempDir := t.TempDir()
tempFile, err := ioutil.TempFile(tempDir, "")
if err != nil {
t.Errorf("Failed to create temp file: %s, error: %v", tempFile.Name(), err)
Expand Down Expand Up @@ -1616,10 +1601,7 @@ func TestValidateFile(t *testing.T) {

func TestCopyFile(t *testing.T) {
// Create temp dir
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Errorf("Failed to create temp dir: %s, error: %v", tempDir, err)
}
tempDir := t.TempDir()

// Create temp file under temp dir as source file
tempFile, err := ioutil.TempFile(tempDir, "")
Expand Down Expand Up @@ -2410,12 +2392,7 @@ func TestGetCommandStringFromEnvs(t *testing.T) {
}

func TestGetGitOriginPath(t *testing.T) {
tempGitDirWithOrigin, err := ioutil.TempDir("", "")
if err != nil {
t.Errorf("unexpected error %v", err)
}

defer os.RemoveAll(tempGitDirWithOrigin)
tempGitDirWithOrigin := t.TempDir()

repoWithOrigin, err := git.PlainInit(tempGitDirWithOrigin, true)
if err != nil {
Expand All @@ -2430,12 +2407,7 @@ func TestGetGitOriginPath(t *testing.T) {
t.Errorf("unexpected error %v", err)
}

tempGitDirWithoutOrigin, err := ioutil.TempDir("", "")
if err != nil {
t.Errorf("unexpected error %v", err)
}

defer os.RemoveAll(tempGitDirWithoutOrigin)
tempGitDirWithoutOrigin := t.TempDir()

repoWithoutOrigin, err := git.PlainInit(tempGitDirWithoutOrigin, true)
if err != nil {
Expand Down

0 comments on commit e2dda16

Please sign in to comment.