/
PublicApiTestCase.cs
103 lines (88 loc) · 3.66 KB
/
PublicApiTestCase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#if DOTNET462 // PublicApiGenerator requires .NET Standard 2.0, and we only need to run it once
namespace Castle
{
using System;
using System.IO;
using System.Reflection;
using NUnit.Framework;
using PublicApiGenerator;
/// <summary>
/// Development workflow:
/// - As a developer you run the unit tests and this test will overwrite all the .cs files in the /ref directory for you
/// - The build server (CI=true env var) will compare the public API of the built assemblies to the /ref files on disk,
/// the assertion will obviously fail if someone changes the public API without also including the change to the ref/*.cs files
/// </summary>
[TestFixture]
public class PublicApiTestCase
{
private static readonly string[] assemblies =
{
"Castle.Core",
"Castle.Services.Logging.log4netIntegration",
"Castle.Services.Logging.NLogIntegration",
"Castle.Services.Logging.SerilogIntegration",
};
[Test]
[Platform(Exclude = "Mono", Reason = "On Mono, the FrameworkDisplayNameAttribute isn't populated")]
public void PublicApi()
{
// Determine if we are in write (developer) or compare (CI server) mode
string ci = Environment.GetEnvironmentVariable("CI");
bool compare = string.Equals(ci, "true", StringComparison.OrdinalIgnoreCase);
// Determine assembly locations
string testCodeBase = typeof(PublicApiTestCase).Assembly.CodeBase;
UriBuilder testUri = new UriBuilder(new Uri(testCodeBase));
string testAssemblyPath = Uri.UnescapeDataString(testUri.Path);
string testContainingDirectory = Path.GetDirectoryName(testAssemblyPath);
string configuration = new DirectoryInfo(testContainingDirectory).Parent.Name;
string rootDir = Path.GetFullPath(Path.Combine(testContainingDirectory, "../../../../.."));
// Ensure reference source directory exists and is empty
string refDir = Path.Combine(rootDir, "ref");
if (!compare)
{
Directory.CreateDirectory(refDir);
foreach (string file in Directory.GetFiles(refDir, "*.cs"))
{
File.Delete(file);
}
}
// Process each assembly
foreach (string assemblyName in assemblies)
{
string configurationDir = $"{rootDir}/src/{assemblyName}/bin/{configuration}";
foreach (string frameworkDir in Directory.GetDirectories(configurationDir))
{
string framework = Path.GetFileName(frameworkDir);
string assemblyBinPath = $"{configurationDir}/{framework}/{assemblyName}.dll";
string assemblyRefPath = $"{refDir}/{assemblyName}-{framework}.cs";
var assembly = Assembly.LoadFile(Path.GetFullPath(assemblyBinPath));
string publicApi = assembly.GeneratePublicApi();
if (compare)
{
Assert.IsTrue(File.Exists(assemblyRefPath), $"ref/{assemblyName}-{framework}.cs does not exist");
string expectedPublicApi = string.Join(Environment.NewLine, File.ReadAllLines(assemblyRefPath));
Assert.AreEqual(expectedPublicApi, publicApi, $"ref/{assemblyName}-{framework}.cs does not match {assemblyName}.dll");
}
else
{
File.WriteAllText(assemblyRefPath, publicApi);
}
}
}
}
}
}
#endif