Skip to content

Commit

Permalink
test: use t.tmpdir to create temp directory in tests (#5508)
Browse files Browse the repository at this point in the history
* refactor: use t.tmpdir to create temp directory in tests

replace manual mkdir+remove+error-handling with t.tmpdir

* lint: fix linter issues
  • Loading branch information
kruskall authored Sep 11, 2024
1 parent dc3ee01 commit eb0f130
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 73 deletions.
4 changes: 1 addition & 3 deletions internal/pkg/agent/application/filelock/locker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package filelock

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -15,8 +14,7 @@ import (
const testLockFile = "test.lock"

func TestAppLocker(t *testing.T) {
tmp, _ := os.MkdirTemp("", "locker")
defer os.RemoveAll(tmp)
tmp := t.TempDir()

locker1 := NewAppLocker(tmp, testLockFile)
locker2 := NewAppLocker(tmp, testLockFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"sync"
"testing"
Expand Down Expand Up @@ -498,19 +497,14 @@ func runFleetGateway(ctx context.Context, g coordinator.FleetGateway) <-chan err
}

func newStateStore(t *testing.T, log *logger.Logger) *store.StateStore {
dir, err := os.MkdirTemp("", "fleet-gateway-unit-test")
require.NoError(t, err)
dir := t.TempDir()

filename := filepath.Join(dir, "state.enc")
diskStore, err := storage.NewDiskStore(filename)
require.NoError(t, err)
stateStore, err := store.NewStateStore(log, diskStore)
require.NoError(t, err)

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

return stateStore
}

Expand Down
3 changes: 0 additions & 3 deletions internal/pkg/agent/application/secret/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package secret

import (
"context"
"os"
"path/filepath"
"testing"
"time"
Expand Down Expand Up @@ -67,6 +66,4 @@ func TestCreate(t *testing.T) {
t.Fatal(err)
}
}

os.RemoveAll(filepath.Dir(getTestVaultPath(t)))
}
28 changes: 9 additions & 19 deletions internal/pkg/agent/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ func TestReplaceOrRollbackStore(t *testing.T) {
failure := NewHandlerStore(func(_ io.Reader) error { return errors.New("fail") })

t.Run("when the save is successful with target and source don't match", func(t *testing.T) {
target, err := genFile(oldContent)
target, err := genFile(t, oldContent)
require.NoError(t, err)
dir := filepath.Dir(target)
defer os.RemoveAll(dir)

requireFilesCount(t, dir, 1)

Expand All @@ -53,10 +52,9 @@ func TestReplaceOrRollbackStore(t *testing.T) {
})

t.Run("when save is not successful", func(t *testing.T) {
target, err := genFile(oldContent)
target, err := genFile(t, oldContent)
require.NoError(t, err)
dir := filepath.Dir(target)
defer os.RemoveAll(dir)

requireFilesCount(t, dir, 1)

Expand All @@ -77,10 +75,9 @@ func TestReplaceOrRollbackStore(t *testing.T) {
})

t.Run("when save is successful with target and source content match", func(t *testing.T) {
target, err := genFile(replaceWith)
target, err := genFile(t, replaceWith)
require.NoError(t, err)
dir := filepath.Dir(target)
defer os.RemoveAll(dir)

requireFilesCount(t, dir, 1)

Expand All @@ -104,11 +101,10 @@ func TestReplaceOrRollbackStore(t *testing.T) {
t.Run("when replace is skipped due to target already containing source content", func(t *testing.T) {
yamlTarget := []byte("fleet:\n enabled: true\nother: value\n")
yamlReplaceWith := []byte("#This comment is left out\nfleet:\n enabled: true\n")
target, err := genFile(yamlTarget)
target, err := genFile(t, yamlTarget)

require.NoError(t, err)
dir := filepath.Dir(target)
defer os.RemoveAll(dir)

requireFilesCount(t, dir, 1)

Expand Down Expand Up @@ -141,9 +137,8 @@ func TestReplaceOrRollbackStore(t *testing.T) {

func TestDiskStore(t *testing.T) {
t.Run("when the target file already exists", func(t *testing.T) {
target, err := genFile([]byte("hello world"))
target, err := genFile(t, []byte("hello world"))
require.NoError(t, err)
defer os.Remove(target)
d, err := NewDiskStore(target)
require.NoError(t, err)

Expand All @@ -159,9 +154,7 @@ func TestDiskStore(t *testing.T) {
})

t.Run("when the target do no exist", func(t *testing.T) {
dir, err := os.MkdirTemp("", "configs")
require.NoError(t, err)
defer os.Remove(dir)
dir := t.TempDir()

target := filepath.Join(dir, "hello.txt")
d, err := NewDiskStore(target)
Expand All @@ -180,7 +173,7 @@ func TestDiskStore(t *testing.T) {

t.Run("return an io.ReadCloser to the target file", func(t *testing.T) {
msg := []byte("bonjour la famille")
target, err := genFile(msg)
target, err := genFile(t, msg)
require.NoError(t, err)

d, err := NewDiskStore(target)
Expand All @@ -197,11 +190,8 @@ func TestDiskStore(t *testing.T) {
})
}

func genFile(b []byte) (string, error) {
dir, err := os.MkdirTemp("", "configs")
if err != nil {
return "", err
}
func genFile(t *testing.T, b []byte) (string, error) {
dir := t.TempDir()

f, err := os.CreateTemp(dir, "config-")
if err != nil {
Expand Down
8 changes: 2 additions & 6 deletions internal/pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ func TestInputsResolveNOOP(t *testing.T) {
},
}

tmp, err := os.MkdirTemp("", "config")
require.NoError(t, err)
defer os.RemoveAll(tmp)
tmp := t.TempDir()

cfgPath := filepath.Join(tmp, "config.yml")
dumpToYAML(t, cfgPath, contents)
Expand Down Expand Up @@ -95,9 +93,7 @@ func TestCommaParsing(t *testing.T) {
}

func testLoadFiles(t *testing.T) {
tmp, err := os.MkdirTemp("", "watch")
require.NoError(t, err)
defer os.RemoveAll(tmp)
tmp := t.TempDir()

f1 := filepath.Join(tmp, "1.yml")
dumpToYAML(t, f1, map[string]interface{}{
Expand Down
3 changes: 1 addition & 2 deletions internal/pkg/dir/discover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ func TestDiscover(t *testing.T) {

func withFiles(files []string, fn func(dst string, t *testing.T)) func(t *testing.T) {
return func(t *testing.T) {
tmp, _ := os.MkdirTemp("", "watch")
defer os.RemoveAll(tmp)
tmp := t.TempDir()

for _, file := range files {
path := filepath.Join(tmp, file)
Expand Down
36 changes: 12 additions & 24 deletions internal/pkg/filewatcher/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ func TestWatch(t *testing.T) {
}))

t.Run("newly added files are discovered", withWatch(func(t *testing.T, w *Watch) {
tmp, err := os.MkdirTemp("", "watch")
require.NoError(t, err)
defer os.RemoveAll(tmp)
tmp := t.TempDir()

path := filepath.Join(tmp, "hello.txt")
empty, err := os.Create(path)
Expand All @@ -42,9 +40,7 @@ func TestWatch(t *testing.T) {
}))

t.Run("ignore old files", withWatch(func(t *testing.T, w *Watch) {
tmp, err := os.MkdirTemp("", "watch")
require.NoError(t, err)
defer os.RemoveAll(tmp)
tmp := t.TempDir()

path := filepath.Join(tmp, "hello.txt")
empty, err := os.Create(path)
Expand All @@ -67,9 +63,7 @@ func TestWatch(t *testing.T) {
}))

t.Run("can unwatch a watched file", withWatch(func(t *testing.T, w *Watch) {
tmp, err := os.MkdirTemp("", "watch")
require.NoError(t, err)
defer os.RemoveAll(tmp)
tmp := t.TempDir()

path := filepath.Join(tmp, "hello.txt")
empty, err := os.Create(path)
Expand All @@ -96,7 +90,7 @@ func TestWatch(t *testing.T) {
w.Unwatch(path)

// Add new content to the file.
os.WriteFile(path, []byte("heeeelo"), 0644)
assert.NoError(t, os.WriteFile(path, []byte("heeeelo"), 0644))

// Should not find the file.
r, u, err = w.scan()
Expand All @@ -106,9 +100,7 @@ func TestWatch(t *testing.T) {
}))

t.Run("can returns the list of watched files", withWatch(func(t *testing.T, w *Watch) {
tmp, err := os.MkdirTemp("", "watch")
require.NoError(t, err)
defer os.RemoveAll(tmp)
tmp := t.TempDir()

path := filepath.Join(tmp, "hello.txt")
empty, err := os.Create(path)
Expand All @@ -124,9 +116,7 @@ func TestWatch(t *testing.T) {
}))

t.Run("update returns updated, unchanged and watched files", withWatch(func(t *testing.T, w *Watch) {
tmp, err := os.MkdirTemp("", "watch")
require.NoError(t, err)
defer os.RemoveAll(tmp)
tmp := t.TempDir()

path1 := filepath.Join(tmp, "hello-1.txt")
empty, err := os.Create(path1)
Expand All @@ -151,7 +141,8 @@ func TestWatch(t *testing.T) {
w.Watch(path3)

// Set initial state
w.Update()
_, err = w.Update()
require.NoError(t, err)

// Reset watched files.
w.Reset()
Expand All @@ -165,7 +156,8 @@ func TestWatch(t *testing.T) {
// Add new content to the file.
f, err := os.OpenFile(path3, os.O_APPEND|os.O_WRONLY, 0600)
require.NoError(t, err)
f.Write([]byte("more-hello"))
_, err = f.Write([]byte("more-hello"))
require.NoError(t, err)
require.NoError(t, f.Sync())
f.Close()

Expand All @@ -183,9 +175,7 @@ func TestWatch(t *testing.T) {
}))

t.Run("should cleanup files that disapear", withWatch(func(t *testing.T, w *Watch) {
tmp, err := os.MkdirTemp("", "watch")
require.NoError(t, err)
defer os.RemoveAll(tmp)
tmp := t.TempDir()

path1 := filepath.Join(tmp, "hello.txt")
empty, err := os.Create(path1)
Expand All @@ -200,9 +190,7 @@ func TestWatch(t *testing.T) {
}))

t.Run("should allow to invalidate the cache ", withWatch(func(t *testing.T, w *Watch) {
tmp, err := os.MkdirTemp("", "watch")
require.NoError(t, err)
defer os.RemoveAll(tmp)
tmp := t.TempDir()

path1 := filepath.Join(tmp, "hello.txt")
empty, err := os.Create(path1)
Expand Down
7 changes: 1 addition & 6 deletions pkg/component/runtime/conn_info_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"io"
"net"
"net/url"
"os"
"runtime"
"syscall"
"testing"
Expand Down Expand Up @@ -92,11 +91,7 @@ func getAddress(dir string, isLocal bool) string {
}

func runTests(t *testing.T, fn func(*testing.T, string)) {
sockdir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(sockdir)
sockdir := t.TempDir()

tests := []struct {
name string
Expand Down
4 changes: 1 addition & 3 deletions pkg/packer/packer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ func TestPacker(t *testing.T) {

withFiles := func(test tt, fn func(pattern []string, t *testing.T)) func(t *testing.T) {
return func(t *testing.T) {
d, err := os.MkdirTemp("", "packer")
require.NoError(t, err)
defer os.RemoveAll(d)
d := t.TempDir()

for f, v := range test.content {
path := filepath.Join(d, f)
Expand Down

0 comments on commit eb0f130

Please sign in to comment.