Bläddra i källkod

Add DiffPlexReporter for api approvals (#571)

This allows for diffing in the test execution and outputting it
using xunit's output helper. This allows for all builds to report
some context regarding changes that cause approvals to fail.
Ryan Wersal 7 år sedan
förälder
incheckning
2928a12aa0

+ 10 - 0
Rx.NET/Source/tests/Tests.System.Reactive.ApiApprovals/Api/ApiApprovalTests.cs

@@ -9,13 +9,23 @@ using System;
 using System.Linq;
 using System.Reflection;
 using Xunit;
+using Xunit.Abstractions;
 
 namespace ReactiveTests.Tests.Api
 {
+#if DEBUG
     [UseReporter(typeof(DiffReporter))]
+#else
+    [UseReporter(typeof(DiffPlexReporter))]
+#endif
     [IgnoreLineEndings(true)]
     public class ApiApprovalTests
     {
+        public ApiApprovalTests(ITestOutputHelper output)
+        {
+            DiffPlexReporter.INSTANCE.Output = output;
+        }
+
         [Fact]
         public void Core()
         {

+ 47 - 0
Rx.NET/Source/tests/Tests.System.Reactive.ApiApprovals/DiffPlexReporter.cs

@@ -0,0 +1,47 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the Apache 2.0 License.
+// See the LICENSE file in the project root for more information. 
+
+using ApprovalTests.Core;
+using DiffPlex;
+using DiffPlex.DiffBuilder;
+using DiffPlex.DiffBuilder.Model;
+using System.IO;
+using Xunit.Abstractions;
+
+namespace ReactiveTests.Tests
+{
+    public class DiffPlexReporter : IApprovalFailureReporter
+    {
+        public static DiffPlexReporter INSTANCE = new DiffPlexReporter();
+
+        public ITestOutputHelper Output { get; set; }
+
+        public void Report(string approved, string received)
+        {
+            var approvedText = File.Exists(approved) ? File.ReadAllText(approved) : string.Empty;
+            var receivedText = File.ReadAllText(received);
+
+            var diffBuilder = new InlineDiffBuilder(new Differ());
+            var diff = diffBuilder.BuildDiffModel(approvedText, receivedText);
+
+            foreach (var line in diff.Lines)
+            {
+                if (line.Type == ChangeType.Unchanged) continue;
+
+                var prefix = "  ";
+                switch (line.Type)
+                {
+                    case ChangeType.Inserted:
+                        prefix = "+ ";
+                        break;
+                    case ChangeType.Deleted:
+                        prefix = "- ";
+                        break;
+                }
+
+                Output.WriteLine("{0}{1}", prefix, line.Text);
+            }
+        }
+    }
+}

+ 1 - 0
Rx.NET/Source/tests/Tests.System.Reactive.ApiApprovals/Tests.System.Reactive.ApiApprovals.csproj

@@ -25,6 +25,7 @@
     <PackageReference Include="xunit" Version="2.4.0-beta.2.build4010" />
     <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0-beta.2.build4010" />
     <PackageReference Include="ApprovalTests" Version="3.0.14" />
+    <PackageReference Include="DiffPlex" Version="1.4.1" />
     <PackageReference Include="PublicApiGenerator" Version="7.0.1" />
   </ItemGroup>