Skip to content

Commit c6c6fb3

Browse files
committed
Fix global properties not propagating in new dotnet test for MTP
1 parent 4708311 commit c6c6fb3

File tree

10 files changed

+211
-4
lines changed

10 files changed

+211
-4
lines changed

src/Cli/dotnet/commands/dotnet-test/MSBuildUtility.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static (IEnumerable<TestModule> Projects, bool IsBuiltOrRestored) GetProj
3636
{
3737
bool isBuiltOrRestored = BuildOrRestoreProjectOrSolution(projectFilePath, buildOptions);
3838

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

4141
isBuiltOrRestored |= projects.Any();
4242

@@ -73,6 +73,7 @@ public static BuildOptions GetBuildOptions(ParseResult parseResult, int degreeOf
7373
parseResult.GetValue(TestingPlatformOptions.NoBuildOption),
7474
parseResult.HasOption(CommonOptions.VerbosityOption) ? parseResult.GetValue(CommonOptions.VerbosityOption) : null,
7575
degreeOfParallelism,
76+
parseResult.GetValue(CommonOptions.PropertiesOption),
7677
unmatchedTokens,
7778
msbuildArgs);
7879
}
@@ -135,7 +136,7 @@ private static ConcurrentBag<TestModule> GetProjectsProperties(ProjectCollection
135136
new ParallelOptions { MaxDegreeOfParallelism = buildOptions.DegreeOfParallelism },
136137
(project) =>
137138
{
138-
IEnumerable<TestModule> projectsMetadata = SolutionAndProjectUtility.GetProjectProperties(project, GetGlobalProperties(buildOptions.BuildProperties), projectCollection);
139+
IEnumerable<TestModule> projectsMetadata = SolutionAndProjectUtility.GetProjectProperties(project, GetGlobalProperties(buildOptions), projectCollection);
139140
foreach (var projectMetadata in projectsMetadata)
140141
{
141142
allProjects.Add(projectMetadata);
@@ -145,9 +146,25 @@ private static ConcurrentBag<TestModule> GetProjectsProperties(ProjectCollection
145146
return allProjects;
146147
}
147148

148-
private static Dictionary<string, string> GetGlobalProperties(BuildProperties buildProperties)
149+
private static Dictionary<string, string> GetGlobalProperties(BuildOptions buildOptions)
149150
{
150151
var globalProperties = new Dictionary<string, string>();
152+
var buildProperties = buildOptions.BuildProperties;
153+
154+
foreach (var property in buildOptions.UserSpecifiedProperties)
155+
{
156+
foreach (var (key, value) in MSBuildPropertyParser.ParseProperties(property))
157+
{
158+
if (globalProperties.TryGetValue(key, out var existingValues))
159+
{
160+
globalProperties[key] = $"{existingValues};{value}";
161+
}
162+
else
163+
{
164+
globalProperties[key] = value;
165+
}
166+
}
167+
}
151168

152169
if (!string.IsNullOrEmpty(buildProperties.Configuration))
153170
{

src/Cli/dotnet/commands/dotnet-test/Options.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ internal record PathOptions(string ProjectPath, string SolutionPath, string Dire
99

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

12-
internal record BuildOptions(PathOptions PathOptions, BuildProperties BuildProperties, bool HasNoRestore, bool HasNoBuild, VerbosityOptions? Verbosity, int DegreeOfParallelism, List<string> UnmatchedTokens, IEnumerable<string> MSBuildArgs);
12+
internal record BuildOptions(PathOptions PathOptions, BuildProperties BuildProperties, bool HasNoRestore, bool HasNoBuild, VerbosityOptions? Verbosity, int DegreeOfParallelism, string[] UserSpecifiedProperties, List<string> UnmatchedTokens, IEnumerable<string> MSBuildArgs);
1313
}

src/Cli/dotnet/commands/dotnet-test/TestCommandParser.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ private static CliCommand GetTestingPlatformCliCommand()
234234
command.Options.Add(TestingPlatformOptions.TestModulesRootDirectoryOption);
235235
command.Options.Add(TestingPlatformOptions.MaxParallelTestModulesOption);
236236
command.Options.Add(CommonOptions.ArchitectureOption);
237+
command.Options.Add(CommonOptions.PropertiesOption);
237238
command.Options.Add(TestingPlatformOptions.ConfigurationOption);
238239
command.Options.Add(TestingPlatformOptions.FrameworkOption);
239240
command.Options.Add(CommonOptions.OperatingSystemOption);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), testAsset.props))\testAsset.props" />
3+
4+
<PropertyGroup>
5+
<TargetFramework>$(CurrentTargetFramework)</TargetFramework>
6+
<OutputType>Exe</OutputType>
7+
8+
<ImplicitUsings>enable</ImplicitUsings>
9+
<Nullable>enable</Nullable>
10+
11+
<GenerateProgramFile>false</GenerateProgramFile>
12+
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
13+
<IsTestingPlatformApplication Condition="'$(PROPERTY_TO_ENABLE_MTP)' != ''">true</IsTestingPlatformApplication>
14+
</PropertyGroup>
15+
16+
<ItemGroup>
17+
<PackageReference Include="Microsoft.Testing.Platform" Version="$(MicrosoftTestingPlatformVersion)" />
18+
</ItemGroup>
19+
</Project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Microsoft.Testing.Platform.Builder;
2+
using Microsoft.Testing.Platform.Capabilities.TestFramework;
3+
using Microsoft.Testing.Platform.Extensions.Messages;
4+
using Microsoft.Testing.Platform.Extensions.TestFramework;
5+
6+
var testApplicationBuilder = await TestApplication.CreateBuilderAsync(args);
7+
8+
testApplicationBuilder.RegisterTestFramework(_ => new TestFrameworkCapabilities(), (_, __) => new DummyTestAdapter());
9+
10+
using var testApplication = await testApplicationBuilder.BuildAsync();
11+
return await testApplication.RunAsync();
12+
13+
public class DummyTestAdapter : ITestFramework, IDataProducer
14+
{
15+
public string Uid => nameof(DummyTestAdapter);
16+
17+
public string Version => "2.0.0";
18+
19+
public string DisplayName => nameof(DummyTestAdapter);
20+
21+
public string Description => nameof(DummyTestAdapter);
22+
23+
public Task<bool> IsEnabledAsync() => Task.FromResult(true);
24+
25+
public Type[] DataTypesProduced => new[] {
26+
typeof(TestNodeUpdateMessage)
27+
};
28+
29+
public Task<CreateTestSessionResult> CreateTestSessionAsync(CreateTestSessionContext context)
30+
=> Task.FromResult(new CreateTestSessionResult() { IsSuccess = true });
31+
32+
public Task<CloseTestSessionResult> CloseTestSessionAsync(CloseTestSessionContext context)
33+
=> Task.FromResult(new CloseTestSessionResult() { IsSuccess = true });
34+
35+
public async Task ExecuteRequestAsync(ExecuteRequestContext context)
36+
{
37+
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid, new TestNode()
38+
{
39+
Uid = "Test1",
40+
DisplayName = "Test1",
41+
Properties = new PropertyBag(new PassedTestNodeStateProperty("OK")),
42+
}));
43+
44+
context.Complete();
45+
}
46+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using Microsoft.Testing.Platform.Builder;
2+
using Microsoft.Testing.Platform.Capabilities.TestFramework;
3+
using Microsoft.Testing.Platform.Extensions.Messages;
4+
using Microsoft.Testing.Platform.Extensions.TestFramework;
5+
6+
ITestApplicationBuilder testApplicationBuilder = await TestApplication.CreateBuilderAsync(args);
7+
testApplicationBuilder.RegisterTestFramework(_ => new TestFrameworkCapabilities(), (_, __) => new DummyTestAdapter());
8+
9+
ITestApplication testApplication = await testApplicationBuilder.BuildAsync();
10+
return await testApplication.RunAsync();
11+
12+
public class DummyTestAdapter : ITestFramework, IDataProducer
13+
{
14+
public string Uid => nameof(DummyTestAdapter);
15+
16+
public string Version => "2.0.0";
17+
18+
public string DisplayName => nameof(DummyTestAdapter);
19+
20+
public string Description => nameof(DummyTestAdapter);
21+
22+
public Task<bool> IsEnabledAsync() => Task.FromResult(true);
23+
24+
public Type[] DataTypesProduced => new[] { typeof(TestNodeUpdateMessage) };
25+
26+
public Task<CreateTestSessionResult> CreateTestSessionAsync(CreateTestSessionContext context)
27+
=> Task.FromResult(new CreateTestSessionResult() { IsSuccess = true });
28+
29+
public Task<CloseTestSessionResult> CloseTestSessionAsync(CloseTestSessionContext context)
30+
=> Task.FromResult(new CloseTestSessionResult() { IsSuccess = true });
31+
32+
public async Task ExecuteRequestAsync(ExecuteRequestContext context)
33+
{
34+
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid, new TestNode()
35+
{
36+
Uid = "Test0",
37+
DisplayName = "Test0",
38+
Properties = new PropertyBag(new PassedTestNodeStateProperty("OK")),
39+
}));
40+
41+
context.Complete();
42+
}
43+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), testAsset.props))\testAsset.props" />
3+
4+
<PropertyGroup>
5+
<OutputType>Exe</OutputType>
6+
<TargetFramework>$(CurrentTargetFramework)</TargetFramework>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
<GenerateProgramFile>false</GenerateProgramFile>
10+
<LangVersion>latest</LangVersion>
11+
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
12+
<IsTestingPlatformApplication Condition="'$(PROPERTY_TO_ENABLE_MTP)' != ''">true</IsTestingPlatformApplication>
13+
</PropertyGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="Microsoft.Testing.Platform" Version="$(MicrosoftTestingPlatformVersion)" />
17+
</ItemGroup>
18+
19+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.13.35505.181
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestProject", "TestProject\TestProject.csproj", "{D2E321F2-3513-99DE-C37E-6D48D15F404D}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OtherTestProject", "OtherTestProject/OtherTestProject.csproj", "{6171FC1F-E2F2-4F66-A8DE-EC4765081661}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{D2E321F2-3513-99DE-C37E-6D48D15F404D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{D2E321F2-3513-99DE-C37E-6D48D15F404D}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{D2E321F2-3513-99DE-C37E-6D48D15F404D}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{D2E321F2-3513-99DE-C37E-6D48D15F404D}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{6171FC1F-E2F2-4F66-A8DE-EC4765081661}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{6171FC1F-E2F2-4F66-A8DE-EC4765081661}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{6171FC1F-E2F2-4F66-A8DE-EC4765081661}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{6171FC1F-E2F2-4F66-A8DE-EC4765081661}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {3D7914D1-7D03-4FFB-8D0F-7FFA6B6BA6A0}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[dotnet.test:runner]
2+
name= "Microsoft.Testing.Platform"

test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,5 +256,34 @@ public void RunOnProjectWithClassLibrary_ShouldReturnExitCodeSuccess(string conf
256256

257257
result.ExitCode.Should().Be(ExitCode.Success);
258258
}
259+
260+
[InlineData(TestingConstants.Debug)]
261+
[InlineData(TestingConstants.Release)]
262+
[Theory]
263+
public void RunningWithGlobalPropertyShouldProperlyPropagate(string configuration)
264+
{
265+
TestAsset testInstance = _testAssetsManager.CopyTestAsset("TestProjectWithConditionOnGlobalProperty", Guid.NewGuid().ToString())
266+
.WithSource();
267+
testInstance.WithTargetFramework($"{DotnetVersionHelper.GetPreviousDotnetVersion()}", "TestProject");
268+
269+
CommandResult result = new DotnetTestCommand(Log, disableNewOutput: false)
270+
.WithWorkingDirectory(testInstance.Path)
271+
.WithEnableTestingPlatform()
272+
.Execute(
273+
TestingPlatformOptions.ConfigurationOption.Name, configuration,
274+
CommonOptions.PropertiesOption.Name, "PROPERTY_TO_ENABLE_MTP=1");
275+
276+
if (!TestContext.IsLocalized())
277+
{
278+
result.StdOut
279+
.Should().Contain("Test run summary: Passed!")
280+
.And.Contain("total: 2")
281+
.And.Contain("succeeded: 2")
282+
.And.Contain("failed: 0")
283+
.And.Contain("skipped: 0");
284+
}
285+
286+
result.ExitCode.Should().Be(ExitCode.Success);
287+
}
259288
}
260289
}

0 commit comments

Comments
 (0)