Skip to content
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
7 changes: 4 additions & 3 deletions src/Cli/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,10 @@ target-produced project state, and the session's success/failure cleanup. Cover

In `--solution` runs,
[`device selection`](dotnet/Commands/Test/MTP/SolutionAndProjectUtility.cs) applies only to
test projects (`IsTestProject` or `IsTestingPlatformApplication`). App and library projects, such as
MAUI heads, can import `ComputeAvailableDevices` in their inner builds and must not prompt for a device. The single-project
path evaluates before restore, so it must not apply this NuGet-dependent gate.
MTP test applications (`IsTestingPlatformApplication`); `IsTestProject` is a VSTest concept and must not
opt a project in. App and library projects, such as MAUI heads, can import `ComputeAvailableDevices` in
their inner builds and must not prompt for a device. The single-project path evaluates before restore, so
it must not apply this NuGet-dependent gate.

Parent-process operations must also use the captured invocation directory: pass it through background
workload updates to both `global.json` discovery and NuGet configuration, and initialize absolute MTP
Expand Down
6 changes: 3 additions & 3 deletions src/Cli/dotnet/Commands/Test/MTP/MSBuildUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,9 @@ private static (ConcurrentBag<ParallelizableTestModuleGroupWithSequentialInnerMo
evaluationContext,
project.Configuration,
project.Platform,
// The solution was already restored and built above, so test-project properties
// imported from NuGet packages are reliable here.
requireTestProject: true);
// The solution was already restored and built above, so IsTestingPlatformApplication
// imported from NuGet packages is reliable here.
requireTestingPlatformApplication: true);

if (evaluation?.SupportsDeviceSelection == true)
{
Expand Down
24 changes: 11 additions & 13 deletions src/Cli/dotnet/Commands/Test/MTP/SolutionAndProjectUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,11 @@ internal static HashSet<string> CreateTraversalProjectVisitSet()
/// The evaluation happens in the collection of <paramref name="buildSession"/>, which is the one
/// every project of the run has to share; pass an <paramref name="evaluationContext"/> to reuse an
/// existing one and avoid redundant evaluation.
/// When <paramref name="requireTestProject"/> is <see langword="true"/>, only target frameworks that
/// are test projects participate in device selection. Solutions use this so that non-test projects
/// (e.g. a MAUI app or library) that import <c>ComputeAvailableDevices</c> through their workload
/// are not asked for a device. It must only be used after restore, since test-project properties
/// typically come from NuGet package imports.
/// When <paramref name="requireTestingPlatformApplication"/> is <see langword="true"/>, only target
/// frameworks that are Microsoft.Testing.Platform applications participate in device selection.
/// Solutions use this so that other projects (e.g. a MAUI app or library) that import
/// <c>ComputeAvailableDevices</c> through their workload are not asked for a device. It must only be
/// used after restore, since <c>IsTestingPlatformApplication</c> typically comes from NuGet package imports.
/// </summary>
internal static DeviceSelectionEvaluation? EvaluateProjectForDeviceSelection(
string projectFilePath,
Expand All @@ -491,7 +491,7 @@ internal static HashSet<string> CreateTraversalProjectVisitSet()
EvaluationContext? evaluationContext = null,
string? configuration = null,
string? platform = null,
bool requireTestProject = false)
bool requireTestingPlatformApplication = false)
{
// --device is already handled by HandleDeviceWithTargetFrameworkSelection
if (!string.IsNullOrWhiteSpace(buildOptions.Device))
Expand Down Expand Up @@ -571,7 +571,7 @@ internal static HashSet<string> CreateTraversalProjectVisitSet()
evaluatedProjects[framework] = frameworkProject;
}

return new DeviceSelectionEvaluation(evaluatedProjects, testTfmsInParallel, requireTestProject);
return new DeviceSelectionEvaluation(evaluatedProjects, testTfmsInParallel, requireTestingPlatformApplication);
}

/// <summary>
Expand Down Expand Up @@ -621,7 +621,7 @@ internal static HashSet<string> CreateTraversalProjectVisitSet()
internal sealed record DeviceSelectionEvaluation(
Dictionary<string, ProjectInstance> EvaluatedProjects,
bool TestTfmsInParallel,
bool RequireTestProject = false)
bool RequireTestingPlatformApplication = false)
{
public IEnumerable<KeyValuePair<string, ProjectInstance>> ProjectsByFramework
=> EvaluatedProjects.Count == 1
Expand All @@ -633,11 +633,9 @@ public bool SupportsDeviceSelection

public bool RequiresDeviceSelection(ProjectInstance project)
=> project.Targets.ContainsKey(Constants.ComputeAvailableDevices)
&& (!RequireTestProject
|| IsTrue(project.GetPropertyValue(ProjectProperties.IsTestProject))
|| IsTrue(project.GetPropertyValue(ProjectProperties.IsTestingPlatformApplication)));

private static bool IsTrue(string value) => bool.TryParse(value, out bool result) && result;
&& (!RequireTestingPlatformApplication
|| (bool.TryParse(project.GetPropertyValue(ProjectProperties.IsTestingPlatformApplication), out bool isTestingPlatformApplication)
&& isTestingPlatformApplication));
}

