Skip to content

Commit

Permalink
fixes #3248 new app template
Browse files Browse the repository at this point in the history
  • Loading branch information
vlada-shubina committed Jun 10, 2021
1 parent 357f056 commit 7439d76
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<PackageReference Remove="Microsoft.NETCore.App" />
<Compile Remove="$(GitInfoFile)" />
<Compile Remove="$(SrcDir)GitInfo.cs" />
<Content Include="content\**" >
<Content Include="content\**">
<PackagePath>content</PackagePath>
</Content>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "http://json.schemastore.org/dotnetcli.host",
"symbolInfo": {
"target-framework-override": {
"isHidden": "true",
"shortName": ""
},
"no-restore": {
"shortName": ""
},
"langVersion": {
"shortName": ""
},
"nullable": {
"shortName": ""
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{
"$schema": "http://json.schemastore.org/template",
"author": "Microsoft",
"classifications": [ "Common", "Console" ],
"name": "Simple Console Application",
"generatorVersions": "[1.0.0.0-*)",
"description": "A project for creating a command line application that uses the latest C# features and runs on .NET Core on Windows, Linux and macOS.",
"groupIdentity": "Microsoft.Common.App",
"precedence": "1000",
"identity": "Microsoft.Common.App.CSharp.6.0",
"shortName": "app",
"tags": {
"language": "C#",
"type": "project"
},
"sourceName": "Company.Application1",
"preferNameDirectory": true,
"symbols": {
"framework": {
"type": "parameter",
"description": "The target framework for the project.",
"datatype": "choice",
"choices": [
{
"choice": "net6.0",
"description": "Target net6.0"
}
],
"replaces": "net6.0",
"defaultValue": "net6.0"
},
"langVersion": {
"type": "parameter",
"datatype": "text",
"description": "Sets the LangVersion property in the created project file",
"defaultValue": "",
"replaces": "$(ProjectLanguageVersion)"
},
"no-restore": {
"type": "parameter",
"datatype": "bool",
"description": "If specified, skips the automatic restore of the project on create.",
"defaultValue": "false"
},
"nullable": {
"type": "parameter",
"datatype": "bool",
"defaultValue": "true",
"description": "Whether to enable nullable reference types for this project."
},
"target-framework-override": {
"type": "parameter",
"description": "Overrides the target framework",
"replaces": "target-framework-override",
"datatype": "string",
"defaultValue": ""
},
"HostIdentifier": {
"type": "bind",
"binding": "HostIdentifier"
}
},
"primaryOutputs": [
{ "path": "Company.Application1.csproj" },
{
"condition": "(HostIdentifier != \"dotnetcli\" && HostIdentifier != \"dotnetcli-preview\")",
"path": "Program.cs"
}
],
"defaultName": "Application1",
"postActions": [
{
"condition": "(!no-restore)",
"description": "Restore NuGet packages required by this project.",
"manualInstructions": [
{ "text": "Run 'dotnet restore'" }
],
"actionId": "210D431B-A78B-4D2F-B762-4ED3E3EA9025",
"continueOnError": true
},
{
"condition": "(HostIdentifier != \"dotnetcli\" && HostIdentifier != \"dotnetcli-preview\")",
"description": "Opens Program.cs in the editor",
"manualInstructions": [],
"actionId": "84C0DA21-51C8-4541-9940-6CA19AF04EE6",
"args": {
"files": "1"
},
"continueOnError": true
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework Condition="'$(target-framework-override)' == ''">net6.0</TargetFramework>
<TargetFramework Condition="'$(target-framework-override)' != ''">target-framework-override</TargetFramework>
<LangVersion Condition="'$(langVersion)' != ''">$(ProjectLanguageVersion)</LangVersion>
<Nullable Condition="'$(nullable)' == 'true'">enable</Nullable>
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using System;

Console.WriteLine("Hello, World!");

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">Company.ClassLibrary1</RootNamespace>
<LangVersion Condition="'$(langVersion)' != ''">$(ProjectLanguageVersion)</LangVersion>
<Nullable Condition="'$(Nullable)' == 'true'">enable</Nullable>
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
</PropertyGroup>

</Project>
169 changes: 169 additions & 0 deletions test/dotnet-new3.UnitTests/CommonTemplatesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Commands;
using Microsoft.TemplateEngine.TestHelper;
Expand Down Expand Up @@ -61,6 +63,10 @@ public CommonTemplatesTests(SharedHomeDirectory fixture, ITestOutputHelper log)
[InlineData("Class Library", "classlib", "C#", "netcoreapp2.1")]
[InlineData("Class Library", "classlib", "F#", "netcoreapp2.1")]
[InlineData("Class Library", "classlib", "VB", "netcoreapp2.1")]

[InlineData("Simple Console Application", "app")]
[InlineData("Simple Console Application", "app", "C#")]
[InlineData("Simple Console Application", "app", "C#", "net6.0")]
public void AllCommonProjectsCreateRestoreAndBuild(string expectedTemplateName, string templateShortName, string? language = null, string? framework = null)
{
string workingDir = TestUtils.CreateTemporaryFolder();
Expand Down Expand Up @@ -161,6 +167,10 @@ Determining projects to restore\.\.\.
[InlineData("Class Library", "classlib", "C#", "netcoreapp2.1")]
[InlineData("Class Library", "classlib", "F#", "netcoreapp2.1")]
[InlineData("Class Library", "classlib", "VB", "netcoreapp2.1")]

[InlineData("Simple Console Application", "app")]
[InlineData("Simple Console Application", "app", "C#")]
[InlineData("Simple Console Application", "app", "C#", "net6.0")]
public void AllCommonProjectsCreate_NoRestore(string expectedTemplateName, string templateShortName, string? language = null, string? framework = null)
{
string workingDir = TestUtils.CreateTemporaryFolder();
Expand Down Expand Up @@ -211,5 +221,164 @@ public void AllCommonItemsCreate(string expectedTemplateName, string templateSho

Directory.Delete(workingDir, true);
}

[Theory]
[InlineData("Nullable", "enable", "Simple Console Application", "app", null, null)]
[InlineData("CheckForOverflowUnderflow", "true", "Simple Console Application", "app", null, null)]
[InlineData("LangVersion", null, "Simple Console Application", "app", null, null)]
[InlineData("TargetFramework", "net6.0", "Simple Console Application", "app", null, null)]

[InlineData("Nullable", null, "Console Application", "console", null, null)]
[InlineData("CheckForOverflowUnderflow", null, "Console Application", "console", null, null)]
[InlineData("LangVersion", null, "Console Application", "console", null, null)]
[InlineData("TargetFramework", "net6.0", "Console Application", "console", null, null)]

[InlineData("Nullable", null, "Console Application", "console", "F#", null)]
[InlineData("CheckForOverflowUnderflow", null, "Console Application", "console", "F#", null)]
[InlineData("LangVersion", null, "Console Application", "console", "F#", null)]
[InlineData("TargetFramework", "net6.0", "Console Application", "console", "F#", null)]
[InlineData("GenerateDocumentationFile", null, "Console Application", "console", "F#", null)]

[InlineData("Nullable", null, "Console Application", "console", "VB", null)]
[InlineData("CheckForOverflowUnderflow", null, "Console Application", "console", "VB", null)]
[InlineData("LangVersion", null, "Console Application", "console", "VB", null)]
[InlineData("TargetFramework", "net6.0", "Console Application", "console", "VB", null)]

[InlineData("Nullable", "enable", "Class Library", "classlib", null, null)]
[InlineData("CheckForOverflowUnderflow", "true", "Class Library", "classlib", null, null)]
[InlineData("LangVersion", null, "Class Library", "classlib", null, null)]
[InlineData("TargetFramework", "net6.0", "Class Library", "classlib", null, null)]

[InlineData("Nullable", null, "Class Library", "classlib", "F#", null)]
[InlineData("CheckForOverflowUnderflow", null, "Class Library", "classlib", "F#", null)]
[InlineData("LangVersion", null, "Class Library", "classlib", "F#", null)]
[InlineData("TargetFramework", "net6.0", "Class Library", "classlib", "F#", null)]
[InlineData("GenerateDocumentationFile", "true", "Class Library", "classlib", "F#", null)]

[InlineData("Nullable", null, "Class Library", "classlib", "VB", null)]
[InlineData("CheckForOverflowUnderflow", null, "Class Library", "classlib", "VB", null)]
[InlineData("LangVersion", null, "Class Library", "classlib", "VB", null)]
[InlineData("TargetFramework", "net6.0", "Class Library", "classlib", "VB", null)]

public void SetPropertiesByDefault(string propertyName, string? propertyValue, string expectedTemplateName, string templateShortName, string? language, string? framework)
{
string workingDir = TestUtils.CreateTemporaryFolder();
List<string> args = new List<string>() { templateShortName, "--no-restore" };
if (!string.IsNullOrWhiteSpace(language))
{
args.Add("--language");
args.Add(language);
}
if (!string.IsNullOrWhiteSpace(framework))
{
args.Add("--framework");
args.Add(framework);
}

new DotnetNewCommand(_log, args.ToArray())
.WithCustomHive(_fixture.HomeDirectory)
.WithWorkingDirectory(workingDir)
.Execute()
.Should()
.ExitWith(0)
.And.NotHaveStdErr()
.And.HaveStdOut($@"The template ""{expectedTemplateName}"" was created successfully.");

string expectedExtension = language switch
{
"C#" => "*.csproj",
"F#" => "*.fsproj",
"VB" => "*.vbproj",
_ => "*.csproj"
};
string projectFile = Directory.GetFiles(workingDir, expectedExtension).Single();
XDocument projectXml = XDocument.Load(projectFile);
XNamespace ns = projectXml.Root?.Name.Namespace ?? throw new Exception("Unexpected project file format");
if (propertyValue != null)
{
Assert.Equal(propertyValue, projectXml.Root?.Element(ns + "PropertyGroup")?.Element(ns + propertyName)?.Value);
}
else
{
Assert.Null(projectXml.Root?.Element(ns + "PropertyGroup")?.Element(ns + propertyName));
}
Directory.Delete(workingDir, true);
}

[Theory]
//unset nullable
[InlineData("Nullable", null, "--nullable", "false", "Simple Console Application", "app", null, null)]
[InlineData("Nullable", null, "--nullable", "false", "Class Library", "classlib", null, null)]

//language version
[InlineData("LangVersion", "9.0", "--langVersion", "9.0", "Simple Console Application", "app", null, null)]
[InlineData("LangVersion", "9.0", "--langVersion", "9.0", "Console Application", "console", null, null)]
[InlineData("LangVersion", "9.0", "--langVersion", "9.0", "Console Application", "console", "VB", null)]
[InlineData("LangVersion", "9.0", "--langVersion", "9.0", "Class Library", "classlib", null, null)]
[InlineData("LangVersion", "9.0", "--langVersion", "9.0", "Class Library", "classlib", "VB", null)]

//framework
[InlineData("TargetFramework", "net5.0", "--framework", "net5.0", "Console Application", "console", null, null)]
[InlineData("TargetFramework", "net5.0", "--framework", "net5.0", "Console Application", "console", "VB", null)]
[InlineData("TargetFramework", "net5.0", "--framework", "net5.0", "Console Application", "console", "F#", null)]
[InlineData("TargetFramework", "net5.0", "--framework", "net5.0", "Class Library", "classlib", null, null)]
[InlineData("TargetFramework", "net5.0", "--framework", "net5.0", "Class Library", "classlib", "VB", null)]
[InlineData("TargetFramework", "net5.0", "--framework", "net5.0", "Class Library", "classlib", "F#", null)]

[InlineData("TargetFramework", "net5.0", "-f", "net5.0", "Console Application", "console", null, null)]
[InlineData("TargetFramework", "net5.0", "-f", "net5.0", "Console Application", "console", "VB", null)]
[InlineData("TargetFramework", "net5.0", "-f", "net5.0", "Console Application", "console", "F#", null)]
[InlineData("TargetFramework", "net5.0", "-f", "net5.0", "Class Library", "classlib", null, null)]
[InlineData("TargetFramework", "net5.0", "-f", "net5.0", "Class Library", "classlib", "VB", null)]
[InlineData("TargetFramework", "net5.0", "-f", "net5.0", "Class Library", "classlib", "F#", null)]
public void CanSetProperty(string propertyName, string? propertyValue, string argName, string argValue, string expectedTemplateName, string templateShortName, string? language, string? framework)
{
string workingDir = TestUtils.CreateTemporaryFolder();
List<string> args = new List<string>() { templateShortName, "--no-restore" };
if (!string.IsNullOrWhiteSpace(language))
{
args.Add("--language");
args.Add(language);
}
if (!string.IsNullOrWhiteSpace(framework))
{
args.Add("--framework");
args.Add(framework);
}
if (!string.IsNullOrWhiteSpace(argName))
{
args.Add(argName);
args.Add(argValue);
}

new DotnetNewCommand(_log, args.ToArray())
.WithCustomHive(_fixture.HomeDirectory)
.WithWorkingDirectory(workingDir)
.Execute()
.Should()
.ExitWith(0)
.And.NotHaveStdErr()
.And.HaveStdOut($@"The template ""{expectedTemplateName}"" was created successfully.");

string expectedExtension = language switch
{
"C#" => "*.csproj",
"F#" => "*.fsproj",
"VB" => "*.vbproj",
_ => "*.csproj"
};
string projectFile = Directory.GetFiles(workingDir, expectedExtension).Single();
XDocument projectXml = XDocument.Load(projectFile);
XNamespace ns = projectXml.Root?.Name.Namespace ?? throw new Exception("Unexpected project file format");
if (propertyValue != null)
{
Assert.Equal(propertyValue, projectXml.Root?.Element(ns + "PropertyGroup")?.Element(ns + propertyName)?.Value);
}
else
{
Assert.Null(projectXml.Root?.Element(ns + "PropertyGroup")?.Element(ns + propertyName));
}
Directory.Delete(workingDir, true);
}
}
}
28 changes: 27 additions & 1 deletion test/dotnet-new3.UnitTests/DotnetNewHelp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@ public class DotnetNewHelp
bool - Optional
Default: true ";

private const string ConsoleAppHelp =
@"Simple Console Application (C#)
Author: Microsoft
Description: A project for creating a command line application that uses the latest C# features and runs on .NET Core on Windows, Linux and macOS.
Options:
--langVersion Sets the LangVersion property in the created project file
text - Optional
--no-restore If specified, skips the automatic restore of the project on create.
bool - Optional
Default: false
--nullable Whether to enable nullable reference types for this project.
bool - Optional
Default: true ";

#endregion
private readonly ITestOutputHelper _log;

Expand Down Expand Up @@ -165,7 +181,7 @@ public void CanShowHelpForTemplate()
string home = TestUtils.CreateTemporaryFolder("Home");
string workingDirectory = TestUtils.CreateTemporaryFolder();

new DotnetNewCommand(_log, "console", "--help", "--quiet")
new DotnetNewCommand(_log, "console", "--help")
.WithCustomHive(home)
.WithWorkingDirectory(workingDirectory)
.Execute()
Expand All @@ -184,6 +200,16 @@ public void CanShowHelpForTemplate()
.And.NotHaveStdErr()
.And.HaveStdOut(ClassLibHelp)
.And.NotHaveStdOutContaining(HelpOutput);

new DotnetNewCommand(_log, "app", "-h")
.WithCustomHive(home)
.WithWorkingDirectory(workingDirectory)
.Execute()
.Should()
.ExitWith(0)
.And.NotHaveStdErr()
.And.HaveStdOut(ConsoleAppHelp)
.And.NotHaveStdOutContaining(HelpOutput);
}

[Fact]
Expand Down
Loading

0 comments on commit 7439d76

Please sign in to comment.