Skip to content

Commit

Permalink
test: use T.TempDir to create temporary test directory (#2710)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Juneezee authored Aug 11, 2022
1 parent 68546d4 commit c6c917f
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 189 deletions.
6 changes: 1 addition & 5 deletions cmd/kapacitord/run/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ import (
)

func TestCommand_PIDFile(t *testing.T) {
tmpdir, err := ioutil.TempDir(os.TempDir(), "kapacitord-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
tmpdir := t.TempDir()

// Write out a config file that does not attempt to connect to influxdb.
configFile := filepath.Join(tmpdir, "kapacitor.conf")
Expand Down
6 changes: 1 addition & 5 deletions integrations/streamer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10602,11 +10602,7 @@ stream
}

func TestStream_AlertLog(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "TestStream_AlertLog")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
tmpDir := t.TempDir()
normalPath := filepath.Join(tmpDir, "normal.log")
modePath := filepath.Join(tmpDir, "mode.log")

Expand Down
26 changes: 9 additions & 17 deletions server/server_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ func (s *Server) Restart() {
}

// OpenServer opens a test server.
func OpenDefaultServer() (*Server, *client.Client) {
c := NewConfig()
func OpenDefaultServer(t *testing.T) (*Server, *client.Client) {
c := NewConfig(t)
s := OpenServer(c)
client := Client(s)
return s, client
}

func OpenLoadServer() (*Server, *server.Config, *client.Client) {
c := NewConfig()
func OpenLoadServer(t *testing.T) (*Server, *server.Config, *client.Client) {
c := NewConfig(t)
if err := copyFiles("testdata/load", c.Load.Dir); err != nil {
panic(err)
}
Expand Down Expand Up @@ -260,31 +260,23 @@ func (s *Server) Stats() (stats, error) {
}

// NewConfig returns the default config with temporary paths.
func NewConfig() *server.Config {
func NewConfig(t *testing.T) *server.Config {
c := server.NewConfig()
c.MQTT = mqtt.Configs{}
c.Slack = slack.Configs{slack.NewConfig()}
c.Reporting.Enabled = false
c.Replay.Dir = MustTempDir()
c.Storage.BoltDBPath = filepath.Join(MustTempDir(), "bolt.db")
c.DataDir = MustTempDir()
c.Replay.Dir = t.TempDir()
c.Storage.BoltDBPath = filepath.Join(t.TempDir(), "bolt.db")
c.DataDir = t.TempDir()
c.HTTP.BindAddress = "127.0.0.1:0"
//c.HTTP.BindAddress = "127.0.0.1:9092"
//c.HTTP.GZIP = false
c.InfluxDB[0].Enabled = false
c.Load.Enabled = true
c.Load.Dir = MustTempDir()
c.Load.Dir = t.TempDir()
return c
}

func MustTempDir() string {
d, err := ioutil.TempDir("", "kapacitord-")
if err != nil {
panic(err)
}
return d
}

func configureLogging() {
if testing.Verbose() {
wlog.SetLevel(wlog.DEBUG)
Expand Down
Loading

0 comments on commit c6c917f

Please sign in to comment.