internal sealed record DeviceSelectionResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,51 +490,76 @@ public void ItDoesNotSelectDevicesForNonTestProjectsInSolution()

// Mimics a MAUI app in the same solution: the workload imports ComputeAvailableDevices into
// its inner builds even though it is not a test project. See https://github.com/dotnet/sdk/issues/57120.
string appDir = Path.Combine(testInstance.Path, "App");
Directory.CreateDirectory(appDir);
File.WriteAllText(Path.Combine(appDir, "Program.cs"), "System.Console.WriteLine();");
File.WriteAllText(Path.Combine(appDir, "App.csproj"),
AddProjectWithDeviceTargetsToSolution(testInstance.Path, "App");

var result = new DotnetTestCommand(Log, disableNewOutput: false)
.WithWorkingDirectory(testInstance.Path)
.WithEnvironmentVariable("DOTNET_CLI_UI_LANGUAGE", "en-US")
.Execute("--solution", "TestSolution.slnx", "-p:SingleDevice=true", "-bl");

result.Should().Pass()
.And.NotHaveStdErrContaining(CliCommandStrings.RunCommandNoDevicesAvailable);

AssertTargetInBinlog(
Path.Combine(testInstance.Path, "msbuild-dotnet-test.binlog"),
"ComputeAvailableDevices",
targets => targets.Should().HaveCount(2, "only the test project's target frameworks should discover devices"));
AssertTargetInBinlog(
Path.Combine(testInstance.Path, "msbuild-dotnet-test.binlog"),
"DeployToDevice",
targets => targets.Should().HaveCount(2, "only the test project's target frameworks should be deployed"));
}

[TestMethod]
public void ItDoesNotSelectDevicesForVSTestProjectsInSolution()
{
var testInstance = TestAssetsManager.CopyTestAsset("DotnetTestDevices")
.WithSource();

// IsTestProject alone marks a VSTest project, which MTP rejects; it must not opt into device selection.
AddProjectWithDeviceTargetsToSolution(testInstance.Path, "VSTestApp", "<IsTestProject>true</IsTestProject>");

var result = new DotnetTestCommand(Log, disableNewOutput: false)
.WithWorkingDirectory(testInstance.Path)
.WithEnvironmentVariable("DOTNET_CLI_UI_LANGUAGE", "en-US")
.Execute("--solution", "TestSolution.slnx", "-p:SingleDevice=true");

result.Should().Fail()
.And.HaveStdErrContaining(string.Format(CliCommandStrings.CmdUnsupportedVSTestTestApplicationsDescription, "VSTestApp.csproj"))
.And.NotHaveStdErrContaining(CliCommandStrings.RunCommandNoDevicesAvailable);
}

private static void AddProjectWithDeviceTargetsToSolution(string solutionDirectory, string projectName, string additionalProperties = "")
{
string projectDirectory = Path.Combine(solutionDirectory, projectName);
Directory.CreateDirectory(projectDirectory);
File.WriteAllText(Path.Combine(projectDirectory, "Program.cs"), "System.Console.WriteLine();");
File.WriteAllText(Path.Combine(projectDirectory, $"{projectName}.csproj"),
$$"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>{{ToolsetInfo.CurrentTargetFramework}}</TargetFrameworks>
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
{{additionalProperties}}
</PropertyGroup>
<Import Project="App.Device.targets" Condition="'$(TargetFramework)' != ''" />
<Import Project="Device.targets" Condition="'$(TargetFramework)' != ''" />
</Project>
""");
File.WriteAllText(Path.Combine(appDir, "App.Device.targets"),
File.WriteAllText(Path.Combine(projectDirectory, "Device.targets"),
"""
<Project>
<Target Name="ComputeAvailableDevices" Returns="@(Devices)" />
</Project>
""");

File.WriteAllText(Path.Combine(testInstance.Path, "TestSolution.slnx"),
"""
File.WriteAllText(Path.Combine(solutionDirectory, "TestSolution.slnx"),
$"""
<Solution>
<Project Path="App\App.csproj" />
<Project Path="{projectName}\{projectName}.csproj" />
<Project Path="DotnetTestDevices.csproj" />
</Solution>
""");

var result = new DotnetTestCommand(Log, disableNewOutput: false)
.WithWorkingDirectory(testInstance.Path)
.WithEnvironmentVariable("DOTNET_CLI_UI_LANGUAGE", "en-US")
.Execute("--solution", "TestSolution.slnx", "-p:SingleDevice=true", "-bl");

result.Should().Pass()
.And.NotHaveStdErrContaining(CliCommandStrings.RunCommandNoDevicesAvailable);

AssertTargetInBinlog(
Path.Combine(testInstance.Path, "msbuild-dotnet-test.binlog"),
"ComputeAvailableDevices",
targets => targets.Should().HaveCount(2, "only the test project's target frameworks should discover devices"));
AssertTargetInBinlog(
Path.Combine(testInstance.Path, "msbuild-dotnet-test.binlog"),
"DeployToDevice",
targets => targets.Should().HaveCount(2, "only the test project's target frameworks should be deployed"));
}

[TestMethod]
Expand Down
Loading