You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/tools/goctl/rpc/generator/template_test.go

114 lines
2.1 KiB
Go

package generator
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
func TestGenTemplates(t *testing.T) {
_ = Clean()
err := GenTemplates(nil)
assert.Nil(t, err)
}
func TestRevertTemplate(t *testing.T) {
_ = Clean()
err := GenTemplates(nil)
assert.Nil(t, err)
fp, err := pathx.GetTemplateDir(category)
if err != nil {
return
}
mainTpl := filepath.Join(fp, mainTemplateFile)
data, err := ioutil.ReadFile(mainTpl)
if err != nil {
return
}
assert.Equal(t, templates[mainTemplateFile], string(data))
err = RevertTemplate("test")
if err != nil {
assert.Equal(t, "test: no such file name", err.Error())
}
err = ioutil.WriteFile(mainTpl, []byte("modify"), os.ModePerm)
if err != nil {
return
}
data, err = ioutil.ReadFile(mainTpl)
if err != nil {
return
}
assert.Equal(t, "modify", string(data))
err = RevertTemplate(mainTemplateFile)
assert.Nil(t, err)
data, err = ioutil.ReadFile(mainTpl)
if err != nil {
return
}
assert.Equal(t, templates[mainTemplateFile], string(data))
}
func TestClean(t *testing.T) {
_ = Clean()
err := GenTemplates(nil)
assert.Nil(t, err)
fp, err := pathx.GetTemplateDir(category)
if err != nil {
return
}
mainTpl := filepath.Join(fp, mainTemplateFile)
_, err = os.Stat(mainTpl)
assert.Nil(t, err)
err = Clean()
assert.Nil(t, err)
_, err = os.Stat(mainTpl)
assert.NotNil(t, err)
}
func TestUpdate(t *testing.T) {
_ = Clean()
err := GenTemplates(nil)
assert.Nil(t, err)
fp, err := pathx.GetTemplateDir(category)
if err != nil {
return
}
mainTpl := filepath.Join(fp, mainTemplateFile)
err = ioutil.WriteFile(mainTpl, []byte("modify"), os.ModePerm)
if err != nil {
return
}
data, err := ioutil.ReadFile(mainTpl)
if err != nil {
return
}
assert.Equal(t, "modify", string(data))
assert.Nil(t, Update())
data, err = ioutil.ReadFile(mainTpl)
if err != nil {
return
}
assert.Equal(t, templates[mainTemplateFile], string(data))
}
func TestGetCategory(t *testing.T) {
_ = Clean()
result := Category()
assert.Equal(t, category, result)
}