|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +#nullable enable |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Diagnostics; |
| 9 | +using System.IO; |
| 10 | +using System.Linq; |
| 11 | +using System.Text; |
| 12 | +using System.Threading.Tasks; |
| 13 | + |
| 14 | +namespace Microsoft.Web.LibraryManager.Build.IntegrationTest; |
| 15 | + |
| 16 | +[TestClass] |
| 17 | +[DeploymentItem(@"TestPackages", "TestPackages")] |
| 18 | +[DeploymentItem(@"TestSolution", "TestSolution")] |
| 19 | +public class BuildTestBase |
| 20 | +{ |
| 21 | + private const string BuildPackageName = "Microsoft.Web.LibraryManager.Build"; |
| 22 | + private const string ManifestFileName = "libman.json"; |
| 23 | + private const string TestProjectFolderName = "Libman.Build.TestApp"; |
| 24 | + private readonly string PackagesFolderPath = Path.Combine(Environment.CurrentDirectory, "TestPackages"); |
| 25 | + protected string TestProjectDirectory { get; private set; } = ""; |
| 26 | + |
| 27 | + [TestInitialize] |
| 28 | + public async Task TestInitialize() |
| 29 | + { |
| 30 | + // test solution should be deployed with every test? |
| 31 | + TestProjectDirectory = Path.Combine(Path.GetFullPath("TestSolution"), TestProjectFolderName); |
| 32 | + |
| 33 | + // create an empty nuget.config with only our package source |
| 34 | + await AddPackageReferenceAsync(); |
| 35 | + } |
| 36 | + |
| 37 | + [TestCleanup] |
| 38 | + public void TestCleanup() |
| 39 | + { |
| 40 | + if (Directory.Exists(TestProjectDirectory)) |
| 41 | + { |
| 42 | + Directory.Delete(TestProjectDirectory, true); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + protected async Task RunDotnetCommandLineAsync(string arguments, string? workingDirectory) |
| 47 | + { |
| 48 | + var processStartInfo = new ProcessStartInfo("dotnet", arguments) |
| 49 | + { |
| 50 | + RedirectStandardInput = true, |
| 51 | + RedirectStandardOutput = true, |
| 52 | + RedirectStandardError = true, |
| 53 | + UseShellExecute = false, |
| 54 | + CreateNoWindow = true, |
| 55 | + WorkingDirectory = workingDirectory, |
| 56 | + }; |
| 57 | + |
| 58 | + using (var process = Process.Start(processStartInfo)) |
| 59 | + { |
| 60 | + await process.WaitForExitAsync(); |
| 61 | + if (process.ExitCode != 0) |
| 62 | + { |
| 63 | + string output = await process.StandardError.ReadToEndAsync() + await process.StandardOutput.ReadToEndAsync(); |
| 64 | + throw new InvalidOperationException($"CLI tool execution failed with arguments: {arguments}.\r\nOutput: {output}"); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private async Task AddPackageReferenceAsync() |
| 70 | + { |
| 71 | + await RunDotnetCommandLineAsync($"nuget add source \"{PackagesFolderPath}\"", TestProjectDirectory); |
| 72 | + |
| 73 | + await RunDotnetCommandLineAsync($"add package {BuildPackageName} --version {GetBuildPackageVersion()}", TestProjectDirectory); |
| 74 | + } |
| 75 | + |
| 76 | + private static string GetBuildPackageVersion() |
| 77 | + { |
| 78 | + return Directory.GetFiles("TestPackages", $"{BuildPackageName}.*.nupkg") |
| 79 | + .Select(Path.GetFileNameWithoutExtension) |
| 80 | + .Select(name => name.Substring(BuildPackageName.Length + 1)) |
| 81 | + .OrderByDescending(version => version) |
| 82 | + .First(); |
| 83 | + } |
| 84 | + |
| 85 | + protected async Task CreateManifestFileAsync(string content) |
| 86 | + { |
| 87 | + string manifestFilePath = Path.Combine(TestProjectDirectory, ManifestFileName); |
| 88 | + await Task.Run(() => File.WriteAllText(manifestFilePath, content)); |
| 89 | + } |
| 90 | + |
| 91 | + protected void AssertFileExists(string relativeFilePath) |
| 92 | + { |
| 93 | + string filePath = Path.Combine(TestProjectDirectory, relativeFilePath); |
| 94 | + Assert.IsTrue(File.Exists(filePath), $"Expected file '{filePath}' does not exist."); |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments