Files
WCTDataMiner/tests/Gpulse.WCT.DataAnalyzer.Tests/CommandTestBase.cs

61 lines
2.2 KiB
C#
Raw Normal View History

2026-08-17 09:24:57 +08:00
using Gpulse.WCT.DataAnalyzer.Commands;
using Gpulse.WCT.DataAnalyzer.Core.Application;
using Gpulse.WCT.DataAnalyzer.Core.Application.Analysis;
using Gpulse.WCT.DataAnalyzer.Core.Application.Exporting;
2026-08-17 09:24:57 +08:00
using Microsoft.Extensions.DependencyInjection;
namespace Gpulse.WCT.DataAnalyzer.Tests;
/// <summary>
/// CLI 命令测试基类:每个测试一个独立的内存数据库 + 临时目录。
/// </summary>
public abstract class CommandTestBase
{
protected CliTestHost Host { get; private set; } = null!;
[SetUp]
public async Task SetUp()
{
// 清理 Datas 和 Output 文件夹,避免测试间干扰
CleanupDatasAndOutput();
Host = await CliTestHost.CreateAsync();
}
2026-08-17 09:24:57 +08:00
[TearDown]
public void TearDown()
{
Host.Dispose();
// 测试结束后再次清理
CleanupDatasAndOutput();
}
/// <summary>
/// 清理 Datas 和 Output 文件夹
/// </summary>
protected static void CleanupDatasAndOutput()
{
var datasDir = Path.Combine(AppContext.BaseDirectory, "Datas");
var outputDir = Path.Combine(AppContext.BaseDirectory, "Output");
if (Directory.Exists(datasDir)) Directory.Delete(datasDir, true);
if (Directory.Exists(outputDir)) Directory.Delete(outputDir, true);
}
2026-08-17 09:24:57 +08:00
// 命令构造方式与 Program.Main 一致:从 DI 解析服务再构造命令
protected ParseCommand Parse() => new(Host.Services.GetRequiredService<ParseService>());
protected AggregateCommand Aggregate() => new(Host.Services.GetRequiredService<AggregationService>());
protected ExportCommand Export() => new(Host.Services.GetRequiredService<ExportService>());
protected CleanCommand Clean() => new(Host.Services.GetRequiredService<CleanService>());
protected AnalyzeCommand Analyze() => new(
Host.Services.GetRequiredService<PlossAnalysisService>(),
Host.Services.GetRequiredService<QfodCalibrationService>(),
Host.Services.GetRequiredService<AnalyzeExcelExporter>());
2026-08-17 09:24:57 +08:00
/// <summary>在测试临时目录下新建一个子目录。</summary>
protected string NewDir() =>
Directory.CreateDirectory(Path.Combine(Host.TempDir, Guid.NewGuid().ToString("N"))).FullName;
}