Skip to content

Commit

Permalink
fix: fix creating tmp dir in unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyJavaBean committed Jan 24, 2025
1 parent 4e23871 commit b5c9a93
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
25 changes: 16 additions & 9 deletions pkg/utils/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ 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 {
tempDir := t.TempDir()

return filepath.Join(tempDir, "test.yaml")
}

var cfg *YamlConfig

Expand Down Expand Up @@ -89,22 +92,26 @@ func TestMain(m *testing.M) {
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)
deleteTestYamlFile(t, testYamlFile)
os.Exit(exit)
}

func Test_ReadYamlConfigFile(t *testing.T) {
createTestYamlFile(t, TestYamlFile)
testYamlFile := getTestYamlFile(t)
createTestYamlFile(t, testYamlFile)
defer func() {
deleteTestYamlFile(t, TestYamlFile)
deleteTestYamlFile(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
16 changes: 14 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,21 @@
package generator

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

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

func TestNilSafe(t *testing.T) {
var q *TemplateExtension
fn := "/tmp/kitex-template-nil.json"

tempDir := t.TempDir()

defer os.RemoveAll(tempDir)

fn := filepath.Join(tempDir, "kitex-template-nil.json")

err := q.ToJSONFile(fn)
test.Assert(t, err == nil, err)
Expand Down Expand Up @@ -56,7 +63,12 @@ func TestMarshal(t *testing.T) {
},
}

fn := "/tmp/kitex-template.json"
tempDir := t.TempDir()

defer os.RemoveAll(tempDir)

fn := filepath.Join(tempDir, "kitex-template.json")

err := p.ToJSONFile(fn)
test.Assert(t, err == nil, err)

Expand Down

0 comments on commit b5c9a93

Please sign in to comment.