diff --git a/global.json b/global.json index 4cebc9516ba..779cf7ad89a 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "tools": { - "dotnet": "6.0.100-preview.7.21362.12", + "dotnet": "6.0.100-preview.7.21364.4", "runtimes": { "dotnet": [ "$(MicrosoftNETCoreAppRefPackageVersion)" diff --git a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.6.0/content/ClassLibrary-CSharp/.template.config/template.json b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.6.0/content/ClassLibrary-CSharp/.template.config/template.json index 13be525a7d0..39c4cd89897 100644 --- a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.6.0/content/ClassLibrary-CSharp/.template.config/template.json +++ b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.6.0/content/ClassLibrary-CSharp/.template.config/template.json @@ -105,15 +105,15 @@ }, "csharpFeature_ImplicitUsings": { "type": "computed", - "value": "Framework != \"netstandard2.0\" && Framework != \"netstandard2.1\" && csharp10orLater == \"true\"" + "value": "Framework == \"net6.0\" && csharp10orLater == \"true\"" }, "csharpFeature_FileScopedNamespaces": { "type": "computed", - "value": "Framework != \"netstandard2.0\" && Framework != \"netstandard2.1\" && csharp10orLater == \"true\"" + "value": "(Framework == \"net6.0\" || langVersion != \"\") && csharp10orLater == \"true\"" }, "csharpFeature_Nullable": { "type": "computed", - "value": "(Framework != \"netstandard2.0\" || Framework == \"netstandard2.0\" && langVersion != \"\") && csharp8orLater == \"true\"" + "value": "(Framework != \"netstandard2.0\" || langVersion != \"\") && csharp8orLater == \"true\"" } }, "primaryOutputs": [ diff --git a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.6.0/content/ClassLibrary-CSharp/Class1.cs b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.6.0/content/ClassLibrary-CSharp/Class1.cs index 2dcafee5a35..24e7f1f8fd4 100644 --- a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.6.0/content/ClassLibrary-CSharp/Class1.cs +++ b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.6.0/content/ClassLibrary-CSharp/Class1.cs @@ -2,6 +2,13 @@ using System; #endif +#if (csharpFeature_FileScopedNamespaces) +namespace Company.ClassLibrary1; +public class Class1 +{ + +} +#else namespace Company.ClassLibrary1 { public class Class1 @@ -9,3 +16,4 @@ public class Class1 } } +#endif diff --git a/test/dotnet-new3.UnitTests/CommonTemplatesTests.cs b/test/dotnet-new3.UnitTests/CommonTemplatesTests.cs index b72c56fc23b..ab7ca0635bf 100644 --- a/test/dotnet-new3.UnitTests/CommonTemplatesTests.cs +++ b/test/dotnet-new3.UnitTests/CommonTemplatesTests.cs @@ -167,7 +167,7 @@ Determining projects to restore\.\.\. } [Theory] - //creates all possible combinations for supported templatees, languages versions and frameworks + //creates all possible combinations for supported templates, language versions and frameworks [MemberData(nameof(TopLevelProgramSupport_Data))] public void TopLevelProgramSupport(string name, bool pass, string? framework, string? langVersion, bool hasTopLevelProgram) { @@ -263,7 +263,7 @@ public void TopLevelProgramSupport(string name, bool pass, string? framework, st } [Theory] - //creates all possible combinations for supported languages and frameworks + //creates all possible combinations for supported templates, language versions and frameworks [MemberData(nameof(NullableSupport_Data))] public void NullableSupport(string name, bool pass, string? framework, string? langVersion, bool supportsNullable) { @@ -348,7 +348,7 @@ public void NullableSupport(string name, bool pass, string? framework, string? l } [Theory] - //creates all possible combinations for supported languages and frameworks + //creates all possible combinations for supported templates, language versions and frameworks [MemberData(nameof(ImplicitUsingsSupport_Data))] public void ImplicitUsingsSupport(string name, bool pass, string? framework, string? langVersion, bool supportsImplicitUsings) { @@ -403,6 +403,107 @@ public void ImplicitUsingsSupport(string name, bool pass, string? framework, str } } + public static IEnumerable FileScopedNamespacesSupport_Data() + { + var templatesToTest = new[] + { + new { Template = "classlib", Frameworks = new[] { null, "net6.0", "netstandard2.0", "netstandard2.1" } } + }; + string[] unsupportedLanguageVersions = { "1", "ISO-1" }; + string?[] supportedLanguageVersions = { null, "ISO-2", "2", "3", "4", "5", "6", "7", "7.1", "7.2", "7.3", "8.0", "9.0", "10.0", "latest", "latestMajor", "default", "preview" }; + + string?[] supportedFrameworks = { null, "net6.0" }; + string?[] fileScopedNamespacesSupportedLanguages = { "10.0", "latest", "latestMajor", "default", "preview" }; + + foreach (var template in templatesToTest) + { + foreach (var langVersion in unsupportedLanguageVersions) + { + foreach (var framework in template.Frameworks) + { + yield return new object?[] { template.Template, false, framework, langVersion, fileScopedNamespacesSupportedLanguages.Contains(langVersion) || langVersion == null && supportedFrameworks.Contains(framework) }; + } + } + foreach (var langVersion in supportedLanguageVersions) + { + foreach (var framework in template.Frameworks) + { + yield return new object?[] { template.Template, true, framework, langVersion, fileScopedNamespacesSupportedLanguages.Contains(langVersion) || langVersion == null && supportedFrameworks.Contains(framework) }; + } + } + } + } + + [Theory] + //creates all possible combinations for supported templates, language versions and frameworks + [MemberData(nameof(FileScopedNamespacesSupport_Data))] + public void FileScopedNamespacesSupport(string name, bool pass, string? framework, string? langVersion, bool supportsFeature) + { + string workingDir = TestUtils.CreateTemporaryFolder(); + + List args = new List() { name, "-o", "MyProject" }; + if (!string.IsNullOrWhiteSpace(framework)) + { + args.Add("--framework"); + args.Add(framework); + } + if (!string.IsNullOrWhiteSpace(langVersion)) + { + args.Add("--langVersion"); + args.Add(langVersion); + } + + new DotnetNewCommand(_log, args.ToArray()) + .WithCustomHive(_fixture.HomeDirectory) + .WithWorkingDirectory(workingDir) + .Execute() + .Should() + .ExitWith(0) + .And.NotHaveStdErr(); + + var buildResult = new DotnetCommand(_log, "build", "MyProject") + .WithWorkingDirectory(workingDir) + .Execute(); + + if (pass) + { + buildResult.Should().ExitWith(0).And.NotHaveStdErr(); + } + else + { + buildResult.Should().Fail(); + return; + } + string codeFileName = name == "console" ? "Program.cs" : "Class1.cs"; + string programFileContent = File.ReadAllText(Path.Combine(workingDir, "MyProject", codeFileName)); + + string supportedContent = +@"namespace MyProject; +public class Class1 +{ + +}"; + string unsupportedContent = +@"namespace MyProject +{ + public class Class1 + { + + } +}"; + + if (supportsFeature) + { + Assert.DoesNotContain(unsupportedContent, programFileContent); + Assert.Contains(supportedContent, programFileContent); + } + else + { + Assert.DoesNotContain(supportedContent, programFileContent); + Assert.Contains(unsupportedContent, programFileContent); + } + } + [Theory] [InlineData("Console Application", "console")] [InlineData("Console Application", "console", "C#")]