Skip to content

Commit

Permalink
Merge pull request #725 from Humanizr/fix-approval-nullref
Browse files Browse the repository at this point in the history
Use DiffPlex reporter to avoid NRE on build
  • Loading branch information
Oren Novotny authored Jun 30, 2018
2 parents f9c685e + da025ad commit 99902de
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
49 changes: 49 additions & 0 deletions src/Humanizer.Tests/ApiApprover/DiffPlexReporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#if NET46
// 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 Humanizer.Tests.ApiApprover
{
public class DiffPlexReporter : IApprovalFailureReporter
{
public static readonly 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);
}
}
}
}
#endif
12 changes: 11 additions & 1 deletion src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@
using ApprovalTests.Reporters;
using PublicApiGenerator;
using Xunit;
using Xunit.Abstractions;

namespace Humanizer.Tests.ApiApprover
{

public class PublicApiApprovalTest
{
public PublicApiApprovalTest(ITestOutputHelper output)
{
DiffPlexReporter.Instance.Output = output;
}

[Fact]
[UseCulture("en-US")]
[UseReporter(typeof(DiffReporter))]
#if DEBUG
[UseReporter(typeof(DiffReporter))]
#else
[UseReporter(typeof(DiffPlexReporter))]
#endif
[IgnoreLineEndings(true)]
public void approve_public_api()
{
Expand Down
1 change: 1 addition & 0 deletions src/Humanizer.Tests/Humanizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<Reference Include="System.ComponentModel.DataAnnotations" />
<PackageReference Include="PublicApiGenerator" Version="7.0.1" />
<PackageReference Include="ApiApprover" Version="7.0.1" />
<PackageReference Include="DiffPlex" Version="1.4.1" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\Humanizer.Tests.Shared\**\*.cs">
Expand Down

0 comments on commit 99902de

Please sign in to comment.