Skip to content

Commit

Permalink
test: Add approval test for public API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmg committed Aug 29, 2024
1 parent 56a7e20 commit 7fc5de3
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ jobs:
run: dotnet build --configuration Release

- name: Run unit tests
run: dotnet test --no-build --configuration Release
run: dotnet test test\Moniker.Tests --configuration Release --no-build --no-restore

- name: Run approval tests
run: dotnet test test\Moniker.ApprovalTests --configuration Release --no-build --no-restore

- name: Create NuGet packages
run: dotnet pack --configuration Release
Expand Down
6 changes: 6 additions & 0 deletions Moniker.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Moniker.PerformanceTests", "test\Moniker.PerformanceTests\Moniker.PerformanceTests.csproj", "{FF324EAB-F8AE-4B7E-B8E0-07BD24B537DD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Moniker.ApprovalTests", "test\Moniker.ApprovalTests\Moniker.ApprovalTests.csproj", "{C0555388-7087-408C-93C5-22D3FC2DA574}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -41,6 +43,10 @@ Global
{FF324EAB-F8AE-4B7E-B8E0-07BD24B537DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FF324EAB-F8AE-4B7E-B8E0-07BD24B537DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FF324EAB-F8AE-4B7E-B8E0-07BD24B537DD}.Release|Any CPU.Build.0 = Release|Any CPU
{C0555388-7087-408C-93C5-22D3FC2DA574}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C0555388-7087-408C-93C5-22D3FC2DA574}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C0555388-7087-408C-93C5-22D3FC2DA574}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C0555388-7087-408C-93C5-22D3FC2DA574}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
24 changes: 24 additions & 0 deletions test/Moniker.ApprovalTests/ApiApprovalTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using PublicApiGenerator;
using Shouldly;
using Xunit;

namespace Moniker.ApprovalTests;

public class ApiApprovalTests
{
[Fact]
public void PublicApiShouldNotChangeUnintentionally()
{
var assembly = typeof(NameGenerator).Assembly;
var publicApi = assembly.GeneratePublicApi(
new ApiGeneratorOptions
{
IncludeAssemblyAttributes = false,
ExcludeAttributes = ["System.Diagnostics.DebuggerDisplayAttribute"],
});

publicApi.ShouldMatchApproved(options =>
options.WithFilenameGenerator((_, _, fileType, fileExtension) =>
$"{assembly.GetName().Name!}.{fileType}.{fileExtension}"));
}
}
20 changes: 20 additions & 0 deletions test/Moniker.ApprovalTests/Moniker.ApprovalTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<DeterministicSourcePaths>false</DeterministicSourcePaths>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" PrivateAssets="all" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="PublicApiGenerator" Version="11.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Moniker\Moniker.csproj" />
</ItemGroup>

</Project>
15 changes: 15 additions & 0 deletions test/Moniker.ApprovalTests/Moniker.approved.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Moniker
{
public enum MonikerStyle
{
Moniker = 0,
Moby = 1,
}
public static class NameGenerator
{
public const string DefaultDelimiter = "-";
public static string Generate(Moniker.MonikerStyle monikerStyle, string delimiter = "-") { }
public static string GenerateMoby(string delimiter = "-") { }
public static string GenerateMoniker(string delimiter = "-") { }
}
}

0 comments on commit 7fc5de3

Please sign in to comment.