template_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package gogen
  2. import (
  3. "io/ioutil"
  4. "path/filepath"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/tal-tech/go-zero/tools/goctl/util"
  8. )
  9. func TestGenTemplates(t *testing.T) {
  10. err := util.InitTemplates(category, templates)
  11. assert.Nil(t, err)
  12. dir, err := util.GetTemplateDir(category)
  13. assert.Nil(t, err)
  14. file := filepath.Join(dir, "main.tpl")
  15. data, err := ioutil.ReadFile(file)
  16. assert.Nil(t, err)
  17. assert.Equal(t, string(data), mainTemplate)
  18. }
  19. func TestRevertTemplate(t *testing.T) {
  20. name := "main.tpl"
  21. err := util.InitTemplates(category, templates)
  22. assert.Nil(t, err)
  23. dir, err := util.GetTemplateDir(category)
  24. assert.Nil(t, err)
  25. file := filepath.Join(dir, name)
  26. data, err := ioutil.ReadFile(file)
  27. assert.Nil(t, err)
  28. modifyData := string(data) + "modify"
  29. err = util.CreateTemplate(category, name, modifyData)
  30. assert.Nil(t, err)
  31. data, err = ioutil.ReadFile(file)
  32. assert.Nil(t, err)
  33. assert.Equal(t, string(data), modifyData)
  34. assert.Nil(t, RevertTemplate(name))
  35. data, err = ioutil.ReadFile(file)
  36. assert.Nil(t, err)
  37. assert.Equal(t, mainTemplate, string(data))
  38. }
  39. func TestClean(t *testing.T) {
  40. name := "main.tpl"
  41. err := util.InitTemplates(category, templates)
  42. assert.Nil(t, err)
  43. assert.Nil(t, Clean())
  44. dir, err := util.GetTemplateDir(category)
  45. assert.Nil(t, err)
  46. file := filepath.Join(dir, name)
  47. _, err = ioutil.ReadFile(file)
  48. assert.NotNil(t, err)
  49. }
  50. func TestUpdate(t *testing.T) {
  51. name := "main.tpl"
  52. err := util.InitTemplates(category, templates)
  53. assert.Nil(t, err)
  54. dir, err := util.GetTemplateDir(category)
  55. assert.Nil(t, err)
  56. file := filepath.Join(dir, name)
  57. data, err := ioutil.ReadFile(file)
  58. assert.Nil(t, err)
  59. modifyData := string(data) + "modify"
  60. err = util.CreateTemplate(category, name, modifyData)
  61. assert.Nil(t, err)
  62. data, err = ioutil.ReadFile(file)
  63. assert.Nil(t, err)
  64. assert.Equal(t, string(data), modifyData)
  65. assert.Nil(t, Update())
  66. data, err = ioutil.ReadFile(file)
  67. assert.Nil(t, err)
  68. assert.Equal(t, mainTemplate, string(data))
  69. }