Skip to content

Commit

Permalink
feat: 添加程序集检查到 github action
Browse files Browse the repository at this point in the history
  • Loading branch information
SlimeNull committed Mar 31, 2024
1 parent 9a052b7 commit 6be84fa
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ jobs:
- name: Build
run: dotnet build -c Release
working-directory: src

- name: Check
run: dotnet run --project src/AssmeblyCheck/AssmeblyCheck.csproj

- name: Test
run: dotnet test --no-build --verbosity normal
working-directory: src
Expand Down
41 changes: 39 additions & 2 deletions src/AssmeblyCheck/AssemblyCheckCore.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using EleCho.GoCqHttpSdk;
using EleCho.GoCqHttpSdk.Action;
using EleCho.GoCqHttpSdk.Post;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -17,8 +18,10 @@ internal static class AssemblyCheckCore
/// 对 EleCho.GoCqHttpSdk 的程序集进行基础测试
/// </summary>
/// <exception cref="Exception">有测试不通过的内容</exception>
public static void Run()
public static int Run()
{
int warningCount = 0;

Console.WriteLine("程序集检查开始...");
Assembly asm = typeof(CqSession).Assembly;

Expand All @@ -31,7 +34,9 @@ public static void Run()
Type[] cqActionParamsModelTypes = allTypes.Where(t => t.IsSubclassOf(typeCqActionParamsModel)).ToArray();
Type[] cqActionResultTypes = allTypes.Where(t => t.IsSubclassOf(typeof(CqActionResult))).ToArray();
Type[] cqActionResultDataModelTypes = allTypes.Where(t => t.IsSubclassOf(typeCqActionResultDataModel)).ToArray();
Type[] cqPostContextTypes = allTypes.Where(t => t.IsSubclassOf(typeof(CqPostContext))).ToArray();

Console.WriteLine("检查 Action...");
foreach (var action in cqActionTypes)
{
if (!action.Name.EndsWith("Action"))
Expand All @@ -41,10 +46,14 @@ public static void Run()
foreach (var prop in action.GetProperties())
{
if (!prop.CanWrite && prop.Name != nameof(CqAction.ActionType))
{
Console.WriteLine($"程序集检查警告: {action}{prop} 属性没有 '写' 访问器");
warningCount++;
}
}
}

Console.WriteLine("检查 ActionParamsModel...");
foreach (var actionParamsModel in cqActionParamsModelTypes)
{
if (!actionParamsModel.Name.EndsWith("ActionParamsModel"))
Expand All @@ -54,10 +63,14 @@ public static void Run()
foreach (var prop in actionParamsModel.GetProperties())
{
if (prop.CanWrite)
{
Console.WriteLine($"程序集检查警告: {actionParamsModel}{prop} 是可写的");
warningCount++;
}
}
}

Console.WriteLine("检查 ActionResult...");
foreach (var actionResult in cqActionResultTypes)
{
if (!actionResult.Name.EndsWith("ActionResult"))
Expand All @@ -71,10 +84,14 @@ public static void Run()
foreach (var prop in actionResult.GetProperties())
{
if (prop.CanWrite && prop.SetMethod!.IsPublic)
{
Console.WriteLine($"程序集检查警告: {actionResult}{prop} 有公共的 '写' 访问器, 它不应该对用户暴露");
warningCount++;
}
}
}

Console.WriteLine("检查 ActionResultDataModel...");
foreach (var actionResultDataModel in cqActionResultDataModelTypes)
{
if (!actionResultDataModel.Name.EndsWith("ActionResultDataModel"))
Expand All @@ -85,14 +102,27 @@ public static void Run()
throw new Exception($"{actionResultDataModel} 命名空间不对劲");
}

Console.WriteLine("检查 PostContext");
foreach (var postContext in cqPostContextTypes)
{
foreach (var prop in postContext.GetProperties())
{
if (prop.CanWrite && prop.SetMethod!.IsPublic)
{
Console.WriteLine($"程序集检查警告: {postContext}{prop} 有公共的 '写' 访问器, 它不应该对用户暴露");
warningCount++;
}
}
}

Console.WriteLine("检查枚举...");
Type? cqenum = asm.GetType("EleCho.GoCqHttpSdk.CqEnum", true, false);
MethodInfo? cqenumtostring = cqenum?.GetMethod("GetString", new Type[] { typeof(CqActionType) });
Func<CqActionType, string>? cqenumtostringfunc = cqenumtostring?.CreateDelegate<Func<CqActionType, string>>();

if (cqenumtostringfunc == null)
throw new Exception("找不到 CqEnum.GetString 方法");

foreach (var actionType in Enum.GetValues<CqActionType>())
{
try
Expand All @@ -105,6 +135,7 @@ public static void Run()
}
}

Console.WriteLine("检查 ActionResult 转换");
Type actionResultType = typeof(CqActionResult);
MethodInfo createActionResultFromActionTypeMethod = actionResultType.GetMethod("CreateActionResultFromActionType", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, new Type[] { typeof(string) })!;

Expand All @@ -120,6 +151,7 @@ public static void Run()
}
}

Console.WriteLine("检查 ActionResultDataModel 转换");
Type actionResultModelType = asm.GetType("EleCho.GoCqHttpSdk.Action.Model.ResultData.CqActionResultDataModel", true, false)!;
MethodInfo actionResultDataModelFromRaw = actionResultModelType.GetMethod("FromRaw", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, new Type[] { typeof(JsonElement?), typeof(string) })!;

Expand All @@ -137,6 +169,11 @@ public static void Run()
}

Console.WriteLine("程序集基础检查通过");

if (warningCount != 0)
Console.WriteLine($"但是有 {warningCount} 个警告");

return warningCount++;
}
}
}
2 changes: 1 addition & 1 deletion src/AssmeblyCheck/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using TestConsole;

// 运行对程序集的简单检查测试
AssemblyCheckCore.Run();
return AssemblyCheckCore.Run();
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ public record class CqGroupMemberDecreasedPostContext : CqNoticePostContext, IGr
/// <summary>
/// 群号
/// </summary>
public long GroupId { get; set; }
public long GroupId { get; private set; }

/// <summary>
/// 用户 QQ
/// </summary>
public long UserId { get; set; }
public long UserId { get; private set; }

/// <summary>
/// 操作者 QQ
/// </summary>
public long OperatorId { get; set; }
public long OperatorId { get; private set; }

internal CqGroupMemberDecreasedPostContext() { }

Expand Down
1 change: 1 addition & 0 deletions src/TestConsole/TestConsole.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup Condition="'$(Configuration)'=='Debug'">
Expand Down

0 comments on commit 6be84fa

Please sign in to comment.