Skip to content

Commit

Permalink
Fixes #32028: Investigate Template Baseline tests errors
Browse files Browse the repository at this point in the history
Latest `dotnet new` doesn't work with relative path for `--debug:custom-hive`... Hence make it absolute
Also switched to using `--debug:disable-sdk-templates` to avoid potential conflict between SDK and tests installed templates.
  • Loading branch information
DavidKarlas authored and halter73 committed Apr 22, 2021
1 parent cc57bb8 commit 850e934
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 51 deletions.
4 changes: 2 additions & 2 deletions src/ProjectTemplates/Shared/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class Project : IDisposable
// Used to set special options in MSBuild
IDictionary<string, string> environmentVariables = null)
{
var hiveArg = $"--debug:custom-hive \"{TemplatePackageInstaller.CustomHivePath}\"";
var hiveArg = $" --debug:disable-sdk-templates --debug:custom-hive \"{TemplatePackageInstaller.CustomHivePath}\"";
var argString = $"new {templateName} {hiveArg}";
environmentVariables ??= new Dictionary<string, string>();
if (!string.IsNullOrEmpty(auth))
Expand Down Expand Up @@ -323,7 +323,7 @@ internal async Task<ProcessEx> RunDotNetNewRawAsync(string arguments)
AppContext.BaseDirectory,
DotNetMuxer.MuxerPathOrDefault(),
arguments +
$" --debug:custom-hive \"{TemplatePackageInstaller.CustomHivePath}\"" +
$" --debug:disable-sdk-templates --debug:custom-hive \"{TemplatePackageInstaller.CustomHivePath}\"" +
$" -o {TemplateOutputDir}");
await result.Exited;
return result;
Expand Down
60 changes: 11 additions & 49 deletions src/ProjectTemplates/Shared/TemplatePackageInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ internal static class TemplatePackageInstaller
"Microsoft.AspNetCore.Blazor.Templates",
};

public static string CustomHivePath { get; } = (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("helix")))
public static string CustomHivePath { get; } = Path.GetFullPath((string.IsNullOrEmpty(Environment.GetEnvironmentVariable("helix")))
? typeof(TemplatePackageInstaller)
.Assembly.GetCustomAttributes<AssemblyMetadataAttribute>()
.Single(s => s.Key == "CustomTemplateHivePath").Value
: Path.Combine("Hives", ".templateEngine");
: Path.Combine("Hives", ".templateEngine"));

public static async Task EnsureTemplatingEngineInitializedAsync(ITestOutputHelper output)
{
Expand Down Expand Up @@ -78,7 +78,9 @@ public static async Task<ProcessEx> RunDotNetNew(ITestOutputHelper output, strin
output,
AppContext.BaseDirectory,
DotNetMuxer.MuxerPathOrDefault(),
$"new {arguments} --debug:custom-hive \"{CustomHivePath}\"");
//--debug:disable-sdk-templates means, don't include C:\Program Files\dotnet\templates, aka. what comes with SDK, so we don't need to uninstall
//--debug:custom-hive means, don't install templates on CI/developer machine, instead create new temporary instance
$"new {arguments} --debug:disable-sdk-templates --debug:custom-hive \"{CustomHivePath}\"");

await proc.Exited;

Expand All @@ -105,23 +107,12 @@ private static async Task InstallTemplatePackages(ITestOutputHelper output)

Assert.Equal(4, builtPackages.Length);

/*
* The templates are indexed by path, for example:
&USERPROFILE%\.templateengine\dotnetcli\v5.0.100-alpha1-013788\packages\nunit3.dotnetnew.template.1.6.1.nupkg
Templates:
NUnit 3 Test Project (nunit) C#
NUnit 3 Test Item (nunit-test) C#
NUnit 3 Test Project (nunit) F#
NUnit 3 Test Item (nunit-test) F#
NUnit 3 Test Project (nunit) VB
NUnit 3 Test Item (nunit-test) VB
Uninstall Command:
dotnet new -u &USERPROFILE%\.templateengine\dotnetcli\v5.0.100-alpha1-013788\packages\nunit3.dotnetnew.template.1.6.1.nupkg
* We don't want to construct this path so we'll rely on dotnet new --uninstall --help to construct the uninstall command.
*/
// Workaround for https://github.com/dotnet/sdk/issues/16906
// await UninstallExistingTemplatesAsync(output);
await VerifyCannotFindTemplateAsync(output, "web");
await VerifyCannotFindTemplateAsync(output, "webapp");
await VerifyCannotFindTemplateAsync(output, "mvc");
await VerifyCannotFindTemplateAsync(output, "react");
await VerifyCannotFindTemplateAsync(output, "reactredux");
await VerifyCannotFindTemplateAsync(output, "angular");

foreach (var packagePath in builtPackages)
{
Expand All @@ -135,35 +126,6 @@ private static async Task InstallTemplatePackages(ITestOutputHelper output)
await VerifyCanFindTemplate(output, "react");
}

private static async Task UninstallExistingTemplatesAsync(ITestOutputHelper output)
{
var proc = await RunDotNetNew(output, "--uninstall --help");
var lines = proc.Output.Split(Environment.NewLine);

// Remove any previous or prebundled version of the template packages
foreach (var packageName in _templatePackages)
{
// Depending on the ordering, there may be multiple matches:
// Microsoft.DotNet.Web.Spa.ProjectTemplates.3.0.3.0.0-preview7.*.nupkg
// Microsoft.DotNet.Web.Spa.ProjectTemplates.3.0.0-preview7.*.nupkg
// Error on the side of caution and uninstall all of them
foreach (var command in lines.Where(l => l.Contains("dotnet new") && l.Contains(packageName, StringComparison.OrdinalIgnoreCase)))
{
var uninstallCommand = command.TrimStart();
Debug.Assert(uninstallCommand.StartsWith("dotnet new", StringComparison.Ordinal));
uninstallCommand = uninstallCommand.Substring("dotnet new".Length);
await RunDotNetNew(output, uninstallCommand);
}
}

await VerifyCannotFindTemplateAsync(output, "web");
await VerifyCannotFindTemplateAsync(output, "webapp");
await VerifyCannotFindTemplateAsync(output, "mvc");
await VerifyCannotFindTemplateAsync(output, "react");
await VerifyCannotFindTemplateAsync(output, "reactredux");
await VerifyCannotFindTemplateAsync(output, "angular");
}

private static async Task VerifyCanFindTemplate(ITestOutputHelper output, string templateName)
{
var proc = await RunDotNetNew(output, $"");
Expand Down

0 comments on commit 850e934

Please sign in to comment.