From 0da5e1b8502f355efd8e81f00136f5e6147cab40 Mon Sep 17 00:00:00 2001 From: mariam-abdulla Date: Thu, 22 May 2025 10:18:31 +0200 Subject: [PATCH 1/2] add 1 test --- .../DotnetNewInstallTests.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/dotnet-new.IntegrationTests/DotnetNewInstallTests.cs b/test/dotnet-new.IntegrationTests/DotnetNewInstallTests.cs index 1b90e9d43929..5e7189a089e8 100644 --- a/test/dotnet-new.IntegrationTests/DotnetNewInstallTests.cs +++ b/test/dotnet-new.IntegrationTests/DotnetNewInstallTests.cs @@ -603,5 +603,16 @@ public void CanShowWarning_WhenHostDataIsIncorrect() .And.HaveStdOutContaining($"Success: {invalidTemplatePath} installed the following templates:") .And.HaveStdOutContaining("TestAssets.Invalid.InvalidHostData"); } + + [Fact] + public void CanShowWarning_WhenHostDataIsIncorrect1() + { + string templatePackagePath = Path.Combine(RepoTemplatePackages, "Microsoft.DotNet.Common.ProjectTemplates.10.0", "content"); + new DotnetNewCommand(_log, "install", templatePackagePath) + .WithCustomHive(CreateTemporaryFolder(folderName: "Home")) + .WithWorkingDirectory(CreateTemporaryFolder()) + .Execute() + .Should().ExitWith(0); + } } } From 49cc12e697ad831c6a49be1104bd27cdf765de21 Mon Sep 17 00:00:00 2001 From: mariam-abdulla Date: Thu, 22 May 2025 16:23:30 +0200 Subject: [PATCH 2/2] Add tests --- .../DotnetNewInstallTests.cs | 11 - .../DotnetNewTestTemplatesTests.cs | 259 ++++++++++++++++++ 2 files changed, 259 insertions(+), 11 deletions(-) create mode 100644 test/dotnet-new.IntegrationTests/DotnetNewTestTemplatesTests.cs diff --git a/test/dotnet-new.IntegrationTests/DotnetNewInstallTests.cs b/test/dotnet-new.IntegrationTests/DotnetNewInstallTests.cs index 5e7189a089e8..1b90e9d43929 100644 --- a/test/dotnet-new.IntegrationTests/DotnetNewInstallTests.cs +++ b/test/dotnet-new.IntegrationTests/DotnetNewInstallTests.cs @@ -603,16 +603,5 @@ public void CanShowWarning_WhenHostDataIsIncorrect() .And.HaveStdOutContaining($"Success: {invalidTemplatePath} installed the following templates:") .And.HaveStdOutContaining("TestAssets.Invalid.InvalidHostData"); } - - [Fact] - public void CanShowWarning_WhenHostDataIsIncorrect1() - { - string templatePackagePath = Path.Combine(RepoTemplatePackages, "Microsoft.DotNet.Common.ProjectTemplates.10.0", "content"); - new DotnetNewCommand(_log, "install", templatePackagePath) - .WithCustomHive(CreateTemporaryFolder(folderName: "Home")) - .WithWorkingDirectory(CreateTemporaryFolder()) - .Execute() - .Should().ExitWith(0); - } } } diff --git a/test/dotnet-new.IntegrationTests/DotnetNewTestTemplatesTests.cs b/test/dotnet-new.IntegrationTests/DotnetNewTestTemplatesTests.cs new file mode 100644 index 000000000000..36ef06516a93 --- /dev/null +++ b/test/dotnet-new.IntegrationTests/DotnetNewTestTemplatesTests.cs @@ -0,0 +1,259 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Immutable; + +namespace Microsoft.DotNet.Cli.New.IntegrationTests +{ + public class DotnetNewTestTemplatesTests : BaseIntegrationTest + { + private readonly ITestOutputHelper _log; + + private static readonly ImmutableArray SupportedTargetFrameworks = + [ + "net10.0", + ]; + + private static readonly (string ProjectTemplateName, string ItemTemplateName, string[] Languages, bool SupportsTestingPlatform)[] AvailableItemTemplates = + [ + ("nunit", "nunit-test", Languages.All, false), + ("mstest", "mstest-class", Languages.All, false), + ]; + + private static readonly (string ProjectTemplateName, string[] Languages, bool RunDotnetTest, bool SupportsTestingPlatform)[] AvailableProjectTemplates = + [ + ("nunit", Languages.All, true, false), + ("xunit", Languages.All, true, false), + ("nunit-playwright", new[] { Languages.CSharp }, false, false), + ]; + + public DotnetNewTestTemplatesTests(ITestOutputHelper log) : base(log) + { + _log = log; + } + + public static class Languages + { + public const string CSharp = "c#"; + public const string FSharp = "f#"; + public const string VisualBasic = "vb"; + public static readonly string[] All = + [CSharp, FSharp, VisualBasic]; + } + + private class NullTestOutputHelper : ITestOutputHelper + { + public void WriteLine(string message) { } + + public void WriteLine(string format, params object[] args) { } + } + + static DotnetNewTestTemplatesTests() + { + string templatePackagePath = Path.Combine( + RepoTemplatePackages, + "Microsoft.DotNet.Common.ProjectTemplates.10.0", + "content"); + + var dummyLog = new NullTestOutputHelper(); + + new DotnetNewCommand(dummyLog, "uninstall", templatePackagePath) + .WithCustomHive(CreateTemporaryFolder(folderName: "Home")) + .WithWorkingDirectory(CreateTemporaryFolder()) + .Execute(); + + new DotnetNewCommand(dummyLog, "install", templatePackagePath) + .WithCustomHive(CreateTemporaryFolder(folderName: "Home")) + .WithWorkingDirectory(CreateTemporaryFolder()) + .Execute() + .Should().ExitWith(0); + } + + [Theory] + [MemberData(nameof(GetTemplateItemsToTest))] + public void ItemTemplate_CanBeInstalledAndTestArePassing(string targetFramework, string projectTemplate, string itemTemplate, string language) + { + string testProjectName = GenerateTestProjectName(); + string outputDirectory = CreateTemporaryFolder(folderName: "Home"); + string workingDirectory = CreateTemporaryFolder(); + + // Create new test project: dotnet new -n -f -lang + string args = $"{projectTemplate} -n {testProjectName} -f {targetFramework} -lang {language} -o {outputDirectory}"; + new DotnetNewCommand(_log, args) + .WithCustomHive(outputDirectory).WithRawArguments() + .WithWorkingDirectory(workingDirectory) + .Execute() + .Should().ExitWith(0); + + var itemName = "test"; + + // Add test item to test project: dotnet new -n -lang -o + new DotnetNewCommand(_log, $"{itemTemplate} -n {itemName} -lang {language} -o {outputDirectory}") + .WithCustomHive(outputDirectory).WithRawArguments() + .WithWorkingDirectory(workingDirectory) + .Execute() + .Should().ExitWith(0); + + if (language == Languages.FSharp) + { + // f# projects don't include all files by default, so the file is created + // but the project ignores it until you manually add it into the project + // in the right order + AddItemToFsproj(itemName, outputDirectory, testProjectName); + } + + var result = new DotnetTestCommand(_log, false) + .WithWorkingDirectory(outputDirectory) + .Execute(outputDirectory); + + result.Should().ExitWith(0); + + result.StdOut.Should().Contain("Passed!"); + result.StdOut.Should().MatchRegex(@"Passed:\s*2"); + + Directory.Delete(outputDirectory, true); + Directory.Delete(workingDirectory, true); + } + + [Theory] + [MemberData(nameof(GetTemplateProjectsToTest))] + public void ProjectTemplate_CanBeInstalledAndTestsArePassing(string targetFramework, string projectTemplate, string language, bool runDotnetTest) + { + string testProjectName = GenerateTestProjectName(); + string outputDirectory = CreateTemporaryFolder(folderName: "Home"); + string workingDirectory = CreateTemporaryFolder(); + + // Create new test project: dotnet new -n -f -lang + string args = $"{projectTemplate} -n {testProjectName} -f {targetFramework} -lang {language} -o {outputDirectory}"; + new DotnetNewCommand(_log, args) + .WithCustomHive(outputDirectory).WithRawArguments() + .WithWorkingDirectory(workingDirectory) + .Execute() + .Should().ExitWith(0); + + if (runDotnetTest) + { + var result = new DotnetTestCommand(_log, false) + .WithWorkingDirectory(outputDirectory) + .Execute(outputDirectory); + + result.Should().ExitWith(0); + + result.StdOut.Should().Contain("Passed!"); + result.StdOut.Should().MatchRegex(@"Passed:\s*1"); + } + + Directory.Delete(outputDirectory, true); + Directory.Delete(workingDirectory, true); + } + + [Theory] + [MemberData(nameof(GetMSTestAndPlaywrightCoverageAndRunnerCombinations))] + public void MSTestAndPlaywrightProjectTemplate_WithCoverageToolAndTestRunner_CanBeInstalledAndTestsArePassing( + string projectTemplate, + string targetFramework, + string language, + string coverageTool, + string testRunner, + bool runDotnetTest) + { + string testProjectName = GenerateTestProjectName(); + string outputDirectory = CreateTemporaryFolder(folderName: "Home"); + string workingDirectory = CreateTemporaryFolder(); + + // Create new test project: dotnet new -n -f -lang --coverage-tool --test-runner + string args = $"{projectTemplate} -n {testProjectName} -f {targetFramework} -lang {language} -o {outputDirectory} --coverage-tool {coverageTool} --test-runner {testRunner}"; + new DotnetNewCommand(_log, args) + .WithCustomHive(outputDirectory).WithRawArguments() + .WithWorkingDirectory(workingDirectory) + .Execute() + .Should().ExitWith(0); + + if (runDotnetTest) + { + var result = new DotnetTestCommand(_log, false) + .WithWorkingDirectory(outputDirectory) + .Execute(outputDirectory); + + result.Should().ExitWith(0); + + result.StdOut.Should().Contain("Passed!"); + result.StdOut.Should().MatchRegex(@"Passed:\s*1"); + } + + Directory.Delete(outputDirectory, true); + Directory.Delete(workingDirectory, true); + } + + private void AddItemToFsproj(string itemName, string outputDirectory, string projectName) + { + var fsproj = Path.Combine(outputDirectory, $"{projectName}.fsproj"); + var lines = File.ReadAllLines(fsproj).ToList(); + + lines.Insert(lines.IndexOf(" ") + 1, $@" "); + File.WriteAllLines(fsproj, lines); + } + + private static string GenerateTestProjectName() + { + // Avoiding VB errors because root namespace must not start with number or contain dashes + return "Test_" + Guid.NewGuid().ToString("N"); + } + + public static IEnumerable GetTemplateItemsToTest() + { + foreach (var targetFramework in SupportedTargetFrameworks) + { + foreach (var (projectTemplate, itemTemplate, languages, supportsTestingPlatform) in AvailableItemTemplates) + { + foreach (var language in languages) + { + yield return new object[] { targetFramework, projectTemplate, itemTemplate, language }; + } + } + } + } + + public static IEnumerable GetTemplateProjectsToTest() + { + foreach (var targetFramework in SupportedTargetFrameworks) + { + foreach (var (projectTemplate, languages, runDotnetTest, supportsTestingPlatform) in AvailableProjectTemplates) + { + foreach (var language in languages) + { + yield return new object[] { targetFramework, projectTemplate, language, runDotnetTest }; + } + } + } + } + + public static IEnumerable GetMSTestAndPlaywrightCoverageAndRunnerCombinations() + { + var coverageTools = new[] { "Microsoft.CodeCoverage", "coverlet" }; + var testRunners = new[] { "VSTest", "Microsoft.Testing.Platform" }; + foreach (var targetFramework in SupportedTargetFrameworks) + { + // mstest: all languages, runDotnetTest = true + foreach (var language in Languages.All) + { + foreach (var coverageTool in coverageTools) + { + foreach (var testRunner in testRunners) + { + yield return new object[] { "mstest", targetFramework, language, coverageTool, testRunner, true }; + } + } + } + // mstest-playwright: only c#, runDotnetTest = false + foreach (var coverageTool in coverageTools) + { + foreach (var testRunner in testRunners) + { + yield return new object[] { "mstest-playwright", targetFramework, Languages.CSharp, coverageTool, testRunner, false }; + } + } + } + } + } +}