diff --git a/pkg/utils/yaml_test.go b/pkg/utils/yaml_test.go index c83a3b6b1a..9446d75d2a 100644 --- a/pkg/utils/yaml_test.go +++ b/pkg/utils/yaml_test.go @@ -18,6 +18,7 @@ package utils import ( "os" + "path/filepath" "runtime" "testing" "time" @@ -25,9 +26,9 @@ import ( "github.com/cloudwego/kitex/internal/test" ) -const ( - TestYamlFile = "/tmp/test.yaml" -) +func getTestYamlFile(t *testing.T) string { + return filepath.Join(t.TempDir(), "test.yaml") +} var cfg *YamlConfig @@ -78,33 +79,26 @@ MockFloat64: 123456.789` } } -func deleteTestYamlFile(t *testing.T, path string) { - if err := os.Remove(path); err != nil { - t.Errorf("Remove file: %s error, err: %s", path, err.Error()) - } -} - func TestMain(m *testing.M) { if runtime.GOOS == "windows" { return } t := &testing.T{} - createTestYamlFile(t, TestYamlFile) - cfg, _ = ReadYamlConfigFile(TestYamlFile) + testYamlFile := getTestYamlFile(t) + + createTestYamlFile(t, testYamlFile) + + cfg, _ = ReadYamlConfigFile(testYamlFile) - exit := m.Run() - deleteTestYamlFile(t, TestYamlFile) - os.Exit(exit) + os.Exit(m.Run()) } func Test_ReadYamlConfigFile(t *testing.T) { - createTestYamlFile(t, TestYamlFile) - defer func() { - deleteTestYamlFile(t, TestYamlFile) - }() + testYamlFile := getTestYamlFile(t) + createTestYamlFile(t, testYamlFile) - cfg, err := ReadYamlConfigFile(TestYamlFile) + cfg, err := ReadYamlConfigFile(testYamlFile) test.Assert(t, err == nil) addr, ok := cfg.GetString("Address") test.Assert(t, ok && addr == ":8888") diff --git a/tool/internal_pkg/generator/template_test.go b/tool/internal_pkg/generator/template_test.go index 79d464006f..4b3b5520e5 100644 --- a/tool/internal_pkg/generator/template_test.go +++ b/tool/internal_pkg/generator/template_test.go @@ -15,6 +15,7 @@ package generator import ( + "path/filepath" "testing" "github.com/cloudwego/kitex/internal/test" @@ -22,7 +23,7 @@ import ( func TestNilSafe(t *testing.T) { var q *TemplateExtension - fn := "/tmp/kitex-template-nil.json" + fn := filepath.Join(t.TempDir(), "kitex-template-nil.json") err := q.ToJSONFile(fn) test.Assert(t, err == nil, err) @@ -56,7 +57,7 @@ func TestMarshal(t *testing.T) { }, } - fn := "/tmp/kitex-template.json" + fn := filepath.Join(t.TempDir(), "kitex-template.json") err := p.ToJSONFile(fn) test.Assert(t, err == nil, err)