unit_test_helper.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package unit_test_helper
  2. import (
  3. "github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
  4. "io"
  5. "os"
  6. "path/filepath"
  7. "strconv"
  8. )
  9. // GetTestDataResourceRootPath 向上返回几层就能够到 ChineseSubFinder-TestData 同级目录,然后进入其中的 resourceFolderName 资源文件夹中
  10. func GetTestDataResourceRootPath(resourceFolderNames []string, goBackTimes int, userCopyData bool) string {
  11. times := ""
  12. for i := 0; i < goBackTimes; i++ {
  13. times += oneBackTime
  14. }
  15. outPath := times + testResourceProjectName
  16. for _, name := range resourceFolderNames {
  17. outPath += "/" + name
  18. }
  19. outPath = filepath.FromSlash(outPath)
  20. if userCopyData == true {
  21. // 想要 copy org 中的数据到 test 中去处理
  22. orgDir := filepath.Join(outPath, "org")
  23. if my_util.IsDir(orgDir) == false {
  24. // 如果没有发现 org 文件夹,就返回之前的路径即可
  25. return outPath
  26. }
  27. // 如果发现有,那启动 copy 的操作
  28. testDataPath, err := copyTestData(outPath)
  29. if err != nil {
  30. return outPath
  31. }
  32. return filepath.FromSlash(testDataPath)
  33. }
  34. return outPath
  35. }
  36. // GenerateShooterVideoFile 这里为 shooter 的接口专门生成一个视频文件,瑞克和莫蒂 (2013)\Season 5\S05E09 .mkv
  37. func GenerateShooterVideoFile(videoPartsRootPath string) (string, error) {
  38. const videoSize int64 = 640302895
  39. const videoName = "S05E09.mkv"
  40. const ext = ".videoPart"
  41. partNames := []string{"4096", "213434298", "426868596", "640294703"}
  42. outVideoFPath := filepath.Join(videoPartsRootPath, videoName)
  43. f, err := os.Create(outVideoFPath)
  44. if err != nil {
  45. return "", err
  46. }
  47. defer func() {
  48. _ = f.Close()
  49. }()
  50. if err := f.Truncate(videoSize); err != nil {
  51. return "", err
  52. }
  53. /*
  54. 一共有 4 个检测点
  55. */
  56. for _, name := range partNames {
  57. partF, err := os.Open(filepath.Join(videoPartsRootPath, name+ext))
  58. if err != nil {
  59. return "", err
  60. }
  61. partAll, err := io.ReadAll(partF)
  62. if err != nil {
  63. return "", err
  64. }
  65. int64Numb, err := strconv.ParseInt(name, 10, 64)
  66. if err != nil {
  67. return "", err
  68. }
  69. _, err = f.WriteAt(partAll, int64Numb)
  70. if err != nil {
  71. return "", err
  72. }
  73. }
  74. return outVideoFPath, nil
  75. }
  76. // copyTestData 单元测试前把测试的数据 copy 一份出来操作,src 目录中默认应该有一个 org 原始数据文件夹,然后需要复制一份 test 文件夹出来
  77. func copyTestData(srcDir string) (string, error) {
  78. // 测试数据的文件夹
  79. orgDir := filepath.Join(srcDir, "org")
  80. testDir := filepath.Join(srcDir, "test")
  81. if my_util.IsDir(testDir) == true {
  82. err := my_util.ClearFolder(testDir)
  83. if err != nil {
  84. return "", err
  85. }
  86. }
  87. err := my_util.CopyDir(orgDir, testDir)
  88. if err != nil {
  89. return "", err
  90. }
  91. return testDir, nil
  92. }
  93. const oneBackTime = "../"
  94. const testResourceProjectName = "ChineseSubFinder-TestData"