testdata.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package testdata
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "strings"
  9. "github.com/gookit/color"
  10. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/pathx"
  11. )
  12. type (
  13. File struct {
  14. IsDir bool
  15. Path string
  16. AbsolutePath string
  17. Content string
  18. Cmd string
  19. }
  20. Files []File
  21. )
  22. func (f File) execute(goctl string) error {
  23. printDir := f.Path
  24. dir := f.AbsolutePath
  25. if !f.IsDir {
  26. printDir = filepath.Dir(printDir)
  27. dir = filepath.Dir(dir)
  28. }
  29. printCommand := strings.ReplaceAll(fmt.Sprintf("cd %s && %s", printDir, f.Cmd), "goctl", filepath.Base(goctl))
  30. command := strings.ReplaceAll(fmt.Sprintf("cd %s && %s", dir, f.Cmd), "goctl", goctl)
  31. fmt.Println(color.LightGreen.Render(printCommand))
  32. cmd := exec.Command("sh", "-c", command)
  33. cmd.Env = os.Environ()
  34. cmd.Stdout = os.Stdout
  35. cmd.Stderr = os.Stderr
  36. return cmd.Run()
  37. }
  38. func (fs Files) execute(goctl string) error {
  39. for _, f := range fs {
  40. err := f.execute(goctl)
  41. if err != nil {
  42. return err
  43. }
  44. }
  45. return nil
  46. }
  47. func mustGetTestData(baseDir string) (Files, Files) {
  48. if len(baseDir) == 0 {
  49. dir, err := os.Getwd()
  50. if err != nil {
  51. log.Fatalln(err)
  52. }
  53. baseDir = dir
  54. }
  55. baseDir, err := filepath.Abs(baseDir)
  56. if err != nil {
  57. return nil, nil
  58. }
  59. createFile := func(baseDir string, data File) (File, error) {
  60. fp := filepath.Join(baseDir, data.Path)
  61. dir := filepath.Dir(fp)
  62. if data.IsDir {
  63. dir = fp
  64. }
  65. if err := pathx.MkdirIfNotExist(dir); err != nil {
  66. return data, err
  67. }
  68. data.AbsolutePath = fp
  69. if data.IsDir {
  70. return data, nil
  71. }
  72. return data, os.WriteFile(fp, []byte(data.Content), os.ModePerm)
  73. }
  74. oldDir := filepath.Join(baseDir, "old_fs")
  75. newDir := filepath.Join(baseDir, "new_fs")
  76. os.RemoveAll(oldDir)
  77. os.RemoveAll(newDir)
  78. var oldFiles, newFiles []File
  79. for _, data := range list {
  80. od, err := createFile(oldDir, data)
  81. if err != nil {
  82. log.Fatalln(err)
  83. }
  84. oldFiles = append(oldFiles, od)
  85. nd, err := createFile(newDir, data)
  86. if err != nil {
  87. log.Fatalln(err)
  88. }
  89. newFiles = append(newFiles, nd)
  90. }
  91. return oldFiles, newFiles
  92. }
  93. func MustRun(baseDir string) {
  94. oldFiles, newFiles := mustGetTestData(baseDir)
  95. goctlOld, err := exec.LookPath("goctl.old")
  96. must(err)
  97. goctlNew, err := exec.LookPath("goctl")
  98. must(err)
  99. fmt.Println(color.LightBlue.Render("========================goctl.old======================="))
  100. must(oldFiles.execute(goctlOld))
  101. fmt.Println()
  102. fmt.Println(color.LightBlue.Render("========================goctl.new======================="))
  103. must(newFiles.execute(goctlNew))
  104. }
  105. func must(err error) {
  106. if err != nil {
  107. log.Fatalln(err)
  108. }
  109. }