Skip to content

Commit 3f1fdf0

Browse files
committed
Remove file-scoped namespace
1 parent 5a8f852 commit 3f1fdf0

File tree

1 file changed

+56
-55
lines changed

1 file changed

+56
-55
lines changed

QRCoderApiTests/ApiApprovalTests.cs

Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
using System.Xml.Linq;
99
using Xunit;
1010

11-
namespace QRCoderApiTests;
12-
1311
/*********************************************
1412
*
1513
* This file copied from GraphQL.NET on 4/26/2024
@@ -38,72 +36,75 @@ namespace QRCoderApiTests;
3836
*
3937
*********************************************/
4038

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
4540
{
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
5045
{
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);
6060

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)}");
6464

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();
7171

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)
7973
{
80-
AutoApproveOrFail(item.Key, string.Join("+", item.Select(x => x.tfm).OrderBy(x => x)));
74+
AutoApproveOrFail(publicApi[0].content, "");
8175
}
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
9277
{
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+
}
9482
}
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)
9688
{
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
10092
{
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}"));
10394
}
104-
else
95+
catch (ShouldMatchApprovedException) when (Environment.GetEnvironmentVariable("CI") == null)
10596
{
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+
}
107108
}
108109
}
109110
}

0 commit comments

Comments
 (0)