testdata.go 2.6 KB

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