|
10 | 10 | using Xunit;
|
11 | 11 | using static PublicApiGenerator.ApiGenerator;
|
12 | 12 | using System;
|
| 13 | +using System.IO; |
13 | 14 | using System.Linq;
|
14 | 15 | using System.Runtime.CompilerServices;
|
| 16 | +using System.Text; |
| 17 | +using ApprovalTests.Core; |
| 18 | +using ApprovalTests.Reporters; |
| 19 | +using ApprovalTests.Reporters.Mac; |
| 20 | +using ApprovalUtilities.Utilities; |
| 21 | +using DiffPlex; |
| 22 | +using DiffPlex.DiffBuilder; |
| 23 | +using DiffPlex.DiffBuilder.Model; |
| 24 | +using Xunit.Sdk; |
| 25 | +using P4MergeReporter = ApprovalTests.Reporters.P4MergeReporter; |
| 26 | +using P4MacMergeReporter = ApprovalTests.Reporters.Mac.P4MergeReporter; |
15 | 27 |
|
16 | 28 | namespace Hocon.API.Tests
|
17 | 29 | {
|
| 30 | +#if(DEBUG) |
| 31 | + [UseReporter(typeof(DiffPlexReporter), typeof(CustomDiffReporter), typeof(AllFailingTestsClipboardReporter))] |
| 32 | +#else |
| 33 | + [UseReporter(typeof(DiffPlexReporter), typeof(CustomDiffReporter))] |
| 34 | +#endif |
18 | 35 | public class HoconAPISpec
|
19 | 36 | {
|
20 | 37 | [Fact]
|
@@ -55,9 +72,123 @@ static string Filter(string text)
|
55 | 72 | {
|
56 | 73 | Environment.NewLine
|
57 | 74 | }, StringSplitOptions.RemoveEmptyEntries)
|
58 |
| - .Where(l => !l.StartsWith("[assembly: ReleaseDateAttribute(")) |
| 75 | + .Where(l => !l.StartsWith("[assembly: ReleaseDateAttribute(") && !l.StartsWith("[assembly: System.Reflection.AssemblyMetadataAttribute")) |
59 | 76 | .Where(l => !string.IsNullOrWhiteSpace(l))
|
60 | 77 | );
|
61 | 78 | }
|
62 | 79 | }
|
| 80 | + |
| 81 | + |
| 82 | + internal class ApiNotApprovedException : XunitException |
| 83 | + { |
| 84 | + public ApiNotApprovedException(string message) : base($"Failed API approval. Diff:\n{message}") |
| 85 | + { } |
| 86 | + |
| 87 | + public override string StackTrace { get; } = string.Empty; |
| 88 | + } |
| 89 | + |
| 90 | + #region Suppress FrameworkAssertReporter hack |
| 91 | + // The built-in FrameworkAssertReporter that is being called inside the DiffReporter class |
| 92 | + // is buggy in a CI/CD environment because it is trying to be clever, could not distinguish |
| 93 | + // between XUnit and XUnit2, and will throw Null Reference Exception every time it ran. |
| 94 | + // |
| 95 | + // This is probably fixed in latest version of ApiApprover but we couldn't switch to that |
| 96 | + // version because the latest ApiGenerator returns a different API report format. |
| 97 | + // |
| 98 | + // FIX: This hack removes FrameworkAssertReporter from the possible reporter list and retains |
| 99 | + // all of the other reporters in DiffReporter |
| 100 | + |
| 101 | + internal class CustomDiffReporter : FirstWorkingReporter |
| 102 | + { |
| 103 | + public CustomDiffReporter() : base( |
| 104 | + CustomWindowsDiffReporter.Instance, |
| 105 | + CustomMacDiffReporter.Instance) |
| 106 | + { } |
| 107 | + } |
| 108 | + |
| 109 | + internal class CustomMacDiffReporter : FirstWorkingReporter |
| 110 | + { |
| 111 | + public static readonly CustomMacDiffReporter Instance = new CustomMacDiffReporter(); |
| 112 | + public CustomMacDiffReporter() |
| 113 | + : base( |
| 114 | + |
| 115 | + BeyondCompareMacReporter.INSTANCE, |
| 116 | + DiffMergeReporter.INSTANCE, |
| 117 | + KaleidoscopeDiffReporter.INSTANCE, |
| 118 | + P4MacMergeReporter.INSTANCE, |
| 119 | + KDiff3Reporter.INSTANCE, |
| 120 | + TkDiffReporter.INSTANCE, |
| 121 | + QuietReporter.INSTANCE) |
| 122 | + { } |
| 123 | + |
| 124 | + public override bool IsWorkingInThisEnvironment(string forFile) => OsUtils.IsUnixOs() && base.IsWorkingInThisEnvironment(forFile); |
| 125 | + } |
| 126 | + |
| 127 | + internal class CustomWindowsDiffReporter : FirstWorkingReporter |
| 128 | + { |
| 129 | + public static readonly CustomWindowsDiffReporter Instance = new CustomWindowsDiffReporter(); |
| 130 | + public CustomWindowsDiffReporter() |
| 131 | + : base( |
| 132 | + CodeCompareReporter.INSTANCE, |
| 133 | + BeyondCompareReporter.INSTANCE, |
| 134 | + TortoiseDiffReporter.INSTANCE, |
| 135 | + AraxisMergeReporter.INSTANCE, |
| 136 | + P4MergeReporter.INSTANCE, |
| 137 | + WinMergeReporter.INSTANCE, |
| 138 | + KDiffReporter.INSTANCE, |
| 139 | + VisualStudioReporter.INSTANCE, |
| 140 | + QuietReporter.INSTANCE) |
| 141 | + { } |
| 142 | + |
| 143 | + public override bool IsWorkingInThisEnvironment(string forFile) => OsUtils.IsWindowsOs() && base.IsWorkingInThisEnvironment(forFile); |
| 144 | + } |
| 145 | + |
| 146 | + #endregion |
| 147 | + |
| 148 | + internal class DiffPlexReporter : IApprovalFailureReporter |
| 149 | + { |
| 150 | + public void Report(string approved, string received) |
| 151 | + { |
| 152 | + var approvedText = File.ReadAllText(approved); |
| 153 | + var receivedText = File.ReadAllText(received); |
| 154 | + |
| 155 | + var diffBuilder = new SideBySideDiffBuilder(new Differ()); |
| 156 | + var diff = diffBuilder.BuildDiffModel(approvedText, receivedText); |
| 157 | + |
| 158 | + var sb = new StringBuilder() |
| 159 | + .AppendLine($"<<<<<<<<< {Path.GetFileName(approved)}") |
| 160 | + .AppendDiff(diff.OldText) |
| 161 | + .AppendLine("=========") |
| 162 | + .AppendDiff(diff.NewText) |
| 163 | + .Append($">>>>>>>>> {Path.GetFileName(received)}"); |
| 164 | + |
| 165 | + //_out.WriteLine(sb.ToString()); |
| 166 | + throw new ApiNotApprovedException(sb.ToString()); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + internal static class Extensions |
| 171 | + { |
| 172 | + public static StringBuilder AppendDiff(this StringBuilder output, DiffPaneModel diff) |
| 173 | + { |
| 174 | + foreach (var line in diff.Lines) |
| 175 | + { |
| 176 | + switch (line.Type) |
| 177 | + { |
| 178 | + case ChangeType.Deleted: |
| 179 | + output.AppendLine($"[{line.Position:0000}] - {line.Text}"); |
| 180 | + break; |
| 181 | + case ChangeType.Inserted: |
| 182 | + output.AppendLine($"[{line.Position:0000}] + {line.Text}"); |
| 183 | + break; |
| 184 | + case ChangeType.Modified: |
| 185 | + output.AppendLine($"[{line.Position:0000}] ? {line.Text}"); |
| 186 | + break; |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + return output; |
| 191 | + } |
| 192 | + |
| 193 | + } |
63 | 194 | }
|
0 commit comments