forked from microsoft/testfx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunSettingsEnvironmentVariableProvider.cs
More file actions
99 lines (82 loc) · 3.94 KB
/
RunSettingsEnvironmentVariableProvider.cs
File metadata and controls
99 lines (82 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Testing.Extensions.VSTestBridge.CommandLine;
using Microsoft.Testing.Platform;
using Microsoft.Testing.Platform.CommandLine;
using Microsoft.Testing.Platform.Extensions;
using Microsoft.Testing.Platform.Extensions.TestHostControllers;
using Microsoft.Testing.Platform.Helpers;
namespace Microsoft.Testing.Extensions.VSTestBridge.TestHostControllers;
internal sealed class RunSettingsEnvironmentVariableProvider : ITestHostEnvironmentVariableProvider
{
private readonly IExtension _extension;
private readonly ICommandLineOptions _commandLineOptions;
private readonly IFileSystem _fileSystem;
private readonly IEnvironment _environment;
private XDocument? _runSettings;
public RunSettingsEnvironmentVariableProvider(IExtension extension, ICommandLineOptions commandLineOptions, IFileSystem fileSystem, IEnvironment environment)
{
_extension = extension;
_commandLineOptions = commandLineOptions;
_fileSystem = fileSystem;
_environment = environment;
}
public string Uid => _extension.Uid;
public string Version => _extension.Version;
public string DisplayName => _extension.DisplayName;
public string Description => _extension.Description;
public async Task<bool> IsEnabledAsync()
{
string? runSettingsFilePath = null;
string? runSettingsContent = null;
// Try to get runsettings from command line
if (_commandLineOptions.TryGetOptionArgumentList(RunSettingsCommandLineOptionsProvider.RunSettingsOptionName, out string[]? runsettings)
&& runsettings.Length > 0)
{
if (_fileSystem.ExistFile(runsettings[0]))
{
runSettingsFilePath = runsettings[0];
}
}
// If not from command line, try environment variable with content
if (runSettingsFilePath is null)
{
runSettingsContent = _environment.GetEnvironmentVariable("TESTINGPLATFORM_EXPERIMENTAL_VSTEST_RUNSETTINGS");
}
// If not from content env var, try environment variable with file path
if (runSettingsFilePath is null && RoslynString.IsNullOrEmpty(runSettingsContent))
{
string? envVarFilePath = _environment.GetEnvironmentVariable("TESTINGPLATFORM_VSTESTBRIDGE_RUNSETTINGS_FILE");
if (!RoslynString.IsNullOrEmpty(envVarFilePath) && _fileSystem.ExistFile(envVarFilePath))
{
runSettingsFilePath = envVarFilePath;
}
}
// If we have a file path, read from file
if (runSettingsFilePath is not null)
{
using IFileStream fileStream = _fileSystem.NewFileStream(runSettingsFilePath, FileMode.Open, FileAccess.Read);
_runSettings = await XDocument.LoadAsync(fileStream.Stream, LoadOptions.None, CancellationToken.None).ConfigureAwait(false);
}
else if (!RoslynString.IsNullOrEmpty(runSettingsContent))
{
// If we have content, parse it directly
_runSettings = XDocument.Parse(runSettingsContent);
}
else
{
return false;
}
return _runSettings.Element("RunSettings")?.Element("RunConfiguration")?.Element("EnvironmentVariables") is not null;
}
public Task UpdateAsync(IEnvironmentVariables environmentVariables)
{
foreach (XElement element in _runSettings!.Element("RunSettings")!.Element("RunConfiguration")!.Element("EnvironmentVariables")!.Elements())
{
environmentVariables.SetVariable(new(element.Name.ToString(), element.Value, true, true));
}
return Task.CompletedTask;
}
public Task<ValidationResult> ValidateTestHostEnvironmentVariablesAsync(IReadOnlyEnvironmentVariables environmentVariables)
=> ValidationResult.ValidTask;
}