From 0dfb43b15a355f59b83a6453e71248e197831fed Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Tue, 29 Aug 2017 17:13:17 +0100 Subject: [PATCH] Install latest build of templates before running tests --- test/Helpers/TemplatePackageInstaller.cs | 86 ++++++++++++++++++++++++ test/Helpers/TemplateTestBase.cs | 5 ++ 2 files changed, 91 insertions(+) create mode 100644 test/Helpers/TemplatePackageInstaller.cs diff --git a/test/Helpers/TemplatePackageInstaller.cs b/test/Helpers/TemplatePackageInstaller.cs new file mode 100644 index 0000000000..b495183c66 --- /dev/null +++ b/test/Helpers/TemplatePackageInstaller.cs @@ -0,0 +1,86 @@ +using System; +using System.IO; +using System.Linq; + +namespace Templates.Test.Helpers +{ + internal static class TemplatePackageInstaller + { + private static readonly string[] _templatePackages = new[] + { + "Microsoft.DotNet.Web.ItemTemplates", + "Microsoft.DotNet.Web.ProjectTemplates.2.0", + }; + + public static void ReinstallTemplatePackages() + { + // Remove any previous or prebundled version of the template packages + foreach (var packageName in _templatePackages) + { + var proc = ProcessEx.Run( + Directory.GetCurrentDirectory(), + "dotnet", + $"new --uninstall {packageName}"); + proc.WaitForExit(assertSuccess: true); + } + + VerifyCannotFindTemplate("ASP.NET Core Empty"); + + // Locate the artifacts directory containing the built template packages + var solutionDir = FindAncestorDirectoryContaining("Templating.sln"); + var artifactsDir = Path.Combine(solutionDir, "artifacts"); + var builtPackages = Directory.GetFiles(artifactsDir, "*.nupkg"); + foreach (var packagePath in builtPackages) + { + if (_templatePackages.Any(name => Path.GetFileName(packagePath).StartsWith(name, StringComparison.OrdinalIgnoreCase))) + { + Console.WriteLine($"Installing templates package {packagePath}..."); + var proc = ProcessEx.Run( + Directory.GetCurrentDirectory(), + "dotnet", + $"new --install \"{packagePath}\""); + proc.WaitForExit(assertSuccess: true); + } + } + } + + private static void VerifyCannotFindTemplate(string templateName) + { + // Verify we really did remove the previous templates + var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("D")); + Directory.CreateDirectory(tempDir); + try + { + var proc = ProcessEx.Run( + tempDir, + "dotnet", + $"new \"{templateName}\""); + proc.WaitForExit(assertSuccess: false); + if (!proc.Error.Contains($"No templates matched the input template name: {templateName}.")) + { + throw new InvalidOperationException($"Failed to uninstall previous templates. The template '{templateName}' could still be found."); + } + } + finally + { + Directory.Delete(tempDir); + } + } + + private static string FindAncestorDirectoryContaining(string filename) + { + var dir = Directory.GetCurrentDirectory(); + while (dir != null) + { + if (File.Exists(Path.Combine(dir, filename))) + { + return dir; + } + + dir = Directory.GetParent(dir)?.FullName; + } + + throw new InvalidOperationException($"Could not find any ancestor directory containing {filename} at or above {Directory.GetCurrentDirectory()}"); + } + } +} diff --git a/test/Helpers/TemplateTestBase.cs b/test/Helpers/TemplateTestBase.cs index 8727126b97..fd50ed4666 100644 --- a/test/Helpers/TemplateTestBase.cs +++ b/test/Helpers/TemplateTestBase.cs @@ -12,6 +12,11 @@ namespace Templates.Test protected string ProjectName { get; set; } protected string TemplateOutputDir { get; private set; } + static TemplateTestBase() + { + TemplatePackageInstaller.ReinstallTemplatePackages(); + } + public TemplateTestBase() { ProjectName = Guid.NewGuid().ToString().Replace("-", "");