|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Net; |
| 5 | +using System.Text; |
| 6 | +using System.Text.RegularExpressions; |
| 7 | +using FolderDiffIL4DotNet.Runner; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace FolderDiffIL4DotNet.Tests.Services |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Verifies that the checked-in report samples reflect the maintainer-managed creator profile. |
| 14 | + /// commit 済みレポートサンプルがメンテナー管理の creator profile を反映していることを検証します。 |
| 15 | + /// </summary> |
| 16 | + [Trait("Category", "Unit")] |
| 17 | + public sealed class GoldenReportSampleConsistencyTests |
| 18 | + { |
| 19 | + private const string SAMPLE_ONLY_NORMALIZATION_VALUE = "buildserver1_artifact"; |
| 20 | + private static readonly string RepositoryRoot = FindRepositoryRoot(); |
| 21 | + |
| 22 | + [Fact] |
| 23 | + public void MarkdownNormalizationTable_MatchesCreatorDefaultProfile() |
| 24 | + { |
| 25 | + string markdown = File.ReadAllText( |
| 26 | + Path.Combine(RepositoryRoot, "doc", "samples", "diff_report.md")); |
| 27 | + |
| 28 | + Assert.Equal( |
| 29 | + GetExpectedNormalizationValues(), |
| 30 | + ExtractMarkdownNormalizationValues(markdown)); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public void HtmlNormalizationTable_MatchesCreatorDefaultProfile() |
| 35 | + { |
| 36 | + string html = File.ReadAllText( |
| 37 | + Path.Combine(RepositoryRoot, "doc", "samples", "diff_report.html")); |
| 38 | + |
| 39 | + Assert.Equal( |
| 40 | + GetExpectedNormalizationValues(), |
| 41 | + ExtractHtmlNormalizationValues(html)); |
| 42 | + } |
| 43 | + |
| 44 | + private static string[] GetExpectedNormalizationValues() |
| 45 | + { |
| 46 | + IReadOnlyList<string> profileValues = CreatorPrivilegeIlIgnoreProfiles.GetStringsOrThrow( |
| 47 | + CreatorPrivilegeIlIgnoreProfiles.DefaultProfileName); |
| 48 | + var expected = new string[profileValues.Count + 1]; |
| 49 | + |
| 50 | + for (int index = 0; index < profileValues.Count; index++) |
| 51 | + { |
| 52 | + expected[index] = profileValues[index]; |
| 53 | + } |
| 54 | + |
| 55 | + // The sample appends one user-configured value to demonstrate overlap warnings. |
| 56 | + // sample は overlap warning の例示用に、ユーザー設定値を1件追加します。 |
| 57 | + expected[^1] = SAMPLE_ONLY_NORMALIZATION_VALUE; |
| 58 | + return expected; |
| 59 | + } |
| 60 | + |
| 61 | + private static string[] ExtractMarkdownNormalizationValues(string markdown) |
| 62 | + { |
| 63 | + const string header = "| Substring to Normalize (Escaped) |"; |
| 64 | + int headerIndex = markdown.IndexOf(header, StringComparison.Ordinal); |
| 65 | + Assert.True(headerIndex >= 0, "Markdown normalization table header was not found."); |
| 66 | + |
| 67 | + int firstRowIndex = markdown.IndexOf('\n', headerIndex + header.Length); |
| 68 | + Assert.True(firstRowIndex >= 0, "Markdown normalization table separator was not found."); |
| 69 | + |
| 70 | + var values = new List<string>(); |
| 71 | + using var reader = new StringReader(markdown[(firstRowIndex + 1)..]); |
| 72 | + // Skip the table separator. |
| 73 | + // table separator を読み飛ばします。 |
| 74 | + _ = reader.ReadLine(); |
| 75 | + |
| 76 | + string? line; |
| 77 | + while (!string.IsNullOrEmpty(line = reader.ReadLine())) |
| 78 | + { |
| 79 | + Assert.StartsWith("| ", line, StringComparison.Ordinal); |
| 80 | + Assert.EndsWith(" |", line, StringComparison.Ordinal); |
| 81 | + values.Add(DecodeMarkdownConfiguredValue(line[2..^2])); |
| 82 | + } |
| 83 | + |
| 84 | + return values.ToArray(); |
| 85 | + } |
| 86 | + |
| 87 | + private static string DecodeMarkdownConfiguredValue(string value) |
| 88 | + { |
| 89 | + string decoded = WebUtility.HtmlDecode(value); |
| 90 | + var original = new StringBuilder(decoded.Length); |
| 91 | + |
| 92 | + for (int index = 0; index < decoded.Length; index++) |
| 93 | + { |
| 94 | + char current = decoded[index]; |
| 95 | + if (current != '\\') |
| 96 | + { |
| 97 | + original.Append(current); |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + Assert.True( |
| 102 | + index + 1 < decoded.Length && decoded[index + 1] == '\\', |
| 103 | + "Markdown configured-value backslashes must be doubled."); |
| 104 | + original.Append('\\'); |
| 105 | + index++; |
| 106 | + } |
| 107 | + |
| 108 | + return original.ToString(); |
| 109 | + } |
| 110 | + |
| 111 | + private static string[] ExtractHtmlNormalizationValues(string html) |
| 112 | + { |
| 113 | + const string tableMarker = "aria-label=\"Substring to Normalize\""; |
| 114 | + int markerIndex = html.IndexOf(tableMarker, StringComparison.Ordinal); |
| 115 | + Assert.True(markerIndex >= 0, "HTML normalization table was not found."); |
| 116 | + |
| 117 | + int bodyStart = html.IndexOf("<tbody>", markerIndex, StringComparison.Ordinal); |
| 118 | + int bodyEnd = html.IndexOf("</tbody>", bodyStart, StringComparison.Ordinal); |
| 119 | + Assert.True(bodyStart >= 0 && bodyEnd > bodyStart, "HTML normalization table body was not found."); |
| 120 | + |
| 121 | + string body = html.Substring(bodyStart, bodyEnd - bodyStart); |
| 122 | + MatchCollection matches = Regex.Matches( |
| 123 | + body, |
| 124 | + @"<tr><td><code>(.*?)</code></td></tr>", |
| 125 | + RegexOptions.CultureInvariant | RegexOptions.Singleline); |
| 126 | + var values = new string[matches.Count]; |
| 127 | + |
| 128 | + for (int index = 0; index < matches.Count; index++) |
| 129 | + { |
| 130 | + values[index] = WebUtility.HtmlDecode(matches[index].Groups[1].Value); |
| 131 | + } |
| 132 | + |
| 133 | + return values; |
| 134 | + } |
| 135 | + |
| 136 | + private static string FindRepositoryRoot() |
| 137 | + { |
| 138 | + string? directory = AppContext.BaseDirectory; |
| 139 | + while (directory != null) |
| 140 | + { |
| 141 | + if (File.Exists(Path.Combine(directory, "FolderDiffIL4DotNet.sln"))) |
| 142 | + { |
| 143 | + return directory; |
| 144 | + } |
| 145 | + |
| 146 | + directory = Directory.GetParent(directory)?.FullName; |
| 147 | + } |
| 148 | + |
| 149 | + throw new DirectoryNotFoundException("Could not locate repository root."); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments