template_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package generator
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/tal-tech/go-zero/tools/goctl/util"
  9. )
  10. func TestGenTemplates(t *testing.T) {
  11. _ = Clean()
  12. err := GenTemplates(nil)
  13. assert.Nil(t, err)
  14. }
  15. func TestRevertTemplate(t *testing.T) {
  16. _ = Clean()
  17. err := GenTemplates(nil)
  18. assert.Nil(t, err)
  19. fp, err := util.GetTemplateDir(category)
  20. if err != nil {
  21. return
  22. }
  23. mainTpl := filepath.Join(fp, mainTemplateFile)
  24. data, err := ioutil.ReadFile(mainTpl)
  25. if err != nil {
  26. return
  27. }
  28. assert.Equal(t, templates[mainTemplateFile], string(data))
  29. err = RevertTemplate("test")
  30. if err != nil {
  31. assert.Equal(t, "test: no such file name", err.Error())
  32. }
  33. err = ioutil.WriteFile(mainTpl, []byte("modify"), os.ModePerm)
  34. if err != nil {
  35. return
  36. }
  37. data, err = ioutil.ReadFile(mainTpl)
  38. if err != nil {
  39. return
  40. }
  41. assert.Equal(t, "modify", string(data))
  42. err = RevertTemplate(mainTemplateFile)
  43. assert.Nil(t, err)
  44. data, err = ioutil.ReadFile(mainTpl)
  45. if err != nil {
  46. return
  47. }
  48. assert.Equal(t, templates[mainTemplateFile], string(data))
  49. }
  50. func TestClean(t *testing.T) {
  51. _ = Clean()
  52. err := GenTemplates(nil)
  53. assert.Nil(t, err)
  54. fp, err := util.GetTemplateDir(category)
  55. if err != nil {
  56. return
  57. }
  58. mainTpl := filepath.Join(fp, mainTemplateFile)
  59. _, err = os.Stat(mainTpl)
  60. assert.Nil(t, err)
  61. err = Clean()
  62. assert.Nil(t, err)
  63. _, err = os.Stat(mainTpl)
  64. assert.NotNil(t, err)
  65. }
  66. func TestUpdate(t *testing.T) {
  67. _ = Clean()
  68. err := GenTemplates(nil)
  69. assert.Nil(t, err)
  70. fp, err := util.GetTemplateDir(category)
  71. if err != nil {
  72. return
  73. }
  74. mainTpl := filepath.Join(fp, mainTemplateFile)
  75. err = ioutil.WriteFile(mainTpl, []byte("modify"), os.ModePerm)
  76. if err != nil {
  77. return
  78. }
  79. data, err := ioutil.ReadFile(mainTpl)
  80. if err != nil {
  81. return
  82. }
  83. assert.Equal(t, "modify", string(data))
  84. assert.Nil(t, Update())
  85. data, err = ioutil.ReadFile(mainTpl)
  86. if err != nil {
  87. return
  88. }
  89. assert.Equal(t, templates[mainTemplateFile], string(data))
  90. }
  91. func TestGetCategory(t *testing.T) {
  92. _ = Clean()
  93. result := Category()
  94. assert.Equal(t, category, result)
  95. }