Skip to content

Cleanup TFM handling for the new dotnet test for MTP #47489

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 13, 2025
Merged
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
77 changes: 16 additions & 61 deletions src/Cli/dotnet/commands/dotnet-test/SolutionAndProjectUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.Build.Evaluation;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools;
using Microsoft.DotNet.Tools.Test;
using NuGet.Packaging;
using LocalizableStrings = Microsoft.DotNet.Tools.Test.LocalizableStrings;
Expand Down Expand Up @@ -104,85 +105,39 @@ public static IEnumerable<TestModule> GetProjectProperties(string projectFilePat
{
var projects = new List<TestModule>();

var globalPropertiesWithoutTargetFramework = new Dictionary<string, string>(globalProperties);
globalPropertiesWithoutTargetFramework.Remove(ProjectProperties.TargetFramework);

var project = projectCollection.LoadProject(projectFilePath, globalPropertiesWithoutTargetFramework, null);
var project = projectCollection.LoadProject(projectFilePath, globalProperties, null);

// Check if TargetFramework is specified in global properties
if (globalProperties.TryGetValue(ProjectProperties.TargetFramework, out string targetFramework))
{
Logger.LogTrace(() => $"Loaded project '{Path.GetFileName(projectFilePath)}' with global property TargetFramework '{targetFramework}'.");

if (IsValidTargetFramework(project, targetFramework))
{
Logger.LogTrace(() => $"Project '{Path.GetFileName(projectFilePath)}' with TargetFramework '{targetFramework}': before re-evaluation '{ProjectProperties.IsTestingPlatformApplication}' is '{project.GetPropertyValue(ProjectProperties.IsTestingPlatformApplication)}'.");
var targetFramework = project.GetPropertyValue(ProjectProperties.TargetFramework);
var targetFrameworks = project.GetPropertyValue(ProjectProperties.TargetFrameworks);
Logger.LogTrace(() => $"Loaded project '{Path.GetFileName(projectFilePath)}' with TargetFramework '{targetFramework}', TargetFrameworks '{targetFrameworks}', IsTestProject '{project.GetPropertyValue(ProjectProperties.IsTestProject)}', and '{ProjectProperties.IsTestingPlatformApplication}' is '{project.GetPropertyValue(ProjectProperties.IsTestingPlatformApplication)}'.");

project.SetProperty(ProjectProperties.TargetFramework, targetFramework);
project.ReevaluateIfNecessary();

Logger.LogTrace(() => $"Project '{Path.GetFileName(projectFilePath)}' with TargetFramework '{targetFramework}': after re-evaluation '{ProjectProperties.IsTestingPlatformApplication}' is '{project.GetPropertyValue(ProjectProperties.IsTestingPlatformApplication)}'.");

if (GetModuleFromProject(project) is { } module)
{
projects.Add(module);
}
}
else
if (!string.IsNullOrEmpty(targetFramework) || string.IsNullOrEmpty(targetFrameworks))
{
if (GetModuleFromProject(project) is { } module)
{
// TODO: When can this happen? Should we explicitly error?
Logger.LogTrace(() => $"Project '{Path.GetFileName(projectFilePath)}' with TargetFramework '{targetFramework}' was considered invalid.");
projects.Add(module);
}
}
else
{
string targetFrameworks = project.GetPropertyValue(ProjectProperties.TargetFrameworks);

if (string.IsNullOrEmpty(targetFrameworks))
var frameworks = targetFrameworks.Split(CliConstants.SemiColon, StringSplitOptions.RemoveEmptyEntries);
foreach (var framework in frameworks)
{
Logger.LogTrace(() => $"Loaded project '{Path.GetFileName(projectFilePath)}' has '{ProjectProperties.IsTestingPlatformApplication}' = '{project.GetPropertyValue(ProjectProperties.IsTestingPlatformApplication)}'.");
project.SetProperty(ProjectProperties.TargetFramework, framework);
project.ReevaluateIfNecessary();
Logger.LogTrace(() => $"Loaded inner project '{Path.GetFileName(projectFilePath)}' has '{ProjectProperties.IsTestingPlatformApplication}' = '{project.GetPropertyValue(ProjectProperties.IsTestingPlatformApplication)}' (TFM: '{framework}').");

if (GetModuleFromProject(project) is { } module)
{
projects.Add(module);
}
}
else
{
Logger.LogTrace(() => $"Loaded project '{Path.GetFileName(projectFilePath)}' has '{ProjectProperties.IsTestingPlatformApplication}' = '{project.GetPropertyValue(ProjectProperties.IsTestingPlatformApplication)}' (TFMs: '{targetFrameworks}').");

var frameworks = targetFrameworks.Split(CliConstants.SemiColon, StringSplitOptions.RemoveEmptyEntries);
foreach (var framework in frameworks)
{
project.SetProperty(ProjectProperties.TargetFramework, framework);
project.ReevaluateIfNecessary();

Logger.LogTrace(() => $"Loaded project '{Path.GetFileName(projectFilePath)}' has '{ProjectProperties.IsTestingPlatformApplication}' = '{project.GetPropertyValue(ProjectProperties.IsTestingPlatformApplication)}' (TFM: '{framework}').");

if (GetModuleFromProject(project) is { } module)
{
projects.Add(module);
}
}
}
}

return projects;
}


private static bool IsValidTargetFramework(Project project, string targetFramework)
{
string targetFrameworks = project.GetPropertyValue(ProjectProperties.TargetFrameworks);
if (string.IsNullOrEmpty(targetFrameworks))
{
return project.GetPropertyValue(ProjectProperties.TargetFramework) == targetFramework;
}

var frameworks = targetFrameworks.Split(CliConstants.SemiColon, StringSplitOptions.RemoveEmptyEntries);
return frameworks.Contains(targetFramework);
}

private static TestModule? GetModuleFromProject(Project project)
{
_ = bool.TryParse(project.GetPropertyValue(ProjectProperties.IsTestProject), out bool isTestProject);
Expand Down Expand Up @@ -211,8 +166,8 @@ private static string GetExecutablePath(Project project)
string targetFrameworkIdentifier = project.GetPropertyValue(ProjectProperties.TargetFrameworkIdentifier);

if (targetFrameworkIdentifier.Equals(CliConstants.NetCoreIdentifier, StringComparison.OrdinalIgnoreCase) &&
isExecutable &&
useAppHost)
isExecutable &&
useAppHost)
{
string targetDir = project.GetPropertyValue(ProjectProperties.TargetDir);
string assemblyName = project.GetPropertyValue(ProjectProperties.AssemblyName);
Expand Down
Loading