Skip to content

Commit

Permalink
added unit and integration tests for debug options
Browse files Browse the repository at this point in the history
  • Loading branch information
vlada-shubina committed Jan 14, 2022
1 parent e7ae6c0 commit 3771a89
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 2 deletions.
115 changes: 115 additions & 0 deletions test/Microsoft.TemplateEngine.Cli.UnitTests/ParserTests/MiscTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@

#nullable enable

using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Completions;
using System.CommandLine.Parsing;
using Microsoft.TemplateEngine.Abstractions;
using Microsoft.TemplateEngine.Cli.Commands;
using Microsoft.TemplateEngine.TestHelper;
using Xunit;

namespace Microsoft.TemplateEngine.Cli.UnitTests.ParserTests
Expand Down Expand Up @@ -54,5 +57,117 @@ public void CompletionItemCompareIsAsExpected()
new CompletionItem("my-label", kind: "Argument", sortText: "sort-text2", insertText: "insert-text2", documentation: "doc2", detail: "det2"));

}

[Theory]
[InlineData("new --debug:attach", "--debug:attach")]
[InlineData("new --debug:attach console", "--debug:attach")]
[InlineData("new --debug:reinit", "--debug:reinit")]
[InlineData("new --debug:ephemeral-hive", "--debug:ephemeral-hive")]
[InlineData("new --debug:virtual-hive", "--debug:ephemeral-hive")]
[InlineData("new --debug:rebuildcache", "--debug:rebuild-cache")]
[InlineData("new --debug:rebuild-cache", "--debug:rebuild-cache")]
[InlineData("new --debug:show-config", "--debug:show-config")]
[InlineData("new --debug:showconfig", "--debug:show-config")]

public void DebugFlagCanBeParsedOnNewLevel(string command, string option)
{
Dictionary<string, Func<GlobalArgs, bool>> optionsMap = new Dictionary<string, Func<GlobalArgs, bool>>()
{
{ "--debug:attach", args => args.DebugAttach },
{ "--debug:ephemeral-hive", args => args.DebugVirtualizeSettings },
{ "--debug:reinit", args => args.DebugReinit },
{ "--debug:rebuild-cache", args => args.DebugRebuildCache },
{ "--debug:show-config", args => args.DebugShowConfig }
};

ITemplateEngineHost host = TestHost.GetVirtualHost(additionalComponents: BuiltInTemplatePackagesProviderFactory.GetComponents(includeTestTemplates: false));
NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host, _ => new TelemetryLogger(null, false), new NewCommandCallbacks());
var parseResult = ParserFactory.CreateParser(myCommand).Parse(command);
NewCommandArgs args = new NewCommandArgs(myCommand, parseResult);

Assert.True(optionsMap[option](args));
}

[Theory]
[InlineData("new install source --debug:attach", "--debug:attach")]
[InlineData("new install source --debug:attach --add-source s1", "--debug:attach")]
[InlineData("new install --debug:reinit source", "--debug:reinit")]
[InlineData("new install source --debug:ephemeral-hive", "--debug:ephemeral-hive")]
[InlineData("new install source --debug:virtual-hive", "--debug:ephemeral-hive")]
[InlineData("new install source --debug:rebuildcache", "--debug:rebuild-cache")]
[InlineData("new install source --debug:rebuild-cache", "--debug:rebuild-cache")]
[InlineData("new install source --debug:show-config", "--debug:show-config")]
[InlineData("new install source --debug:showconfig", "--debug:show-config")]

public void DebugFlagCanBeParsedOnSubcommandLevel(string command, string option)
{
Dictionary<string, Func<GlobalArgs, bool>> optionsMap = new Dictionary<string, Func<GlobalArgs, bool>>()
{
{ "--debug:attach", args => args.DebugAttach },
{ "--debug:ephemeral-hive", args => args.DebugVirtualizeSettings },
{ "--debug:reinit", args => args.DebugReinit },
{ "--debug:rebuild-cache", args => args.DebugRebuildCache },
{ "--debug:show-config", args => args.DebugShowConfig }
};

ITemplateEngineHost host = TestHost.GetVirtualHost(additionalComponents: BuiltInTemplatePackagesProviderFactory.GetComponents(includeTestTemplates: false));
NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host, _ => new TelemetryLogger(null, false), new NewCommandCallbacks());
var parseResult = ParserFactory.CreateParser(myCommand).Parse(command);
InstallCommandArgs args = new InstallCommandArgs((InstallCommand)parseResult.CommandResult.Command, parseResult);

