|
8 | 8 | using System.Xml.Linq;
|
9 | 9 | using Xunit;
|
10 | 10 |
|
11 |
| -namespace QRCoderApiTests; |
12 |
| - |
13 | 11 | /*********************************************
|
14 | 12 | *
|
15 | 13 | * This file copied from GraphQL.NET on 4/26/2024
|
@@ -38,72 +36,75 @@ namespace QRCoderApiTests;
|
38 | 36 | *
|
39 | 37 | *********************************************/
|
40 | 38 |
|
41 |
| -/// <summary> |
42 |
| -/// See more info about API approval tests here <see href="https://github.com/JakeGinnivan/ApiApprover"/>. |
43 |
| -/// </summary> |
44 |
| -public class ApiApprovalTests |
| 39 | +namespace QRCoderApiTests |
45 | 40 | {
|
46 |
| - [Theory] |
47 |
| - [InlineData(typeof(QRCoder.QRCodeData))] |
48 |
| - [InlineData(typeof(QRCoder.Xaml.XamlQRCode))] |
49 |
| - public void PublicApi(Type type) |
| 41 | + /// <summary> |
| 42 | + /// See more info about API approval tests here <see href="https://github.com/JakeGinnivan/ApiApprover"/>. |
| 43 | + /// </summary> |
| 44 | + public class ApiApprovalTests |
50 | 45 | {
|
51 |
| - string baseDir = AppDomain.CurrentDomain.BaseDirectory; |
52 |
| - string projectName = type.Assembly.GetName().Name!; |
53 |
| - string testDir = Path.Combine(baseDir, $"..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}.."); |
54 |
| - string projectDir = Path.Combine(testDir, ".."); |
55 |
| - string buildDir = Path.Combine(projectDir, projectName, "bin", "Debug"); |
56 |
| - Debug.Assert(Directory.Exists(buildDir), $"Directory '{buildDir}' doesn't exist"); |
57 |
| - string csProject = Path.Combine(projectDir, projectName, projectName + ".csproj"); |
58 |
| - var project = XDocument.Load(csProject); |
59 |
| - string[] tfms = project.Descendants("TargetFrameworks").Union(project.Descendants("TargetFramework")).First().Value.Split(";", StringSplitOptions.RemoveEmptyEntries); |
| 46 | + [Theory] |
| 47 | + [InlineData(typeof(QRCoder.QRCodeData))] |
| 48 | + [InlineData(typeof(QRCoder.Xaml.XamlQRCode))] |
| 49 | + public void PublicApi(Type type) |
| 50 | + { |
| 51 | + string baseDir = AppDomain.CurrentDomain.BaseDirectory; |
| 52 | + string projectName = type.Assembly.GetName().Name!; |
| 53 | + string testDir = Path.Combine(baseDir, $"..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}.."); |
| 54 | + string projectDir = Path.Combine(testDir, ".."); |
| 55 | + string buildDir = Path.Combine(projectDir, projectName, "bin", "Debug"); |
| 56 | + Debug.Assert(Directory.Exists(buildDir), $"Directory '{buildDir}' doesn't exist"); |
| 57 | + string csProject = Path.Combine(projectDir, projectName, projectName + ".csproj"); |
| 58 | + var project = XDocument.Load(csProject); |
| 59 | + string[] tfms = project.Descendants("TargetFrameworks").Union(project.Descendants("TargetFramework")).First().Value.Split(";", StringSplitOptions.RemoveEmptyEntries); |
60 | 60 |
|
61 |
| - // There may be old stuff from earlier builds like net45, netcoreapp3.0, etc. so filter it out |
62 |
| - string[] actualTfmDirs = Directory.GetDirectories(buildDir).Where(dir => tfms.Any(tfm => dir.EndsWith(tfm))).ToArray(); |
63 |
| - Debug.Assert(actualTfmDirs.Length > 0, $"Directory '{buildDir}' doesn't contain subdirectories matching {string.Join(";", tfms)}"); |
| 61 | + // There may be old stuff from earlier builds like net45, netcoreapp3.0, etc. so filter it out |
| 62 | + string[] actualTfmDirs = Directory.GetDirectories(buildDir).Where(dir => tfms.Any(tfm => dir.EndsWith(tfm))).ToArray(); |
| 63 | + Debug.Assert(actualTfmDirs.Length > 0, $"Directory '{buildDir}' doesn't contain subdirectories matching {string.Join(";", tfms)}"); |
64 | 64 |
|
65 |
| - (string tfm, string content)[] publicApi = actualTfmDirs.Select(tfmDir => (new DirectoryInfo(tfmDir).Name.Replace(".", ""), Assembly.LoadFile(Path.Combine(tfmDir, projectName + ".dll")).GeneratePublicApi(new ApiGeneratorOptions |
66 |
| - { |
67 |
| - IncludeAssemblyAttributes = false, |
68 |
| - //AllowNamespacePrefixes = new[] { "Microsoft.Extensions.DependencyInjection" }, |
69 |
| - ExcludeAttributes = new[] { "System.Diagnostics.DebuggerDisplayAttribute", "System.Diagnostics.CodeAnalysis.AllowNullAttribute" } |
70 |
| - }) + Environment.NewLine)).ToArray(); |
| 65 | + (string tfm, string content)[] publicApi = actualTfmDirs.Select(tfmDir => (new DirectoryInfo(tfmDir).Name.Replace(".", ""), Assembly.LoadFile(Path.Combine(tfmDir, projectName + ".dll")).GeneratePublicApi(new ApiGeneratorOptions |
| 66 | + { |
| 67 | + IncludeAssemblyAttributes = false, |
| 68 | + //AllowNamespacePrefixes = new[] { "Microsoft.Extensions.DependencyInjection" }, |
| 69 | + ExcludeAttributes = new[] { "System.Diagnostics.DebuggerDisplayAttribute", "System.Diagnostics.CodeAnalysis.AllowNullAttribute" } |
| 70 | + }) + Environment.NewLine)).ToArray(); |
71 | 71 |
|
72 |
| - if (publicApi.DistinctBy(item => item.content).Count() == 1) |
73 |
| - { |
74 |
| - AutoApproveOrFail(publicApi[0].content, ""); |
75 |
| - } |
76 |
| - else |
77 |
| - { |
78 |
| - foreach (var item in publicApi.ToLookup(item => item.content)) |
| 72 | + if (publicApi.DistinctBy(item => item.content).Count() == 1) |
79 | 73 | {
|
80 |
| - AutoApproveOrFail(item.Key, string.Join("+", item.Select(x => x.tfm).OrderBy(x => x))); |
| 74 | + AutoApproveOrFail(publicApi[0].content, ""); |
81 | 75 | }
|
82 |
| - } |
83 |
| - |
84 |
| - // Approval test should (re)generate approved.txt files locally if needed. |
85 |
| - // Approval test should fail on CI. |
86 |
| - // https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables |
87 |
| - void AutoApproveOrFail(string publicApi, string folder) |
88 |
| - { |
89 |
| - string file = null!; |
90 |
| - |
91 |
| - try |
| 76 | + else |
92 | 77 | {
|
93 |
| - publicApi.ShouldMatchApproved(options => options.SubFolder(folder).NoDiff().WithFilenameGenerator((testMethodInfo, discriminator, fileType, fileExtension) => file = $"{type.Assembly.GetName().Name}.{fileType}.{fileExtension}")); |
| 78 | + foreach (var item in publicApi.ToLookup(item => item.content)) |
| 79 | + { |
| 80 | + AutoApproveOrFail(item.Key, string.Join("+", item.Select(x => x.tfm).OrderBy(x => x))); |
| 81 | + } |
94 | 82 | }
|
95 |
| - catch (ShouldMatchApprovedException) when (Environment.GetEnvironmentVariable("CI") == null) |
| 83 | + |
| 84 | + // Approval test should (re)generate approved.txt files locally if needed. |
| 85 | + // Approval test should fail on CI. |
| 86 | + // https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables |
| 87 | + void AutoApproveOrFail(string publicApi, string folder) |
96 | 88 | {
|
97 |
| - string? received = Path.Combine(testDir, folder, file); |
98 |
| - string? approved = received.Replace(".received.txt", ".approved.txt"); |
99 |
| - if (File.Exists(received) && File.Exists(approved)) |
| 89 | + string file = null!; |
| 90 | + |
| 91 | + try |
100 | 92 | {
|
101 |
| - File.Copy(received, approved, overwrite: true); |
102 |
| - File.Delete(received); |
| 93 | + publicApi.ShouldMatchApproved(options => options.SubFolder(folder).NoDiff().WithFilenameGenerator((testMethodInfo, discriminator, fileType, fileExtension) => file = $"{type.Assembly.GetName().Name}.{fileType}.{fileExtension}")); |
103 | 94 | }
|
104 |
| - else |
| 95 | + catch (ShouldMatchApprovedException) when (Environment.GetEnvironmentVariable("CI") == null) |
105 | 96 | {
|
106 |
| - throw; |
| 97 | + string? received = Path.Combine(testDir, folder, file); |
| 98 | + string? approved = received.Replace(".received.txt", ".approved.txt"); |
| 99 | + if (File.Exists(received) && File.Exists(approved)) |
| 100 | + { |
| 101 | + File.Copy(received, approved, overwrite: true); |
| 102 | + File.Delete(received); |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + throw; |
| 107 | + } |
107 | 108 | }
|
108 | 109 | }
|
109 | 110 | }
|
|
0 commit comments