|
| 1 | +using PublicApiGenerator; |
| 2 | +using Shouldly; |
| 3 | +using System; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Reflection; |
| 8 | +using System.Xml.Linq; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +/********************************************* |
| 12 | + * |
| 13 | + * This file copied from GraphQL.NET on 4/26/2024 |
| 14 | + * https://github.com/graphql-dotnet/graphql-dotnet/blob/dce3a8d9335eb2ff0674a1e48af01fdd6b942119/src/GraphQL.ApiTests/ApiApprovalTests.cs |
| 15 | + * |
| 16 | + * Unmodified portions of this file are subject to the following license: |
| 17 | + * https://github.com/graphql-dotnet/graphql-dotnet/blob/dce3a8d9335eb2ff0674a1e48af01fdd6b942119/LICENSE.md |
| 18 | + * |
| 19 | + * The MIT License (MIT) |
| 20 | + * |
| 21 | + * Copyright (c) 2015-2023 Joseph T. McBride, Ivan Maximov, Shane Krueger, et al. All rights reserved. |
| 22 | + * |
| 23 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and |
| 24 | + * associated documentation files (the "Software"), to deal in the Software without restriction, including |
| 25 | + * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 26 | + * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the |
| 27 | + * following conditions: |
| 28 | + * |
| 29 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 30 | + * |
| 31 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 32 | + * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 33 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 34 | + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 35 | + * IN THE SOFTWARE. |
| 36 | + * |
| 37 | + *********************************************/ |
| 38 | + |
| 39 | +namespace QRCoderApiTests |
| 40 | +{ |
| 41 | + /// <summary> |
| 42 | + /// See more info about API approval tests here <see href="https://github.com/JakeGinnivan/ApiApprover"/>. |
| 43 | + /// </summary> |
| 44 | + public class ApiApprovalTests |
| 45 | + { |
| 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", |
| 56 | +#if DEBUG |
| 57 | + "Debug"); |
| 58 | +#else |
| 59 | + "Release"); |
| 60 | +#endif |
| 61 | + Debug.Assert(Directory.Exists(buildDir), $"Directory '{buildDir}' doesn't exist"); |
| 62 | + string csProject = Path.Combine(projectDir, projectName, projectName + ".csproj"); |
| 63 | + var project = XDocument.Load(csProject); |
| 64 | + string[] tfms = project.Descendants("TargetFrameworks").Union(project.Descendants("TargetFramework")).First().Value.Split(";", StringSplitOptions.RemoveEmptyEntries); |
| 65 | + |
| 66 | + // There may be old stuff from earlier builds like net45, netcoreapp3.0, etc. so filter it out |
| 67 | + string[] actualTfmDirs = Directory.GetDirectories(buildDir).Where(dir => tfms.Any(tfm => dir.EndsWith(tfm))).ToArray(); |
| 68 | + Debug.Assert(actualTfmDirs.Length > 0, $"Directory '{buildDir}' doesn't contain subdirectories matching {string.Join(";", tfms)}"); |
| 69 | + |
| 70 | + (string tfm, string content)[] publicApi = actualTfmDirs.Select(tfmDir => (new DirectoryInfo(tfmDir).Name.Replace(".", ""), Assembly.LoadFile(Path.Combine(tfmDir, projectName + ".dll")).GeneratePublicApi(new ApiGeneratorOptions |
| 71 | + { |
| 72 | + IncludeAssemblyAttributes = false, |
| 73 | + //AllowNamespacePrefixes = new[] { "Microsoft.Extensions.DependencyInjection" }, |
| 74 | + ExcludeAttributes = new[] { "System.Diagnostics.DebuggerDisplayAttribute", "System.Diagnostics.CodeAnalysis.AllowNullAttribute" } |
| 75 | + }) + Environment.NewLine)).ToArray(); |
| 76 | + |
| 77 | + if (publicApi.DistinctBy(item => item.content).Count() == 1) |
| 78 | + { |
| 79 | + AutoApproveOrFail(publicApi[0].content, ""); |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + foreach (var item in publicApi.ToLookup(item => item.content)) |
| 84 | + { |
| 85 | + AutoApproveOrFail(item.Key, string.Join("+", item.Select(x => x.tfm).OrderBy(x => x))); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // Approval test should (re)generate approved.txt files locally if needed. |
| 90 | + // Approval test should fail on CI. |
| 91 | + // https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables |
| 92 | + void AutoApproveOrFail(string publicApi, string folder) |
| 93 | + { |
| 94 | + string file = null!; |
| 95 | + |
| 96 | + try |
| 97 | + { |
| 98 | + publicApi.ShouldMatchApproved(options => options.SubFolder(folder).NoDiff().WithFilenameGenerator((testMethodInfo, discriminator, fileType, fileExtension) => file = $"{type.Assembly.GetName().Name}.{fileType}.{fileExtension}")); |
| 99 | + } |
| 100 | + catch (ShouldMatchApprovedException) when (Environment.GetEnvironmentVariable("CI") == null) |
| 101 | + { |
| 102 | + string? received = Path.Combine(testDir, folder, file); |
| 103 | + string? approved = received.Replace(".received.txt", ".approved.txt"); |
| 104 | + if (File.Exists(received) && File.Exists(approved)) |
| 105 | + { |
| 106 | + File.Copy(received, approved, overwrite: true); |
| 107 | + File.Delete(received); |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + throw; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments