Install latest build of templates before running tests

This commit is contained in:
Steve Sanderson 2017-08-29 17:13:17 +01:00
parent 1f9dd4db5d
commit 0dfb43b15a
2 changed files with 91 additions and 0 deletions

View File

@ -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()}");
}
}
}

View File

@ -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("-", "");