123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- package generator
- import (
- "io/ioutil"
- "os"
- "path/filepath"
- "testing"
- "github.com/stretchr/testify/assert"
- "github.com/tal-tech/go-zero/tools/goctl/util"
- )
- 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 := util.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 := util.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 := util.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)
- }
|