Assert.True(optionsMap[option](args));
}

[Theory]
[InlineData("new console --framework net5.0 --flag --debug:attach", "--debug:attach")]
[InlineData("new console --framework net5.0 --debug:attach --flag", "--debug:attach")]
[InlineData("new console --debug:reinit --framework net5.0 --flag", "--debug:reinit")]
[InlineData("new console --framework net5.0 --debug:ephemeral-hive --flag", "--debug:ephemeral-hive")]
[InlineData("new console --framework net5.0 --debug:virtual-hive --flag", "--debug:ephemeral-hive")]
[InlineData("new console --framework net5.0 --debug:rebuildcache --flag", "--debug:rebuild-cache")]
[InlineData("new console --framework net5.0 --debug:rebuild-cache --flag", "--debug:rebuild-cache")]
[InlineData("new console --framework net5.0 --debug:show-config --flag", "--debug:show-config")]
[InlineData("new console --framework net5.0 --debug:showconfig --flag", "--debug:show-config")]

public void DebugFlagCanBeParsedOnTemplateSubcommandLevel(string command, string option)
{
Dictionary<string, Func<GlobalArgs, bool>> optionsMap = new Dictionary<string, Func<GlobalArgs, bool>>()
{
{ "--debug:attach", args => args.DebugAttach },
{ "--debug:ephemeral-hive", args => args.DebugVirtualizeSettings },
{ "--debug:reinit", args => args.DebugReinit },
{ "--debug:rebuild-cache", args => args.DebugRebuildCache },
{ "--debug:show-config", args => args.DebugShowConfig }
};

ITemplateEngineHost host = TestHost.GetVirtualHost(additionalComponents: BuiltInTemplatePackagesProviderFactory.GetComponents(includeTestTemplates: false));
NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host, _ => new TelemetryLogger(null, false), new NewCommandCallbacks());
InstantiateCommand instantiateCommand = InstantiateCommand.FromNewCommand(myCommand);
var parseResult = instantiateCommand.Parse(command);
InstantiateCommandArgs args = new InstantiateCommandArgs(instantiateCommand, parseResult);

Assert.True(optionsMap[option](args));
Assert.Equal("console", args.ShortName);
Assert.Equal(new[] { "--framework", "net5.0", "--flag" }, args.RemainingArguments);
}

[Fact]
public void ManuallyAddedOptionIsPreservedOnTemplateSubcommandLevel()
{
ITemplateEngineHost host = TestHost.GetVirtualHost(additionalComponents: BuiltInTemplatePackagesProviderFactory.GetComponents(includeTestTemplates: false));
NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host, _ => new TelemetryLogger(null, false), new NewCommandCallbacks());

var customOption = new Option<string>("--newOption");
myCommand.AddGlobalOption(customOption);

InstantiateCommand instantiateCommand = InstantiateCommand.FromNewCommand(myCommand);
var parseResult = instantiateCommand.Parse("new console --newOption val");
InstantiateCommandArgs args = new InstantiateCommandArgs(instantiateCommand, parseResult);

Assert.NotNull(args.ParseResult);
Assert.Equal("console", args.ShortName);
Assert.Empty(args.RemainingArguments);
Assert.Equal("val", args.ParseResult.GetValueForOption(customOption));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Current configuration:

Mount Point Factories Type Assembly
------------------------------------ -------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------
94e92610-cf4c-4f6d-aeb6-9e42dde1899d Microsoft.TemplateEngine.Edge.Mount.Archive.ZipFileMountPointFactory Microsoft.TemplateEngine.Edge, Version=<version>, Culture=neutral, PublicKeyToken=<token>
8c19221b-dea3-4250-86fe-2d4e189a11d2 Microsoft.TemplateEngine.Edge.Mount.FileSystem.FileSystemMountPointFactory Microsoft.TemplateEngine.Edge, Version=<version>, Culture=neutral, PublicKeyToken=<token>


