From 6be84fab4b3d8c1f8231cd22fc9726c50ae35784 Mon Sep 17 00:00:00 2001 From: SlimeNull Date: Sun, 31 Mar 2024 18:38:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E9=9B=86=E6=A3=80=E6=9F=A5=E5=88=B0=20github=20action?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/dotnet.yml | 4 ++ src/AssmeblyCheck/AssemblyCheckCore.cs | 41 ++++++++++++++++++- src/AssmeblyCheck/Program.cs | 2 +- .../Post/CqGroupMemberDecreasedPostContext.cs | 6 +-- src/TestConsole/TestConsole.csproj | 1 + 5 files changed, 48 insertions(+), 6 deletions(-) diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index b934f05..904d14f 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -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 diff --git a/src/AssmeblyCheck/AssemblyCheckCore.cs b/src/AssmeblyCheck/AssemblyCheckCore.cs index d5e7a0a..372b98a 100644 --- a/src/AssmeblyCheck/AssemblyCheckCore.cs +++ b/src/AssmeblyCheck/AssemblyCheckCore.cs @@ -1,5 +1,6 @@ using EleCho.GoCqHttpSdk; using EleCho.GoCqHttpSdk.Action; +using EleCho.GoCqHttpSdk.Post; using System; using System.Collections.Generic; using System.Linq; @@ -17,8 +18,10 @@ internal static class AssemblyCheckCore /// 对 EleCho.GoCqHttpSdk 的程序集进行基础测试 /// /// 有测试不通过的内容 - public static void Run() + public static int Run() { + int warningCount = 0; + Console.WriteLine("程序集检查开始..."); Assembly asm = typeof(CqSession).Assembly; @@ -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")) @@ -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")) @@ -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")) @@ -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")) @@ -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? cqenumtostringfunc = cqenumtostring?.CreateDelegate>(); if (cqenumtostringfunc == null) throw new Exception("找不到 CqEnum.GetString 方法"); - + foreach (var actionType in Enum.GetValues()) { try @@ -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) })!; @@ -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) })!; @@ -137,6 +169,11 @@ public static void Run() } Console.WriteLine("程序集基础检查通过"); + + if (warningCount != 0) + Console.WriteLine($"但是有 {warningCount} 个警告"); + + return warningCount++; } } } diff --git a/src/AssmeblyCheck/Program.cs b/src/AssmeblyCheck/Program.cs index 1abff3d..42c4f74 100644 --- a/src/AssmeblyCheck/Program.cs +++ b/src/AssmeblyCheck/Program.cs @@ -1,4 +1,4 @@ using TestConsole; // 运行对程序集的简单检查测试 -AssemblyCheckCore.Run(); +return AssemblyCheckCore.Run(); diff --git a/src/EleCho.GoCqHttpSdk/Post/CqGroupMemberDecreasedPostContext.cs b/src/EleCho.GoCqHttpSdk/Post/CqGroupMemberDecreasedPostContext.cs index bc0a7de..d0696b2 100644 --- a/src/EleCho.GoCqHttpSdk/Post/CqGroupMemberDecreasedPostContext.cs +++ b/src/EleCho.GoCqHttpSdk/Post/CqGroupMemberDecreasedPostContext.cs @@ -22,17 +22,17 @@ public record class CqGroupMemberDecreasedPostContext : CqNoticePostContext, IGr /// /// 群号 /// - public long GroupId { get; set; } + public long GroupId { get; private set; } /// /// 用户 QQ /// - public long UserId { get; set; } + public long UserId { get; private set; } /// /// 操作者 QQ /// - public long OperatorId { get; set; } + public long OperatorId { get; private set; } internal CqGroupMemberDecreasedPostContext() { } diff --git a/src/TestConsole/TestConsole.csproj b/src/TestConsole/TestConsole.csproj index 7164e4d..07448b7 100644 --- a/src/TestConsole/TestConsole.csproj +++ b/src/TestConsole/TestConsole.csproj @@ -4,6 +4,7 @@ Exe net6.0 latest + enable