forked from microsoft/vstest
/
RunSettingsArgumentProcessor.cs
214 lines (175 loc) · 8.13 KB
/
RunSettingsArgumentProcessor.cs
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors
{
using System;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.IO;
using System.Text;
using System.Xml;
using Microsoft.VisualStudio.TestPlatform.Common;
using Microsoft.VisualStudio.TestPlatform.Common.Interfaces;
using Microsoft.VisualStudio.TestPlatform.Common.Utilities;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;
using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources;
internal class RunSettingsArgumentProcessor : IArgumentProcessor
{
#region Constants
/// <summary>
/// The name of the command line argument that the PortArgumentExecutor handles.
/// </summary>
public const string CommandName = "/Settings";
#endregion
private Lazy<IArgumentProcessorCapabilities> metadata;
private Lazy<IArgumentExecutor> executor;
/// <summary>
/// Gets the metadata.
/// </summary>
public Lazy<IArgumentProcessorCapabilities> Metadata
{
get
{
if (this.metadata == null)
{
this.metadata = new Lazy<IArgumentProcessorCapabilities>(() => new RunSettingsArgumentProcessorCapabilities());
}
return this.metadata;
}
}
/// <summary>
/// Gets or sets the executor.
/// </summary>
public Lazy<IArgumentExecutor> Executor
{
get
{
if (this.executor == null)
{
this.executor = new Lazy<IArgumentExecutor>(() => new RunSettingsArgumentExecutor(CommandLineOptions.Instance, RunSettingsManager.Instance));
}
return this.executor;
}
set
{
this.executor = value;
}
}
}
internal class RunSettingsArgumentProcessorCapabilities : BaseArgumentProcessorCapabilities
{
public override string CommandName => RunSettingsArgumentProcessor.CommandName;
public override bool AllowMultiple => false;
public override bool IsAction => false;
public override ArgumentProcessorPriority Priority => ArgumentProcessorPriority.RunSettings;
public override string HelpContentResourceName => CommandLineResources.RunSettingsArgumentHelp;
public override HelpContentPriority HelpPriority => HelpContentPriority.RunSettingsArgumentProcessorHelpPriority;
}
internal class RunSettingsArgumentExecutor : IArgumentExecutor
{
private CommandLineOptions commandLineOptions;
private IRunSettingsProvider runSettingsManager;
internal IFileHelper FileHelper { get; set; }
internal RunSettingsArgumentExecutor(CommandLineOptions commandLineOptions, IRunSettingsProvider runSettingsManager)
{
this.commandLineOptions = commandLineOptions;
this.runSettingsManager = runSettingsManager;
this.FileHelper = new FileHelper();
}
public void Initialize(string argument)
{
if (string.IsNullOrWhiteSpace(argument))
{
throw new CommandLineException(CommandLineResources.RunSettingsRequired);
}
if (!this.FileHelper.Exists(argument))
{
throw new CommandLineException(
string.Format(
CultureInfo.CurrentCulture,
CommandLineResources.RunSettingsFileNotFound,
argument));
}
Contract.EndContractBlock();
// Load up the run settings and set it as the active run settings.
try
{
XmlDocument document = this.GetRunSettingsDocument(argument);
this.runSettingsManager.UpdateRunSettings(document.OuterXml);
// To determine whether to infer framework and platform.
ExtractFrameworkAndPlatform();
//Add default runsettings values if not exists in given runsettings file.
this.runSettingsManager.AddDefaultRunSettings();
this.commandLineOptions.SettingsFile = argument;
if (this.runSettingsManager.QueryRunSettingsNode("RunConfiguration.EnvironmentVariables") != null)
{
this.commandLineOptions.InIsolation = true;
this.runSettingsManager.UpdateRunSettingsNode(InIsolationArgumentExecutor.RunSettingsPath, "true");
}
var testCaseFilter = this.runSettingsManager.QueryRunSettingsNode("RunConfiguration.TestCaseFilter");
if (testCaseFilter != null)
{
this.commandLineOptions.TestCaseFilterValue = testCaseFilter;
}
}
catch (XmlException exception)
{
throw new SettingsException(
string.Format(CultureInfo.CurrentCulture, "{0} {1}", ObjectModel.Resources.CommonResources.MalformedRunSettingsFile, exception.Message),
exception);
}
}
private void ExtractFrameworkAndPlatform()
{
var framworkStr = this.runSettingsManager.QueryRunSettingsNode(FrameworkArgumentExecutor.RunSettingsPath);
Framework framework = Framework.FromString(framworkStr);
if (framework != null)
{
this.commandLineOptions.TargetFrameworkVersion = framework;
}
var platformStr = this.runSettingsManager.QueryRunSettingsNode(PlatformArgumentExecutor.RunSettingsPath);
if (Enum.TryParse<Architecture>(platformStr, true, out var architecture))
{
this.commandLineOptions.TargetArchitecture = architecture;
}
}
[SuppressMessage("Microsoft.Security.Xml", "CA3053:UseXmlSecureResolver",
Justification = "XmlReaderSettings.XmlResolver is not available in core. Suppress until fxcop issue is fixed.")]
protected virtual XmlReader GetReaderForFile(string runSettingsFile)
{
return XmlReader.Create(new StringReader(File.ReadAllText(runSettingsFile, Encoding.UTF8)), XmlRunSettingsUtilities.ReaderSettings);
}
[SuppressMessage("Microsoft.Security.Xml", "CA3053:UseXmlSecureResolver",
Justification = "XmlDocument.XmlResolver is not available in core. Suppress until fxcop issue is fixed.")]
private XmlDocument GetRunSettingsDocument(string runSettingsFile)
{
XmlDocument runSettingsDocument;
if (!MSTestSettingsUtilities.IsLegacyTestSettingsFile(runSettingsFile))
{
using (XmlReader reader = this.GetReaderForFile(runSettingsFile))
{
var settingsDocument = new XmlDocument();
settingsDocument.Load(reader);
ClientUtilities.FixRelativePathsInRunSettings(settingsDocument, runSettingsFile);
runSettingsDocument = settingsDocument;
}
}
else
{
runSettingsDocument = XmlRunSettingsUtilities.CreateDefaultRunSettings();
runSettingsDocument = MSTestSettingsUtilities.Import(runSettingsFile, runSettingsDocument);
}
return runSettingsDocument;
}
public ArgumentProcessorResult Execute()
{
// Nothing to do here, the work was done in initialization.
return ArgumentProcessorResult.Success;
}
}
}