TestUtils.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. namespace MeloongCore.Tests;
  2. /// <summary>
  3. /// 用于带文件测试的基类。
  4. /// 会在极端路径下创建测试用的临时文件夹。
  5. /// </summary>
  6. public abstract class TestWithFolder {
  7. /// <summary>
  8. /// 测试用的临时文件夹路径。
  9. /// 这是一个包含特殊字符的长路径,不以 \\?\ 开头,以 \ 结尾。
  10. /// </summary>
  11. public readonly string tempFolder;
  12. public TestWithFolder() {
  13. tempFolder = Path.Combine(
  14. Path.GetTempPath(), "PCL", "Tests",
  15. $"{GetType().Name}-{Guid.NewGuid()}",
  16. "文件夹 Dir_!@#$%^&()_+={}[];',_",
  17. new string('X', 200), new string('X', 200)) + @"\";
  18. DirectoryUtils.Create(tempFolder);
  19. }
  20. /// <summary>
  21. /// 输出指定的测试用文件,返回文件路径。
  22. /// </summary>
  23. public string GetTestFile(string fileName) {
  24. var sourceFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles", GetType().Name.Replace("Test", ""), fileName);
  25. var distPath = Path.Combine(tempFolder, PathUtils.GetLastPart(sourceFilePath));
  26. FileUtils.Copy(sourceFilePath, distPath);
  27. return distPath;
  28. }
  29. }