Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove NUnit dependency #202

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions src/Particular.Approvals/Approver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
{
using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using NUnit.Framework;

/// <summary>
/// Verifies that values contain approved content.
/// </summary>
public static class Approver
{
static readonly string approvalFilesPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "..", "..", "..", "ApprovalFiles");
static readonly string approvalFilesPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "..", "..", "..", "ApprovalFiles");
static readonly JsonSerializerOptions jsonSerializerOptions;

static Approver()
Expand All @@ -32,32 +33,37 @@ static Approver()
/// <param name="value">The string to verify.</param>
/// <param name="scrubber">A delegate that modifies the received string before comparing it to the approval file.</param>
/// <param name="scenario">A value that will be added to the name of the approval file.</param>
public static void Verify(string value, Func<string, string> scrubber = null, string scenario = null)
/// <param name="callerFilePath">Do not provide a value for this parameter. It is populated with the value from <see cref="CallerFilePathAttribute"/>.</param>
/// <param name="callerMemberName">Do not provide a value for this parameter. It is populated with the value from <see cref="CallerMemberNameAttribute"/>.</param>
public static void Verify(string value, Func<string, string> scrubber = null, string scenario = null, [CallerFilePath] string callerFilePath = null, [CallerMemberName] string callerMemberName = null)
{
var parts = TestContext.CurrentContext.Test.ClassName.Split('.');
var className = parts[parts.Length - 1];
var methodName = TestContext.CurrentContext.Test.MethodName;
var fileName = Path.GetFileNameWithoutExtension(callerFilePath);
var scenarioName = string.IsNullOrEmpty(scenario) ? "" : scenario + ".";

if (scrubber != null)
{
value = scrubber(value);
}

var receivedFile = Path.Combine(approvalFilesPath, $"{className}.{methodName}.{scenarioName}received.txt");
var receivedFile = Path.Combine(approvalFilesPath, $"{fileName}.{callerMemberName}.{scenarioName}received.txt");
File.WriteAllText(receivedFile, value);

var approvedFile = Path.Combine(approvalFilesPath, $"{className}.{methodName}.{scenarioName}approved.txt");
var approvedFile = Path.Combine(approvalFilesPath, $"{fileName}.{callerMemberName}.{scenarioName}approved.txt");

if (!File.Exists(approvedFile))
{
File.WriteAllText(approvedFile, string.Empty);
}

var approvedText = File.ReadAllText(approvedFile);

var normalizedApprovedText = approvedText.Replace("\r\n", "\n");
var normalizedReceivedText = value.Replace("\r\n", "\n");

Assert.AreEqual(normalizedApprovedText, normalizedReceivedText, "Approval verification failed.");
if (!string.Equals(normalizedApprovedText, normalizedReceivedText))
{
throw new Exception("Approval verification failed.");
}

File.Delete(receivedFile);
}
Expand All @@ -68,11 +74,13 @@ public static void Verify(string value, Func<string, string> scrubber = null, st
/// <param name="value">The object to verify.</param>
/// <param name="scrubber">A delegate that modifies the received object, after it has been serialized, before comparing it to the approval file.</param>
/// <param name="scenario">A value that will be added to the name of the approval file.</param>
public static void Verify(object value, Func<string, string> scrubber = null, string scenario = null)
/// <param name="callerFilePath">Do not provide a value for this parameter. It is populated with the value from <see cref="CallerFilePathAttribute"/>.</param>
/// <param name="callerMemberName">Do not provide a value for this parameter. It is populated with the value from <see cref="CallerMemberNameAttribute"/>.</param>
public static void Verify(object value, Func<string, string> scrubber = null, string scenario = null, [CallerFilePath] string callerFilePath = null, [CallerMemberName] string callerMemberName = null)
{
var json = JsonSerializer.Serialize(value, value.GetType(), jsonSerializerOptions);

Verify(json, scrubber, scenario);
Verify(json, scrubber, scenario, callerFilePath, callerMemberName);
}
}
}
3 changes: 1 addition & 2 deletions src/Particular.Approvals/Particular.Approvals.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net472;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net472;net6.0</TargetFrameworks>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\NServiceBus.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnit" Version="[3.14.0, 4.0.0)" />
<PackageReference Include="Particular.Packaging" Version="4.0.0" PrivateAssets="All" />
<PackageReference Include="System.Text.Json" Version="8.0.0" />
</ItemGroup>
Expand Down
13 changes: 9 additions & 4 deletions src/Tests/ApproverTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Tests
{
using System;
using System.IO;
using NUnit.Framework;
using Particular.Approvals;
Expand Down Expand Up @@ -73,11 +74,15 @@ public void Can_handle_missing_approval_file_for_new_test()
File.Delete(approved);
}

var exception = Assert.Throws<AssertionException>(() => Approver.Verify(sample));
Assert.That(exception.Message, Contains.Substring("Approval verification failed"));
var exception = Assert.Throws<Exception>(() => Approver.Verify(sample));

Assert.That(File.Exists(approved));
Assert.That(File.ReadAllText(approved), Is.Empty);
Assert.Multiple(() =>
{
Assert.That(exception.Message, Contains.Substring("Approval verification failed"));

Assert.That(File.Exists(approved));
Assert.That(File.ReadAllText(approved), Is.Empty);
});

File.Delete(approved);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net472;net6.0;net7.0;net8.0</TargetFrameworks>
Expand All @@ -11,6 +11,8 @@
<ItemGroup>
<PackageReference Include="GitHubActionsTestLogger" Version="2.3.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="4.0.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>

Expand Down