Skip to content

Commit

Permalink
Create tempdir in test using t.TempDir()
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed May 18, 2024
1 parent 6145be2 commit 231ed05
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 56 deletions.
3 changes: 1 addition & 2 deletions app/preferences_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
69 changes: 15 additions & 54 deletions internal/repository/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 231ed05

Please sign in to comment.