diff --git a/tools/goctl/api/parser/g4/test/apiparser_test.go b/tools/goctl/api/parser/g4/test/apiparser_test.go index 03cd4cb1..70143067 100644 --- a/tools/goctl/api/parser/g4/test/apiparser_test.go +++ b/tools/goctl/api/parser/g4/test/apiparser_test.go @@ -8,7 +8,9 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/ast" + "github.com/tal-tech/go-zero/tools/goctl/util" ) var ( @@ -118,7 +120,7 @@ func TestApiParser(t *testing.T) { }) t.Run("nestedImport", func(t *testing.T) { - file := filepath.Join(t.TempDir(), "foo.api") + file := filepath.Join(util.MustTempDir(), "foo.api") err := ioutil.WriteFile(file, []byte(nestedAPIImport), os.ModePerm) if err != nil { return @@ -148,7 +150,7 @@ func TestApiParser(t *testing.T) { }) t.Run("ambiguousSyntax", func(t *testing.T) { - file := filepath.Join(t.TempDir(), "foo.api") + file := filepath.Join(util.MustTempDir(), "foo.api") err := ioutil.WriteFile(file, []byte(ambiguousSyntax), os.ModePerm) if err != nil { return @@ -162,7 +164,7 @@ func TestApiParser(t *testing.T) { }) t.Run("ambiguousSyntax", func(t *testing.T) { - file := filepath.Join(t.TempDir(), "foo.api") + file := filepath.Join(util.MustTempDir(), "foo.api") err := ioutil.WriteFile(file, []byte(ambiguousSyntax), os.ModePerm) if err != nil { return @@ -176,7 +178,7 @@ func TestApiParser(t *testing.T) { }) t.Run("ambiguousService", func(t *testing.T) { - file := filepath.Join(t.TempDir(), "foo.api") + file := filepath.Join(util.MustTempDir(), "foo.api") err := ioutil.WriteFile(file, []byte(ambiguousService), os.ModePerm) if err != nil { return @@ -206,7 +208,7 @@ func TestApiParser(t *testing.T) { `) assert.Error(t, err) - file := filepath.Join(t.TempDir(), "foo.api") + file := filepath.Join(util.MustTempDir(), "foo.api") err = ioutil.WriteFile(file, []byte(duplicateHandler), os.ModePerm) if err != nil { return @@ -235,7 +237,7 @@ func TestApiParser(t *testing.T) { `) assert.Error(t, err) - file := filepath.Join(t.TempDir(), "foo.api") + file := filepath.Join(util.MustTempDir(), "foo.api") err = ioutil.WriteFile(file, []byte(duplicateRoute), os.ModePerm) if err != nil { return @@ -259,7 +261,7 @@ func TestApiParser(t *testing.T) { `) assert.Error(t, err) - file := filepath.Join(t.TempDir(), "foo.api") + file := filepath.Join(util.MustTempDir(), "foo.api") err = ioutil.WriteFile(file, []byte(duplicateType), os.ModePerm) if err != nil { return diff --git a/tools/goctl/internal/version/version.go b/tools/goctl/internal/version/version.go index d89c4a4b..bcd8f31d 100644 --- a/tools/goctl/internal/version/version.go +++ b/tools/goctl/internal/version/version.go @@ -48,7 +48,7 @@ func convertVersion(version string) (versionNumber float64, tag string) { } return '_' }, splits[0]) - numberStr = strings.ReplaceAll(numberStr, "_", "") + numberStr = strings.Replace(numberStr, "_", "", -1) versionNumber, _ = json.Number(numberStr).Float64() return } diff --git a/tools/goctl/model/mongo/generate/generate_test.go b/tools/goctl/model/mongo/generate/generate_test.go index 5298644b..fac8247a 100644 --- a/tools/goctl/model/mongo/generate/generate_test.go +++ b/tools/goctl/model/mongo/generate/generate_test.go @@ -6,7 +6,9 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/tal-tech/go-zero/tools/goctl/config" + "github.com/tal-tech/go-zero/tools/goctl/util" ) var testTypes = ` @@ -18,7 +20,7 @@ func TestDo(t *testing.T) { cfg, err := config.NewConfig(config.DefaultFormat) assert.Nil(t, err) - tempDir := t.TempDir() + tempDir := util.MustTempDir() typesfile := filepath.Join(tempDir, "types.go") err = ioutil.WriteFile(typesfile, []byte(testTypes), 0o666) assert.Nil(t, err) diff --git a/tools/goctl/model/sql/command/command_test.go b/tools/goctl/model/sql/command/command_test.go index 72fe7020..f2fac8f5 100644 --- a/tools/goctl/model/sql/command/command_test.go +++ b/tools/goctl/model/sql/command/command_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/tal-tech/go-zero/tools/goctl/config" "github.com/tal-tech/go-zero/tools/goctl/model/sql/gen" "github.com/tal-tech/go-zero/tools/goctl/util" @@ -23,12 +24,12 @@ func TestFromDDl(t *testing.T) { err := gen.Clean() assert.Nil(t, err) - err = fromDDL("./user.sql", t.TempDir(), cfg, true, false, "go_zero") + err = fromDDL("./user.sql", util.MustTempDir(), cfg, true, false, "go_zero") assert.Equal(t, errNotMatched, err) // case dir is not exists - unknownDir := filepath.Join(t.TempDir(), "test", "user.sql") - err = fromDDL(unknownDir, t.TempDir(), cfg, true, false, "go_zero") + unknownDir := filepath.Join(util.MustTempDir(), "test", "user.sql") + err = fromDDL(unknownDir, util.MustTempDir(), cfg, true, false, "go_zero") assert.True(t, func() bool { switch err.(type) { case *os.PathError: @@ -39,12 +40,12 @@ func TestFromDDl(t *testing.T) { }()) // case empty src - err = fromDDL("", t.TempDir(), cfg, true, false, "go_zero") + err = fromDDL("", util.MustTempDir(), cfg, true, false, "go_zero") if err != nil { assert.Equal(t, "expected path or path globbing patterns, but nothing found", err.Error()) } - tempDir := filepath.Join(t.TempDir(), "test") + tempDir := filepath.Join(util.MustTempDir(), "test") err = util.MkdirIfNotExist(tempDir) if err != nil { return diff --git a/tools/goctl/model/sql/gen/gen_test.go b/tools/goctl/model/sql/gen/gen_test.go index 2d87e003..8e1e5960 100644 --- a/tools/goctl/model/sql/gen/gen_test.go +++ b/tools/goctl/model/sql/gen/gen_test.go @@ -10,10 +10,12 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/stringx" "github.com/tal-tech/go-zero/tools/goctl/config" "github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx" + "github.com/tal-tech/go-zero/tools/goctl/util" ) var source = "CREATE TABLE `test_user` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `mobile` varchar(255) COLLATE utf8mb4_bin NOT NULL,\n `class` bigint NOT NULL,\n `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `mobile_unique` (`mobile`),\n UNIQUE KEY `class_name_unique` (`class`,`name`),\n KEY `create_index` (`create_time`),\n KEY `name_index` (`name`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;" @@ -22,11 +24,11 @@ func TestCacheModel(t *testing.T) { logx.Disable() _ = Clean() - sqlFile := filepath.Join(t.TempDir(), "tmp.sql") + sqlFile := filepath.Join(util.MustTempDir(), "tmp.sql") err := ioutil.WriteFile(sqlFile, []byte(source), 0o777) assert.Nil(t, err) - dir := filepath.Join(t.TempDir(), "./testmodel") + dir := filepath.Join(util.MustTempDir(), "./testmodel") cacheDir := filepath.Join(dir, "cache") noCacheDir := filepath.Join(dir, "nocache") g, err := NewDefaultGenerator(cacheDir, &config.Config{ @@ -57,7 +59,7 @@ func TestNamingModel(t *testing.T) { logx.Disable() _ = Clean() - sqlFile := filepath.Join(t.TempDir(), "tmp.sql") + sqlFile := filepath.Join(util.MustTempDir(), "tmp.sql") err := ioutil.WriteFile(sqlFile, []byte(source), 0o777) assert.Nil(t, err) diff --git a/tools/goctl/model/sql/parser/parser_test.go b/tools/goctl/model/sql/parser/parser_test.go index 25485fb9..db090a9f 100644 --- a/tools/goctl/model/sql/parser/parser_test.go +++ b/tools/goctl/model/sql/parser/parser_test.go @@ -6,12 +6,14 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/tal-tech/go-zero/tools/goctl/model/sql/model" "github.com/tal-tech/go-zero/tools/goctl/model/sql/util" + ctlutil "github.com/tal-tech/go-zero/tools/goctl/util" ) func TestParsePlainText(t *testing.T) { - sqlFile := filepath.Join(t.TempDir(), "tmp.sql") + sqlFile := filepath.Join(ctlutil.MustTempDir(), "tmp.sql") err := ioutil.WriteFile(sqlFile, []byte("plain text"), 0o777) assert.Nil(t, err) @@ -20,7 +22,7 @@ func TestParsePlainText(t *testing.T) { } func TestParseSelect(t *testing.T) { - sqlFile := filepath.Join(t.TempDir(), "tmp.sql") + sqlFile := filepath.Join(ctlutil.MustTempDir(), "tmp.sql") err := ioutil.WriteFile(sqlFile, []byte("select * from user"), 0o777) assert.Nil(t, err) @@ -30,7 +32,7 @@ func TestParseSelect(t *testing.T) { } func TestParseCreateTable(t *testing.T) { - sqlFile := filepath.Join(t.TempDir(), "tmp.sql") + sqlFile := filepath.Join(ctlutil.MustTempDir(), "tmp.sql") err := ioutil.WriteFile(sqlFile, []byte("CREATE TABLE `test_user` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `mobile` varchar(255) COLLATE utf8mb4_bin NOT NULL comment '手\\t机 号',\n `class` bigint NOT NULL comment '班级',\n `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL comment '姓\n 名',\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP comment '创建\\r时间',\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `mobile_unique` (`mobile`),\n UNIQUE KEY `class_name_unique` (`class`,`name`),\n KEY `create_index` (`create_time`),\n KEY `name_index` (`name`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"), 0o777) assert.Nil(t, err) diff --git a/tools/goctl/rpc/generator/gen_test.go b/tools/goctl/rpc/generator/gen_test.go index 31c28335..eb8cdefa 100644 --- a/tools/goctl/rpc/generator/gen_test.go +++ b/tools/goctl/rpc/generator/gen_test.go @@ -12,6 +12,7 @@ import ( "github.com/tal-tech/go-zero/core/stringx" conf "github.com/tal-tech/go-zero/tools/goctl/config" "github.com/tal-tech/go-zero/tools/goctl/rpc/execx" + "github.com/tal-tech/go-zero/tools/goctl/util" ) var cfg = &conf.Config{ @@ -57,7 +58,7 @@ func TestRpcGenerate(t *testing.T) { // case go mod t.Run("GOMOD", func(t *testing.T) { - workDir := t.TempDir() + workDir := util.MustTempDir() name := filepath.Base(workDir) _, err = execx.Run("go mod init "+name, workDir) if err != nil { diff --git a/tools/goctl/rpc/generator/prototmpl.go b/tools/goctl/rpc/generator/prototmpl.go index f7149141..637cb03d 100644 --- a/tools/goctl/rpc/generator/prototmpl.go +++ b/tools/goctl/rpc/generator/prototmpl.go @@ -25,7 +25,7 @@ service {{.serviceName}} { } ` -// ProtoTmpl returns an sample of a proto file +// ProtoTmpl returns a sample of a proto file func ProtoTmpl(out string) error { protoFilename := filepath.Base(out) serviceName := stringx.From(strings.TrimSuffix(protoFilename, filepath.Ext(protoFilename))) diff --git a/tools/goctl/rpc/generator/prototmpl_test.go b/tools/goctl/rpc/generator/prototmpl_test.go index e735c9e5..32e9bc1e 100644 --- a/tools/goctl/rpc/generator/prototmpl_test.go +++ b/tools/goctl/rpc/generator/prototmpl_test.go @@ -5,16 +5,18 @@ import ( "testing" "github.com/stretchr/testify/assert" + + "github.com/tal-tech/go-zero/tools/goctl/util" ) func TestProtoTmpl(t *testing.T) { _ = Clean() // exists dir - err := ProtoTmpl(t.TempDir()) + err := ProtoTmpl(util.MustTempDir()) assert.Nil(t, err) // not exist dir - dir := filepath.Join(t.TempDir(), "test") + dir := filepath.Join(util.MustTempDir(), "test") err = ProtoTmpl(dir) assert.Nil(t, err) } diff --git a/tools/goctl/util/file.go b/tools/goctl/util/file.go index 149d2a83..5b42a51d 100644 --- a/tools/goctl/util/file.go +++ b/tools/goctl/util/file.go @@ -4,6 +4,7 @@ import ( "bufio" "fmt" "io/ioutil" + "log" "os" "path/filepath" "strings" @@ -180,3 +181,13 @@ func createTemplate(file, content string, force bool) error { _, err = f.WriteString(content) return err } + +// MustTempDir creates a temporary directory +func MustTempDir() string { + dir, err := ioutil.TempDir("", "") + if err != nil { + log.Fatalln(err) + } + + return dir +} diff --git a/tools/goctl/util/stringx/string.go b/tools/goctl/util/stringx/string.go index 1e3e6161..ae148c2f 100644 --- a/tools/goctl/util/stringx/string.go +++ b/tools/goctl/util/stringx/string.go @@ -39,7 +39,7 @@ func (s String) Upper() string { // ReplaceAll calls the strings.ReplaceAll func (s String) ReplaceAll(old, new string) string { - return strings.ReplaceAll(s.source, old, new) + return strings.Replace(s.source, old, new, -1) } // Source returns the source string value