generate_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package generate
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/zeromicro/go-zero/tools/goctl/config"
  8. "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
  9. )
  10. var testTypes = `
  11. type User struct{}
  12. type Class struct{}
  13. `
  14. func TestDo(t *testing.T) {
  15. t.Run("should generate model", func(t *testing.T) {
  16. cfg, err := config.NewConfig(config.DefaultFormat)
  17. assert.Nil(t, err)
  18. tempDir := pathx.MustTempDir()
  19. typesfile := filepath.Join(tempDir, "types.go")
  20. err = os.WriteFile(typesfile, []byte(testTypes), 0o666)
  21. assert.Nil(t, err)
  22. err = Do(&Context{
  23. Types: []string{"User", "Class"},
  24. Cache: false,
  25. Output: tempDir,
  26. Cfg: cfg,
  27. })
  28. assert.Nil(t, err)
  29. })
  30. t.Run("missing config", func(t *testing.T) {
  31. tempDir := t.TempDir()
  32. typesfile := filepath.Join(tempDir, "types.go")
  33. err := os.WriteFile(typesfile, []byte(testTypes), 0o666)
  34. assert.Nil(t, err)
  35. err = Do(&Context{
  36. Types: []string{"User", "Class"},
  37. Cache: false,
  38. Output: tempDir,
  39. Cfg: nil,
  40. })
  41. assert.Error(t, err)
  42. })
  43. t.Run("invalid config", func(t *testing.T) {
  44. cfg := &config.Config{NamingFormat: "foo"}
  45. tempDir := t.TempDir()
  46. typesfile := filepath.Join(tempDir, "types.go")
  47. err := os.WriteFile(typesfile, []byte(testTypes), 0o666)
  48. assert.Nil(t, err)
  49. err = Do(&Context{
  50. Types: []string{"User", "Class"},
  51. Cache: false,
  52. Output: tempDir,
  53. Cfg: cfg,
  54. })
  55. assert.Error(t, err)
  56. })
  57. }