Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for globbing filter #46495

Merged
merged 3 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public bool RunWithTestModulesFilter(ParseResult parseResult)

private static IEnumerable<string> GetMatchedModulePaths(string testModules, string rootDirectory)
{
var testModulePatterns = testModules.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
var testModulePatterns = testModules.Split([';'], StringSplitOptions.RemoveEmptyEntries);

Matcher matcher = new();
matcher.AddIncludePatterns(testModulePatterns);
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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")
Expand All @@ -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();
Expand All @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
1 change: 0 additions & 1 deletion test/dotnet.Tests/dotnet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
<Compile Include="..\Microsoft.DotNet.Configurer.UnitTests\**\*.cs" LinkBase="Configurer" />
<Compile Include="..\Microsoft.DotNet.ShellShim.Tests\**\*.cs" LinkBase="ShellShim" />
<Compile Remove="..\dotnet-test.Tests\GivenDotnetTestBuildsAndRunsHelp.cs" />
<Compile Remove="..\dotnet-test.Tests\GivenDotnetTestBuildsAndRunsTestBasedOnGlobbingFilter.cs" />

<None Include="..\Microsoft.DotNet.ShellShim.Tests\WpfBinaryTestAsssets\testwpf.dll" LinkBase="WpfBinaryTestAsssets">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
Loading