|
8 | 8 | using Shouldly;
|
9 | 9 | using Xunit;
|
10 | 10 |
|
11 |
| -namespace GraphQLParser.ApiTests |
| 11 | +namespace GraphQLParser.ApiTests; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// Tests to verify public API surface. |
| 15 | +/// </summary> |
| 16 | +public class ApiApprovalTests |
12 | 17 | {
|
13 |
| - /// <summary> |
14 |
| - /// Tests to verify public API surface. |
15 |
| - /// </summary> |
16 |
| - public class ApiApprovalTests |
| 18 | + [Theory] |
| 19 | + [InlineData(typeof(Lexer))] |
| 20 | + public void Public_Api_Should_Not_Change_Inadvertently(Type type) |
17 | 21 | {
|
18 |
| - [Theory] |
19 |
| - [InlineData(typeof(Lexer))] |
20 |
| - public void Public_Api_Should_Not_Change_Inadvertently(Type type) |
21 |
| - { |
22 |
| - string baseDir = AppDomain.CurrentDomain.BaseDirectory; |
23 |
| - string projectName = type.Assembly.GetName().Name!; |
24 |
| - string testDir = Path.Combine(baseDir, $"..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}.."); |
25 |
| - string projectDir = Path.Combine(testDir, ".."); |
26 |
| - string buildDir = Path.Combine(projectDir, projectName, "bin", "Debug"); |
27 |
| - Debug.Assert(Directory.Exists(buildDir), $"Directory '{buildDir}' doesn't exist"); |
28 |
| - string csProject = Path.Combine(projectDir, projectName, projectName + ".csproj"); |
29 |
| - var project = XDocument.Load(csProject); |
30 |
| - string[] tfms = project.Descendants("TargetFrameworks").Union(project.Descendants("TargetFramework")).First().Value.Split(";", StringSplitOptions.RemoveEmptyEntries); |
| 22 | + string baseDir = AppDomain.CurrentDomain.BaseDirectory; |
| 23 | + string projectName = type.Assembly.GetName().Name!; |
| 24 | + string testDir = Path.Combine(baseDir, $"..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}.."); |
| 25 | + string projectDir = Path.Combine(testDir, ".."); |
| 26 | + string buildDir = Path.Combine(projectDir, projectName, "bin", "Debug"); |
| 27 | + Debug.Assert(Directory.Exists(buildDir), $"Directory '{buildDir}' doesn't exist"); |
| 28 | + string csProject = Path.Combine(projectDir, projectName, projectName + ".csproj"); |
| 29 | + var project = XDocument.Load(csProject); |
| 30 | + string[] tfms = project.Descendants("TargetFrameworks").Union(project.Descendants("TargetFramework")).First().Value.Split(";", StringSplitOptions.RemoveEmptyEntries); |
31 | 31 |
|
32 |
| - // There may be old stuff from earlier builds like net45, netcoreapp3.0, etc. so filter it out |
33 |
| - string[] actualTfmDirs = Directory.GetDirectories(buildDir).Where(dir => tfms.Any(tfm => dir.EndsWith(tfm))).ToArray(); |
34 |
| - Debug.Assert(actualTfmDirs.Length > 0, $"Directory '{buildDir}' doesn't contain subdirectories matching {string.Join(";", tfms)}"); |
| 32 | + // There may be old stuff from earlier builds like net45, netcoreapp3.0, etc. so filter it out |
| 33 | + string[] actualTfmDirs = Directory.GetDirectories(buildDir).Where(dir => tfms.Any(tfm => dir.EndsWith(tfm))).ToArray(); |
| 34 | + Debug.Assert(actualTfmDirs.Length > 0, $"Directory '{buildDir}' doesn't contain subdirectories matching {string.Join(";", tfms)}"); |
35 | 35 |
|
36 |
| - (string tfm, string content)[] publicApi = actualTfmDirs.Select(tfmDir => (new DirectoryInfo(tfmDir).Name.Replace(".", ""), Assembly.LoadFile(Path.Combine(tfmDir, projectName + ".dll")).GeneratePublicApi(new ApiGeneratorOptions |
37 |
| - { |
38 |
| - IncludeAssemblyAttributes = false, |
39 |
| - //WhitelistedNamespacePrefixes = new[] { "Microsoft.Extensions.DependencyInjection" }, |
40 |
| - ExcludeAttributes = new[] { "System.Diagnostics.DebuggerDisplayAttribute", "System.Diagnostics.CodeAnalysis.AllowNullAttribute" } |
41 |
| - }))).ToArray(); |
| 36 | + (string tfm, string content)[] publicApi = actualTfmDirs.Select(tfmDir => (new DirectoryInfo(tfmDir).Name.Replace(".", ""), Assembly.LoadFile(Path.Combine(tfmDir, projectName + ".dll")).GeneratePublicApi(new ApiGeneratorOptions |
| 37 | + { |
| 38 | + IncludeAssemblyAttributes = false, |
| 39 | + //WhitelistedNamespacePrefixes = new[] { "Microsoft.Extensions.DependencyInjection" }, |
| 40 | + ExcludeAttributes = new[] { "System.Diagnostics.DebuggerDisplayAttribute", "System.Diagnostics.CodeAnalysis.AllowNullAttribute" } |
| 41 | + }))).ToArray(); |
42 | 42 |
|
43 |
| - if (publicApi.DistinctBy(item => item.content).Count() == 1) |
| 43 | + if (publicApi.DistinctBy(item => item.content).Count() == 1) |
| 44 | + { |
| 45 | + AutoApproveOrFail(publicApi[0].content, ""); |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + foreach (var item in publicApi.ToLookup(item => item.content)) |
44 | 50 | {
|
45 |
| - AutoApproveOrFail(publicApi[0].content, ""); |
| 51 | + AutoApproveOrFail(item.Key, string.Join("+", item.Select(x => x.tfm).OrderBy(x => x))); |
46 | 52 | }
|
47 |
| - else |
| 53 | + } |
| 54 | + |
| 55 | + // Approval test should (re)generate approved.txt files locally if needed. |
| 56 | + // Approval test should fail on CI. |
| 57 | + // https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables |
| 58 | + void AutoApproveOrFail(string publicApi, string folder) |
| 59 | + { |
| 60 | + string file = null!; |
| 61 | + |
| 62 | + try |
48 | 63 | {
|
49 |
| - foreach (var item in publicApi.ToLookup(item => item.content)) |
50 |
| - { |
51 |
| - AutoApproveOrFail(item.Key, string.Join("+", item.Select(x => x.tfm).OrderBy(x => x))); |
52 |
| - } |
| 64 | + publicApi.ShouldMatchApproved(options => options.SubFolder(folder).NoDiff().WithFilenameGenerator((testMethodInfo, discriminator, fileType, fileExtension) => file = $"{type.Assembly.GetName().Name}.{fileType}.{fileExtension}")); |
53 | 65 | }
|
54 |
| - |
55 |
| - // Approval test should (re)generate approved.txt files locally if needed. |
56 |
| - // Approval test should fail on CI. |
57 |
| - // https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables |
58 |
| - void AutoApproveOrFail(string publicApi, string folder) |
| 66 | + catch (ShouldMatchApprovedException) when (Environment.GetEnvironmentVariable("CI") == null) |
59 | 67 | {
|
60 |
| - string file = null!; |
61 |
| - |
62 |
| - try |
| 68 | + string? received = Path.Combine(testDir, folder, file); |
| 69 | + string? approved = received.Replace(".received.txt", ".approved.txt"); |
| 70 | + if (File.Exists(received) && File.Exists(approved)) |
63 | 71 | {
|
64 |
| - publicApi.ShouldMatchApproved(options => options.SubFolder(folder).NoDiff().WithFilenameGenerator((testMethodInfo, discriminator, fileType, fileExtension) => file = $"{type.Assembly.GetName().Name}.{fileType}.{fileExtension}")); |
| 72 | + File.Copy(received, approved, overwrite: true); |
| 73 | + File.Delete(received); |
65 | 74 | }
|
66 |
| - catch (ShouldMatchApprovedException) when (Environment.GetEnvironmentVariable("CI") == null) |
| 75 | + else |
67 | 76 | {
|
68 |
| - string? received = Path.Combine(testDir, folder, file); |
69 |
| - string? approved = received.Replace(".received.txt", ".approved.txt"); |
70 |
| - if (File.Exists(received) && File.Exists(approved)) |
71 |
| - { |
72 |
| - File.Copy(received, approved, overwrite: true); |
73 |
| - File.Delete(received); |
74 |
| - } |
75 |
| - else |
76 |
| - { |
77 |
| - throw; |
78 |
| - } |
| 77 | + throw; |
79 | 78 | }
|
80 | 79 | }
|
81 | 80 | }
|
|
0 commit comments