-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add API approvals to tests #504
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
c845fb5
Add API approvals
Shane32 42d53c1
Update QRCoder/ASCIIQRCode.cs
Shane32 1e5dec0
Update newline
Shane32 55b41de
Merge branch 'apiapprovals' of https://github.com/Shane32/QRCoder int…
Shane32 a4e8805
Fix indents
Shane32 0cad5cf
Update QRCoderApiTests/QRCoderApiTests.csproj
Shane32 cdac65f
Update for .NET 6
Shane32 5a8f852
Include license
Shane32 3f1fdf0
Remove file-scoped namespace
Shane32 8bbd59a
Update workflow
Shane32 95c72e6
Update
Shane32 56eacae
Revert no-build
Shane32 109989c
Update QRCoderApiTests/ApiApprovalTests.cs
Shane32 e12a695
Fix ApiApprovalTests to run in debug or release
Shane32 d0852a5
Fix api approvals to eliminate sample 'testme' method
Shane32 d0354be
Manually fix api approvals
Shane32 c63df18
Updates to test workflow
Shane32 2013cab
Fix workflow
Shane32 4a7a1f2
Update workflows
Shane32 a063646
Merge remote-tracking branch 'origin/master' into apiapprovals
Shane32 38aa647
Update for merge
Shane32 50f40ec
Update other workflows
Shane32 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -219,3 +219,6 @@ pip-log.txt | |
*.userprefs | ||
|
||
QRCoder/PortabilityAnalysis.html | ||
|
||
# Unaccepted approval files | ||
*.received.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using PublicApiGenerator; | ||
using Shouldly; | ||
using System.Diagnostics; | ||
using System.Reflection; | ||
using System.Xml.Linq; | ||
using Xunit; | ||
|
||
namespace QRCoderApiTests; | ||
|
||
/// <summary> | ||
/// See more info about API approval tests here <see href="https://github.com/JakeGinnivan/ApiApprover"/>. | ||
/// </summary> | ||
public class ApiApprovalTests | ||
{ | ||
[Theory] | ||
[InlineData(typeof(QRCoder.QRCodeData))] | ||
[InlineData(typeof(QRCoder.Xaml.XamlQRCode))] | ||
public void PublicApi(Type type) | ||
{ | ||
string baseDir = AppDomain.CurrentDomain.BaseDirectory; | ||
string projectName = type.Assembly.GetName().Name!; | ||
string testDir = Path.Combine(baseDir, $"..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}.."); | ||
string projectDir = Path.Combine(testDir, ".."); | ||
string buildDir = Path.Combine(projectDir, projectName, "bin", "Debug"); | ||
Debug.Assert(Directory.Exists(buildDir), $"Directory '{buildDir}' doesn't exist"); | ||
string csProject = Path.Combine(projectDir, projectName, projectName + ".csproj"); | ||
var project = XDocument.Load(csProject); | ||
string[] tfms = project.Descendants("TargetFrameworks").Union(project.Descendants("TargetFramework")).First().Value.Split(";", StringSplitOptions.RemoveEmptyEntries); | ||
|
||
// There may be old stuff from earlier builds like net45, netcoreapp3.0, etc. so filter it out | ||
string[] actualTfmDirs = Directory.GetDirectories(buildDir).Where(dir => tfms.Any(tfm => dir.EndsWith(tfm))).ToArray(); | ||
Debug.Assert(actualTfmDirs.Length > 0, $"Directory '{buildDir}' doesn't contain subdirectories matching {string.Join(";", tfms)}"); | ||
|
||
(string tfm, string content)[] publicApi = actualTfmDirs.Select(tfmDir => (new DirectoryInfo(tfmDir).Name.Replace(".", ""), Assembly.LoadFile(Path.Combine(tfmDir, projectName + ".dll")).GeneratePublicApi(new ApiGeneratorOptions | ||
{ | ||
IncludeAssemblyAttributes = false, | ||
//AllowNamespacePrefixes = new[] { "Microsoft.Extensions.DependencyInjection" }, | ||
ExcludeAttributes = new[] { "System.Diagnostics.DebuggerDisplayAttribute", "System.Diagnostics.CodeAnalysis.AllowNullAttribute" } | ||
}) + Environment.NewLine)).ToArray(); | ||
|
||
if (publicApi.DistinctBy(item => item.content).Count() == 1) | ||
{ | ||
AutoApproveOrFail(publicApi[0].content, ""); | ||
} | ||
else | ||
{ | ||
foreach (var item in publicApi.ToLookup(item => item.content)) | ||
{ | ||
AutoApproveOrFail(item.Key, string.Join("+", item.Select(x => x.tfm).OrderBy(x => x))); | ||
} | ||
} | ||
|
||
// Approval test should (re)generate approved.txt files locally if needed. | ||
// Approval test should fail on CI. | ||
// https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables | ||
void AutoApproveOrFail(string publicApi, string folder) | ||
{ | ||
string file = null!; | ||
|
||
try | ||
{ | ||
publicApi.ShouldMatchApproved(options => options.SubFolder(folder).NoDiff().WithFilenameGenerator((testMethodInfo, discriminator, fileType, fileExtension) => file = $"{type.Assembly.GetName().Name}.{fileType}.{fileExtension}")); | ||
} | ||
catch (ShouldMatchApprovedException) when (Environment.GetEnvironmentVariable("CI") == null) | ||
{ | ||
string? received = Path.Combine(testDir, folder, file); | ||
string? approved = received.Replace(".received.txt", ".approved.txt"); | ||
if (File.Exists(received) && File.Exists(approved)) | ||
{ | ||
File.Copy(received, approved, overwrite: true); | ||
File.Delete(received); | ||
} | ||
else | ||
{ | ||
throw; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace QRCoder.Xaml | ||
{ | ||
public class XamlQRCode : QRCoder.AbstractQRCode, System.IDisposable | ||
{ | ||
public XamlQRCode() { } | ||
public XamlQRCode(QRCoder.QRCodeData data) { } | ||
public System.Windows.Media.DrawingImage GetGraphic(int pixelsPerModule) { } | ||
public System.Windows.Media.DrawingImage GetGraphic(int pixelsPerModule, bool drawQuietZones) { } | ||
public System.Windows.Media.DrawingImage GetGraphic(System.Windows.Size viewBox, bool drawQuietZones = true) { } | ||
public System.Windows.Media.DrawingImage GetGraphic(int pixelsPerModule, string darkColorHex, string lightColorHex, bool drawQuietZones = true) { } | ||
public System.Windows.Media.DrawingImage GetGraphic(System.Windows.Size viewBox, System.Windows.Media.Brush darkBrush, System.Windows.Media.Brush lightBrush, bool drawQuietZones = true) { } | ||
public double GetUnitsPerModule(System.Windows.Size viewBox, bool drawQuietZones = true) { } | ||
} | ||
public static class XamlQRCodeHelper | ||
{ | ||
public static System.Windows.Media.DrawingImage GetQRCode(string plainText, int pixelsPerModule, string darkColorHex, string lightColorHex, QRCoder.QRCodeGenerator.ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false, QRCoder.QRCodeGenerator.EciMode eciMode = 0, int requestedVersion = -1, bool drawQuietZones = true) { } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\QRCoder.Xaml\QRCoder.Xaml.csproj" /> | ||
<ProjectReference Include="..\QRCoder\QRCoder.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="PublicApiGenerator" Version="11.1.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" /> | ||
<PackageReference Include="shouldly" Version="4.0.3" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.