Skip to content

Commit b83cda9

Browse files
Unify reading runsettings by @Youssef1313 in #6422 (backport to rel/3.10) (#6434)
Co-authored-by: Youssef1313 <youssefvictor00@gmail.com>
1 parent 3f38950 commit b83cda9

File tree

3 files changed

+63
-39
lines changed

3 files changed

+63
-39
lines changed

src/Platform/Microsoft.Testing.Extensions.VSTestBridge/Configurations/RunSettingsConfigurationProvider.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using Microsoft.Testing.Extensions.VSTestBridge.CommandLine;
4+
using Microsoft.Testing.Extensions.VSTestBridge.Helpers;
5+
using Microsoft.Testing.Platform;
56
using Microsoft.Testing.Platform.CommandLine;
67
using Microsoft.Testing.Platform.Configurations;
78
using Microsoft.Testing.Platform.Helpers;
@@ -37,7 +38,7 @@ internal sealed class RunSettingsConfigurationProvider(IFileSystem fileSystem) :
3738
/// <inheritdoc />
3839
public bool TryGet(string key, out string? value)
3940
{
40-
if (_runSettingsFileContent is null)
41+
if (RoslynString.IsNullOrEmpty(_runSettingsFileContent))
4142
{
4243
value = null;
4344
return false;
@@ -58,16 +59,9 @@ public bool TryGet(string key, out string? value)
5859
}
5960

6061
/// <inheritdoc />
61-
public async Task<IConfigurationProvider> BuildAsync(CommandLineParseResult commandLineParseResult)
62+
public Task<IConfigurationProvider> BuildAsync(CommandLineParseResult commandLineParseResult)
6263
{
63-
if (commandLineParseResult.TryGetOptionArgumentList(RunSettingsCommandLineOptionsProvider.RunSettingsOptionName, out string[]? runSettingsFilePath))
64-
{
65-
if (_fileSystem.ExistFile(runSettingsFilePath[0]))
66-
{
67-
_runSettingsFileContent = await _fileSystem.ReadAllTextAsync(runSettingsFilePath[0]).ConfigureAwait(false);
68-
}
69-
}
70-
71-
return this;
64+
_runSettingsFileContent = RunSettingsHelpers.ReadRunSettings(commandLineParseResult, _fileSystem);
65+
return Task.FromResult<IConfigurationProvider>(this);
7266
}
7367
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Microsoft.Testing.Extensions.VSTestBridge.CommandLine;
5+
using Microsoft.Testing.Platform;
6+
using Microsoft.Testing.Platform.CommandLine;
7+
using Microsoft.Testing.Platform.Helpers;
8+
9+
namespace Microsoft.Testing.Extensions.VSTestBridge.Helpers;
10+
11+
internal static class RunSettingsHelpers
12+
{
13+
// TODO: There are two code paths that read runsettings. One is calling the ICommandLineOptions overload, other is calling the CommandLineParseResult overload.
14+
// Figure out if they can/should be unified so that we do one I/O operation instead of two.
15+
public static string ReadRunSettings(ICommandLineOptions commandLineOptions, IFileSystem fileSystem)
16+
{
17+
_ = commandLineOptions.TryGetOptionArgumentList(RunSettingsCommandLineOptionsProvider.RunSettingsOptionName, out string[]? fileNames);
18+
return ReadRunSettings(fileNames, fileSystem);
19+
}
20+
21+
public static string ReadRunSettings(CommandLineParseResult commandLineParseResult, IFileSystem fileSystem)
22+
{
23+
_ = commandLineParseResult.TryGetOptionArgumentList(RunSettingsCommandLineOptionsProvider.RunSettingsOptionName, out string[]? fileNames);
24+
return ReadRunSettings(fileNames, fileSystem);
25+
}
26+
27+
private static string ReadRunSettings(string[]? runsettingsFileFromCommandLine, IFileSystem fileSystem)
28+
{
29+
if (runsettingsFileFromCommandLine is not null &&
30+
runsettingsFileFromCommandLine.Length == 1 &&
31+
fileSystem.ExistFile(runsettingsFileFromCommandLine[0]))
32+
{
33+
return fileSystem.ReadAllText(runsettingsFileFromCommandLine[0]);
34+
}
35+
else
36+
{
37+
string? envVariableRunSettings = Environment.GetEnvironmentVariable("TESTINGPLATFORM_EXPERIMENTAL_VSTEST_RUNSETTINGS");
38+
if (!RoslynString.IsNullOrEmpty(envVariableRunSettings))
39+
{
40+
return envVariableRunSettings;
41+
}
42+
else
43+
{
44+
string? runSettingsFilePath = Environment.GetEnvironmentVariable("TESTINGPLATFORM_VSTESTBRIDGE_RUNSETTINGS_FILE");
45+
46+
if (!RoslynString.IsNullOrEmpty(runSettingsFilePath) && File.Exists(runSettingsFilePath))
47+
{
48+
return fileSystem.ReadAllText(runSettingsFilePath);
49+
}
50+
}
51+
}
52+
53+
return string.Empty;
54+
}
55+
}

src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/RunSettingsAdapter.cs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using Microsoft.Testing.Extensions.VSTestBridge.CommandLine;
4+
using Microsoft.Testing.Extensions.VSTestBridge.Helpers;
55
using Microsoft.Testing.Extensions.VSTestBridge.Resources;
6-
using Microsoft.Testing.Platform;
76
using Microsoft.Testing.Platform.CommandLine;
87
using Microsoft.Testing.Platform.Configurations;
98
using Microsoft.Testing.Platform.Helpers;
@@ -38,31 +37,7 @@ public RunSettingsAdapter(
3837
ILoggerFactory loggerFactory,
3938
IMessageLogger messageLogger)
4039
{
41-
string? runSettingsXml = string.Empty;
42-
43-
if (commandLineOptions.TryGetOptionArgumentList(RunSettingsCommandLineOptionsProvider.RunSettingsOptionName, out string[]? fileNames)
44-
&& fileNames is not null
45-
&& fileNames.Length == 1
46-
&& fileSystem.ExistFile(fileNames[0]))
47-
{
48-
runSettingsXml = fileSystem.ReadAllText(fileNames[0]);
49-
}
50-
else
51-
{
52-
if (!RoslynString.IsNullOrEmpty(Environment.GetEnvironmentVariable("TESTINGPLATFORM_EXPERIMENTAL_VSTEST_RUNSETTINGS")))
53-
{
54-
runSettingsXml = Environment.GetEnvironmentVariable("TESTINGPLATFORM_EXPERIMENTAL_VSTEST_RUNSETTINGS");
55-
}
56-
else
57-
{
58-
string? runSettingsFilePath = Environment.GetEnvironmentVariable("TESTINGPLATFORM_VSTESTBRIDGE_RUNSETTINGS_FILE");
59-
60-
if (!RoslynString.IsNullOrEmpty(runSettingsFilePath) && File.Exists(runSettingsFilePath))
61-
{
62-
runSettingsXml = fileSystem.ReadAllText(runSettingsFilePath);
63-
}
64-
}
65-
}
40+
string runSettingsXml = RunSettingsHelpers.ReadRunSettings(commandLineOptions, fileSystem);
6641

6742
XDocument runSettingsDocument = RunSettingsPatcher.Patch(runSettingsXml, configuration, client, commandLineOptions);
6843
WarnOnUnsupportedEntries(runSettingsDocument, messageLogger);

0 commit comments

Comments
 (0)