|
| 1 | +using System.Reflection; |
| 2 | +using System.Runtime.CompilerServices; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | +using System.Runtime.Versioning; |
| 5 | +using System.Xml.Linq; |
| 6 | +using System.Xml.XPath; |
| 7 | +using CliWrap; |
| 8 | +using FluentAssertions; |
| 9 | +using Medallion.Threading; |
| 10 | +using Medallion.Threading.FileSystem; |
| 11 | +using NuGet.Frameworks; |
| 12 | +using Xunit.Abstractions; |
| 13 | +using Xunit.Sdk; |
| 14 | + |
| 15 | +namespace Serilog.Settings.Configuration.Tests; |
| 16 | + |
| 17 | +public class TestApp : IAsyncLifetime |
| 18 | +{ |
| 19 | + readonly IMessageSink _messageSink; |
| 20 | + readonly DirectoryInfo _workingDirectory; |
| 21 | + readonly List<DirectoryInfo> _directoriesToCleanup; |
| 22 | + readonly IDistributedLock _lock; |
| 23 | + IDistributedSynchronizationHandle? _lockHandle; |
| 24 | + |
| 25 | + public TestApp(IMessageSink messageSink) |
| 26 | + { |
| 27 | + _messageSink = messageSink; |
| 28 | + _workingDirectory = GetDirectory("test", "TestApp"); |
| 29 | + _directoriesToCleanup = new List<DirectoryInfo>(); |
| 30 | + _lock = new FileDistributedLock(new FileInfo(Path.Combine(_workingDirectory.FullName, "dotnet-restore.lock"))); |
| 31 | + } |
| 32 | + |
| 33 | + public async Task InitializeAsync() |
| 34 | + { |
| 35 | + _lockHandle = await _lock.AcquireAsync(); |
| 36 | + |
| 37 | + var targetFrameworkAttribute = typeof(TestApp).Assembly.GetCustomAttribute<TargetFrameworkAttribute>(); |
| 38 | + if (targetFrameworkAttribute == null) |
| 39 | + { |
| 40 | + throw new Exception($"Assembly {typeof(TestApp).Assembly} does not have a {nameof(TargetFrameworkAttribute)}"); |
| 41 | + } |
| 42 | + |
| 43 | + var targetFramework = NuGetFramework.Parse(targetFrameworkAttribute.FrameworkName); |
| 44 | + foreach (var singleFile in new[] { true, false }) |
| 45 | + { |
| 46 | + var framework = targetFramework.GetShortFolderName(); |
| 47 | + var isDesktop = targetFramework.IsDesktop(); |
| 48 | + |
| 49 | + var outputDirectory = new DirectoryInfo(Path.Combine(_workingDirectory.FullName, framework, singleFile ? "publish-single-file" : "publish-standard")); |
| 50 | + _directoriesToCleanup.Add(outputDirectory.Parent!); |
| 51 | + |
| 52 | + var restoreArgs = new[] { "restore", "--no-dependencies", $"-p:TargetFrameworks={string.Join("%3B", GetProjectTargetFrameworks().Append(framework).Distinct())}" }; |
| 53 | + await RunDotnetAsync(_workingDirectory, restoreArgs); |
| 54 | + |
| 55 | + File.WriteAllText(Path.Combine(_workingDirectory.FullName, "FodyWeavers.xml"), singleFile && isDesktop ? "<Weavers><Costura/></Weavers>" : "<Weavers/>"); |
| 56 | + |
| 57 | + var args = new[] { "publish", "--no-restore", "--configuration", "Release", "--output", outputDirectory.FullName, $"-p:TargetFramework={framework}" }; |
| 58 | + await RunDotnetAsync(_workingDirectory, isDesktop ? args : args.Append($"-p:PublishSingleFile={singleFile}").ToArray()); |
| 59 | + |
| 60 | + var executableFileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "TestApp.exe" : "TestApp"; |
| 61 | + var executableFile = new FileInfo(Path.Combine(outputDirectory.FullName, executableFileName)); |
| 62 | + executableFile.Exists.Should().BeTrue(); |
| 63 | + var dlls = executableFile.Directory!.EnumerateFiles("*.dll"); |
| 64 | + if (singleFile) |
| 65 | + { |
| 66 | + dlls.Should().BeEmpty(because: "the test app was published as single-file"); |
| 67 | + executableFile.Directory.EnumerateFiles().Should().ContainSingle().Which.FullName.Should().Be(executableFile.FullName); |
| 68 | + SingleFileExe = executableFile; |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + dlls.Should().NotBeEmpty(because: "the test app was _not_ published as single-file"); |
| 73 | + StandardExe = executableFile; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public async Task DisposeAsync() |
| 79 | + { |
| 80 | + try |
| 81 | + { |
| 82 | + foreach (var directoryToCleanup in _directoriesToCleanup.Where(e => e.Exists)) |
| 83 | + { |
| 84 | + directoryToCleanup.Delete(recursive: true); |
| 85 | + } |
| 86 | + } |
| 87 | + finally |
| 88 | + { |
| 89 | + await _lockHandle!.DisposeAsync(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public FileInfo SingleFileExe { get; private set; } = null!; |
| 94 | + public FileInfo StandardExe { get; private set; } = null!; |
| 95 | + |
| 96 | + async Task RunDotnetAsync(DirectoryInfo workingDirectory, params string[] arguments) |
| 97 | + { |
| 98 | + _messageSink.OnMessage(new DiagnosticMessage($"cd {workingDirectory}")); |
| 99 | + _messageSink.OnMessage(new DiagnosticMessage($"dotnet {string.Join(" ", arguments)}")); |
| 100 | + var messageSinkTarget = PipeTarget.ToDelegate(line => _messageSink.OnMessage(new DiagnosticMessage(line))); |
| 101 | + await Cli.Wrap("dotnet") |
| 102 | + .WithWorkingDirectory(workingDirectory.FullName) |
| 103 | + .WithArguments(arguments) |
| 104 | + .WithStandardOutputPipe(messageSinkTarget) |
| 105 | + .WithStandardErrorPipe(messageSinkTarget) |
| 106 | + .ExecuteAsync(); |
| 107 | + } |
| 108 | + |
| 109 | + static IEnumerable<string> GetProjectTargetFrameworks() |
| 110 | + { |
| 111 | + var projectFile = GetFile("src", "Serilog.Settings.Configuration", "Serilog.Settings.Configuration.csproj"); |
| 112 | + var project = XDocument.Load(projectFile.FullName); |
| 113 | + var targetFrameworks = project.XPathSelectElement("/Project/PropertyGroup/TargetFrameworks") ?? throw new Exception($"TargetFrameworks element not found in {projectFile}"); |
| 114 | + return targetFrameworks.Value.Split(';'); |
| 115 | + } |
| 116 | + |
| 117 | + static DirectoryInfo GetDirectory(params string[] paths) |
| 118 | + { |
| 119 | + var directory = new DirectoryInfo(GetFullPath(paths)); |
| 120 | + if (!directory.Exists) |
| 121 | + { |
| 122 | + throw new DirectoryNotFoundException($"The {directory.Name} directory must exist at {directory.FullName}"); |
| 123 | + } |
| 124 | + return directory; |
| 125 | + } |
| 126 | + |
| 127 | + static FileInfo GetFile(params string[] paths) |
| 128 | + { |
| 129 | + var file = new FileInfo(GetFullPath(paths)); |
| 130 | + if (!file.Exists) |
| 131 | + { |
| 132 | + throw new FileNotFoundException($"The {file.Name} file must exist at {file.FullName}"); |
| 133 | + } |
| 134 | + return file; |
| 135 | + } |
| 136 | + |
| 137 | + static string GetFullPath(params string[] paths) => Path.GetFullPath(Path.Combine(new[] { GetThisDirectory(), "..", ".." }.Concat(paths).ToArray())); |
| 138 | + |
| 139 | + static string GetThisDirectory([CallerFilePath] string path = "") => Path.GetDirectoryName(path)!; |
| 140 | +} |
0 commit comments