Generators Type Assembly
------------------------------------ ------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------------------------------------
0c434df7-e2cb-4dee-b216-d7c58c8eb4b3 Microsoft.TemplateEngine.Orchestrator.RunnableProjects.RunnableProjectGenerator Microsoft.TemplateEngine.Orchestrator.RunnableProjects, Version=<version>, Culture=neutral, PublicKeyToken=<token>


The 'dotnet new3' command creates a .NET project based on a template.

Common templates are:
Template Name Short Name Language Tags
------------- ---------- ---------- --------------
Class Library classlib [C#],F#,VB Common/Library
Console App console [C#],F#,VB Common/Console

An example would be:
dotnet new3 console

Display template options with:
dotnet new3 console -h
Display all installed templates with:
dotnet new3 --list
Display templates available on NuGet.org with:
dotnet new3 web --search
4 changes: 2 additions & 2 deletions test/dotnet-new3.UnitTests/DotnetNew3Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public void CanDisableBuiltInTemplates_List()

commandResult
.Should()
.Fail()
.Pass()
.And.NotHaveStdOutContaining("console")
.And.HaveStdErrContaining("No templates installed.");
.And.HaveStdOutContaining("No templates installed.");
}

[Fact]
Expand Down
52 changes: 52 additions & 0 deletions test/dotnet-new3.UnitTests/DotnetNewDebugOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#nullable enable

using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using FluentAssertions;
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.TemplateEngine.TestHelper;
Expand Down Expand Up @@ -67,5 +69,55 @@ public void CanShowBasicInfoWithDebugRebuildCache()
Assert.True(File.Exists(cacheFilePath));
Assert.True(lastUpdateDate < File.GetLastWriteTimeUtc(cacheFilePath));
}

[Fact]
public void CanShowConfigWithDebugShowConfig()
{
string home = TestUtils.CreateTemporaryFolder("Home");
var commandResult = new DotnetNewCommand(_log, "--debug:show-config")
.WithCustomHive(home)
.Execute();

commandResult.Should().ExitWith(0).And.NotHaveStdErr();
ApprovalTests.Approvals.Verify(commandResult.StdOut, (output) =>
{
//remove versions
var finalOutput = Regex.Replace(output, "Version=[A-Za-z0-9\\.]+", "Version=<version>");
//remove tokens
finalOutput = Regex.Replace(finalOutput, "PublicKeyToken=[A-Za-z0-9]+", "PublicKeyToken=<token>");
return finalOutput;
});
}

[Fact]
public void DoesNotCreateCacheWhenVirtualHiveIsUsed()
{
string home = TestUtils.CreateTemporaryFolder("Home");
var envVariable = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "USERPROFILE" : "HOME";

new DotnetNewCommand(_log, "--debug:ephemeral-hive")
.WithoutCustomHive()
.WithEnvironmentVariable(envVariable, home)
.Execute()
.Should().Pass().And.NotHaveStdErr();

Assert.Empty(new DirectoryInfo(home).EnumerateFiles());
}

[Fact]
public void DoesCreateCacheInDifferentLocationWhenCustomHiveIsUsed()
{
string home = TestUtils.CreateTemporaryFolder("Home");
new DotnetNewCommand(_log, "--debug:custom-hive", home)
.WithoutCustomHive()
.Execute()
.Should().Pass().And.NotHaveStdErr();

var createdCacheEntries = Directory.GetFileSystemEntries(home);

Assert.Equal(2, createdCacheEntries.Count());
Assert.Contains(Path.Combine(home, "packages"), createdCacheEntries);
Assert.True(File.Exists(Path.Combine(home, "dotnetcli-preview", "v2.0.0", "templatecache.json")));
}
}
}

0 comments on commit 3771a89

Please sign in to comment.