mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-02-18 13:57:43 +01:00
Merge 95e7299017 into 4ac4505d72
This commit is contained in:
commit
54db39446e
13 changed files with 194 additions and 33 deletions
102
Dalamud.Test/Compliance/PublicApiTests.cs
Normal file
102
Dalamud.Test/Compliance/PublicApiTests.cs
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
using Xunit;
|
||||
|
||||
|
||||
namespace Dalamud.Test.Compliance;
|
||||
|
||||
public class PublicApiTests
|
||||
{
|
||||
private static List<Type> IgnoredTypes { get; } =
|
||||
[
|
||||
typeof(Utility.CStringExtensions),
|
||||
];
|
||||
|
||||
private static List<Assembly> PermittedAssemblies { get; } =
|
||||
[
|
||||
typeof(object).Assembly,
|
||||
typeof(Dalamud).Assembly,
|
||||
|
||||
// Imgui and friends
|
||||
typeof(SharpDX.Color).Assembly,
|
||||
typeof(Bindings.ImGui.ImGui).Assembly,
|
||||
typeof(Bindings.ImGuizmo.ImGuizmo).Assembly,
|
||||
typeof(Bindings.ImPlot.ImPlot).Assembly,
|
||||
|
||||
// exposed to plugins via API
|
||||
typeof(Lumina.GameData).Assembly,
|
||||
typeof(Lumina.Excel.Sheets.Action).Assembly,
|
||||
];
|
||||
|
||||
private static List<Type> PermittedTypes { get; } = [
|
||||
// Used for IPluginLog, limited serilog exposure is OK.
|
||||
typeof(Serilog.ILogger),
|
||||
typeof(Serilog.Core.LoggingLevelSwitch),
|
||||
typeof(Serilog.Events.LogEventLevel),
|
||||
];
|
||||
|
||||
[Fact]
|
||||
public void NoRestrictedTypes()
|
||||
{
|
||||
foreach (var type in typeof(Dalamud).Assembly.GetTypes().Where(t => t.IsPublic).Except(IgnoredTypes))
|
||||
{
|
||||
if (type.GetCustomAttribute<ObsoleteAttribute>() != null) continue;
|
||||
|
||||
foreach (var m in type.GetMethods().Where(m => m.IsPublic && !m.IsSpecialName))
|
||||
{
|
||||
if (m.GetCustomAttribute<ObsoleteAttribute>() != null) continue;
|
||||
|
||||
if (!this.IsPermittedType(m.ReturnType))
|
||||
{
|
||||
Assert.Fail($"Method {type.FullName}.{m.Name} returns unapproved type: {m.ReturnType.FullName}");
|
||||
}
|
||||
|
||||
foreach (var param in m.GetParameters())
|
||||
{
|
||||
if (!this.IsPermittedType(param.ParameterType))
|
||||
{
|
||||
Assert.Fail($"Method {type.FullName}.{m.Name} uses unapproved type: {param.ParameterType.FullName}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var p in type.GetProperties())
|
||||
{
|
||||
if (p.GetCustomAttribute<ObsoleteAttribute>() != null) continue;
|
||||
if (p.GetMethod?.IsPrivate == true && p.SetMethod?.IsPrivate == true) continue;
|
||||
|
||||
if (!this.IsPermittedType(p.PropertyType))
|
||||
{
|
||||
Assert.Fail(
|
||||
$"Property {type.FullName}.{p.Name} is unapproved type: {p.PropertyType.FullName}");
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var f in type.GetFields().Where(f => f.IsPublic && !f.IsSpecialName))
|
||||
{
|
||||
if (f.GetCustomAttribute<ObsoleteAttribute>() != null) continue;
|
||||
|
||||
if (!this.IsPermittedType(f.FieldType))
|
||||
{
|
||||
Assert.Fail(
|
||||
$"Field {type.FullName}.{f.Name} is unapproved type: {f.FieldType.FullName}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsPermittedType(Type subject)
|
||||
{
|
||||
if (subject.IsGenericType && !subject.GetGenericArguments().All(this.IsPermittedType))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return subject.Namespace?.StartsWith("System") == true ||
|
||||
PermittedTypes.Any(t => t == subject) ||
|
||||
PermittedAssemblies.Any(a => a == subject.Assembly);
|
||||
}
|
||||
}
|
||||
|
|
@ -43,14 +43,14 @@
|
|||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit" Version="2.4.2" />
|
||||
<PackageReference Include="xunit.abstractions" Version="2.0.3" />
|
||||
<PackageReference Include="xunit.analyzers" Version="0.10.0" />
|
||||
<PackageReference Include="xunit.assert" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.core" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.extensibility.core" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.extensibility.execution" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.console" Version="2.4.1">
|
||||
<PackageReference Include="xunit.analyzers" Version="1.0.0" />
|
||||
<PackageReference Include="xunit.assert" Version="2.4.2" />
|
||||
<PackageReference Include="xunit.core" Version="2.4.2" />
|
||||
<PackageReference Include="xunit.extensibility.core" Version="2.4.2" />
|
||||
<PackageReference Include="xunit.extensibility.execution" Version="2.4.2" />
|
||||
<PackageReference Include="xunit.runner.console" Version="2.4.2">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue