mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-05-07 17:21:16 +08:00
- 在 EnumExtensionsGeneratorSnapshotTests.cs 中补充 snapshotFileNameSelector 的 null 分支覆盖,新增默认快照文件名选择器用例及对应快照资产 - 强化 GeneratorSnapshotTest.cs 的快照路径校验,拒绝空白文件名、绝对路径和目录遍历攻击;将辅助器改为通过 Roslyn GeneratorDriver 读取真实生成结果并验证编译,消除仅依赖 TestState.GeneratedSources 导致的空跑风险 - 新增 GeneratorSnapshotTestSecurityTests.cs 安全回归测试,覆盖绝对路径拒绝和目录逃逸防护两个分支 - 将 Priority、Logger、ContextAware 三组生成器测试统一指向仓库内快照目录,并补齐缺失的快照资产以支持现在强制执行的生成验证
109 lines
4.5 KiB
C#
109 lines
4.5 KiB
C#
using System.IO;
|
||
using GFramework.Core.SourceGenerators.Rule;
|
||
using GFramework.SourceGenerators.Tests.Core;
|
||
|
||
namespace GFramework.SourceGenerators.Tests.Rule;
|
||
|
||
/// <summary>
|
||
/// 上下文感知生成器快照测试类
|
||
/// 用于测试ContextAwareGenerator源代码生成器的输出快照
|
||
/// </summary>
|
||
[TestFixture]
|
||
public class ContextAwareGeneratorSnapshotTests
|
||
{
|
||
/// <summary>
|
||
/// 测试ContextAwareGenerator源代码生成器的快照功能
|
||
/// 验证生成器对带有ContextAware特性的类的处理结果
|
||
/// </summary>
|
||
/// <returns>异步任务,无返回值</returns>
|
||
[Test]
|
||
public async Task Snapshot_ContextAwareGenerator()
|
||
{
|
||
// 定义测试用的源代码,包含ContextAware特性和相关接口定义
|
||
const string source = """
|
||
using System;
|
||
|
||
namespace GFramework.Core.SourceGenerators.Abstractions.Rule
|
||
{
|
||
[AttributeUsage(AttributeTargets.Class)]
|
||
public sealed class ContextAwareAttribute : Attribute { }
|
||
}
|
||
|
||
namespace GFramework.Core.Abstractions.Rule
|
||
{
|
||
public interface IContextAware
|
||
{
|
||
void SetContext(
|
||
GFramework.Core.Abstractions.Architectures.IArchitectureContext context);
|
||
|
||
GFramework.Core.Abstractions.Architectures.IArchitectureContext GetContext();
|
||
}
|
||
}
|
||
|
||
namespace GFramework.Core.Abstractions.Architectures
|
||
{
|
||
public interface IArchitectureContext { }
|
||
|
||
public interface IArchitectureContextProvider
|
||
{
|
||
IArchitectureContext GetContext();
|
||
bool TryGetContext<T>(out T? context) where T : class, IArchitectureContext;
|
||
}
|
||
}
|
||
|
||
namespace GFramework.Core.Architectures
|
||
{
|
||
using GFramework.Core.Abstractions.Architectures;
|
||
|
||
public sealed class GameContextProvider : IArchitectureContextProvider
|
||
{
|
||
public IArchitectureContext GetContext() => null;
|
||
public bool TryGetContext<T>(out T? context) where T : class, IArchitectureContext
|
||
{
|
||
context = null;
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public static class GameContext
|
||
{
|
||
public static IArchitectureContext GetFirstArchitectureContext() => null;
|
||
}
|
||
}
|
||
|
||
namespace TestApp
|
||
{
|
||
using GFramework.Core.SourceGenerators.Abstractions.Rule;
|
||
using GFramework.Core.Abstractions.Rule;
|
||
|
||
[ContextAware]
|
||
public partial class MyRule : IContextAware
|
||
{
|
||
}
|
||
}
|
||
""";
|
||
|
||
// 执行生成器快照测试,将生成的代码与预期快照进行比较
|
||
await GeneratorSnapshotTest<ContextAwareGenerator>.RunAsync(
|
||
source,
|
||
GetSnapshotFolder());
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将运行时测试目录映射回仓库内已提交的上下文感知生成器快照目录。
|
||
/// </summary>
|
||
/// <returns>快照目录的绝对路径。</returns>
|
||
private static string GetSnapshotFolder()
|
||
{
|
||
return Path.GetFullPath(
|
||
Path.Combine(
|
||
TestContext.CurrentContext.TestDirectory,
|
||
"..",
|
||
"..",
|
||
"..",
|
||
"Rule",
|
||
"snapshots",
|
||
"ContextAwareGenerator"));
|
||
}
|
||
}
|