diff --git a/Tests/Approval.Tests/ApiApproval.cs b/Tests/Approval.Tests/ApiApproval.cs index c9b3726f6f..44163b414e 100644 --- a/Tests/Approval.Tests/ApiApproval.cs +++ b/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; @@ -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()!.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 OnlyIncludeChanges(string received, string verified, IReadOnlyDictionary _) - { - 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 { 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"); @@ -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())); } diff --git a/Tests/Approval.Tests/Approval.Tests.csproj b/Tests/Approval.Tests/Approval.Tests.csproj index 4241fef2be..6e05e11a5e 100644 --- a/Tests/Approval.Tests/Approval.Tests.csproj +++ b/Tests/Approval.Tests/Approval.Tests.csproj @@ -12,7 +12,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - +