Skip to content

Commit

Permalink
housekeeping: move to using Verify (#781)
Browse files Browse the repository at this point in the history
* move to using Verify

* use scrubbing

* rename snapshots

* rename snapshots

* apply verify to drawing api test

* fix method name

* Update Splat.Drawing.csproj

* remove regex
  • Loading branch information
SimonCropp authored Oct 1, 2021
1 parent beccc78 commit 5e82105
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 513 deletions.
2 changes: 1 addition & 1 deletion src/Directory.build.props
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<PackageReference Include="FluentAssertions" Version="6.1.0" />
<PackageReference Include="Microsoft.Reactive.Testing" Version="5.0.0" />
<PackageReference Include="PublicApiGenerator" Version="10.2.0" />
<PackageReference Include="DiffEngine" Version="8.0.0" />
<PackageReference Include="Verify.Xunit" Version="12.2.0" />
</ItemGroup>

<ItemGroup Condition="$(IsTestProject)">
Expand Down

This file was deleted.

10 changes: 6 additions & 4 deletions src/Splat.Drawing.Tests/API/ApiApprovalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@
// See the LICENSE file in the project root for full license information.

using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using VerifyXunit;
using Xunit;

#pragma warning disable SA1615 // Element return value should be documented

namespace Splat.Tests
{
/// <summary>
/// Tests to make sure that the API matches the approved ones.
/// </summary>
[ExcludeFromCodeCoverage]
[UsesVerify]
public class ApiApprovalTests
{
/// <summary>
/// Tests to make sure the splat project is approved.
/// </summary>
[Fact]
public void SplatUIProject()
{
typeof(IPlatformModeDetector).Assembly.CheckApproval();
}
public Task SplatUIProject() => typeof(IPlatformModeDetector).Assembly.CheckApproval();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Splat.Drawing.Tests")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Splat.Drawing.Tests")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Splat.TestRunner.Android")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Splat.TestRunner.Uwp")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Splat.Tests")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Splat.Drawing.Tests")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Splat.Drawing.Tests")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Splat.TestRunner.Android")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Splat.TestRunner.Uwp")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Splat.Tests")]
Expand Down
9 changes: 5 additions & 4 deletions src/Splat.Tests/API/ApiApprovalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@
#if !WINDOWS_UWP && !ANDROID

using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using VerifyXunit;
using Xunit;
#pragma warning disable SA1615 // Element return value should be documented

namespace Splat.Tests
{
/// <summary>
/// Tests to make sure that the API matches the approved ones.
/// </summary>
[ExcludeFromCodeCoverage]
[UsesVerify]
public class ApiApprovalTests
{
/// <summary>
/// Tests to make sure the splat project is approved.
/// </summary>
[Fact]
public void SplatProject()
{
typeof(AssemblyFinder).Assembly.CheckApproval();
}
public Task SplatProject() => typeof(AssemblyFinder).Assembly.CheckApproval();
}
}

Expand Down
65 changes: 12 additions & 53 deletions src/Splat.Tests/ApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,13 @@
// See the LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

using DiffEngine;

using PublicApiGenerator;
using VerifyXunit;

using Xunit;
#pragma warning disable SA1615 // Element return value should be documented

namespace Splat.Tests
{
Expand All @@ -26,57 +19,23 @@ namespace Splat.Tests
/// </summary>
public static class ApiExtensions
{
private static readonly Regex _removeCoverletSectionRegex = new(@"^namespace Coverlet\.Core\.Instrumentation\.Tracker.*?^}", RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.Compiled);

/// <summary>
/// Checks to make sure the API is approved.
/// </summary>
/// <param name="assembly">The assembly that is being checked.</param>
/// <param name="memberName">The caller member.</param>
/// <param name="filePath">The caller file path.</param>
public static void CheckApproval(this Assembly assembly, [CallerMemberName] string? memberName = null, [CallerFilePath] string? filePath = null)
public static Task CheckApproval(this Assembly assembly, [CallerFilePath] string filePath = "")
{
var targetFrameworkName = Assembly.GetExecutingAssembly().GetTargetFrameworkName();

var sourceDirectory = Path.GetDirectoryName(filePath);

var approvedFileName = Path.Combine(sourceDirectory!, $"ApiApprovalTests.{memberName}.{targetFrameworkName}.approved.txt");
var receivedFileName = Path.Combine(sourceDirectory!, $"ApiApprovalTests.{memberName}.{targetFrameworkName}.received.txt");

string approvedPublicApi = string.Empty;

if (File.Exists(approvedFileName))
{
approvedPublicApi = File.ReadAllText(approvedFileName);
}

var generatorOptions = new ApiGeneratorOptions { WhitelistedNamespacePrefixes = new[] { "Splat" } };
var receivedPublicApi = Filter(ApiGenerator.GeneratePublicApi(assembly, generatorOptions));

if (!string.Equals(receivedPublicApi, approvedPublicApi, StringComparison.Ordinal))
{
File.WriteAllText(receivedFileName, receivedPublicApi);
DiffRunner.Launch(receivedFileName, approvedFileName);
}

Assert.Equal(approvedPublicApi, receivedPublicApi);
}

private static string Filter(string text)
{
text = _removeCoverletSectionRegex.Replace(text, string.Empty);
return string.Join(Environment.NewLine, text.Split(
new[]
{
Environment.NewLine
},
StringSplitOptions.RemoveEmptyEntries)
.Where(l =>
!l.StartsWith("[assembly: AssemblyVersion(", StringComparison.InvariantCulture) &&
!l.StartsWith("[assembly: AssemblyFileVersion(", StringComparison.InvariantCulture) &&
!l.StartsWith("[assembly: AssemblyInformationalVersion(", StringComparison.InvariantCulture) &&
!l.StartsWith("[assembly: System.Reflection.AssemblyMetadata(", StringComparison.InvariantCulture) &&
!string.IsNullOrWhiteSpace(l)));
var apiText = assembly.GeneratePublicApi(generatorOptions);
return Verifier.Verify(apiText, null, filePath)
.UniqueForRuntimeAndVersion()
.ScrubEmptyLines()
.ScrubLines(l =>
l.StartsWith("[assembly: AssemblyVersion(", StringComparison.InvariantCulture) ||
l.StartsWith("[assembly: AssemblyFileVersion(", StringComparison.InvariantCulture) ||
l.StartsWith("[assembly: AssemblyInformationalVersion(", StringComparison.InvariantCulture) ||
l.StartsWith("[assembly: System.Reflection.AssemblyMetadata(", StringComparison.InvariantCulture));
}
}
}

0 comments on commit 5e82105

Please sign in to comment.