|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.IO; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 6 | + |
| 7 | +namespace DotNetProjectStarter.Tests; |
| 8 | + |
| 9 | +[TestClass] |
| 10 | +public sealed class LibraryTemplateGeneratorEndToEndTests |
| 11 | +{ |
| 12 | + private readonly string _outputDirectory; |
| 13 | + |
| 14 | + public LibraryTemplateGeneratorEndToEndTests() |
| 15 | + => _outputDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); |
| 16 | + |
| 17 | + [TestCleanup] |
| 18 | + public void TestCleanup() |
| 19 | + // TODO: Consider not deleting on failure, and adding to attachments? |
| 20 | + => Directory.Delete(_outputDirectory, recursive: true); |
| 21 | + |
| 22 | + [TestMethod] |
| 23 | + public async Task CreateProjectShouldSucceed() |
| 24 | + { |
| 25 | + var result = await RunToolWithArgumentsAsync(["--name", "CreateProjectShouldSucceedProjectName", "--output-directory", _outputDirectory]); |
| 26 | + Assert.AreEqual(0, result.ExitCode); |
| 27 | + |
| 28 | + var buildResult = await RunDotNetBuildWithArgumentsAsync([], Path.Combine(_outputDirectory, "CreateProjectShouldSucceedProjectName")); |
| 29 | + Assert.AreEqual(0, buildResult.ExitCode); |
| 30 | + } |
| 31 | + |
| 32 | + [TestMethod] |
| 33 | + public async Task CreateProjectShouldSucceedWithTreatWarningsAsErrors() |
| 34 | + { |
| 35 | + var result = await RunToolWithArgumentsAsync(["--name", "CreateProjectShouldSucceedWithTreatWarningsAsErrorsProjectName", "--output-directory", _outputDirectory]); |
| 36 | + Assert.AreEqual(0, result.ExitCode); |
| 37 | + |
| 38 | + var buildResult = await RunDotNetBuildWithArgumentsAsync(["-p:TreatWarningsAsErrors=true"], Path.Combine(_outputDirectory, "CreateProjectShouldSucceedWithTreatWarningsAsErrorsProjectName")); |
| 39 | + Assert.AreEqual(0, buildResult.ExitCode); |
| 40 | + } |
| 41 | + |
| 42 | + private static async Task<ToolResult> RunToolWithArgumentsAsync(string[] arguments, string? workingDirectory = null) |
| 43 | + => await RunProcessWithArgumentsAsync( |
| 44 | + Path.Combine(Directory.GetCurrentDirectory(), "DotNetProjectStarter" + (OperatingSystem.IsWindows() ? ".exe" : string.Empty)), |
| 45 | + arguments, |
| 46 | + workingDirectory: workingDirectory ?? Directory.GetCurrentDirectory()); |
| 47 | + |
| 48 | + private static async Task<ToolResult> RunDotNetBuildWithArgumentsAsync(string[] arguments, string? workingDirectory = null) |
| 49 | + => await RunProcessWithArgumentsAsync( |
| 50 | + "dotnet", |
| 51 | + ["build", .. arguments], |
| 52 | + workingDirectory: workingDirectory ?? Directory.GetCurrentDirectory()); |
| 53 | + |
| 54 | + private static async Task<ToolResult> RunProcessWithArgumentsAsync(string filePath, string[] arguments, string workingDirectory) |
| 55 | + { |
| 56 | + var processStartInfo = new ProcessStartInfo(filePath, arguments) |
| 57 | + { |
| 58 | + UseShellExecute = false, |
| 59 | + RedirectStandardError = true, |
| 60 | + RedirectStandardOutput = true, |
| 61 | + WorkingDirectory = workingDirectory |
| 62 | + }; |
| 63 | + |
| 64 | + using var process = Process.Start(processStartInfo); |
| 65 | + Assert.IsNotNull(process); |
| 66 | + |
| 67 | + var stdoutTask = Task.Factory.StartNew(() => process.StandardOutput.ReadToEnd()); |
| 68 | + var stderrTask = Task.Factory.StartNew(() => process.StandardError.ReadToEnd()); |
| 69 | + |
| 70 | + var standardOutputAndError = await Task.WhenAll(stdoutTask, stderrTask).ConfigureAwait(false); |
| 71 | + |
| 72 | + await process.WaitForExitAsync(); |
| 73 | + |
| 74 | + return new ToolResult(process.ExitCode, standardOutputAndError[0], standardOutputAndError[1]); |
| 75 | + } |
| 76 | + |
| 77 | + private sealed record ToolResult(int ExitCode, string StandardOutput, string StandardError); |
| 78 | +} |
0 commit comments