Skip to content

Commit

Permalink
Simplify the ApiApproval test (#2381)
Browse files Browse the repository at this point in the history
* Simplify the ApiApproval test (1/2)

Simplify how paths relative to the solution directory are accessed.

* Simplify the ApiApproval test (2/2)

Actually use the Verify.DiffPlex integration instead of using DiffPlex directly.

* Address pull request feedback

* Use the new minimal diff output style
  • Loading branch information
0xced committed Oct 27, 2023
1 parent 2059c96 commit d0199f3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 57 deletions.
73 changes: 17 additions & 56 deletions Tests/Approval.Tests/ApiApproval.cs
@@ -1,16 +1,13 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.XPath;
using DiffPlex.DiffBuilder;
using DiffPlex.DiffBuilder.Model;
using PublicApiGenerator;
using VerifyTests;
using VerifyTests.DiffPlex;
using VerifyXunit;
using Xunit;

Expand All @@ -19,72 +16,30 @@ namespace Approval.Tests;
[UsesVerify]
public class ApiApproval
{
static ApiApproval() => VerifyDiffPlex.Initialize(OutputType.Minimal);

[Theory]
[ClassData(typeof(TargetFrameworksTheoryData))]
public Task ApproveApi(string frameworkVersion)
public Task ApproveApi(string framework)
{
string codeBase = Assembly.GetExecutingAssembly().Location;
var uri = new UriBuilder(new Uri(codeBase));
string assemblyPath = Uri.UnescapeDataString(uri.Path);
var containingDirectory = Path.GetDirectoryName(assemblyPath);
var configurationName = new DirectoryInfo(containingDirectory).Parent.Name;

var assemblyFile = Path.GetFullPath(
Path.Combine(
GetSourceDirectory(),
Path.Combine("..", "..", "Src", "FluentAssertions", "bin", configurationName, frameworkVersion,
"FluentAssertions.dll")));

var assembly = Assembly.LoadFile(Path.GetFullPath(assemblyFile));
var configuration = typeof(ApiApproval).Assembly.GetCustomAttribute<AssemblyConfigurationAttribute>()!.Configuration;
var assemblyFile = CombinedPaths("Src", "FluentAssertions", "bin", configuration, framework, "FluentAssertions.dll");
var assembly = Assembly.LoadFile(assemblyFile);
var publicApi = assembly.GeneratePublicApi(options: null);

return Verifier
.Verify(publicApi)
.ScrubLinesContaining("FrameworkDisplayName")
.UseDirectory(Path.Combine("ApprovedApi", "FluentAssertions"))
.UseStringComparer(OnlyIncludeChanges)
.UseFileName(frameworkVersion)
.UseFileName(framework)
.DisableDiff();
}

private static string GetSourceDirectory([CallerFilePath] string path = "") => Path.GetDirectoryName(path);

// Copied from https://github.com/VerifyTests/Verify.DiffPlex/blob/master/src/Verify.DiffPlex/VerifyDiffPlex.cs
public static Task<CompareResult> OnlyIncludeChanges(string received, string verified, IReadOnlyDictionary<string, object> _)
{
var diff = InlineDiffBuilder.Diff(verified, received);

var builder = new StringBuilder();

foreach (var line in diff.Lines)
{
switch (line.Type)
{
case ChangeType.Inserted:
builder.Append("+ ");
break;
case ChangeType.Deleted:
builder.Append("- ");
break;
default:
// omit unchanged files
continue;
}

builder.AppendLine(line.Text);
}

var compareResult = CompareResult.NotEqual(builder.ToString());
return Task.FromResult(compareResult);
}

private class TargetFrameworksTheoryData : TheoryData<string>
{
public TargetFrameworksTheoryData()
{
var csproj = Path.Combine(GetSourceDirectory(),
Path.Combine("..", "..", "Src", "FluentAssertions", "FluentAssertions.csproj"));

var csproj = CombinedPaths("Src", "FluentAssertions", "FluentAssertions.csproj");
var project = XDocument.Load(csproj);
var targetFrameworks = project.XPathSelectElement("/Project/PropertyGroup/TargetFrameworks");

Expand All @@ -94,4 +49,10 @@ public TargetFrameworksTheoryData()
}
}
}

private static string GetSolutionDirectory([CallerFilePath] string path = "") =>
Path.Combine(Path.GetDirectoryName(path)!, "..", "..");

private static string CombinedPaths(params string[] paths) =>
Path.GetFullPath(Path.Combine(paths.Prepend(GetSolutionDirectory()).ToArray()));
}
2 changes: 1 addition & 1 deletion Tests/Approval.Tests/Approval.Tests.csproj
Expand Up @@ -12,7 +12,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="PublicApiGenerator" Version="11.0.0" />
<PackageReference Include="Verify.DiffPlex" Version="2.2.1" />
<PackageReference Include="Verify.DiffPlex" Version="2.3.0" />
<PackageReference Include="Verify.Xunit" Version="22.1.4" />
</ItemGroup>

Expand Down

0 comments on commit d0199f3

Please sign in to comment.