Skip to content

Commit 0f1d226

Browse files
authored
test: use t.TempDir to create temporary test directory (#6533)
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]> Signed-off-by: Eng Zer Jun <[email protected]>
1 parent 95a6b6d commit 0f1d226

File tree

3 files changed

+14
-68
lines changed

3 files changed

+14
-68
lines changed

pkg/sync/sync_test.go

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,7 @@ func TestSyncFiles(t *testing.T) {
8585
testComponentName := "test"
8686

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

9390
jsFile, e := os.Create(filepath.Join(directory, "red.js"))
9491
if e != nil {
@@ -188,22 +185,14 @@ func TestSyncFiles(t *testing.T) {
188185
if err != nil {
189186
t.Errorf("TestSyncFiles error: error deleting the temp dir %s, err: %v", directory, err)
190187
}
191-
// Remove the temp dir created for the file indexer
192-
err = os.RemoveAll(directory)
193-
if err != nil {
194-
t.Errorf("TestSyncFiles error: error deleting the temp dir %s, err: %v", directory, err)
195-
}
196188
}
197189

198190
func TestPushLocal(t *testing.T) {
199191

200192
testComponentName := "test"
201193

202194
// create a temp dir for the file indexer
203-
directory, e := ioutil.TempDir("", "")
204-
if e != nil {
205-
t.Errorf("TestPushLocal error: error creating temporary directory for the indexer: %v", e)
206-
}
195+
directory := t.TempDir()
207196

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

323312
})
324313
}
325-
326-
// Remove the temp dir created for the file indexer
327-
err := os.RemoveAll(directory)
328-
if err != nil {
329-
t.Errorf("TestPushLocal error: error deleting the temp dir %s", directory)
330-
}
331314
}
332315

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

362345
// create a temp dir for the fake component
363-
directory, err := ioutil.TempDir("", "")
364-
if err != nil {
365-
t.Fatalf("TestUpdateIndexWithWatchChangesLocal error: error creating temporary directory for the indexer: %v", err)
366-
}
346+
directory := t.TempDir()
367347

368348
fileIndexPath, err := util.ResolveIndexFilePath(directory)
369349
if err != nil {

pkg/util/file_indexer_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,7 @@ func mockDirectoryInfo(create bool, contextDir string, fs filesystem.Filesystem)
140140
func TestCalculateFileDataKeyFromPath(t *testing.T) {
141141

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

148145
tests := []struct {
149146
absolutePath string
@@ -186,10 +183,7 @@ func TestCalculateFileDataKeyFromPath(t *testing.T) {
186183
func TestGenerateNewFileDataEntry(t *testing.T) {
187184

188185
// create a temp dir for the fake component
189-
directory, err := ioutil.TempDir("", "")
190-
if err != nil {
191-
t.Fatalf("TestUpdateIndexWithWatchChangesLocal error: error creating temporary directory for the indexer: %v", err)
192-
}
186+
directory := t.TempDir()
193187

194188
tests := []struct {
195189
testName string

pkg/util/util_test.go

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -518,11 +518,7 @@ func RemoveContentsFromDir(dir string) error {
518518
}
519519

520520
func TestGetIgnoreRulesFromDirectory(t *testing.T) {
521-
testDir, err := ioutil.TempDir(os.TempDir(), "odo-tests")
522-
if err != nil {
523-
t.Fatal(err)
524-
}
525-
defer os.RemoveAll(testDir)
521+
testDir := t.TempDir()
526522
tests := []struct {
527523
name string
528524
directoryName string
@@ -1326,14 +1322,10 @@ func TestUnzip(t *testing.T) {
13261322

13271323
for _, tt := range tests {
13281324
t.Run(tt.name, func(t *testing.T) {
1329-
dir, err := ioutil.TempDir("", "unzip")
1330-
if err != nil {
1331-
t.Errorf("Error creating temp dir: %s", err)
1332-
}
1333-
defer os.RemoveAll(dir)
1325+
dir := t.TempDir()
13341326
t.Logf(dir)
13351327

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

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

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

1420-
err = IsValidProjectDir(tmpDir, tt.devfilePath)
1408+
err := IsValidProjectDir(tmpDir, tt.devfilePath)
14211409
expectedError := tt.expectedError
14221410
if expectedError != "" {
14231411
expectedError = fmt.Sprintf(expectedError, tmpDir)
@@ -1573,10 +1561,7 @@ func TestValidateURL(t *testing.T) {
15731561

15741562
func TestValidateFile(t *testing.T) {
15751563
// Create temp dir and temp file
1576-
tempDir, err := ioutil.TempDir("", "")
1577-
if err != nil {
1578-
t.Errorf("Failed to create temp dir: %s, error: %v", tempDir, err)
1579-
}
1564+
tempDir := t.TempDir()
15801565
tempFile, err := ioutil.TempFile(tempDir, "")
15811566
if err != nil {
15821567
t.Errorf("Failed to create temp file: %s, error: %v", tempFile.Name(), err)
@@ -1616,10 +1601,7 @@ func TestValidateFile(t *testing.T) {
16161601

16171602
func TestCopyFile(t *testing.T) {
16181603
// Create temp dir
1619-
tempDir, err := ioutil.TempDir("", "")
1620-
if err != nil {
1621-
t.Errorf("Failed to create temp dir: %s, error: %v", tempDir, err)
1622-
}
1604+
tempDir := t.TempDir()
16231605

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

24122394
func TestGetGitOriginPath(t *testing.T) {
2413-
tempGitDirWithOrigin, err := ioutil.TempDir("", "")
2414-
if err != nil {
2415-
t.Errorf("unexpected error %v", err)
2416-
}
2417-
2418-
defer os.RemoveAll(tempGitDirWithOrigin)
2395+
tempGitDirWithOrigin := t.TempDir()
24192396

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

2433-
tempGitDirWithoutOrigin, err := ioutil.TempDir("", "")
2434-
if err != nil {
2435-
t.Errorf("unexpected error %v", err)
2436-
}
2437-
2438-
defer os.RemoveAll(tempGitDirWithoutOrigin)
2410+
tempGitDirWithoutOrigin := t.TempDir()
24392411

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

0 commit comments

Comments
 (0)