Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests for covering adding item templates (C# and VB) #15343

Merged
merged 4 commits into from
Feb 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 69 additions & 16 deletions test/EndToEnd/ProjectBuildTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -115,7 +116,7 @@ public void ItCanPublishArm64Winforms(string TargetFramework)
.Execute(newArgs)
.Should().Pass();

string publishArgs="-r win-arm64";
string publishArgs = "-r win-arm64";
new PublishCommand()
.WithWorkingDirectory(projectDirectory)
.Execute(publishArgs)
Expand Down Expand Up @@ -149,7 +150,7 @@ public void ItCanPublishArm64Wpf(string TargetFramework)
.Execute(newArgs)
.Should().Pass();

string publishArgs="-r win-arm64";
string publishArgs = "-r win-arm64";
new PublishCommand()
.WithWorkingDirectory(projectDirectory)
.Execute(publishArgs)
Expand Down Expand Up @@ -248,6 +249,47 @@ public void ItCanCreateItemTemplate(string templateName)
Assert.True(directory.EnumerateFileSystemInfos().Any());
}

[Theory]
// microsoft.dotnet.common.itemtemplates templates
[InlineData("class")]
[InlineData("struct")]
[InlineData("enum")]
[InlineData("record")]
[InlineData("interface")]
[InlineData("class", "C#")]
[InlineData("class", "VB")]
[InlineData("struct", "VB")]
[InlineData("enum", "VB")]
[InlineData("interface", "VB")]
public void ItCanCreateItemTemplateWithProjectRestriction(string templateName, string language = "")
{
var languageExtensionMap = new Dictionary<string, string>()
{
{ "", ".cs" },
{ "C#", ".cs" },
{ "VB", ".vb" }
};

DirectoryInfo directory = InstantiateProjectTemplate("classlib", language, withNoRestore: false);
string projectDirectory = directory.FullName;
string expectedItemName = $"TestItem_{templateName}";
string newArgs = $"{templateName} --name {expectedItemName} --debug:ephemeral-hive";
if (!string.IsNullOrWhiteSpace(language))
{
newArgs += $" --language {language}";
}

new NewCommandShim()
.WithWorkingDirectory(projectDirectory)
.Execute(newArgs)
.Should().Pass();

//check if the template created files
Assert.True(directory.Exists);
Assert.True(directory.EnumerateFileSystemInfos().Any());
Assert.True(directory.GetFile($"{expectedItemName}.{languageExtensionMap[language]}") != null);
}

[WindowsOnlyTheory]
[InlineData("wpf", Skip = "https://github.com/dotnet/wpf/issues/2363")]
[InlineData("winforms", Skip = "https://github.com/dotnet/wpf/issues/2363")]
Expand Down Expand Up @@ -402,20 +444,9 @@ private static string DetectExpectedDefaultFramework(string template = "")

private static void TestTemplateCreateAndBuild(string templateName, bool build = true, bool selfContained = false, string language = "", string framework = "")
{
DirectoryInfo directory = TestAssets.CreateTestDirectory(identifier: string.IsNullOrWhiteSpace(language) ? templateName : $"{templateName}[{language}]");
DirectoryInfo directory = InstantiateProjectTemplate(templateName, language);
string projectDirectory = directory.FullName;

string newArgs = $"{templateName} --debug:ephemeral-hive --no-restore";
if (!string.IsNullOrWhiteSpace(language))
{
newArgs += $" --language {language}";
}

new NewCommandShim()
.WithWorkingDirectory(projectDirectory)
.Execute(newArgs)
.Should().Pass();

if (!string.IsNullOrWhiteSpace(framework))
{
//check if MSBuild TargetFramework property for *proj is set to expected framework
Expand All @@ -439,13 +470,13 @@ private static void TestTemplateCreateAndBuild(string templateName, bool build =
{
buildArgs += $" --framework {framework}";
}

// Remove this (or formalize it) after https://github.com/dotnet/installer/issues/12479 is resolved.
if (language == "F#")
{
buildArgs += $" /p:_NETCoreSdkIsPreview=true";
}

string dotnetRoot = Path.GetDirectoryName(RepoDirectoriesProvider.DotnetUnderTest);
new BuildCommand()
.WithEnvironmentVariable("PATH", dotnetRoot) // override PATH since razor rely on PATH to find dotnet
Expand All @@ -454,5 +485,27 @@ private static void TestTemplateCreateAndBuild(string templateName, bool build =
.Should().Pass();
}
}

private static DirectoryInfo InstantiateProjectTemplate(string templateName, string language = "", bool withNoRestore = true)
{
DirectoryInfo directory = TestAssets.CreateTestDirectory(
identifier: string.IsNullOrWhiteSpace(language)
? templateName
: $"{templateName}[{language}]");
string projectDirectory = directory.FullName;

string newArgs = $"{templateName} --debug:ephemeral-hive {(withNoRestore ? "--no-restore" : "")}";
if (!string.IsNullOrWhiteSpace(language))
{
newArgs += $" --language {language}";
}

new NewCommandShim()
.WithWorkingDirectory(projectDirectory)
.Execute(newArgs)
.Should().Pass();

return directory;
}
}
}