Skip to content

Commit 37fd20d

Browse files
authored
Auto approve API (#689)
1 parent fe040c8 commit 37fd20d

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

tests/ApiApprovalTests/ApiApprovalTests.cs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.IO;
44
using System.Linq;
55
using System.Reflection;
6-
using System.Runtime.ExceptionServices;
76
using System.Xml.Linq;
87
using PublicApiGenerator;
98
using Shouldly;
@@ -31,7 +30,8 @@ public void public_api_should_not_change_unintentionally(Type type)
3130
string baseDir = AppDomain.CurrentDomain.BaseDirectory;
3231
string projectName = type.Assembly.GetName().Name!;
3332
string projectFolderName = projectName["GraphQL.Server.".Length..];
34-
string projectDir = Path.Combine(baseDir, $"..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..", "src");
33+
string testDir = Path.Combine(baseDir, $"..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..");
34+
string projectDir = Path.Combine(testDir, $"..{Path.DirectorySeparatorChar}..", "src");
3535
string buildDir = Path.Combine(projectDir, projectFolderName, "bin", "Debug");
3636
Debug.Assert(Directory.Exists(buildDir), $"Directory '{buildDir}' doesn't exist");
3737
string csProject = Path.Combine(projectDir, projectFolderName, projectFolderName + ".csproj");
@@ -49,35 +49,43 @@ public void public_api_should_not_change_unintentionally(Type type)
4949
ExcludeAttributes = new[] { "System.Diagnostics.DebuggerDisplayAttribute", "System.Diagnostics.CodeAnalysis.AllowNullAttribute" }
5050
}))).ToArray();
5151

52-
// See: https://shouldly.readthedocs.io/en/latest/assertions/shouldMatchApproved.html
53-
// Note: If the AssemblyName.approved.txt file doesn't match the latest publicApi value,
54-
// this call will try to launch a diff tool to help you out but that can fail on
55-
// your machine if a diff tool isn't configured/setup.
5652
if (publicApi.DistinctBy(item => item.content).Count() == 1)
5753
{
58-
publicApi[0].content.ShouldMatchApproved(options => options.NoDiff().WithFilenameGenerator((testMethodInfo, discriminator, fileType, fileExtension) => $"{type.Assembly.GetName().Name}.{fileType}.{fileExtension}"));
54+
AutoApproveOrFail(publicApi[0].content, "");
5955
}
6056
else
6157
{
62-
// https://github.com/graphql-dotnet/server/pull/675#issuecomment-1001283947
63-
ExceptionDispatchInfo? error = null;
58+
foreach (var item in publicApi.ToLookup(item => item.content))
59+
{
60+
AutoApproveOrFail(item.Key, string.Join("+", item.Select(x => x.tfm).OrderBy(x => x)));
61+
}
62+
}
63+
64+
// Approval test should (re)generate approved.txt files locally if needed.
65+
// Approval test should fail on CI.
66+
// https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
67+
void AutoApproveOrFail(string publicApi, string folder)
68+
{
69+
string file = null!;
6470

65-
var uniqueApi = publicApi.ToLookup(item => item.content);
66-
foreach (var item in uniqueApi)
71+
try
72+
{
73+
publicApi.ShouldMatchApproved(options => options.SubFolder(folder).NoDiff().WithFilenameGenerator((testMethodInfo, discriminator, fileType, fileExtension) => file = $"{type.Assembly.GetName().Name}.{fileType}.{fileExtension}"));
74+
}
75+
catch (ShouldMatchApprovedException) when (Environment.GetEnvironmentVariable("CI") == null)
6776
{
68-
try
77+
string? received = Path.Combine(testDir, folder, file);
78+
string? approved = received.Replace(".received.txt", ".approved.txt");
79+
if (File.Exists(received) && File.Exists(approved))
6980
{
70-
item.Key.ShouldMatchApproved(options => options.SubFolder(string.Join("+", item.Select(x => x.tfm).OrderBy(x => x))).NoDiff().WithFilenameGenerator((testMethodInfo, discriminator, fileType, fileExtension) => $"{type.Assembly.GetName().Name}.{fileType}.{fileExtension}"));
81+
File.Copy(received, approved, overwrite: true);
82+
File.Delete(received);
7183
}
72-
catch (Exception ex)
84+
else
7385
{
74-
error = ExceptionDispatchInfo.Capture(ex);
86+
throw;
7587
}
7688
}
77-
78-
// It's OK to throw any exception occurred.
79-
if (error != null)
80-
error.Throw();
8189
}
8290
}
8391
}

0 commit comments

Comments
 (0)