TestUtils.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace SharpGLTF
  5. {
  6. static class TestUtils
  7. {
  8. public static string ToShortDisplayPath(this string path)
  9. {
  10. var dir = System.IO.Path.GetDirectoryName(path);
  11. var fxt = System.IO.Path.GetFileName(path);
  12. const int maxdir = 12;
  13. if (dir.Length > maxdir)
  14. {
  15. dir = "..." + dir.Substring(dir.Length - maxdir);
  16. }
  17. return System.IO.Path.Combine(dir, fxt);
  18. }
  19. public static string GetAttachmentPath(this NUnit.Framework.TestContext context, string fileName, bool ensureDirectoryExists = false)
  20. {
  21. var path = System.IO.Path.Combine(context.TestDirectory, "TestResults", $"{context.Test.ID}");
  22. var dir = path;
  23. if (!string.IsNullOrWhiteSpace(fileName))
  24. {
  25. if (System.IO.Path.IsPathRooted(fileName)) throw new ArgumentException(nameof(fileName), "path must be a relative path");
  26. path = System.IO.Path.Combine(path, fileName);
  27. dir = System.IO.Path.GetDirectoryName(path);
  28. }
  29. System.IO.Directory.CreateDirectory(dir);
  30. return path;
  31. }
  32. public static void AttachToCurrentTest(this Schema2.ModelRoot model, string fileName)
  33. {
  34. // find the output path for the current test
  35. fileName = NUnit.Framework.TestContext.CurrentContext.GetAttachmentPath(fileName, true);
  36. if (fileName.ToLower().EndsWith(".glb"))
  37. {
  38. // clone the model so merging the buffers will not affect the source model.
  39. model = model.DeepClone();
  40. // ensure the model has just one buffer
  41. model.MergeImages();
  42. model.MergeBuffers();
  43. model.SaveGLB(fileName);
  44. }
  45. else if (fileName.ToLower().EndsWith(".gltf"))
  46. {
  47. model.SaveGLTF(fileName, Newtonsoft.Json.Formatting.Indented);
  48. }
  49. else if (fileName.ToLower().EndsWith(".obj"))
  50. {
  51. // evaluate all triangles of the model
  52. var wavefront = Schema2.ModelDumpUtils.ToWavefrontWriter(model).ToString();
  53. System.IO.File.WriteAllText(fileName, wavefront);
  54. }
  55. // Attach the saved file to the current test
  56. NUnit.Framework.TestContext.AddTestAttachment(fileName);
  57. }
  58. public static void SyncronizeGitRepository(string remoteUrl, string localDirectory)
  59. {
  60. if (LibGit2Sharp.Repository.Discover(localDirectory) == null)
  61. {
  62. NUnit.Framework.TestContext.Progress.WriteLine($"Cloning {remoteUrl} can take several minutes; Please wait...");
  63. LibGit2Sharp.Repository.Clone(remoteUrl, localDirectory);
  64. NUnit.Framework.TestContext.Progress.WriteLine($"... Clone Completed");
  65. return;
  66. }
  67. using (var repo = new LibGit2Sharp.Repository(localDirectory))
  68. {
  69. var options = new LibGit2Sharp.PullOptions
  70. {
  71. FetchOptions = new LibGit2Sharp.FetchOptions()
  72. };
  73. var r = LibGit2Sharp.Commands.Pull(repo, new LibGit2Sharp.Signature("Anonymous", "[email protected]", new DateTimeOffset(DateTime.Now)), options);
  74. NUnit.Framework.TestContext.Progress.WriteLine($"{remoteUrl} is {r.Status}");
  75. }
  76. }
  77. public static void AttachShowDirLink(this NUnit.Framework.TestContext context)
  78. {
  79. context.AttachFileLink("📂 Show Directory", context.GetAttachmentPath(string.Empty));
  80. }
  81. public static void AttachGltfValidatorLink(this NUnit.Framework.TestContext context)
  82. {
  83. context.AttachUrlLink("🌍 glTF Validator", "http://github.khronos.org/glTF-Validator/");
  84. }
  85. public static void AttachFileLink(this NUnit.Framework.TestContext context, string linkPath, string targetPath)
  86. {
  87. var sb = new StringBuilder();
  88. sb.AppendLine("[InternetShortcut]");
  89. sb.AppendLine("URL=file:///" + targetPath);
  90. sb.AppendLine("IconIndex=0");
  91. string icon = targetPath.Replace('\\', '/');
  92. sb.AppendLine("IconFile=" + icon);
  93. linkPath = System.IO.Path.ChangeExtension(linkPath, ".url");
  94. linkPath = context.GetAttachmentPath(linkPath, true);
  95. System.IO.File.WriteAllText(linkPath, sb.ToString());
  96. NUnit.Framework.TestContext.AddTestAttachment(linkPath);
  97. }
  98. public static void AttachUrlLink(this NUnit.Framework.TestContext context, string linkPath, string url)
  99. {
  100. var sb = new StringBuilder();
  101. sb.AppendLine("[InternetShortcut]");
  102. sb.AppendLine("URL=" + url);
  103. linkPath = System.IO.Path.ChangeExtension(linkPath, ".url");
  104. linkPath = context.GetAttachmentPath(linkPath, true);
  105. System.IO.File.WriteAllText(linkPath, sb.ToString());
  106. NUnit.Framework.TestContext.AddTestAttachment(linkPath);
  107. }
  108. }
  109. }