Add MustTempDir (#1069)

This commit is contained in:
anqiansong
2021-09-21 10:13:43 +08:00
committed by GitHub
parent 30e49f2939
commit 9a724fe907
11 changed files with 48 additions and 25 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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)