|
| 1 | +using System.Diagnostics; |
| 2 | + |
| 3 | +namespace ElectronNET.IntegrationTests.Tests; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Tests for the Electron clean targets. |
| 7 | +/// Covers GitHub issue #1096: cleaning must not require a restore, so the clean targets |
| 8 | +/// must not depend on targets that need the assets file (NETSDK1004). |
| 9 | +/// </summary> |
| 10 | +public class ElectronCleanTargetsTests |
| 11 | +{ |
| 12 | + private static readonly string CorePropsPath = FindBuildFile("src/ElectronNET/build/ElectronNET.Core.props", "ElectronNET/build/ElectronNET.Core.props"); |
| 13 | + private static readonly string CoreTargetsPath = FindBuildFile("src/ElectronNET/build/ElectronNET.Core.targets", "ElectronNET/build/ElectronNET.Core.targets"); |
| 14 | + |
| 15 | + [Fact] |
| 16 | + public async Task ElectronCleanTargets_WithoutRestore_ShouldSucceed() |
| 17 | + { |
| 18 | + var tempDir = CreateTempProjectDirectory(); |
| 19 | + |
| 20 | + try |
| 21 | + { |
| 22 | + await WriteMinimalCsprojAsync(tempDir); |
| 23 | + |
| 24 | + var (exitCode, output) = await RunDotnetMsBuildAsync(tempDir, "Clean"); |
| 25 | + |
| 26 | + output.Should().NotContain("NETSDK1004", |
| 27 | + $"cleaning must not require the assets file of a previous restore. Full output:\n{output}"); |
| 28 | + |
| 29 | + exitCode.Should().Be(0, |
| 30 | + $"cleaning an unrestored project must succeed. Full output:\n{output}"); |
| 31 | + } |
| 32 | + finally |
| 33 | + { |
| 34 | + Directory.Delete(tempDir, recursive: true); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private static string FindBuildFile(string relativeFromRepoRoot, string relativeFromSrc) |
| 39 | + { |
| 40 | + var dir = new DirectoryInfo(AppContext.BaseDirectory); |
| 41 | + while (dir != null) |
| 42 | + { |
| 43 | + var fromRepoRoot = Path.Combine(dir.FullName, relativeFromRepoRoot); |
| 44 | + if (File.Exists(fromRepoRoot)) |
| 45 | + { |
| 46 | + return Path.GetFullPath(fromRepoRoot); |
| 47 | + } |
| 48 | + |
| 49 | + var fromSrc = Path.Combine(dir.FullName, relativeFromSrc); |
| 50 | + if (File.Exists(fromSrc)) |
| 51 | + { |
| 52 | + return Path.GetFullPath(fromSrc); |
| 53 | + } |
| 54 | + |
| 55 | + dir = dir.Parent; |
| 56 | + } |
| 57 | + |
| 58 | + throw new FileNotFoundException( |
| 59 | + $"Could not locate '{relativeFromRepoRoot}' by walking up from '{AppContext.BaseDirectory}'."); |
| 60 | + } |
| 61 | + |
| 62 | + private static string CreateTempProjectDirectory() |
| 63 | + { |
| 64 | + var tempDir = Path.Combine(Path.GetTempPath(), $"electron-net-clean-test-{Guid.NewGuid():N}"); |
| 65 | + Directory.CreateDirectory(tempDir); |
| 66 | + Directory.CreateDirectory(Path.Combine(tempDir, "Properties")); |
| 67 | + return tempDir; |
| 68 | + } |
| 69 | + |
| 70 | + private static Task WriteMinimalCsprojAsync(string tempDir) |
| 71 | + { |
| 72 | + var propsPathEscaped = CorePropsPath.Replace("'", "'"); |
| 73 | + var targetsPathEscaped = CoreTargetsPath.Replace("'", "'"); |
| 74 | + |
| 75 | + return File.WriteAllTextAsync( |
| 76 | + Path.Combine(tempDir, "TestApp.csproj"), |
| 77 | + $$""" |
| 78 | + <Project Sdk="Microsoft.NET.Sdk"> |
| 79 | + <PropertyGroup> |
| 80 | + <TargetFramework>net10.0</TargetFramework> |
| 81 | + </PropertyGroup> |
| 82 | +
|
| 83 | + <Import Project="{{propsPathEscaped}}" /> |
| 84 | +
|
| 85 | + <Import Project="{{targetsPathEscaped}}" /> |
| 86 | + </Project> |
| 87 | + """); |
| 88 | + } |
| 89 | + |
| 90 | + private static async Task<(int ExitCode, string Output)> RunDotnetMsBuildAsync(string workingDirectory, string target) |
| 91 | + { |
| 92 | + // Deliberately without /restore - that is what issue #1096 is about. |
| 93 | + var psi = new ProcessStartInfo("dotnet", $"msbuild TestApp.csproj --nologo -v:minimal /t:{target}") |
| 94 | + { |
| 95 | + WorkingDirectory = workingDirectory, |
| 96 | + RedirectStandardOutput = true, |
| 97 | + RedirectStandardError = true, |
| 98 | + UseShellExecute = false, |
| 99 | + }; |
| 100 | + |
| 101 | + using var process = Process.Start(psi)!; |
| 102 | + var stdOut = await process.StandardOutput.ReadToEndAsync(); |
| 103 | + var stdErr = await process.StandardError.ReadToEndAsync(); |
| 104 | + await process.WaitForExitAsync(); |
| 105 | + |
| 106 | + return (process.ExitCode, stdOut + stdErr); |
| 107 | + } |
| 108 | +} |
0 commit comments