unit_test_helper.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. // GenerateXunleiVideoFile 这里为 xunlei 的接口专门生成一个视频文件,手机 (2003) 720p Cooker.rmvb
  77. func GenerateXunleiVideoFile(videoPartsRootPath string) (string, error) {
  78. const videoSize int64 = 640302895
  79. const videoName = "手机 (2003) 720p Cooker.rmvb"
  80. const ext = ".videoPart"
  81. partNames := []string{"0", "311177499", "933512018"}
  82. outVideoFPath := filepath.Join(videoPartsRootPath, videoName)
  83. f, err := os.Create(outVideoFPath)
  84. if err != nil {
  85. return "", err
  86. }
  87. defer func() {
  88. _ = f.Close()
  89. }()
  90. if err := f.Truncate(videoSize); err != nil {
  91. return "", err
  92. }
  93. /*
  94. 一共有 3 个检测点
  95. */
  96. for _, name := range partNames {
  97. partF, err := os.Open(filepath.Join(videoPartsRootPath, name+ext))
  98. if err != nil {
  99. return "", err
  100. }
  101. partAll, err := io.ReadAll(partF)
  102. if err != nil {
  103. return "", err
  104. }
  105. int64Numb, err := strconv.ParseInt(name, 10, 64)
  106. if err != nil {
  107. return "", err
  108. }
  109. _, err = f.WriteAt(partAll, int64Numb)
  110. if err != nil {
  111. return "", err
  112. }
  113. }
  114. return outVideoFPath, nil
  115. }
  116. // copyTestData 单元测试前把测试的数据 copy 一份出来操作,src 目录中默认应该有一个 org 原始数据文件夹,然后需要复制一份 test 文件夹出来
  117. func copyTestData(srcDir string) (string, error) {
  118. // 因为会出现,批量测试的需求,那么如果每次都进行一次清理,那么就会导致之前创建的被清理掉,测试用例失败
  119. // 可以简单的按时间来判断,如果当前时间与以及存在文件夹名称相差在 5min,那么就清理掉
  120. addString, _, _, _ := my_util.GetNowTimeString()
  121. // 测试数据的文件夹
  122. orgDir := filepath.Join(srcDir, "org")
  123. testDir := filepath.Join(srcDir, "test")
  124. if my_util.IsDir(testDir) == true {
  125. err := my_util.ClearFolderEx(testDir, 2)
  126. if err != nil {
  127. return "", err
  128. }
  129. }
  130. // 多加一层,这样在批量测试的时候才不会出错
  131. testDirEx := filepath.Join(testDir, addString)
  132. err := my_util.CopyDir(orgDir, testDirEx)
  133. if err != nil {
  134. return "", err
  135. }
  136. return testDirEx, nil
  137. }
  138. const oneBackTime = "../"
  139. const testResourceProjectName = "ChineseSubFinder-TestData"