Skip to content

Fix global properties not propagating in new dotnet test for MTP #47464

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

Merged
merged 1 commit into from
Mar 12, 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
37 changes: 21 additions & 16 deletions src/Cli/dotnet/commands/dotnet-test/MSBuildUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static (IEnumerable<TestModule> Projects, bool IsBuiltOrRestored) GetProj
{
bool isBuiltOrRestored = BuildOrRestoreProjectOrSolution(projectFilePath, buildOptions);

IEnumerable<TestModule> projects = SolutionAndProjectUtility.GetProjectProperties(projectFilePath, GetGlobalProperties(buildOptions.BuildProperties), new ProjectCollection());
IEnumerable<TestModule> projects = SolutionAndProjectUtility.GetProjectProperties(projectFilePath, GetGlobalProperties(buildOptions), new ProjectCollection());

isBuiltOrRestored |= projects.Any();

Expand All @@ -45,15 +45,12 @@ public static (IEnumerable<TestModule> Projects, bool IsBuiltOrRestored) GetProj

public static BuildOptions GetBuildOptions(ParseResult parseResult, int degreeOfParallelism)
{
IEnumerable<string> propertyTokens = GetPropertyTokens(parseResult.UnmatchedTokens);
IEnumerable<string> binaryLoggerTokens = GetBinaryLoggerTokens(parseResult.UnmatchedTokens);

var msbuildArgs = parseResult.OptionValuesToBeForwarded(TestCommandParser.GetCommand())
.Concat(propertyTokens)
.Concat(binaryLoggerTokens);

List<string> unmatchedTokens = [.. parseResult.UnmatchedTokens];
unmatchedTokens.RemoveAll(arg => propertyTokens.Contains(arg));
unmatchedTokens.RemoveAll(arg => binaryLoggerTokens.Contains(arg));

PathOptions pathOptions = new(parseResult.GetValue(
Expand All @@ -73,6 +70,7 @@ public static BuildOptions GetBuildOptions(ParseResult parseResult, int degreeOf
parseResult.GetValue(TestingPlatformOptions.NoBuildOption),
parseResult.HasOption(CommonOptions.VerbosityOption) ? parseResult.GetValue(CommonOptions.VerbosityOption) : null,
degreeOfParallelism,
parseResult.GetValue(CommonOptions.PropertiesOption),
unmatchedTokens,
msbuildArgs);
}
Expand All @@ -92,16 +90,7 @@ private static string ResolveRuntimeIdentifier(ParseResult parseResult)
return CommonOptions.ResolveRidShorthandOptionsToRuntimeIdentifier(parseResult.GetValue(CommonOptions.OperatingSystemOption), parseResult.GetValue(CommonOptions.ArchitectureOption));
}

public static IEnumerable<string> GetPropertyTokens(IEnumerable<string> unmatchedTokens)
{
return unmatchedTokens.Where(token =>
token.StartsWith("--property:", StringComparison.OrdinalIgnoreCase) ||
token.StartsWith("/property:", StringComparison.OrdinalIgnoreCase) ||
token.StartsWith("-p:", StringComparison.OrdinalIgnoreCase) ||
token.StartsWith("/p:", StringComparison.OrdinalIgnoreCase));
}

public static IEnumerable<string> GetBinaryLoggerTokens(IEnumerable<string> args)
private static IEnumerable<string> GetBinaryLoggerTokens(IEnumerable<string> args)
{
return args.Where(arg =>
arg.StartsWith("/bl:", StringComparison.OrdinalIgnoreCase) || arg.Equals("/bl", StringComparison.OrdinalIgnoreCase) ||
Expand Down Expand Up @@ -135,7 +124,7 @@ private static ConcurrentBag<TestModule> GetProjectsProperties(ProjectCollection
new ParallelOptions { MaxDegreeOfParallelism = buildOptions.DegreeOfParallelism },
(project) =>
{
IEnumerable<TestModule> projectsMetadata = SolutionAndProjectUtility.GetProjectProperties(project, GetGlobalProperties(buildOptions.BuildProperties), projectCollection);
IEnumerable<TestModule> projectsMetadata = SolutionAndProjectUtility.GetProjectProperties(project, GetGlobalProperties(buildOptions), projectCollection);
foreach (var projectMetadata in projectsMetadata)
{
allProjects.Add(projectMetadata);
Expand All @@ -145,9 +134,25 @@ private static ConcurrentBag<TestModule> GetProjectsProperties(ProjectCollection
return allProjects;
}

private static Dictionary<string, string> GetGlobalProperties(BuildProperties buildProperties)
private static Dictionary<string, string> GetGlobalProperties(BuildOptions buildOptions)
{
var globalProperties = new Dictionary<string, string>();
var buildProperties = buildOptions.BuildProperties;

foreach (var property in buildOptions.UserSpecifiedProperties)
{
foreach (var (key, value) in MSBuildPropertyParser.ParseProperties(property))
{
if (globalProperties.TryGetValue(key, out var existingValues))
{
globalProperties[key] = $"{existingValues};{value}";
}
else
{
globalProperties[key] = value;
}
}
}

if (!string.IsNullOrEmpty(buildProperties.Configuration))
{
Expand Down
2 changes: 1 addition & 1 deletion src/Cli/dotnet/commands/dotnet-test/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ internal record PathOptions(string ProjectPath, string SolutionPath, string Dire

internal record BuildProperties(string Configuration, string RuntimeIdentifier, string TargetFramework);

internal record BuildOptions(PathOptions PathOptions, BuildProperties BuildProperties, bool HasNoRestore, bool HasNoBuild, VerbosityOptions? Verbosity, int DegreeOfParallelism, List<string> UnmatchedTokens, IEnumerable<string> MSBuildArgs);
internal record BuildOptions(PathOptions PathOptions, BuildProperties BuildProperties, bool HasNoRestore, bool HasNoBuild, VerbosityOptions? Verbosity, int DegreeOfParallelism, string[] UserSpecifiedProperties, List<string> UnmatchedTokens, IEnumerable<string> MSBuildArgs);
}
1 change: 1 addition & 0 deletions src/Cli/dotnet/commands/dotnet-test/TestCommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ private static CliCommand GetTestingPlatformCliCommand()
command.Options.Add(TestingPlatformOptions.TestModulesRootDirectoryOption);
command.Options.Add(TestingPlatformOptions.MaxParallelTestModulesOption);
command.Options.Add(CommonOptions.ArchitectureOption);
command.Options.Add(CommonOptions.PropertiesOption);
command.Options.Add(TestingPlatformOptions.ConfigurationOption);
command.Options.Add(TestingPlatformOptions.FrameworkOption);
command.Options.Add(CommonOptions.OperatingSystemOption);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), testAsset.props))\testAsset.props" />

<PropertyGroup>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Is it normal to have tabs?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should consider ensuring formatting is aligned with .editorconfig which seems to be using two spaces for csproj. cc @mariam-abdulla

<TargetFramework>$(CurrentTargetFramework)</TargetFramework>
<OutputType>Exe</OutputType>

<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<GenerateProgramFile>false</GenerateProgramFile>
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
<IsTestingPlatformApplication Condition="'$(PROPERTY_TO_ENABLE_MTP)' != ''">true</IsTestingPlatformApplication>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Testing.Platform" Version="$(MicrosoftTestingPlatformVersion)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Microsoft.Testing.Platform.Builder;
using Microsoft.Testing.Platform.Capabilities.TestFramework;
using Microsoft.Testing.Platform.Extensions.Messages;
using Microsoft.Testing.Platform.Extensions.TestFramework;

var testApplicationBuilder = await TestApplication.CreateBuilderAsync(args);

testApplicationBuilder.RegisterTestFramework(_ => new TestFrameworkCapabilities(), (_, __) => new DummyTestAdapter());

using var testApplication = await testApplicationBuilder.BuildAsync();
return await testApplication.RunAsync();

public class DummyTestAdapter : ITestFramework, IDataProducer
{
public string Uid => nameof(DummyTestAdapter);

public string Version => "2.0.0";

public string DisplayName => nameof(DummyTestAdapter);

public string Description => nameof(DummyTestAdapter);

public Task<bool> IsEnabledAsync() => Task.FromResult(true);

public Type[] DataTypesProduced => new[] {
typeof(TestNodeUpdateMessage)
};

public Task<CreateTestSessionResult> CreateTestSessionAsync(CreateTestSessionContext context)
=> Task.FromResult(new CreateTestSessionResult() { IsSuccess = true });

public Task<CloseTestSessionResult> CloseTestSessionAsync(CloseTestSessionContext context)
=> Task.FromResult(new CloseTestSessionResult() { IsSuccess = true });

public async Task ExecuteRequestAsync(ExecuteRequestContext context)
{
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid, new TestNode()
{
Uid = "Test1",
DisplayName = "Test1",
Properties = new PropertyBag(new PassedTestNodeStateProperty("OK")),
}));

context.Complete();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.Testing.Platform.Builder;
using Microsoft.Testing.Platform.Capabilities.TestFramework;
using Microsoft.Testing.Platform.Extensions.Messages;
using Microsoft.Testing.Platform.Extensions.TestFramework;

ITestApplicationBuilder testApplicationBuilder = await TestApplication.CreateBuilderAsync(args);
testApplicationBuilder.RegisterTestFramework(_ => new TestFrameworkCapabilities(), (_, __) => new DummyTestAdapter());

ITestApplication testApplication = await testApplicationBuilder.BuildAsync();
return await testApplication.RunAsync();

public class DummyTestAdapter : ITestFramework, IDataProducer
{
public string Uid => nameof(DummyTestAdapter);

public string Version => "2.0.0";

public string DisplayName => nameof(DummyTestAdapter);

public string Description => nameof(DummyTestAdapter);

public Task<bool> IsEnabledAsync() => Task.FromResult(true);

public Type[] DataTypesProduced => new[] { typeof(TestNodeUpdateMessage) };

public Task<CreateTestSessionResult> CreateTestSessionAsync(CreateTestSessionContext context)
=> Task.FromResult(new CreateTestSessionResult() { IsSuccess = true });

public Task<CloseTestSessionResult> CloseTestSessionAsync(CloseTestSessionContext context)
=> Task.FromResult(new CloseTestSessionResult() { IsSuccess = true });

public async Task ExecuteRequestAsync(ExecuteRequestContext context)
{
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid, new TestNode()
{
Uid = "Test0",
DisplayName = "Test0",
Properties = new PropertyBag(new PassedTestNodeStateProperty("OK")),
}));

context.Complete();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), testAsset.props))\testAsset.props" />

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>$(CurrentTargetFramework)</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateProgramFile>false</GenerateProgramFile>
<LangVersion>latest</LangVersion>
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
<IsTestingPlatformApplication Condition="'$(PROPERTY_TO_ENABLE_MTP)' != ''">true</IsTestingPlatformApplication>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Testing.Platform" Version="$(MicrosoftTestingPlatformVersion)" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.13.35505.181
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestProject", "TestProject\TestProject.csproj", "{D2E321F2-3513-99DE-C37E-6D48D15F404D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OtherTestProject", "OtherTestProject/OtherTestProject.csproj", "{6171FC1F-E2F2-4F66-A8DE-EC4765081661}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D2E321F2-3513-99DE-C37E-6D48D15F404D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D2E321F2-3513-99DE-C37E-6D48D15F404D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D2E321F2-3513-99DE-C37E-6D48D15F404D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D2E321F2-3513-99DE-C37E-6D48D15F404D}.Release|Any CPU.Build.0 = Release|Any CPU
{6171FC1F-E2F2-4F66-A8DE-EC4765081661}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6171FC1F-E2F2-4F66-A8DE-EC4765081661}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6171FC1F-E2F2-4F66-A8DE-EC4765081661}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6171FC1F-E2F2-4F66-A8DE-EC4765081661}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3D7914D1-7D03-4FFB-8D0F-7FFA6B6BA6A0}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[dotnet.test:runner]
name= "Microsoft.Testing.Platform"
29 changes: 29 additions & 0 deletions test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,5 +256,34 @@ public void RunOnProjectWithClassLibrary_ShouldReturnExitCodeSuccess(string conf

result.ExitCode.Should().Be(ExitCode.Success);
}

[InlineData(TestingConstants.Debug)]
[InlineData(TestingConstants.Release)]
[Theory]
public void RunningWithGlobalPropertyShouldProperlyPropagate(string configuration)
{
TestAsset testInstance = _testAssetsManager.CopyTestAsset("TestProjectWithConditionOnGlobalProperty", Guid.NewGuid().ToString())
.WithSource();
testInstance.WithTargetFramework($"{DotnetVersionHelper.GetPreviousDotnetVersion()}", "TestProject");

CommandResult result = new DotnetTestCommand(Log, disableNewOutput: false)
.WithWorkingDirectory(testInstance.Path)
.WithEnableTestingPlatform()
.Execute(
TestingPlatformOptions.ConfigurationOption.Name, configuration,
CommonOptions.PropertiesOption.Name, "PROPERTY_TO_ENABLE_MTP=1");

if (!TestContext.IsLocalized())
{
result.StdOut
.Should().Contain("Test run summary: Passed!")
.And.Contain("total: 2")
.And.Contain("succeeded: 2")
.And.Contain("failed: 0")
.And.Contain("skipped: 0");
}

result.ExitCode.Should().Be(ExitCode.Success);
}
}
}