Skip to content

Commit 9ce4f15

Browse files
authored
Print informational message for usage of launchSettings.json in dotnet test (#50112)
1 parent 0362a72 commit 9ce4f15

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

src/Cli/dotnet/Commands/Test/MSBuildUtility.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static (IEnumerable<ParallelizableTestModuleGroupWithSequentialInnerModul
5353
FacadeLogger? logger = LoggerUtility.DetermineBinlogger([.. buildOptions.MSBuildArgs], dotnetTestVerb);
5454
var collection = new ProjectCollection(globalProperties: CommonRunHelpers.GetGlobalPropertiesFromArgs([.. buildOptions.MSBuildArgs]), logger is null ? null : [logger], toolsetDefinitionLocations: ToolsetDefinitionLocations.Default);
5555

56-
IEnumerable<ParallelizableTestModuleGroupWithSequentialInnerModules> projects = SolutionAndProjectUtility.GetProjectProperties(projectFilePath, collection, buildOptions.NoLaunchProfile);
56+
IEnumerable<ParallelizableTestModuleGroupWithSequentialInnerModules> projects = SolutionAndProjectUtility.GetProjectProperties(projectFilePath, collection, buildOptions);
5757
logger?.ReallyShutdown();
5858

5959
return (projects, isBuiltOrRestored);
@@ -119,7 +119,7 @@ private static ConcurrentBag<ParallelizableTestModuleGroupWithSequentialInnerMod
119119
new ParallelOptions { MaxDegreeOfParallelism = buildOptions.DegreeOfParallelism },
120120
(project) =>
121121
{
122-
IEnumerable<ParallelizableTestModuleGroupWithSequentialInnerModules> projectsMetadata = SolutionAndProjectUtility.GetProjectProperties(project, projectCollection, buildOptions.NoLaunchProfile);
122+
IEnumerable<ParallelizableTestModuleGroupWithSequentialInnerModules> projectsMetadata = SolutionAndProjectUtility.GetProjectProperties(project, projectCollection, buildOptions);
123123
foreach (var projectMetadata in projectsMetadata)
124124
{
125125
allProjects.Add(projectMetadata);

src/Cli/dotnet/Commands/Test/SolutionAndProjectUtility.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public static string GetRootDirectory(string solutionOrProjectFilePath)
147147
return string.IsNullOrEmpty(fileDirectory) ? Directory.GetCurrentDirectory() : fileDirectory;
148148
}
149149

150-
public static IEnumerable<ParallelizableTestModuleGroupWithSequentialInnerModules> GetProjectProperties(string projectFilePath, ProjectCollection projectCollection, bool noLaunchProfile)
150+
public static IEnumerable<ParallelizableTestModuleGroupWithSequentialInnerModules> GetProjectProperties(string projectFilePath, ProjectCollection projectCollection, BuildOptions buildOptions)
151151
{
152152
var projects = new List<ParallelizableTestModuleGroupWithSequentialInnerModules>();
153153
ProjectInstance projectInstance = EvaluateProject(projectCollection, projectFilePath, null);
@@ -159,7 +159,7 @@ public static IEnumerable<ParallelizableTestModuleGroupWithSequentialInnerModule
159159

160160
if (!string.IsNullOrEmpty(targetFramework) || string.IsNullOrEmpty(targetFrameworks))
161161
{
162-
if (GetModuleFromProject(projectInstance, projectCollection.Loggers, noLaunchProfile) is { } module)
162+
if (GetModuleFromProject(projectInstance, projectCollection.Loggers, buildOptions) is { } module)
163163
{
164164
projects.Add(new ParallelizableTestModuleGroupWithSequentialInnerModules(module));
165165
}
@@ -187,7 +187,7 @@ public static IEnumerable<ParallelizableTestModuleGroupWithSequentialInnerModule
187187
projectInstance = EvaluateProject(projectCollection, projectFilePath, framework);
188188
Logger.LogTrace(() => $"Loaded inner project '{Path.GetFileName(projectFilePath)}' has '{ProjectProperties.IsTestingPlatformApplication}' = '{projectInstance.GetPropertyValue(ProjectProperties.IsTestingPlatformApplication)}' (TFM: '{framework}').");
189189

190-
if (GetModuleFromProject(projectInstance, projectCollection.Loggers, noLaunchProfile) is { } module)
190+
if (GetModuleFromProject(projectInstance, projectCollection.Loggers, buildOptions) is { } module)
191191
{
192192
projects.Add(new ParallelizableTestModuleGroupWithSequentialInnerModules(module));
193193
}
@@ -201,7 +201,7 @@ public static IEnumerable<ParallelizableTestModuleGroupWithSequentialInnerModule
201201
projectInstance = EvaluateProject(projectCollection, projectFilePath, framework);
202202
Logger.LogTrace(() => $"Loaded inner project '{Path.GetFileName(projectFilePath)}' has '{ProjectProperties.IsTestingPlatformApplication}' = '{projectInstance.GetPropertyValue(ProjectProperties.IsTestingPlatformApplication)}' (TFM: '{framework}').");
203203

204-
if (GetModuleFromProject(projectInstance, projectCollection.Loggers, noLaunchProfile) is { } module)
204+
if (GetModuleFromProject(projectInstance, projectCollection.Loggers, buildOptions) is { } module)
205205
{
206206
innerModules ??= new List<TestModule>();
207207
innerModules.Add(module);
@@ -218,7 +218,7 @@ public static IEnumerable<ParallelizableTestModuleGroupWithSequentialInnerModule
218218
return projects;
219219
}
220220

221-
private static TestModule? GetModuleFromProject(ProjectInstance project, ICollection<ILogger>? loggers, bool noLaunchProfile)
221+
private static TestModule? GetModuleFromProject(ProjectInstance project, ICollection<ILogger>? loggers, BuildOptions buildOptions)
222222
{
223223
_ = bool.TryParse(project.GetPropertyValue(ProjectProperties.IsTestProject), out bool isTestProject);
224224
_ = bool.TryParse(project.GetPropertyValue(ProjectProperties.IsTestingPlatformApplication), out bool isTestingPlatformApplication);
@@ -248,7 +248,7 @@ public static IEnumerable<ParallelizableTestModuleGroupWithSequentialInnerModule
248248
string projectFullPath = project.GetPropertyValue(ProjectProperties.ProjectFullPath);
249249

250250
// TODO: Support --launch-profile and pass it here.
251-
var launchSettings = TryGetLaunchProfileSettings(Path.GetDirectoryName(projectFullPath)!, Path.GetFileNameWithoutExtension(projectFullPath), project.GetPropertyValue(ProjectProperties.AppDesignerFolder), noLaunchProfile, profileName: null);
251+
var launchSettings = TryGetLaunchProfileSettings(Path.GetDirectoryName(projectFullPath)!, Path.GetFileNameWithoutExtension(projectFullPath), project.GetPropertyValue(ProjectProperties.AppDesignerFolder), buildOptions, profileName: null);
252252

253253
return new TestModule(runProperties, PathUtility.FixFilePath(projectFullPath), targetFramework, isTestingPlatformApplication, isTestProject, launchSettings, project.GetPropertyValue(ProjectProperties.TargetPath));
254254

@@ -270,9 +270,9 @@ static RunProperties GetRunProperties(ProjectInstance project, ICollection<ILogg
270270
}
271271
}
272272

273-
private static ProjectLaunchSettingsModel? TryGetLaunchProfileSettings(string projectDirectory, string projectNameWithoutExtension, string appDesignerFolder, bool noLaunchProfile, string? profileName)
273+
private static ProjectLaunchSettingsModel? TryGetLaunchProfileSettings(string projectDirectory, string projectNameWithoutExtension, string appDesignerFolder, BuildOptions buildOptions, string? profileName)
274274
{
275-
if (noLaunchProfile)
275+
if (buildOptions.NoLaunchProfile)
276276
{
277277
return null;
278278
}
@@ -299,6 +299,12 @@ static RunProperties GetRunProperties(ProjectInstance project, ICollection<ILogg
299299
return null;
300300
}
301301

302+
// If buildOptions.Verbosity is null, we still want to print the message.
303+
if (buildOptions.Verbosity != VerbosityOptions.quiet)
304+
{
305+
Reporter.Output.WriteLine(string.Format(CliCommandStrings.UsingLaunchSettingsFromMessage, launchSettingsPath));
306+
}
307+
302308
var result = LaunchSettingsManager.TryApplyLaunchSettings(launchSettingsPath, profileName);
303309
if (!result.Success)
304310
{

test/dotnet.Tests/CommandTests/Test/GivenDotnetTestBuildsAndRunsTests.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ public void RunTestProjectWithTestsAndLaunchSettings_ShouldReturnExitCodeSuccess
137137
if (!TestContext.IsLocalized())
138138
{
139139
result.StdOut
140-
.Should().Contain("Test run summary: Passed!")
140+
.Should().Contain("Using launch settings from")
141+
.And.Contain(runJson ? "TestProjectWithLaunchSettings.run.json..." : $"Properties{Path.DirectorySeparatorChar}launchSettings.json...")
142+
.And.Contain("Test run summary: Passed!")
141143
.And.Contain("skipped Test1")
142144
.And.Contain("total: 2")
143145
.And.Contain("succeeded: 1")
@@ -163,7 +165,8 @@ public void RunTestProjectWithTestsAndNoLaunchSettings_ShouldReturnExitCodeSucce
163165
TestingPlatformOptions.NoLaunchProfileOption.Name);
164166

165167
result.StdOut.Should()
166-
.Contain("FAILED to find argument from launchSettings.json");
168+
.Contain("FAILED to find argument from launchSettings.json")
169+
.And.NotContain("Using launch settings from");
167170
}
168171

169172
[InlineData(TestingConstants.Debug)]
@@ -181,7 +184,9 @@ public void RunTestProjectWithTestsAndNoLaunchSettingsArguments_ShouldReturnExit
181184
TestingPlatformOptions.NoLaunchProfileArgumentsOption.Name, "true");
182185

183186
result.StdOut.Should()
184-
.Contain("FAILED to find argument from launchSettings.json");
187+
.Contain("Using launch settings from")
188+
.And.Contain($"Properties{Path.DirectorySeparatorChar}launchSettings.json...")
189+
.And.Contain("FAILED to find argument from launchSettings.json");
185190
}
186191

187192
[InlineData(TestingConstants.Debug)]

0 commit comments

Comments
 (0)