From 53751617774cdcd473c1b3f7ed28afeb8c10f625 Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Wed, 4 May 2022 11:35:50 +0800 Subject: [PATCH] test: use `T.TempDir` to create temporary test directory (#16758) 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. Fixes: #16759 Reference: https://pkg.go.dev/testing#T.TempDir Signed-off-by: Eng Zer Jun --- internal/kv/etcd/embed_etcd_kv_test.go | 9 +-------- internal/log/log_test.go | 5 +---- internal/log/zap_log_test.go | 24 +++++++++++------------- internal/mq/msgstream/mq_factory_test.go | 9 ++------- 4 files changed, 15 insertions(+), 32 deletions(-) diff --git a/internal/kv/etcd/embed_etcd_kv_test.go b/internal/kv/etcd/embed_etcd_kv_test.go index b197f32f77..3833456b8f 100644 --- a/internal/kv/etcd/embed_etcd_kv_test.go +++ b/internal/kv/etcd/embed_etcd_kv_test.go @@ -18,7 +18,6 @@ package etcdkv_test import ( "errors" - "io/ioutil" "os" "testing" "time" @@ -40,17 +39,11 @@ func TestEmbedEtcd(te *testing.T) { param.BaseTable.Save("etcd.use.embed", "true") param.BaseTable.Save("etcd.config.path", "../../../configs/advanced/etcd.yaml") - dir, err := ioutil.TempDir(os.TempDir(), "kv_etcd") - assert.Nil(te, err) + dir := te.TempDir() param.BaseTable.Save("etcd.data.dir", dir) param.EtcdCfg.LoadCfgToMemory() - //clean up data - defer func() { - err = os.RemoveAll(dir) - }() - te.Run("EtcdKV SaveAndLoad", func(t *testing.T) { rootPath := "/etcd/test/root/saveandload" metaKv, err := embed_etcd_kv.NewMetaKvFactory(rootPath, ¶m.EtcdCfg) diff --git a/internal/log/log_test.go b/internal/log/log_test.go index af24426127..6a4b73356b 100644 --- a/internal/log/log_test.go +++ b/internal/log/log_test.go @@ -33,8 +33,6 @@ package log import ( "bufio" "bytes" - "io/ioutil" - "os" "testing" "time" @@ -82,8 +80,7 @@ func TestZapTextEncoder(t *testing.T) { } func TestInvalidFileConfig(t *testing.T) { - tmpDir, _ := ioutil.TempDir("/tmp", "invalid-log-test") - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() invalidFileConf := FileLogConfig{ Filename: tmpDir, diff --git a/internal/log/zap_log_test.go b/internal/log/zap_log_test.go index 07204c7701..253386a7cd 100644 --- a/internal/log/zap_log_test.go +++ b/internal/log/zap_log_test.go @@ -35,7 +35,6 @@ import ( "io/ioutil" "math" "net" - "os" "strings" "testing" "time" @@ -114,14 +113,14 @@ func TestLog(t *testing.T) { zap.Duration("duration", 10*time.Second), ) ts.assertMessages( - `[INFO] [zap_log_test.go:66] ["failed to fetch URL"] [url=http://example.com] [attempt=3] [backoff=1s]`, - `[INFO] [zap_log_test.go:71] ["failed to \"fetch\" [URL]: http://example.com"]`, - `[DEBUG] [zap_log_test.go:72] ["Slow query"] [sql="SELECT * FROM TABLE\n\tWHERE ID=\"abc\""] [duration=1.3s] ["process keys"=1500]`, - `[INFO] [zap_log_test.go:78] [Welcome]`, - `[INFO] [zap_log_test.go:79] [欢迎]`, - `[WARN] [zap_log_test.go:80] [Type] [Counter=NaN] [Score=+Inf]`, - `[INFO] [zap_log_test.go:85] ["new connection"] [connID=1] [traceID=dse1121]`, - `[INFO] [zap_log_test.go:86] ["Testing typs"] [filed1=noquote] `+ + `[INFO] [zap_log_test.go:65] ["failed to fetch URL"] [url=http://example.com] [attempt=3] [backoff=1s]`, + `[INFO] [zap_log_test.go:70] ["failed to \"fetch\" [URL]: http://example.com"]`, + `[DEBUG] [zap_log_test.go:71] ["Slow query"] [sql="SELECT * FROM TABLE\n\tWHERE ID=\"abc\""] [duration=1.3s] ["process keys"=1500]`, + `[INFO] [zap_log_test.go:77] [Welcome]`, + `[INFO] [zap_log_test.go:78] [欢迎]`, + `[WARN] [zap_log_test.go:79] [Type] [Counter=NaN] [Score=+Inf]`, + `[INFO] [zap_log_test.go:84] ["new connection"] [connID=1] [traceID=dse1121]`, + `[INFO] [zap_log_test.go:85] ["Testing typs"] [filed1=noquote] `+ `[filed2="in quote"] [urls="[http://mock1.com:2347,http://mock2.com:2432]"] `+ `[urls-peer="[t1,\"t2 fine\"]"] ["store ids"="[1,4,5]"] [object="{username=user1}"] `+ `[object2="{username=\"user 2\"}"] [binary="YWIxMjM="] ["is processed"=true] `+ @@ -192,8 +191,8 @@ func TestLogJSON(t *testing.T) { "backoff", time.Second, ) logger.With(zap.String("connID", "1"), zap.String("traceID", "dse1121")).Info("new connection") - ts.assertMessages("{\"level\":\"INFO\",\"caller\":\"zap_log_test.go:189\",\"message\":\"failed to fetch URL\",\"url\":\"http://example.com\",\"attempt\":3,\"backoff\":\"1s\"}", - "{\"level\":\"INFO\",\"caller\":\"zap_log_test.go:194\",\"message\":\"new connection\",\"connID\":\"1\",\"traceID\":\"dse1121\"}") + ts.assertMessages("{\"level\":\"INFO\",\"caller\":\"zap_log_test.go:188\",\"message\":\"failed to fetch URL\",\"url\":\"http://example.com\",\"attempt\":3,\"backoff\":\"1s\"}", + "{\"level\":\"INFO\",\"caller\":\"zap_log_test.go:193\",\"message\":\"new connection\",\"connID\":\"1\",\"traceID\":\"dse1121\"}") } func TestRotateLog(t *testing.T) { @@ -207,7 +206,7 @@ func TestRotateLog(t *testing.T) { } for _, c := range cases { t.Run(c.desc, func(t *testing.T) { - tempDir, _ := ioutil.TempDir("/tmp", "pd-test-log") + tempDir := t.TempDir() conf := &Config{ Level: "info", File: FileLogConfig{ @@ -229,7 +228,6 @@ func TestRotateLog(t *testing.T) { } files, _ := ioutil.ReadDir(tempDir) assert.Len(t, files, c.expectedFileNum) - _ = os.RemoveAll(tempDir) }) } } diff --git a/internal/mq/msgstream/mq_factory_test.go b/internal/mq/msgstream/mq_factory_test.go index f4055c40e1..5c3dfd9c13 100644 --- a/internal/mq/msgstream/mq_factory_test.go +++ b/internal/mq/msgstream/mq_factory_test.go @@ -18,7 +18,6 @@ package msgstream import ( "context" - "io/ioutil" "os" "testing" @@ -42,13 +41,12 @@ func TestPmsFactory(t *testing.T) { func TestRmsFactory(t *testing.T) { defer os.Unsetenv("ROCKSMQ_PATH") - dir, err := ioutil.TempDir(os.TempDir(), "mq") - assert.Nil(t, err) + dir := t.TempDir() rmsFactory := NewRmsFactory(dir) ctx := context.Background() - _, err = rmsFactory.NewMsgStream(ctx) + _, err := rmsFactory.NewMsgStream(ctx) assert.Nil(t, err) _, err = rmsFactory.NewTtMsgStream(ctx) @@ -56,9 +54,6 @@ func TestRmsFactory(t *testing.T) { _, err = rmsFactory.NewQueryMsgStream(ctx) assert.Nil(t, err) - - err = os.RemoveAll(dir) - assert.Nil(t, err) } func TestKafkaFactory(t *testing.T) {