GFramework/GFramework.SourceGenerators.Tests/Rule/ContextAwareGeneratorSnapshotTests.cs
GeWuYou d0b4946bba test: 增强快照测试基础设施,添加安全验证和覆盖率改进
- 在 EnumExtensionsGeneratorSnapshotTests.cs 中补充 snapshotFileNameSelector 的 null 分支覆盖,新增默认快照文件名选择器用例及对应快照资产

- 强化 GeneratorSnapshotTest.cs 的快照路径校验,拒绝空白文件名、绝对路径和目录遍历攻击;将辅助器改为通过 Roslyn GeneratorDriver 读取真实生成结果并验证编译,消除仅依赖 TestState.GeneratedSources 导致的空跑风险

- 新增 GeneratorSnapshotTestSecurityTests.cs 安全回归测试,覆盖绝对路径拒绝和目录逃逸防护两个分支

- 将 Priority、Logger、ContextAware 三组生成器测试统一指向仓库内快照目录,并补齐缺失的快照资产以支持现在强制执行的生成验证
2026-04-17 07:40:36 +08:00

109 lines
4.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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"));
}
}