using System.Diagnostics; using System.Security; using System.Text.Json; using Shouldly; using Xunit; namespace AtomUI.Build.Tasks.Tests; public class RegistrationSourceConsumerTests { [Fact] public void Analyzer_does_not_export_build_host_runtime_files_as_application_content() { var root = new DirectoryInfo(AppContext.BaseDirectory); while (root is not null && !File.Exists(Path.Combine(root.FullName, "AtomUI.slnx"))) root = root.Parent; using var process = Process.Start(new ProcessStartInfo("dotnet") { WorkingDirectory = root!.FullName, ArgumentList = { "msbuild", "src/AtomUI.Generator/AtomUI.Generator.csproj", "-target:GetCopyToOutputDirectoryItems", "-getTargetResult:GetCopyToOutputDirectoryItems" }, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true })!; var output = process.StandardOutput.ReadToEnd(); var error = process.StandardError.ReadToEnd(); process.WaitForExit(); process.ExitCode.ShouldBe(0, error); using var result = JsonDocument.Parse(output); result.RootElement.GetProperty("TargetResults").GetProperty("GetCopyToOutputDirectoryItems").GetProperty("Items").GetArrayLength().ShouldBe(0); } [Theory] [InlineData(false, false)] [InlineData(true, false)] [InlineData(false, true)] [InlineData(true, true)] public void Source_and_test_projects_receive_only_the_applicable_registration_generator(bool hasGenerator, bool isTestProject) { var root = new DirectoryInfo(AppContext.BaseDirectory); while (root is not null && !File.Exists(Path.Combine(root.FullName, "AtomUI.slnx"))) root = root.Parent; var directory = Path.Combine(Path.GetTempPath(), "atomui-source-consumer-" + Guid.NewGuid().ToString("N")); Directory.CreateDirectory(directory); try { var repo = SecurityElement.Escape(root!.FullName); File.WriteAllText(Path.Combine(directory, "Directory.Build.props"), $""); File.WriteAllText(Path.Combine(directory, "Directory.Build.targets"), $""); var existing = hasGenerator ? $"" : ""; var project = Path.Combine(directory, "Probe.csproj"); File.WriteAllText(project, $"net10.0Exe{isTestProject}{existing}"); using var process = Process.Start(new ProcessStartInfo("dotnet") { WorkingDirectory = root.FullName, ArgumentList = { "msbuild", project, "-getItem:ProjectReference" }, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true })!; var output = process.StandardOutput.ReadToEnd(); var error = process.StandardError.ReadToEnd(); process.WaitForExit(); process.ExitCode.ShouldBe(0, error); using var result = JsonDocument.Parse(output); var references = result.RootElement.GetProperty("Items").GetProperty("ProjectReference").EnumerateArray() .Select(i => Path.GetFileName(i.GetProperty("Identity").GetString())).ToArray(); references.Count(n => n == "AtomUI.Generator.csproj").ShouldBe(hasGenerator || !isTestProject ? 1 : 0); references.ShouldContain("UnrelatedAnalyzer.csproj"); } finally { Directory.Delete(directory, true); } } }