|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Collections.Immutable; |
| 5 | + |
| 6 | +namespace Microsoft.DotNet.Cli.New.IntegrationTests |
| 7 | +{ |
| 8 | + public class DotnetNewTestTemplatesTests : BaseIntegrationTest |
| 9 | + { |
| 10 | + private readonly ITestOutputHelper _log; |
| 11 | + |
| 12 | + private static readonly ImmutableArray<string> SupportedTargetFrameworks = |
| 13 | + [ |
| 14 | + "net10.0", |
| 15 | + ]; |
| 16 | + |
| 17 | + private static readonly (string ProjectTemplateName, string ItemTemplateName, string[] Languages, bool SupportsTestingPlatform)[] AvailableItemTemplates = |
| 18 | + [ |
| 19 | + ("nunit", "nunit-test", Languages.All, false), |
| 20 | + ("mstest", "mstest-class", Languages.All, false), |
| 21 | + ]; |
| 22 | + |
| 23 | + private static readonly (string ProjectTemplateName, string[] Languages, bool RunDotnetTest, bool SupportsTestingPlatform)[] AvailableProjectTemplates = |
| 24 | + [ |
| 25 | + ("nunit", Languages.All, true, false), |
| 26 | + ("xunit", Languages.All, true, false), |
| 27 | + ("nunit-playwright", new[] { Languages.CSharp }, false, false), |
| 28 | + ]; |
| 29 | + |
| 30 | + public DotnetNewTestTemplatesTests(ITestOutputHelper log) : base(log) |
| 31 | + { |
| 32 | + _log = log; |
| 33 | + } |
| 34 | + |
| 35 | + public static class Languages |
| 36 | + { |
| 37 | + public const string CSharp = "c#"; |
| 38 | + public const string FSharp = "f#"; |
| 39 | + public const string VisualBasic = "vb"; |
| 40 | + public static readonly string[] All = |
| 41 | + [CSharp, FSharp, VisualBasic]; |
| 42 | + } |
| 43 | + |
| 44 | + private class NullTestOutputHelper : ITestOutputHelper |
| 45 | + { |
| 46 | + public void WriteLine(string message) { } |
| 47 | + |
| 48 | + public void WriteLine(string format, params object[] args) { } |
| 49 | + } |
| 50 | + |
| 51 | + static DotnetNewTestTemplatesTests() |
| 52 | + { |
| 53 | + string templatePackagePath = Path.Combine( |
| 54 | + RepoTemplatePackages, |
| 55 | + "Microsoft.DotNet.Common.ProjectTemplates.10.0", |
| 56 | + "content"); |
| 57 | + |
| 58 | + var dummyLog = new NullTestOutputHelper(); |
| 59 | + |
| 60 | + new DotnetNewCommand(dummyLog, "uninstall", templatePackagePath) |
| 61 | + .WithCustomHive(CreateTemporaryFolder(folderName: "Home")) |
| 62 | + .WithWorkingDirectory(CreateTemporaryFolder()) |
| 63 | + .Execute(); |
| 64 | + |
| 65 | + new DotnetNewCommand(dummyLog, "install", templatePackagePath) |
| 66 | + .WithCustomHive(CreateTemporaryFolder(folderName: "Home")) |
| 67 | + .WithWorkingDirectory(CreateTemporaryFolder()) |
| 68 | + .Execute() |
| 69 | + .Should().ExitWith(0); |
| 70 | + } |
| 71 | + |
| 72 | + [Theory] |
| 73 | + [MemberData(nameof(GetTemplateItemsToTest))] |
| 74 | + public void ItemTemplate_CanBeInstalledAndTestArePassing(string targetFramework, string projectTemplate, string itemTemplate, string language) |
| 75 | + { |
| 76 | + string testProjectName = GenerateTestProjectName(); |
| 77 | + string outputDirectory = CreateTemporaryFolder(folderName: "Home"); |
| 78 | + string workingDirectory = CreateTemporaryFolder(); |
| 79 | + |
| 80 | + // Create new test project: dotnet new <projectTemplate> -n <testProjectName> -f <targetFramework> -lang <language> |
| 81 | + string args = $"{projectTemplate} -n {testProjectName} -f {targetFramework} -lang {language} -o {outputDirectory}"; |
| 82 | + new DotnetNewCommand(_log, args) |
| 83 | + .WithCustomHive(outputDirectory).WithRawArguments() |
| 84 | + .WithWorkingDirectory(workingDirectory) |
| 85 | + .Execute() |
| 86 | + .Should().ExitWith(0); |
| 87 | + |
| 88 | + var itemName = "test"; |
| 89 | + |
| 90 | + // Add test item to test project: dotnet new <itemTemplate> -n <test> -lang <language> -o <testProjectName> |
| 91 | + new DotnetNewCommand(_log, $"{itemTemplate} -n {itemName} -lang {language} -o {outputDirectory}") |
| 92 | + .WithCustomHive(outputDirectory).WithRawArguments() |
| 93 | + .WithWorkingDirectory(workingDirectory) |
| 94 | + .Execute() |
| 95 | + .Should().ExitWith(0); |
| 96 | + |
| 97 | + if (language == Languages.FSharp) |
| 98 | + { |
| 99 | + // f# projects don't include all files by default, so the file is created |
| 100 | + // but the project ignores it until you manually add it into the project |
| 101 | + // in the right order |
| 102 | + AddItemToFsproj(itemName, outputDirectory, testProjectName); |
| 103 | + } |
| 104 | + |
| 105 | + var result = new DotnetTestCommand(_log, false) |
| 106 | + .WithWorkingDirectory(outputDirectory) |
| 107 | + .Execute(outputDirectory); |
| 108 | + |
| 109 | + result.Should().ExitWith(0); |
| 110 | + |
| 111 | + result.StdOut.Should().Contain("Passed!"); |
| 112 | + result.StdOut.Should().MatchRegex(@"Passed:\s*2"); |
| 113 | + |
| 114 | + Directory.Delete(outputDirectory, true); |
| 115 | + Directory.Delete(workingDirectory, true); |
| 116 | + } |
| 117 | + |
| 118 | + [Theory] |
| 119 | + [MemberData(nameof(GetTemplateProjectsToTest))] |
| 120 | + public void ProjectTemplate_CanBeInstalledAndTestsArePassing(string targetFramework, string projectTemplate, string language, bool runDotnetTest) |
| 121 | + { |
| 122 | + string testProjectName = GenerateTestProjectName(); |
| 123 | + string outputDirectory = CreateTemporaryFolder(folderName: "Home"); |
| 124 | + string workingDirectory = CreateTemporaryFolder(); |
| 125 | + |
| 126 | + // Create new test project: dotnet new <projectTemplate> -n <testProjectName> -f <targetFramework> -lang <language> |
| 127 | + string args = $"{projectTemplate} -n {testProjectName} -f {targetFramework} -lang {language} -o {outputDirectory}"; |
| 128 | + new DotnetNewCommand(_log, args) |
| 129 | + .WithCustomHive(outputDirectory).WithRawArguments() |
| 130 | + .WithWorkingDirectory(workingDirectory) |
| 131 | + .Execute() |
| 132 | + .Should().ExitWith(0); |
| 133 | + |
| 134 | + if (runDotnetTest) |
| 135 | + { |
| 136 | + var result = new DotnetTestCommand(_log, false) |
| 137 | + .WithWorkingDirectory(outputDirectory) |
| 138 | + .Execute(outputDirectory); |
| 139 | + |
| 140 | + result.Should().ExitWith(0); |
| 141 | + |
| 142 | + result.StdOut.Should().Contain("Passed!"); |
| 143 | + result.StdOut.Should().MatchRegex(@"Passed:\s*1"); |
| 144 | + } |
| 145 | + |
| 146 | + Directory.Delete(outputDirectory, true); |
| 147 | + Directory.Delete(workingDirectory, true); |
| 148 | + } |
| 149 | + |
| 150 | + [Theory] |
| 151 | + [MemberData(nameof(GetMSTestAndPlaywrightCoverageAndRunnerCombinations))] |
| 152 | + public void MSTestAndPlaywrightProjectTemplate_WithCoverageToolAndTestRunner_CanBeInstalledAndTestsArePassing( |
| 153 | + string projectTemplate, |
| 154 | + string targetFramework, |
| 155 | + string language, |
| 156 | + string coverageTool, |
| 157 | + string testRunner, |
| 158 | + bool runDotnetTest) |
| 159 | + { |
| 160 | + string testProjectName = GenerateTestProjectName(); |
| 161 | + string outputDirectory = CreateTemporaryFolder(folderName: "Home"); |
| 162 | + string workingDirectory = CreateTemporaryFolder(); |
| 163 | + |
| 164 | + // Create new test project: dotnet new <projectTemplate> -n <testProjectName> -f <targetFramework> -lang <language> --coverage-tool <coverageTool> --test-runner <testRunner> |
| 165 | + string args = $"{projectTemplate} -n {testProjectName} -f {targetFramework} -lang {language} -o {outputDirectory} --coverage-tool {coverageTool} --test-runner {testRunner}"; |
| 166 | + new DotnetNewCommand(_log, args) |
| 167 | + .WithCustomHive(outputDirectory).WithRawArguments() |
| 168 | + .WithWorkingDirectory(workingDirectory) |
| 169 | + .Execute() |
| 170 | + .Should().ExitWith(0); |
| 171 | + |
| 172 | + if (runDotnetTest) |
| 173 | + { |
| 174 | + var result = new DotnetTestCommand(_log, false) |
| 175 | + .WithWorkingDirectory(outputDirectory) |
| 176 | + .Execute(outputDirectory); |
| 177 | + |
| 178 | + result.Should().ExitWith(0); |
| 179 | + |
| 180 | + result.StdOut.Should().Contain("Passed!"); |
| 181 | + result.StdOut.Should().MatchRegex(@"Passed:\s*1"); |
| 182 | + } |
| 183 | + |
| 184 | + Directory.Delete(outputDirectory, true); |
| 185 | + Directory.Delete(workingDirectory, true); |
| 186 | + } |
| 187 | + |
| 188 | + private void AddItemToFsproj(string itemName, string outputDirectory, string projectName) |
| 189 | + { |
| 190 | + var fsproj = Path.Combine(outputDirectory, $"{projectName}.fsproj"); |
| 191 | + var lines = File.ReadAllLines(fsproj).ToList(); |
| 192 | + |
| 193 | + lines.Insert(lines.IndexOf(" <ItemGroup>") + 1, $@" <Compile Include=""{itemName}.fs""/>"); |
| 194 | + File.WriteAllLines(fsproj, lines); |
| 195 | + } |
| 196 | + |
| 197 | + private static string GenerateTestProjectName() |
| 198 | + { |
| 199 | + // Avoiding VB errors because root namespace must not start with number or contain dashes |
| 200 | + return "Test_" + Guid.NewGuid().ToString("N"); |
| 201 | + } |
| 202 | + |
| 203 | + public static IEnumerable<object[]> GetTemplateItemsToTest() |
| 204 | + { |
| 205 | + foreach (var targetFramework in SupportedTargetFrameworks) |
| 206 | + { |
| 207 | + foreach (var (projectTemplate, itemTemplate, languages, supportsTestingPlatform) in AvailableItemTemplates) |
| 208 | + { |
| 209 | + foreach (var language in languages) |
| 210 | + { |
| 211 | + yield return new object[] { targetFramework, projectTemplate, itemTemplate, language }; |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + public static IEnumerable<object[]> GetTemplateProjectsToTest() |
| 218 | + { |
| 219 | + foreach (var targetFramework in SupportedTargetFrameworks) |
| 220 | + { |
| 221 | + foreach (var (projectTemplate, languages, runDotnetTest, supportsTestingPlatform) in AvailableProjectTemplates) |
| 222 | + { |
| 223 | + foreach (var language in languages) |
| 224 | + { |
| 225 | + yield return new object[] { targetFramework, projectTemplate, language, runDotnetTest }; |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + public static IEnumerable<object[]> GetMSTestAndPlaywrightCoverageAndRunnerCombinations() |
| 232 | + { |
| 233 | + var coverageTools = new[] { "Microsoft.CodeCoverage", "coverlet" }; |
| 234 | + var testRunners = new[] { "VSTest", "Microsoft.Testing.Platform" }; |
| 235 | + foreach (var targetFramework in SupportedTargetFrameworks) |
| 236 | + { |
| 237 | + // mstest: all languages, runDotnetTest = true |
| 238 | + foreach (var language in Languages.All) |
| 239 | + { |
| 240 | + foreach (var coverageTool in coverageTools) |
| 241 | + { |
| 242 | + foreach (var testRunner in testRunners) |
| 243 | + { |
| 244 | + yield return new object[] { "mstest", targetFramework, language, coverageTool, testRunner, true }; |
| 245 | + } |
| 246 | + } |
| 247 | + } |
| 248 | + // mstest-playwright: only c#, runDotnetTest = false |
| 249 | + foreach (var coverageTool in coverageTools) |
| 250 | + { |
| 251 | + foreach (var testRunner in testRunners) |
| 252 | + { |
| 253 | + yield return new object[] { "mstest-playwright", targetFramework, Languages.CSharp, coverageTool, testRunner, false }; |
| 254 | + } |
| 255 | + } |
| 256 | + } |
| 257 | + } |
| 258 | + } |
| 259 | +} |
0 commit comments