Skip to content

Commit

Permalink
test: fix creating tmp dir in unit test (#1684)
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyJavaBean authored Jan 24, 2025
1 parent d840d1a commit 98e1162
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
32 changes: 13 additions & 19 deletions pkg/utils/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ package utils

import (
"os"
"path/filepath"
"runtime"
"testing"
"time"

"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

Expand Down Expand Up @@ -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")
Expand Down
5 changes: 3 additions & 2 deletions tool/internal_pkg/generator/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
package generator

import (
"path/filepath"
"testing"

"github.com/cloudwego/kitex/internal/test"
)

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)
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 98e1162

Please sign in to comment.