SchemaTests.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System.Globalization;
  2. using System.Text.Json;
  3. using System.Text.Json.Nodes;
  4. using Json.Schema;
  5. using YamlDotNet.RepresentationModel;
  6. namespace Tests.Yaml;
  7. public class SchemaConformanceTests {
  8. private static JsonSchema LoadSchema(int version) {
  9. var schemaText = File.ReadAllText($"schemas/schema.v{version}.json");
  10. return JsonSchema.FromText(schemaText);
  11. }
  12. private static JsonElement ConvertYamlToJsonElement(string yaml) {
  13. // Load YAML into YAML DOM
  14. var yamlStream = new YamlStream();
  15. yamlStream.Load(new StringReader(yaml));
  16. YamlNode root = yamlStream.Documents[0].RootNode;
  17. // Convert YAML node → JSON string
  18. var json = ConvertYamlNodeToJson(root);
  19. using var document = JsonDocument.Parse(json);
  20. return document.RootElement.Clone();
  21. }
  22. private static string ConvertYamlNodeToJson(YamlNode node) {
  23. if (node is YamlScalarNode scalar) {
  24. // Try numeric
  25. if (int.TryParse(scalar.Value, out var i))
  26. return i.ToString();
  27. if (double.TryParse(scalar.Value, out var d))
  28. return d.ToString(CultureInfo.InvariantCulture);
  29. if (bool.TryParse(scalar.Value, out var b))
  30. return b.ToString().ToLowerInvariant();
  31. // Otherwise string
  32. return JsonSerializer.Serialize(scalar.Value);
  33. }
  34. if (node is YamlSequenceNode sequence) {
  35. IEnumerable<string> items = sequence.Children
  36. .Select(ConvertYamlNodeToJson);
  37. return "[" + string.Join(",", items) + "]";
  38. }
  39. if (node is YamlMappingNode mapping) {
  40. IEnumerable<string> props = mapping.Children
  41. .Select(kvp =>
  42. JsonSerializer.Serialize(((YamlScalarNode)kvp.Key).Value)
  43. + ":"
  44. + ConvertYamlNodeToJson(kvp.Value));
  45. return "{" + string.Join(",", props) + "}";
  46. }
  47. return "null";
  48. }
  49. /// <summary>
  50. /// The schema is published three times: the repo root copy tests validate
  51. /// against, and the copies the web app and the viewer serve at
  52. /// /schemas/v{n}/schema.v{n}.json. They are hand-synced, and #310/#311 were
  53. /// what happens when a sync is missed — this pins them together for good.
  54. /// </summary>
  55. [Theory]
  56. [InlineData(1)]
  57. [InlineData(2)]
  58. [InlineData(3)]
  59. [InlineData(4)]
  60. [InlineData(5)]
  61. public void The_served_schema_copies_never_drift_from_the_published_one(int version) {
  62. var published = JsonNode.Parse(
  63. File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "schemas", $"schema.v{version}.json")));
  64. foreach (var host in new[] { "web", "viewer" }) {
  65. var served = JsonNode.Parse(File.ReadAllText(
  66. Path.Combine(AppContext.BaseDirectory, "wwwroot-schemas", host, $"schema.v{version}.json")));
  67. Assert.True(
  68. JsonNode.DeepEquals(published, served),
  69. $"The {host} wwwroot copy of schema.v{version}.json differs from schemas/ — " +
  70. "update both together, or documents RackPeek writes will fail the schema it serves.");
  71. }
  72. }
  73. [Theory]
  74. [InlineData(1)]
  75. [InlineData(2)]
  76. [InlineData(3)]
  77. [InlineData(4)]
  78. [InlineData(5)]
  79. public void All_yaml_files_conform_to_schema(int version) {
  80. // Arrange
  81. JsonSchema schema = LoadSchema(version);
  82. var yamlFolder = Path.Combine(
  83. AppContext.BaseDirectory,
  84. "TestConfigs",
  85. $"v{version}");
  86. var yamlFiles = Directory
  87. .EnumerateFiles(yamlFolder, "*.yaml", SearchOption.AllDirectories)
  88. .ToList();
  89. Assert.NotEmpty(yamlFiles);
  90. var failures = new List<string>();
  91. // Act
  92. foreach (var file in yamlFiles) {
  93. var yaml = File.ReadAllText(file);
  94. JsonElement jsonNode = ConvertYamlToJsonElement(yaml);
  95. var options = new EvaluationOptions {
  96. OutputFormat = OutputFormat.Hierarchical
  97. };
  98. EvaluationResults result = schema.Evaluate(jsonNode, options);
  99. if (!result.IsValid) {
  100. var errors = new List<string>();
  101. void CollectErrors(EvaluationResults node) {
  102. if (node.Errors != null)
  103. foreach (KeyValuePair<string, string> error in node.Errors)
  104. errors.Add($"{error.Key}: {error.Value}");
  105. if (node.Details != null)
  106. foreach (EvaluationResults child in node.Details)
  107. CollectErrors(child);
  108. }
  109. CollectErrors(result);
  110. failures.Add(
  111. $"File: {file}{Environment.NewLine}" +
  112. string.Join(Environment.NewLine, errors));
  113. }
  114. }
  115. // Assert
  116. if (failures.Any()) {
  117. var message = string.Join(
  118. $"{Environment.NewLine}--------------------{Environment.NewLine}",
  119. failures);
  120. Assert.Fail(message);
  121. }
  122. }
  123. }