|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | +using System.Runtime.CompilerServices; |
| 5 | +using System.Text.RegularExpressions; |
| 6 | +using ShellUI.Templates; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace ShellUI.Tests; |
| 10 | + |
| 11 | +/// Asserts the @code block of each live src/ShellUI.Components/Components/*.razor |
| 12 | +/// matches the corresponding CLI template's emitted Content. Compares after stripping |
| 13 | +/// comments, blank lines, and whitespace differences so the legitimate divergence |
| 14 | +/// (namespace, formatting) doesn't fire, but real divergence (parameter list, JS |
| 15 | +/// interop calls, lifecycle methods) does. |
| 16 | +public class TemplateSyncTests |
| 17 | +{ |
| 18 | + // Component name → reason. Empty by default — fix the drift instead of adding entries. |
| 19 | + private static readonly Dictionary<string, string> AllowedDrift = new() |
| 20 | + { |
| 21 | + }; |
| 22 | + |
| 23 | + [Theory] |
| 24 | + [InlineData("sidebar-trigger", "SidebarTrigger.razor")] |
| 25 | + [InlineData("theme-toggle", "ThemeToggle.razor")] |
| 26 | + [InlineData("input-otp", "InputOTP.razor")] |
| 27 | + public void TemplateCodeBlock_MatchesLiveLibrary(string templateName, string razorFileName) |
| 28 | + { |
| 29 | + if (AllowedDrift.ContainsKey(templateName)) return; |
| 30 | + |
| 31 | + var liveContent = File.ReadAllText(GetLiveRazorPath(razorFileName)); |
| 32 | + var templateContent = ComponentRegistry.GetComponentContent(templateName) |
| 33 | + ?? throw new InvalidOperationException($"Template '{templateName}' not found in registry"); |
| 34 | + |
| 35 | + var liveCode = ExtractCodeBlock(liveContent) |
| 36 | + ?? throw new InvalidOperationException($"Live {razorFileName} has no @code block"); |
| 37 | + var templateCode = ExtractCodeBlock(templateContent) |
| 38 | + ?? throw new InvalidOperationException($"Template {templateName} has no @code block"); |
| 39 | + |
| 40 | + var normalizedLive = Normalize(liveCode); |
| 41 | + var normalizedTemplate = Normalize(templateCode); |
| 42 | + |
| 43 | + Assert.True(normalizedLive == normalizedTemplate, |
| 44 | + $"Drift detected between live {razorFileName} and template {templateName}.\n" + |
| 45 | + $"This usually means someone updated one but not the other. Sync them, or add " + |
| 46 | + $"\"{templateName}\" to AllowedDrift in TemplateSyncTests with a reason.\n\n" + |
| 47 | + DiffSummary(normalizedLive, normalizedTemplate)); |
| 48 | + } |
| 49 | + |
| 50 | + // [CallerFilePath] captures the absolute path of this source file at compile time, |
| 51 | + // so the test resolves the live components directory regardless of cwd on CI. |
| 52 | + private static string GetLiveRazorPath(string razorFileName, [CallerFilePath] string thisFile = "") |
| 53 | + { |
| 54 | + var testDir = Path.GetDirectoryName(thisFile) ?? throw new InvalidOperationException("CallerFilePath is empty"); |
| 55 | + var repoRoot = Path.GetFullPath(Path.Combine(testDir, "..")); |
| 56 | + return Path.Combine(repoRoot, "src", "ShellUI.Components", "Components", razorFileName); |
| 57 | + } |
| 58 | + |
| 59 | + private static string Normalize(string code) |
| 60 | + { |
| 61 | + var withoutBlock = Regex.Replace(code, @"/\*.*?\*/", string.Empty, RegexOptions.Singleline); |
| 62 | + var lines = withoutBlock.Split('\n') |
| 63 | + .Select(l => Regex.Replace(l, @"//.*$", string.Empty)) |
| 64 | + .Select(l => Regex.Replace(l.Trim(), @"\s+", " ")) |
| 65 | + .Where(l => !string.IsNullOrWhiteSpace(l)); |
| 66 | + return string.Join("\n", lines); |
| 67 | + } |
| 68 | + |
| 69 | + private static string DiffSummary(string live, string template) |
| 70 | + { |
| 71 | + var liveLines = live.Split('\n'); |
| 72 | + var tmplLines = template.Split('\n'); |
| 73 | + var max = Math.Max(liveLines.Length, tmplLines.Length); |
| 74 | + var diffs = new System.Text.StringBuilder(); |
| 75 | + var shown = 0; |
| 76 | + for (var i = 0; i < max && shown < 5; i++) |
| 77 | + { |
| 78 | + var l = i < liveLines.Length ? liveLines[i] : "<missing>"; |
| 79 | + var t = i < tmplLines.Length ? tmplLines[i] : "<missing>"; |
| 80 | + if (l != t) |
| 81 | + { |
| 82 | + diffs.AppendLine($" line {i + 1}"); |
| 83 | + diffs.AppendLine($" live: {l}"); |
| 84 | + diffs.AppendLine($" template: {t}"); |
| 85 | + shown++; |
| 86 | + } |
| 87 | + } |
| 88 | + return diffs.Length == 0 ? "(no per-line diff — file lengths differ)" : diffs.ToString(); |
| 89 | + } |
| 90 | + |
| 91 | + /// Extracts the body of the first `@code { ... }` block, balancing braces while |
| 92 | + /// respecting strings, verbatim strings, char literals, line comments, and block comments. |
| 93 | + /// Returns null if no `@code` block is found or braces are unbalanced. |
| 94 | + private static string? ExtractCodeBlock(string razor) |
| 95 | + { |
| 96 | + var match = Regex.Match(razor, @"@code\s*\{"); |
| 97 | + if (!match.Success) return null; |
| 98 | + |
| 99 | + var start = match.Index + match.Length; |
| 100 | + var depth = 1; |
| 101 | + var inString = false; |
| 102 | + var inVerbatimString = false; |
| 103 | + var inCharLiteral = false; |
| 104 | + var inLineComment = false; |
| 105 | + var inBlockComment = false; |
| 106 | + |
| 107 | + for (var i = start; i < razor.Length; i++) |
| 108 | + { |
| 109 | + var c = razor[i]; |
| 110 | + var next = i + 1 < razor.Length ? razor[i + 1] : '\0'; |
| 111 | + |
| 112 | + if (inLineComment) |
| 113 | + { |
| 114 | + if (c == '\n') inLineComment = false; |
| 115 | + continue; |
| 116 | + } |
| 117 | + if (inBlockComment) |
| 118 | + { |
| 119 | + if (c == '*' && next == '/') { inBlockComment = false; i++; } |
| 120 | + continue; |
| 121 | + } |
| 122 | + if (inVerbatimString) |
| 123 | + { |
| 124 | + if (c == '"' && next == '"') { i++; continue; } |
| 125 | + if (c == '"') inVerbatimString = false; |
| 126 | + continue; |
| 127 | + } |
| 128 | + if (inString) |
| 129 | + { |
| 130 | + if (c == '\\' && next != '\0') { i++; continue; } |
| 131 | + if (c == '"') inString = false; |
| 132 | + continue; |
| 133 | + } |
| 134 | + if (inCharLiteral) |
| 135 | + { |
| 136 | + if (c == '\\' && next != '\0') { i++; continue; } |
| 137 | + if (c == '\'') inCharLiteral = false; |
| 138 | + continue; |
| 139 | + } |
| 140 | + |
| 141 | + if (c == '/' && next == '/') { inLineComment = true; i++; continue; } |
| 142 | + if (c == '/' && next == '*') { inBlockComment = true; i++; continue; } |
| 143 | + if (c == '@' && next == '"') { inVerbatimString = true; i++; continue; } |
| 144 | + if (c == '"') { inString = true; continue; } |
| 145 | + if (c == '\'') { inCharLiteral = true; continue; } |
| 146 | + |
| 147 | + if (c == '{') depth++; |
| 148 | + else if (c == '}') |
| 149 | + { |
| 150 | + depth--; |
| 151 | + if (depth == 0) return razor.Substring(start, i - start); |
| 152 | + } |
| 153 | + } |
| 154 | + return null; |
| 155 | + } |
| 156 | +} |
0 commit comments