From 231ed055575e0669f73a884dbf1f5fa0234883e0 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 18 May 2024 23:14:19 +0200 Subject: [PATCH] Create tempdir in test using t.TempDir() --- app/preferences_test.go | 3 +- internal/repository/file_test.go | 69 +++++++------------------------- 2 files changed, 16 insertions(+), 56 deletions(-) diff --git a/app/preferences_test.go b/app/preferences_test.go index 9f0de61e2c..f30df7021f 100644 --- a/app/preferences_test.go +++ b/app/preferences_test.go @@ -72,8 +72,7 @@ func TestPreferences_Save_OverwriteFast(t *testing.T) { val["key"] = "value" }) - path := filepath.Join(os.TempDir(), "fynePrefs2.json") - defer os.Remove(path) + path := filepath.Join(t.TempDir(), "fynePrefs2.json") p.saveToFile(path) p.WriteValues(func(val map[string]any) { diff --git a/internal/repository/file_test.go b/internal/repository/file_test.go index b9fbedea2a..653406b425 100644 --- a/internal/repository/file_test.go +++ b/internal/repository/file_test.go @@ -41,16 +41,12 @@ func TestFileRepositoryRegistration(t *testing.T) { } func TestFileRepositoryExists(t *testing.T) { - dir, err := os.MkdirTemp("", "FyneInternalRepositoryFileTest") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) + dir := t.TempDir() existsPath := path.Join(dir, "exists") notExistsPath := path.Join(dir, "notExists") - err = os.WriteFile(existsPath, []byte{1, 2, 3, 4}, 0755) + err := os.WriteFile(existsPath, []byte{1, 2, 3, 4}, 0755) if err != nil { t.Fatal(err) } @@ -65,18 +61,13 @@ func TestFileRepositoryExists(t *testing.T) { } func TestFileRepositoryReader(t *testing.T) { - // Set up a temporary directory. - dir, err := os.MkdirTemp("", "FyneInternalRepositoryFileTest") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) + dir := t.TempDir() // Create some files to test with. fooPath := path.Join(dir, "foo") barPath := path.Join(dir, "bar") bazPath := path.Join(dir, "baz") - err = os.WriteFile(fooPath, []byte{}, 0755) + err := os.WriteFile(fooPath, []byte{}, 0755) if err != nil { t.Fatal(err) } @@ -127,19 +118,14 @@ func TestFileRepositoryReader(t *testing.T) { } func TestFileRepositoryWriter(t *testing.T) { - // Set up a temporary directory. - dir, err := os.MkdirTemp("", "FyneInternalRepositoryFileTest") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) + dir := t.TempDir() // Create some files to test with. fooPath := path.Join(dir, "foo") barPath := path.Join(dir, "bar") bazPath := path.Join(dir, "baz") spamHamPath := path.Join(dir, "spam", "ham") - err = os.WriteFile(fooPath, []byte{}, 0755) + err := os.WriteFile(fooPath, []byte{}, 0755) if err != nil { t.Fatal(err) } @@ -247,18 +233,13 @@ func TestFileRepositoryWriter(t *testing.T) { } func TestFileRepositoryCanWrite(t *testing.T) { - // Set up a temporary directory. - dir, err := os.MkdirTemp("", "FyneInternalRepositoryFileTest") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) + dir := t.TempDir() // Create some files to test with. fooPath := path.Join(dir, "foo") barPath := path.Join(dir, "bar") bazPath := path.Join(dir, "baz") - err = os.WriteFile(fooPath, []byte{}, 0755) + err := os.WriteFile(fooPath, []byte{}, 0755) if err != nil { t.Fatal(err) } @@ -365,17 +346,12 @@ func TestFileRepositoryChild(t *testing.T) { } func TestFileRepositoryCopy(t *testing.T) { - // Set up a temporary directory. - dir, err := os.MkdirTemp("", "FyneInternalRepositoryFileTest") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) + dir := t.TempDir() // Create some files to test with. fooPath := path.Join(dir, "foo") barPath := path.Join(dir, "bar") - err = os.WriteFile(fooPath, []byte{1, 2, 3, 4, 5}, 0755) + err := os.WriteFile(fooPath, []byte{1, 2, 3, 4, 5}, 0755) if err != nil { t.Fatal(err) } @@ -396,17 +372,12 @@ func TestFileRepositoryCopy(t *testing.T) { } func TestFileRepositoryMove(t *testing.T) { - // Set up a temporary directory. - dir, err := os.MkdirTemp("", "FyneInternalRepositoryFileTest") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) + dir := t.TempDir() // Create some files to test with. fooPath := path.Join(dir, "foo") barPath := path.Join(dir, "bar") - err = os.WriteFile(fooPath, []byte{1, 2, 3, 4, 5}, 0755) + err := os.WriteFile(fooPath, []byte{1, 2, 3, 4, 5}, 0755) if err != nil { t.Fatal(err) } @@ -429,12 +400,7 @@ func TestFileRepositoryMove(t *testing.T) { } func TestFileRepositoryListing(t *testing.T) { - // Set up a temporary directory. - dir, err := os.MkdirTemp("", "FyneInternalRepositoryFileTest") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) + dir := t.TempDir() // Create some files to tests with. fooPath := path.Join(dir, "foo") @@ -466,12 +432,7 @@ func TestFileRepositoryListing(t *testing.T) { } func TestFileRepositoryCreateListable(t *testing.T) { - // Set up a temporary directory. - dir, err := os.MkdirTemp("", "FyneInternalRepositoryFileTest") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) + dir := t.TempDir() f := NewFileRepository() repository.Register("file", f) @@ -482,7 +443,7 @@ func TestFileRepositoryCreateListable(t *testing.T) { fooBar := storage.NewFileURI(fooBarPath) // Creating a dir with no parent should fail - err = storage.CreateListable(fooBar) + err := storage.CreateListable(fooBar) assert.NotNil(t, err) // Creating foo should work though