61 lines
2.2 KiB
C#
61 lines
2.2 KiB
C#
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;
|
|
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();
|
|
}
|
|
|
|
[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);
|
|
}
|
|
|
|
// 命令构造方式与 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>());
|
|
|
|
/// <summary>在测试临时目录下新建一个子目录。</summary>
|
|
protected string NewDir() =>
|
|
Directory.CreateDirectory(Path.Combine(Host.TempDir, Guid.NewGuid().ToString("N"))).FullName;
|
|
}
|