TestUtils.cs 1.1 KB

123456789101112131415161718192021222324252627
  1. namespace MeloongCore.Tests;
  2. public static class TestUtils {
  3. /// <summary>
  4. /// 获取一个在极端路径下的测试文件夹。
  5. /// </summary>
  6. public static string GetTempFolder(string? identifier = null) {
  7. string folderPath = Path.Combine(
  8. Path.GetTempPath(), "PCL", "Tests",
  9. (identifier ?? "Test") + "-" + Guid.NewGuid().ToString(),
  10. "文件夹 Dir_!@#$%^&()_+={}[];',_",
  11. new string('X', 200), new string('X', 200));
  12. Directory.CreateDirectory(@"\\?\" + folderPath);
  13. return folderPath;
  14. }
  15. /// <summary>
  16. /// 将指定的测试用文件输出到一个极端路径下,然后返回文件路径。
  17. /// </summary>
  18. public static string GetTestFile(string directoryName, string fileName) {
  19. var sourceFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles", directoryName, fileName);
  20. var distPath = Path.Combine(GetTempFolder("GetTestFile"), Path.GetFileName(sourceFilePath));
  21. File.Copy(sourceFilePath, @"\\?\" + distPath);
  22. return distPath;
  23. }
  24. }