diff --git a/src/Cli/dotnet/commands/dotnet-test/TestModulesFilterHandler.cs b/src/Cli/dotnet/commands/dotnet-test/TestModulesFilterHandler.cs index 80179fbf5732..5bcc2a9a6510 100644 --- a/src/Cli/dotnet/commands/dotnet-test/TestModulesFilterHandler.cs +++ b/src/Cli/dotnet/commands/dotnet-test/TestModulesFilterHandler.cs @@ -60,7 +60,7 @@ public bool RunWithTestModulesFilter(ParseResult parseResult) private static IEnumerable GetMatchedModulePaths(string testModules, string rootDirectory) { - var testModulePatterns = testModules.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); + var testModulePatterns = testModules.Split([';'], StringSplitOptions.RemoveEmptyEntries); Matcher matcher = new(); matcher.AddIncludePatterns(testModulePatterns); diff --git a/test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTestBasedOnGlobbingFilter.cs b/test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTestBasedOnGlobbingFilter.cs index 337b423afc3a..6a283f0df8f2 100644 --- a/test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTestBasedOnGlobbingFilter.cs +++ b/test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTestBasedOnGlobbingFilter.cs @@ -1,24 +1,18 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Text.RegularExpressions; -using dotnet.Tests; using CommandResult = Microsoft.DotNet.Cli.Utils.CommandResult; namespace Microsoft.DotNet.Cli.Test.Tests { public class GivenDotnetTestBuildsAndRunsTestBasedOnGlobbingFilter : SdkTest { - private const string TestApplicationArgsPattern = @".*(Test application arguments).*"; - public GivenDotnetTestBuildsAndRunsTestBasedOnGlobbingFilter(ITestOutputHelper log) : base(log) { } - [InlineData(Constants.Debug)] - [InlineData(Constants.Release)] - [Theory] - public void RunTestProjectWithFilterOfDll_ShouldReturnZeroAsExitCode(string configuration) + [Fact] + public void RunTestProjectWithFilterOfDll_ShouldReturnZeroAsExitCode() { TestAsset testInstance = _testAssetsManager.CopyTestAsset("TestProjectWithTests", Guid.NewGuid().ToString()) .WithSource(); @@ -27,20 +21,22 @@ public void RunTestProjectWithFilterOfDll_ShouldReturnZeroAsExitCode(string conf .Execute() .Should().Pass(); - var binDirectory = new FileInfo($"{testInstance.Path}/bin").Directory; + var binDirectory = new FileInfo($"{testInstance.Path}{Path.DirectorySeparatorChar}bin").Directory; var binDirectoryLastWriteTime = binDirectory?.LastWriteTime; CommandResult result = new DotnetTestCommand(Log, disableNewOutput: false) .WithWorkingDirectory(testInstance.Path) .WithEnableTestingPlatform() - .Execute(TestingPlatformOptions.TestModulesFilterOption.Name, "**/bin/**/Debug/net8.0/TestProject.dll", - TestingPlatformOptions.ConfigurationOption.Name, configuration); + .Execute(TestingPlatformOptions.TestModulesFilterOption.Name, $"**/bin/**/Debug/{ToolsetInfo.CurrentTargetFramework}/TestProject.dll".Replace('/', Path.DirectorySeparatorChar)); // Assert that the bin folder hasn't been modified Assert.Equal(binDirectoryLastWriteTime, binDirectory?.LastWriteTime); + if (!TestContext.IsLocalized()) { + Assert.Matches(RegexPatternHelper.GenerateProjectRegexPattern("TestProject", TestingConstants.Passed, true, TestingConstants.Debug), result.StdOut); + result.StdOut .Should().Contain("Test run summary: Passed!") .And.Contain("total: 2") @@ -52,10 +48,52 @@ public void RunTestProjectWithFilterOfDll_ShouldReturnZeroAsExitCode(string conf result.ExitCode.Should().Be(ExitCodes.Success); } - [InlineData(Constants.Debug)] - [InlineData(Constants.Release)] - [Theory] - public void RunTestProjectWithFilterOfDllWithRootDirectory_ShouldReturnZeroAsExitCode(string configuration) + [Fact] + public void RunTestProjectsWithFilterOfDll_ShouldReturnOneAsExitCode() + { + TestAsset testInstance = _testAssetsManager.CopyTestAsset("MultiTestProjectSolutionWithTests", Guid.NewGuid().ToString()) + .WithSource(); + + new BuildCommand(testInstance, "TestProject") + .Execute() + .Should().Pass(); + + new BuildCommand(testInstance, "OtherTestProject") + .Execute() + .Should().Pass(); + + var binDirectory = new FileInfo($"{testInstance.Path}{Path.DirectorySeparatorChar}bin").Directory; + var binDirectoryLastWriteTime = binDirectory?.LastWriteTime; + + string filterExpression = $"**/bin/**/Debug/{ToolsetInfo.CurrentTargetFramework}/*TestProject.dll".Replace('/', Path.DirectorySeparatorChar); + + CommandResult result = new DotnetTestCommand(Log, disableNewOutput: false) + .WithWorkingDirectory(testInstance.Path) + .WithEnableTestingPlatform() + .Execute(TestingPlatformOptions.TestModulesFilterOption.Name, filterExpression); + + // Assert that the bin folder hasn't been modified + Assert.Equal(binDirectoryLastWriteTime, binDirectory?.LastWriteTime); + + if (!TestContext.IsLocalized()) + { + Assert.Matches(RegexPatternHelper.GenerateProjectRegexPattern("TestProject", TestingConstants.Failed, true, TestingConstants.Debug), result.StdOut); + Assert.Matches(RegexPatternHelper.GenerateProjectRegexPattern("OtherTestProject", TestingConstants.Passed, true, TestingConstants.Debug), result.StdOut); + + result.StdOut + .Should().Contain("Test run summary: Failed!") + .And.Contain("total: 5") + .And.Contain("succeeded: 2") + .And.Contain("failed: 1") + .And.Contain("skipped: 2"); + } + + result.ExitCode.Should().Be(ExitCodes.GenericFailure); + } + + + [Fact] + public void RunTestProjectWithFilterOfDllWithRootDirectory_ShouldReturnZeroAsExitCode() { TestAsset testInstance = _testAssetsManager.CopyTestAsset("TestProjectWithTests", Guid.NewGuid().ToString()) .WithSource(); @@ -68,16 +106,13 @@ public void RunTestProjectWithFilterOfDllWithRootDirectory_ShouldReturnZeroAsExi .WithWorkingDirectory(testInstance.Path) .WithEnableTestingPlatform() .WithTraceOutput() - .Execute(TestingPlatformOptions.TestModulesFilterOption.Name, "**/bin/**/Debug/net8.0/TestProject.dll", - TestingPlatformOptions.TestModulesRootDirectoryOption.Name, testInstance.TestRoot, - TestingPlatformOptions.ConfigurationOption.Name, configuration); - - - var testAppArgs = Regex.Matches(result.StdOut!, TestApplicationArgsPattern); - Assert.Contains($"exec {testInstance.TestRoot}\\bin\\Debug\\net8.0\\TestProject.dll", testAppArgs.FirstOrDefault()?.Value); + .Execute(TestingPlatformOptions.TestModulesFilterOption.Name, $"**/bin/**/Debug/{ToolsetInfo.CurrentTargetFramework}/TestProject.dll".Replace('/', Path.DirectorySeparatorChar), + TestingPlatformOptions.TestModulesRootDirectoryOption.Name, testInstance.TestRoot); if (!TestContext.IsLocalized()) { + Assert.Matches(RegexPatternHelper.GenerateProjectRegexPattern("TestProject", TestingConstants.Passed, true, TestingConstants.Debug), result.StdOut); + result.StdOut .Should().Contain("Test run summary: Passed!") .And.Contain("total: 2") diff --git a/test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTests.cs b/test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTests.cs index 2aea44fc4237..ec280f05a267 100644 --- a/test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTests.cs +++ b/test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTests.cs @@ -132,9 +132,9 @@ public void RunMultipleTestProjectsWithDifferentFailures_ShouldReturnOneAsExitCo if (!TestContext.IsLocalized()) { - Assert.Matches(RegexPatternHelper.GenerateProjectRegexPattern("TestProject", "failed", true, configuration, "8"), result.StdOut); - Assert.Matches(RegexPatternHelper.GenerateProjectRegexPattern("OtherTestProject", "failed", true, configuration, "2"), result.StdOut); - Assert.Matches(RegexPatternHelper.GenerateProjectRegexPattern("AnotherTestProject", "failed", true, configuration, "9"), result.StdOut); + Assert.Matches(RegexPatternHelper.GenerateProjectRegexPattern("TestProject", TestingConstants.Failed, true, configuration, "8"), result.StdOut); + Assert.Matches(RegexPatternHelper.GenerateProjectRegexPattern("OtherTestProject", TestingConstants.Failed, true, configuration, "2"), result.StdOut); + Assert.Matches(RegexPatternHelper.GenerateProjectRegexPattern("AnotherTestProject", TestingConstants.Failed, true, configuration, "9"), result.StdOut); result.StdOut .Should().Contain("Test run summary: Failed!") diff --git a/test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTestsWithDifferentOptions.cs b/test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTestsWithDifferentOptions.cs index f15fc0fef4d3..e4927e4f13ce 100644 --- a/test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTestsWithDifferentOptions.cs +++ b/test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTestsWithDifferentOptions.cs @@ -53,8 +53,8 @@ public void RunWithSolutionPathWithFailingTests_ShouldReturnOneAsExitCode(string .Execute(TestingPlatformOptions.SolutionOption.Name, testSolutionPath, TestingPlatformOptions.ConfigurationOption.Name, configuration); - Assert.Matches(RegexPatternHelper.GenerateProjectRegexPattern("TestProject", "failed", true, configuration), result.StdOut); - Assert.Matches(RegexPatternHelper.GenerateProjectRegexPattern("OtherTestProject", "passed", true, configuration), result.StdOut); + Assert.Matches(RegexPatternHelper.GenerateProjectRegexPattern("TestProject", TestingConstants.Failed, true, configuration), result.StdOut); + Assert.Matches(RegexPatternHelper.GenerateProjectRegexPattern("OtherTestProject", TestingConstants.Passed, true, configuration), result.StdOut); result.ExitCode.Should().Be(ExitCodes.GenericFailure); } @@ -76,8 +76,8 @@ public void RunWithSolutionFilterPathWithFailingTests_ShouldReturnOneAsExitCode( TestingPlatformOptions.ConfigurationOption.Name, configuration); // Assert that only TestProject ran - Assert.Matches(RegexPatternHelper.GenerateProjectRegexPattern("TestProject", "failed", true, configuration), result.StdOut); - Assert.DoesNotMatch(RegexPatternHelper.GenerateProjectRegexPattern("OtherTestProject", "passed", true, configuration), result.StdOut); + Assert.Matches(RegexPatternHelper.GenerateProjectRegexPattern("TestProject", TestingConstants.Failed, true, configuration), result.StdOut); + Assert.DoesNotMatch(RegexPatternHelper.GenerateProjectRegexPattern("OtherTestProject", TestingConstants.Passed, true, configuration), result.StdOut); result.ExitCode.Should().Be(ExitCodes.GenericFailure); } @@ -97,8 +97,8 @@ public void RunWithSolutionFilterPathInOtherDirectory_ShouldReturnOneAsExitCode( .Execute(TestingPlatformOptions.ConfigurationOption.Name, configuration); // Assert that only TestProject ran - Assert.Matches(RegexPatternHelper.GenerateProjectRegexPattern("TestProject", "failed", true, configuration), result.StdOut); - Assert.DoesNotMatch(RegexPatternHelper.GenerateProjectRegexPattern("OtherTestProject", "passed", true, configuration), result.StdOut); + Assert.Matches(RegexPatternHelper.GenerateProjectRegexPattern("TestProject", TestingConstants.Failed, true, configuration), result.StdOut); + Assert.DoesNotMatch(RegexPatternHelper.GenerateProjectRegexPattern("OtherTestProject", TestingConstants.Passed, true, configuration), result.StdOut); result.ExitCode.Should().Be(ExitCodes.GenericFailure); } diff --git a/test/dotnet.Tests/dotnet.Tests.csproj b/test/dotnet.Tests/dotnet.Tests.csproj index e974b3aee799..4ed582f69e38 100644 --- a/test/dotnet.Tests/dotnet.Tests.csproj +++ b/test/dotnet.Tests/dotnet.Tests.csproj @@ -57,7 +57,6 @@ - PreserveNewest