unit_test_helper.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. package unit_test_helper
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "path/filepath"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. // GetTestDataResourceRootPath 向上返回几层就能够到 ChineseSubFinder-TestData 同级目录,然后进入其中的 resourceFolderName 资源文件夹中
  12. func GetTestDataResourceRootPath(resourceFolderNames []string, goBackTimes int, userCopyData bool) string {
  13. times := ""
  14. for i := 0; i < goBackTimes; i++ {
  15. times += oneBackTime
  16. }
  17. outPath := times + testResourceProjectName
  18. for _, name := range resourceFolderNames {
  19. outPath += "/" + name
  20. }
  21. outPath = filepath.FromSlash(outPath)
  22. if userCopyData == true {
  23. // 想要 copy org 中的数据到 test 中去处理
  24. orgDir := filepath.Join(outPath, "org")
  25. if IsDir(orgDir) == false {
  26. // 如果没有发现 org 文件夹,就返回之前的路径即可
  27. return outPath
  28. }
  29. // 如果发现有,那启动 copy 的操作
  30. testDataPath, err := copyTestData(outPath)
  31. if err != nil {
  32. return outPath
  33. }
  34. return filepath.FromSlash(testDataPath)
  35. }
  36. return outPath
  37. }
  38. // GenerateShooterVideoFile 这里为 shooter 的接口专门生成一个视频文件,瑞克和莫蒂 (2013)\Season 5\S05E09 .mkv
  39. func GenerateShooterVideoFile(videoPartsRootPath string) (string, error) {
  40. const videoSize int64 = 640302895
  41. const videoName = "S05E09.mkv"
  42. const ext = ".videoPart"
  43. partNames := []string{"4096", "213434298", "426868596", "640294703"}
  44. outVideoFPath := filepath.Join(videoPartsRootPath, videoName)
  45. f, err := os.Create(outVideoFPath)
  46. if err != nil {
  47. return "", err
  48. }
  49. defer func() {
  50. _ = f.Close()
  51. }()
  52. if err := f.Truncate(videoSize); err != nil {
  53. return "", err
  54. }
  55. /*
  56. 一共有 4 个检测点
  57. */
  58. for _, name := range partNames {
  59. partF, err := os.Open(filepath.Join(videoPartsRootPath, name+ext))
  60. if err != nil {
  61. return "", err
  62. }
  63. partAll, err := io.ReadAll(partF)
  64. if err != nil {
  65. return "", err
  66. }
  67. int64Numb, err := strconv.ParseInt(name, 10, 64)
  68. if err != nil {
  69. return "", err
  70. }
  71. _, err = f.WriteAt(partAll, int64Numb)
  72. if err != nil {
  73. return "", err
  74. }
  75. }
  76. return outVideoFPath, nil
  77. }
  78. // GenerateXunleiVideoFile 这里为 xunlei 的接口专门生成一个视频文件,手机 (2003) 720p Cooker.rmvb
  79. func GenerateXunleiVideoFile(videoPartsRootPath string) (string, error) {
  80. const videoSize int64 = 640302895
  81. const videoName = "手机 (2003) 720p Cooker.rmvb"
  82. const ext = ".videoPart"
  83. partNames := []string{"0", "311177499", "933512018"}
  84. outVideoFPath := filepath.Join(videoPartsRootPath, videoName)
  85. f, err := os.Create(outVideoFPath)
  86. if err != nil {
  87. return "", err
  88. }
  89. defer func() {
  90. _ = f.Close()
  91. }()
  92. if err := f.Truncate(videoSize); err != nil {
  93. return "", err
  94. }
  95. /*
  96. 一共有 3 个检测点
  97. */
  98. for _, name := range partNames {
  99. partF, err := os.Open(filepath.Join(videoPartsRootPath, name+ext))
  100. if err != nil {
  101. return "", err
  102. }
  103. partAll, err := io.ReadAll(partF)
  104. if err != nil {
  105. return "", err
  106. }
  107. int64Numb, err := strconv.ParseInt(name, 10, 64)
  108. if err != nil {
  109. return "", err
  110. }
  111. _, err = f.WriteAt(partAll, int64Numb)
  112. if err != nil {
  113. return "", err
  114. }
  115. }
  116. return outVideoFPath, nil
  117. }
  118. // copyTestData 单元测试前把测试的数据 copy 一份出来操作,src 目录中默认应该有一个 org 原始数据文件夹,然后需要复制一份 test 文件夹出来
  119. func copyTestData(srcDir string) (string, error) {
  120. // 因为会出现,批量测试的需求,那么如果每次都进行一次清理,那么就会导致之前创建的被清理掉,测试用例失败
  121. // 可以简单的按时间来判断,如果当前时间与以及存在文件夹名称相差在 5min,那么就清理掉
  122. addString, _, _, _ := GetNowTimeString()
  123. // 测试数据的文件夹
  124. orgDir := filepath.Join(srcDir, "org")
  125. testDir := filepath.Join(srcDir, "test")
  126. if IsDir(testDir) == true {
  127. err := ClearFolderEx(testDir, 10)
  128. if err != nil {
  129. return "", err
  130. }
  131. }
  132. // 多加一层,这样在批量测试的时候才不会出错
  133. testDirEx := filepath.Join(testDir, addString)
  134. err := CopyDir(orgDir, testDirEx)
  135. if err != nil {
  136. return "", err
  137. }
  138. return testDirEx, nil
  139. }
  140. // IsDir 存在且是文件夹
  141. func IsDir(path string) bool {
  142. s, err := os.Stat(path)
  143. if err != nil {
  144. return false
  145. }
  146. return s.IsDir()
  147. }
  148. // CopyFile copies a single file from src to dst
  149. func CopyFile(src, dst string) error {
  150. var err error
  151. var srcFd *os.File
  152. var dstFd *os.File
  153. var srcInfo os.FileInfo
  154. if srcFd, err = os.Open(src); err != nil {
  155. return err
  156. }
  157. defer func() {
  158. _ = srcFd.Close()
  159. }()
  160. if dstFd, err = os.Create(dst); err != nil {
  161. return err
  162. }
  163. defer func() {
  164. _ = dstFd.Close()
  165. }()
  166. if _, err = io.Copy(dstFd, srcFd); err != nil {
  167. return err
  168. }
  169. if srcInfo, err = os.Stat(src); err != nil {
  170. return err
  171. }
  172. return os.Chmod(dst, srcInfo.Mode())
  173. }
  174. // CopyDir copies a whole directory recursively
  175. func CopyDir(src string, dst string) error {
  176. var err error
  177. var fds []os.DirEntry
  178. var srcInfo os.FileInfo
  179. if srcInfo, err = os.Stat(src); err != nil {
  180. return err
  181. }
  182. if err = os.MkdirAll(dst, srcInfo.Mode()); err != nil {
  183. return err
  184. }
  185. if fds, err = os.ReadDir(src); err != nil {
  186. return err
  187. }
  188. for _, fd := range fds {
  189. srcfp := filepath.Join(src, fd.Name())
  190. dstfp := filepath.Join(dst, fd.Name())
  191. if fd.IsDir() {
  192. if err = CopyDir(srcfp, dstfp); err != nil {
  193. fmt.Println(err)
  194. }
  195. } else {
  196. if err = CopyFile(srcfp, dstfp); err != nil {
  197. fmt.Println(err)
  198. }
  199. }
  200. }
  201. return nil
  202. }
  203. // GetNowTimeString 获取当前的时间,没有秒
  204. func GetNowTimeString() (string, int, int, int) {
  205. nowTime := time.Now()
  206. addString := fmt.Sprintf("%d-%d-%d", nowTime.Hour(), nowTime.Minute(), nowTime.Nanosecond())
  207. return addString, nowTime.Hour(), nowTime.Minute(), nowTime.Nanosecond()
  208. }
  209. // ClearFolderEx 清空文件夹,文件夹名称有特殊之处,Hour-min-Nanosecond 的命名方式
  210. // 如果调用的时候,已存在的文件夹的时间 min < 5 那么则清理
  211. func ClearFolderEx(folderFullPath string, overtime int) error {
  212. _, hour, minute, _ := GetNowTimeString()
  213. pathSep := string(os.PathSeparator)
  214. files, err := os.ReadDir(folderFullPath)
  215. if err != nil {
  216. return err
  217. }
  218. for _, curFile := range files {
  219. fullPath := folderFullPath + pathSep + curFile.Name()
  220. if curFile.IsDir() {
  221. parts := strings.Split(curFile.Name(), "-")
  222. if len(parts) == 3 {
  223. // 基本是符合了,倒是还是需要额外的判断是否时间超过了
  224. tmpHourStr := parts[0]
  225. tmpMinuteStr := parts[1]
  226. tmpHour, err := strconv.Atoi(tmpHourStr)
  227. if err != nil {
  228. // 如果不符合命名格式,直接删除
  229. err = os.RemoveAll(fullPath)
  230. if err != nil {
  231. return err
  232. }
  233. continue
  234. }
  235. tmpMinute, err := strconv.Atoi(tmpMinuteStr)
  236. if err != nil {
  237. // 如果不符合命名格式,直接删除
  238. err = os.RemoveAll(fullPath)
  239. if err != nil {
  240. return err
  241. }
  242. continue
  243. }
  244. // 判断时间
  245. if tmpHour != hour {
  246. // 如果不符合命名格式,直接删除
  247. err = os.RemoveAll(fullPath)
  248. if err != nil {
  249. return err
  250. }
  251. continue
  252. }
  253. // 超过 5 min
  254. if minute-overtime > tmpMinute {
  255. // 如果不符合命名格式,直接删除
  256. err = os.RemoveAll(fullPath)
  257. if err != nil {
  258. return err
  259. }
  260. continue
  261. }
  262. } else {
  263. // 如果不符合命名格式,直接删除
  264. err = os.RemoveAll(fullPath)
  265. if err != nil {
  266. return err
  267. }
  268. }
  269. } else {
  270. // 这里就是文件了
  271. err = os.Remove(fullPath)
  272. if err != nil {
  273. return err
  274. }
  275. }
  276. }
  277. return nil
  278. }
  279. const oneBackTime = "../"
  280. const testResourceProjectName = "ChineseSubFinder-TestData"