mirror of
https://github.com/pomerium/pomerium.git
synced 2025-07-15 15:56:38 +02:00
test: use T.TempDir
to create temporary test directory (#3725)
Prior to this commit, temporary directories in tests were created using `filepath.Join` and `os.MkdirAll`. This commit replaces `os.MkdirAll` with `t.TempDir` in tests. The directory created by `t.TempDir` is automatically removed when the test and all its subtests complete. Reference: https://pkg.go.dev/testing#T.TempDir Signed-off-by: Eng Zer Jun <engzerjun@gmail.com> Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
parent
e1f881f82b
commit
45ce6f693a
5 changed files with 8 additions and 43 deletions
|
@ -8,20 +8,15 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFileWatcherSource(t *testing.T) {
|
func TestFileWatcherSource(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
tmpdir := filepath.Join(os.TempDir(), uuid.New().String())
|
tmpdir := t.TempDir()
|
||||||
err := os.MkdirAll(tmpdir, 0o755)
|
|
||||||
if !assert.NoError(t, err) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = os.WriteFile(filepath.Join(tmpdir, "example.txt"), []byte{1, 2, 3, 4}, 0o600)
|
err := os.WriteFile(filepath.Join(tmpdir, "example.txt"), []byte{1, 2, 3, 4}, 0o600)
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,19 +6,11 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
envoy_config_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
|
envoy_config_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test(t *testing.T) {
|
func Test(t *testing.T) {
|
||||||
dir := filepath.Join(os.TempDir(), uuid.New().String())
|
dir := t.TempDir()
|
||||||
err := os.MkdirAll(dir, 0o755)
|
|
||||||
if !assert.NoError(t, err) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
_ = os.RemoveAll(dir)
|
|
||||||
}()
|
|
||||||
|
|
||||||
t.Run("bytes", func(t *testing.T) {
|
t.Run("bytes", func(t *testing.T) {
|
||||||
mgr := NewManager(WithCacheDir(dir))
|
mgr := NewManager(WithCacheDir(dir))
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package databroker
|
package databroker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/pomerium/pomerium/config"
|
"github.com/pomerium/pomerium/config"
|
||||||
|
@ -11,12 +9,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNew(t *testing.T) {
|
func TestNew(t *testing.T) {
|
||||||
dir, err := os.MkdirTemp("", "example")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(dir)
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
opts config.Options
|
opts config.Options
|
||||||
|
|
|
@ -18,7 +18,6 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -27,7 +26,6 @@ import (
|
||||||
"github.com/go-chi/chi/v5/middleware"
|
"github.com/go-chi/chi/v5/middleware"
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
"github.com/google/go-cmp/cmp/cmpopts"
|
"github.com/google/go-cmp/cmp/cmpopts"
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/mholt/acmez/acme"
|
"github.com/mholt/acmez/acme"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
@ -217,9 +215,7 @@ func TestConfig(t *testing.T) {
|
||||||
|
|
||||||
mockACME = newMockACME(ca, srv)
|
mockACME = newMockACME(ca, srv)
|
||||||
|
|
||||||
tmpdir := filepath.Join(os.TempDir(), uuid.New().String())
|
tmpdir := t.TempDir()
|
||||||
_ = os.MkdirAll(tmpdir, 0o755)
|
|
||||||
defer os.RemoveAll(tmpdir)
|
|
||||||
|
|
||||||
li, err := net.Listen("tcp", "127.0.0.1:0")
|
li, err := net.Listen("tcp", "127.0.0.1:0")
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
|
|
|
@ -6,18 +6,13 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestWatcher(t *testing.T) {
|
func TestWatcher(t *testing.T) {
|
||||||
tmpdir := filepath.Join(os.TempDir(), uuid.New().String())
|
tmpdir := t.TempDir()
|
||||||
err := os.MkdirAll(tmpdir, 0o755)
|
|
||||||
if !assert.NoError(t, err) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = os.WriteFile(filepath.Join(tmpdir, "test1.txt"), []byte{1, 2, 3, 4}, 0o666)
|
err := os.WriteFile(filepath.Join(tmpdir, "test1.txt"), []byte{1, 2, 3, 4}, 0o666)
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -44,14 +39,9 @@ func TestWatcher(t *testing.T) {
|
||||||
func TestWatcherSymlink(t *testing.T) {
|
func TestWatcherSymlink(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
tmpdir := filepath.Join(os.TempDir(), uuid.New().String())
|
tmpdir := t.TempDir()
|
||||||
err := os.MkdirAll(tmpdir, 0o755)
|
|
||||||
if !assert.NoError(t, err) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { os.RemoveAll(tmpdir) })
|
|
||||||
|
|
||||||
err = os.WriteFile(filepath.Join(tmpdir, "test1.txt"), []byte{1, 2, 3, 4}, 0o666)
|
err := os.WriteFile(filepath.Join(tmpdir, "test1.txt"), []byte{1, 2, 3, 4}, 0o666)
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue