Skip to content

Commit 6616a99

Browse files
authored
Cleanup TFM handling for the new dotnet test for MTP (#47489)
1 parent f69cc9c commit 6616a99

File tree

1 file changed

+16
-61
lines changed

1 file changed

+16
-61
lines changed

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

Lines changed: 16 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Microsoft.Build.Evaluation;
55
using Microsoft.DotNet.Cli.Utils;
6+
using Microsoft.DotNet.Tools;
67
using Microsoft.DotNet.Tools.Test;
78
using NuGet.Packaging;
89
using LocalizableStrings = Microsoft.DotNet.Tools.Test.LocalizableStrings;
@@ -104,85 +105,39 @@ public static IEnumerable<TestModule> GetProjectProperties(string projectFilePat
104105
{
105106
var projects = new List<TestModule>();
106107

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

110-
var project = projectCollection.LoadProject(projectFilePath, globalPropertiesWithoutTargetFramework, null);
109+
var project = projectCollection.LoadProject(projectFilePath, globalProperties, null);
111110

112-
// Check if TargetFramework is specified in global properties
113-
if (globalProperties.TryGetValue(ProjectProperties.TargetFramework, out string targetFramework))
114-
{
115-
Logger.LogTrace(() => $"Loaded project '{Path.GetFileName(projectFilePath)}' with global property TargetFramework '{targetFramework}'.");
116-
117-
if (IsValidTargetFramework(project, targetFramework))
118-
{
119-
Logger.LogTrace(() => $"Project '{Path.GetFileName(projectFilePath)}' with TargetFramework '{targetFramework}': before re-evaluation '{ProjectProperties.IsTestingPlatformApplication}' is '{project.GetPropertyValue(ProjectProperties.IsTestingPlatformApplication)}'.");
111+
var targetFramework = project.GetPropertyValue(ProjectProperties.TargetFramework);
112+
var targetFrameworks = project.GetPropertyValue(ProjectProperties.TargetFrameworks);
113+
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)}'.");
120114

121-
project.SetProperty(ProjectProperties.TargetFramework, targetFramework);
122-
project.ReevaluateIfNecessary();
123-
124-
Logger.LogTrace(() => $"Project '{Path.GetFileName(projectFilePath)}' with TargetFramework '{targetFramework}': after re-evaluation '{ProjectProperties.IsTestingPlatformApplication}' is '{project.GetPropertyValue(ProjectProperties.IsTestingPlatformApplication)}'.");
125-
126-
if (GetModuleFromProject(project) is { } module)
127-
{
128-
projects.Add(module);
129-
}
130-
}
131-
else
115+
if (!string.IsNullOrEmpty(targetFramework) || string.IsNullOrEmpty(targetFrameworks))
116+
{
117+
if (GetModuleFromProject(project) is { } module)
132118
{
133-
// TODO: When can this happen? Should we explicitly error?
134-
Logger.LogTrace(() => $"Project '{Path.GetFileName(projectFilePath)}' with TargetFramework '{targetFramework}' was considered invalid.");
119+
projects.Add(module);
135120
}
136121
}
137122
else
138123
{
139-
string targetFrameworks = project.GetPropertyValue(ProjectProperties.TargetFrameworks);
140-
141-
if (string.IsNullOrEmpty(targetFrameworks))
124+
var frameworks = targetFrameworks.Split(CliConstants.SemiColon, StringSplitOptions.RemoveEmptyEntries);
125+
foreach (var framework in frameworks)
142126
{
143-
Logger.LogTrace(() => $"Loaded project '{Path.GetFileName(projectFilePath)}' has '{ProjectProperties.IsTestingPlatformApplication}' = '{project.GetPropertyValue(ProjectProperties.IsTestingPlatformApplication)}'.");
127+
project.SetProperty(ProjectProperties.TargetFramework, framework);
128+
project.ReevaluateIfNecessary();
129+
Logger.LogTrace(() => $"Loaded inner project '{Path.GetFileName(projectFilePath)}' has '{ProjectProperties.IsTestingPlatformApplication}' = '{project.GetPropertyValue(ProjectProperties.IsTestingPlatformApplication)}' (TFM: '{framework}').");
144130

145131
if (GetModuleFromProject(project) is { } module)
146132
{
147133
projects.Add(module);
148134
}
149135
}
150-
else
151-
{
152-
Logger.LogTrace(() => $"Loaded project '{Path.GetFileName(projectFilePath)}' has '{ProjectProperties.IsTestingPlatformApplication}' = '{project.GetPropertyValue(ProjectProperties.IsTestingPlatformApplication)}' (TFMs: '{targetFrameworks}').");
153-
154-
var frameworks = targetFrameworks.Split(CliConstants.SemiColon, StringSplitOptions.RemoveEmptyEntries);
155-
foreach (var framework in frameworks)
156-
{
157-
project.SetProperty(ProjectProperties.TargetFramework, framework);
158-
project.ReevaluateIfNecessary();
159-
160-
Logger.LogTrace(() => $"Loaded project '{Path.GetFileName(projectFilePath)}' has '{ProjectProperties.IsTestingPlatformApplication}' = '{project.GetPropertyValue(ProjectProperties.IsTestingPlatformApplication)}' (TFM: '{framework}').");
161-
162-
if (GetModuleFromProject(project) is { } module)
163-
{
164-
projects.Add(module);
165-
}
166-
}
167-
}
168136
}
169137

170138
return projects;
171139
}
172140

173-
174-
private static bool IsValidTargetFramework(Project project, string targetFramework)
175-
{
176-
string targetFrameworks = project.GetPropertyValue(ProjectProperties.TargetFrameworks);
177-
if (string.IsNullOrEmpty(targetFrameworks))
178-
{
179-
return project.GetPropertyValue(ProjectProperties.TargetFramework) == targetFramework;
180-
}
181-
182-
var frameworks = targetFrameworks.Split(CliConstants.SemiColon, StringSplitOptions.RemoveEmptyEntries);
183-
return frameworks.Contains(targetFramework);
184-
}
185-
186141
private static TestModule? GetModuleFromProject(Project project)
187142
{
188143
_ = bool.TryParse(project.GetPropertyValue(ProjectProperties.IsTestProject), out bool isTestProject);
@@ -211,8 +166,8 @@ private static string GetExecutablePath(Project project)
211166
string targetFrameworkIdentifier = project.GetPropertyValue(ProjectProperties.TargetFrameworkIdentifier);
212167

213168
if (targetFrameworkIdentifier.Equals(CliConstants.NetCoreIdentifier, StringComparison.OrdinalIgnoreCase) &&
214-
isExecutable &&
215-
useAppHost)
169+
isExecutable &&
170+
useAppHost)
216171
{
217172
string targetDir = project.GetPropertyValue(ProjectProperties.TargetDir);
218173
string assemblyName = project.GetPropertyValue(ProjectProperties.AssemblyName);

0 commit comments

Comments
 (0)