/
MSTestSettingsUtilities.cs
96 lines (79 loc) · 4.27 KB
/
MSTestSettingsUtilities.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
// 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.Utilities
{
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Xml;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using UtilitiesResources = Microsoft.VisualStudio.TestPlatform.Utilities.Resources.Resources;
/// <summary>
/// The legacy mstest.exe settings utilities.
/// </summary>
public static class MSTestSettingsUtilities
{
/// <summary>
/// Imports the parameter settings file in the default runsettings.
/// </summary>
/// <param name="settingsFile">
/// Settings file which need to be imported. The file extension of the settings file will be specified by <paramref name="SettingsFileExtension"/> property.
/// </param>
/// <param name="defaultRunSettings"> Input RunSettings document to which settings file need to be imported. </param>
/// <returns> Updated RunSetting Xml document with imported settings. </returns>
[SuppressMessage("Microsoft.Security.Xml", "CA3053:UseXmlSecureResolver",
Justification = "XmlDocument.XmlResolver is not available in core. Suppress until fxcop issue is fixed.")]
public static XmlDocument Import(string settingsFile, XmlDocument defaultRunSettings)
{
ValidateArg.NotNull(settingsFile, "settingsFile");
ValidateArg.NotNull(defaultRunSettings, "defaultRunSettings");
if (IsLegacyTestSettingsFile(settingsFile) == false)
{
throw new XmlException(string.Format(CultureInfo.CurrentCulture, UtilitiesResources.UnExpectedSettingsFile));
}
var navigator = defaultRunSettings.CreateNavigator();
if (!navigator.MoveToChild(Constants.RunSettingsName, string.Empty))
{
throw new XmlException(UtilitiesResources.NoRunSettingsNodeFound);
}
var settingsNode = GenerateMSTestXml(settingsFile);
defaultRunSettings.DocumentElement.PrependChild(defaultRunSettings.ImportNode(settingsNode, true));
// Adding RunConfig
if (!navigator.MoveToChild(Constants.RunConfigurationSettingsName, string.Empty))
{
var doc = new XmlDocument();
var runConfigurationNode = doc.CreateElement(Constants.RunConfigurationSettingsName);
defaultRunSettings.DocumentElement.PrependChild(defaultRunSettings.ImportNode(runConfigurationNode, true));
}
return defaultRunSettings;
}
public static bool IsLegacyTestSettingsFile(string settingsFile)
{
return string.Equals(Path.GetExtension(settingsFile), ".testSettings", StringComparison.OrdinalIgnoreCase)
|| string.Equals(Path.GetExtension(settingsFile), ".testrunConfig", StringComparison.OrdinalIgnoreCase)
|| string.Equals(Path.GetExtension(settingsFile), ".vsmdi", StringComparison.OrdinalIgnoreCase);
}
[SuppressMessage("Microsoft.Security.Xml", "CA3053:UseXmlSecureResolver",
Justification = "XmlDocument.XmlResolver is not available in core. Suppress until fxcop issue is fixed.")]
private static XmlElement GenerateMSTestXml(string settingsFile)
{
// Generate the MSTest xml
//
// <MSTest>
// <TestSettingsFile>C:\local.testsettings</TestSettingsFile>
// <ForcedLegacyMode>true</ForcedLegacyMode>
// </MSTest>
//
XmlDocument doc = new XmlDocument();
XmlElement mstestNode = doc.CreateElement("MSTest");
XmlElement testSettingsFileNode = doc.CreateElement("SettingsFile");
testSettingsFileNode.InnerXml = settingsFile;
mstestNode.AppendChild(testSettingsFileNode);
XmlElement forcedLegacyModeNode = doc.CreateElement("ForcedLegacyMode");
forcedLegacyModeNode.InnerXml = "true";
mstestNode.AppendChild(forcedLegacyModeNode);
return mstestNode;
}
}
}