|
| 1 | +using JD.Efcpt.Build.Tests.Infrastructure; |
| 2 | +using TinyBDD; |
| 3 | +using TinyBDD.Xunit; |
| 4 | +using Xunit; |
| 5 | +using Xunit.Abstractions; |
| 6 | +using Task = System.Threading.Tasks.Task; |
| 7 | + |
| 8 | +namespace JD.Efcpt.Build.Tests; |
| 9 | + |
| 10 | +[Feature("Clean target: dotnet clean removes efcpt output directory")] |
| 11 | +[Collection(nameof(AssemblySetup))] |
| 12 | +public sealed class CleanTargetTests(ITestOutputHelper output) : TinyBddXunitBase(output) |
| 13 | +{ |
| 14 | + private sealed record CleanTestContext( |
| 15 | + TestFolder Folder, |
| 16 | + string AppDir, |
| 17 | + string EfcptOutputDir) : IDisposable |
| 18 | + { |
| 19 | + public void Dispose() => Folder.Dispose(); |
| 20 | + } |
| 21 | + |
| 22 | + private sealed record CleanResult( |
| 23 | + CleanTestContext Context, |
| 24 | + int ExitCode, |
| 25 | + string Output, |
| 26 | + bool EfcptDirExistedBefore, |
| 27 | + bool EfcptDirExistsAfter); |
| 28 | + |
| 29 | + private static CleanTestContext SetupProjectWithEfcptOutput() |
| 30 | + { |
| 31 | + var folder = new TestFolder(); |
| 32 | + var appDir = folder.CreateDir("TestApp"); |
| 33 | + |
| 34 | + // Get the absolute path to the JD.Efcpt.Build source directory |
| 35 | + var efcptBuildRoot = Path.Combine(TestPaths.RepoRoot, "src", "JD.Efcpt.Build"); |
| 36 | + |
| 37 | + // Create a minimal project file that imports our targets with absolute paths |
| 38 | + var csproj = $""" |
| 39 | + <Project Sdk="Microsoft.NET.Sdk"> |
| 40 | + <PropertyGroup> |
| 41 | + <TargetFramework>net8.0</TargetFramework> |
| 42 | + <Nullable>enable</Nullable> |
| 43 | + </PropertyGroup> |
| 44 | +
|
| 45 | + <Import Project="{efcptBuildRoot}/build/JD.Efcpt.Build.props" /> |
| 46 | +
|
| 47 | + <PropertyGroup> |
| 48 | + <EfcptEnabled>true</EfcptEnabled> |
| 49 | + </PropertyGroup> |
| 50 | +
|
| 51 | + <Import Project="{efcptBuildRoot}/build/JD.Efcpt.Build.targets" /> |
| 52 | + </Project> |
| 53 | + """; |
| 54 | + |
| 55 | + File.WriteAllText(Path.Combine(appDir, "TestApp.csproj"), csproj); |
| 56 | + |
| 57 | + // Create the efcpt output directory with sample content (simulating a previous build) |
| 58 | + var efcptOutputDir = Path.Combine(appDir, "obj", "efcpt"); |
| 59 | + Directory.CreateDirectory(efcptOutputDir); |
| 60 | + |
| 61 | + // Add sample files that would exist after a build |
| 62 | + File.WriteAllText(Path.Combine(efcptOutputDir, "fingerprint.txt"), "sample-fingerprint-hash"); |
| 63 | + File.WriteAllText(Path.Combine(efcptOutputDir, "efcpt.stamp"), "stamp"); |
| 64 | + |
| 65 | + var generatedDir = Path.Combine(efcptOutputDir, "Generated"); |
| 66 | + Directory.CreateDirectory(generatedDir); |
| 67 | + File.WriteAllText(Path.Combine(generatedDir, "TestContext.g.cs"), "// generated file"); |
| 68 | + File.WriteAllText(Path.Combine(generatedDir, "TestModel.g.cs"), "// generated model"); |
| 69 | + |
| 70 | + return new CleanTestContext(folder, appDir, efcptOutputDir); |
| 71 | + } |
| 72 | + |
| 73 | + private static CleanResult ExecuteDotNetClean(CleanTestContext context) |
| 74 | + { |
| 75 | + var efcptDirExistedBefore = Directory.Exists(context.EfcptOutputDir); |
| 76 | + |
| 77 | + var psi = new System.Diagnostics.ProcessStartInfo |
| 78 | + { |
| 79 | + FileName = TestPaths.DotNetExe, |
| 80 | + Arguments = "clean", |
| 81 | + WorkingDirectory = context.AppDir, |
| 82 | + RedirectStandardOutput = true, |
| 83 | + RedirectStandardError = true, |
| 84 | + UseShellExecute = false, |
| 85 | + CreateNoWindow = true |
| 86 | + }; |
| 87 | + |
| 88 | + using var process = System.Diagnostics.Process.Start(psi)!; |
| 89 | + var stdout = process.StandardOutput.ReadToEnd(); |
| 90 | + var stderr = process.StandardError.ReadToEnd(); |
| 91 | + process.WaitForExit(60000); |
| 92 | + |
| 93 | + var output = stdout + stderr; |
| 94 | + var efcptDirExistsAfter = Directory.Exists(context.EfcptOutputDir); |
| 95 | + |
| 96 | + return new CleanResult(context, process.ExitCode, output, efcptDirExistedBefore, efcptDirExistsAfter); |
| 97 | + } |
| 98 | + |
| 99 | + [Scenario("dotnet clean removes efcpt output directory")] |
| 100 | + [Fact] |
| 101 | + public Task Dotnet_clean_removes_efcpt_output_directory() |
| 102 | + => Given("project with efcpt output directory", SetupProjectWithEfcptOutput) |
| 103 | + .Then("efcpt directory exists before clean", ctx => Directory.Exists(ctx.EfcptOutputDir)) |
| 104 | + .And("efcpt directory contains files", ctx => |
| 105 | + Directory.GetFiles(ctx.EfcptOutputDir, "*", SearchOption.AllDirectories).Length > 0) |
| 106 | + .When("run dotnet clean", ExecuteDotNetClean) |
| 107 | + .Then("clean command succeeds", r => |
| 108 | + { |
| 109 | + if (r.ExitCode != 0) |
| 110 | + throw new InvalidOperationException($"dotnet clean failed with exit code {r.ExitCode}. Output: {r.Output}"); |
| 111 | + return true; |
| 112 | + }) |
| 113 | + .And("efcpt directory existed before clean", r => r.EfcptDirExistedBefore) |
| 114 | + .And("efcpt directory is removed after clean", r => !r.EfcptDirExistsAfter) |
| 115 | + .Finally(r => r.Context.Dispose()) |
| 116 | + .AssertPassed(); |
| 117 | + |
| 118 | + [Scenario("dotnet clean succeeds when efcpt directory does not exist")] |
| 119 | + [Fact] |
| 120 | + public Task Dotnet_clean_succeeds_when_efcpt_directory_does_not_exist() |
| 121 | + => Given("project without efcpt output directory", () => |
| 122 | + { |
| 123 | + var ctx = SetupProjectWithEfcptOutput(); |
| 124 | + // Remove the efcpt directory to simulate a fresh state |
| 125 | + if (Directory.Exists(ctx.EfcptOutputDir)) |
| 126 | + Directory.Delete(ctx.EfcptOutputDir, recursive: true); |
| 127 | + return ctx; |
| 128 | + }) |
| 129 | + .Then("efcpt directory does not exist", ctx => !Directory.Exists(ctx.EfcptOutputDir)) |
| 130 | + .When("run dotnet clean", ExecuteDotNetClean) |
| 131 | + .Then("clean command succeeds", r => r.ExitCode == 0) |
| 132 | + .And("efcpt directory still does not exist", r => !r.EfcptDirExistsAfter) |
| 133 | + .Finally(r => r.Context.Dispose()) |
| 134 | + .AssertPassed(); |
| 135 | + |
| 136 | + [Scenario("dotnet clean outputs message about cleaning efcpt")] |
| 137 | + [Fact] |
| 138 | + public Task Dotnet_clean_outputs_message_about_cleaning_efcpt() |
| 139 | + => Given("project with efcpt output directory", SetupProjectWithEfcptOutput) |
| 140 | + .When("run dotnet clean with normal verbosity", ctx => |
| 141 | + { |
| 142 | + var efcptDirExistedBefore = Directory.Exists(ctx.EfcptOutputDir); |
| 143 | + |
| 144 | + var psi = new System.Diagnostics.ProcessStartInfo |
| 145 | + { |
| 146 | + FileName = TestPaths.DotNetExe, |
| 147 | + Arguments = "clean -v normal", |
| 148 | + WorkingDirectory = ctx.AppDir, |
| 149 | + RedirectStandardOutput = true, |
| 150 | + RedirectStandardError = true, |
| 151 | + UseShellExecute = false, |
| 152 | + CreateNoWindow = true |
| 153 | + }; |
| 154 | + |
| 155 | + using var process = System.Diagnostics.Process.Start(psi)!; |
| 156 | + var stdout = process.StandardOutput.ReadToEnd(); |
| 157 | + var stderr = process.StandardError.ReadToEnd(); |
| 158 | + process.WaitForExit(60000); |
| 159 | + |
| 160 | + var output = stdout + stderr; |
| 161 | + var efcptDirExistsAfter = Directory.Exists(ctx.EfcptOutputDir); |
| 162 | + |
| 163 | + return new CleanResult(ctx, process.ExitCode, output, efcptDirExistedBefore, efcptDirExistsAfter); |
| 164 | + }) |
| 165 | + .Then("clean command succeeds", r => r.ExitCode == 0) |
| 166 | + .And("output contains efcpt cleaning message", r => |
| 167 | + r.Output.Contains("Cleaning efcpt output", StringComparison.OrdinalIgnoreCase) || |
| 168 | + r.Output.Contains("efcpt", StringComparison.OrdinalIgnoreCase)) |
| 169 | + .Finally(r => r.Context.Dispose()) |
| 170 | + .AssertPassed(); |
| 171 | +} |
0 commit comments