MustangConformanceTestRunner.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System.Diagnostics;
  2. using System.Text;
  3. using System.Xml.Linq;
  4. using QuestPDF.Fluent;
  5. using QuestPDF.Infrastructure;
  6. namespace QuestPDF.ConformanceTests.TestEngine;
  7. public static class MustangConformanceTestRunner
  8. {
  9. public class ValidationResult
  10. {
  11. public bool IsDocumentValid => !FailedRules.Any();
  12. public ICollection<string> FailedRules { get; set; } = [];
  13. public string GetErrorMessage()
  14. {
  15. var errorMessage = new StringBuilder();
  16. foreach (var failedRule in FailedRules)
  17. {
  18. errorMessage.AppendLine($"🟥\tError");
  19. errorMessage.AppendLine($"\t{failedRule}");
  20. errorMessage.AppendLine();
  21. }
  22. return errorMessage.ToString();
  23. }
  24. }
  25. public static void TestConformance(string filePath)
  26. {
  27. var result = RunMustang(filePath);
  28. if (!result.IsDocumentValid)
  29. {
  30. Console.WriteLine(result.GetErrorMessage());
  31. Assert.Fail();
  32. }
  33. }
  34. private static ValidationResult RunMustang(string pdfFilePath)
  35. {
  36. if (!File.Exists(pdfFilePath))
  37. throw new FileNotFoundException($"PDF file not found: {pdfFilePath}");
  38. var mustangExecutablePath = Environment.GetEnvironmentVariable("MUSTANG_EXECUTABLE_PATH");
  39. if (string.IsNullOrEmpty(mustangExecutablePath))
  40. throw new Exception("The location path of the Mustang executable is not set. Set the MUSTANG_EXECUTABLE_PATH environment variable to the path of the Mustang executable.");
  41. var arguments = $"-jar {mustangExecutablePath} --action validate --source {pdfFilePath}";
  42. var process = new Process
  43. {
  44. StartInfo = new ProcessStartInfo
  45. {
  46. FileName = "java",
  47. Arguments = arguments,
  48. RedirectStandardOutput = true,
  49. RedirectStandardError = true,
  50. UseShellExecute = false,
  51. CreateNoWindow = true
  52. }
  53. };
  54. process.Start();
  55. var output = process.StandardOutput.ReadToEnd();
  56. process.WaitForExit();
  57. return new ValidationResult()
  58. {
  59. FailedRules = XDocument
  60. .Parse(output)
  61. .Descendants("error")
  62. .Select(x => x.Value)
  63. .ToList()
  64. };
  65. }
  66. }