Skip to content

Commit 6922920

Browse files
committed
Refactoring
1 parent 2188d5c commit 6922920

File tree

5 files changed

+232
-115
lines changed

5 files changed

+232
-115
lines changed

src/SettingsMigrator/Migrator.cs

+113-102
Original file line numberDiff line numberDiff line change
@@ -10,143 +10,155 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine
1010
public class Migrator
1111
{
1212
/// <summary>
13-
/// Given a runsettings with an embedded testsettings, converts it to runsettings.
13+
/// Given a runSettings with an embedded testSettings, converts it to runSettings.
1414
/// </summary>
15-
/// <param name="oldRunsettingsPath"></param>
16-
/// <param name="newRunsettingsPath"></param>
17-
public void MigrateRunsettings(string oldRunsettingsPath, string newRunsettingsPath)
15+
/// <param name="oldRunSettingsPath"></param>
16+
/// <param name="newRunSettingsPath"></param>
17+
public void MigrateRunSettings(string oldRunSettingsPath, string newRunSettingsPath)
1818
{
19-
string testsettingsPath = null;
20-
using (XmlTextReader reader = new XmlTextReader(oldRunsettingsPath))
19+
string testSettingsPath = null;
20+
using (XmlTextReader reader = new XmlTextReader(oldRunSettingsPath))
2121
{
2222
reader.Namespaces = false;
2323

24-
var document = new XmlDocument();
25-
document.Load(reader);
26-
var root = document.DocumentElement;
24+
var runSettingsXmlDoc = new XmlDocument();
25+
runSettingsXmlDoc.Load(reader);
26+
var root = runSettingsXmlDoc.DocumentElement;
2727

28-
var testsettingsNode = root.SelectSingleNode(@"/RunSettings/MSTest/SettingsFile");
28+
var testSettingsNode = root.SelectSingleNode(@"/RunSettings/MSTest/SettingsFile");
2929

30-
if (testsettingsNode != null)
30+
if (testSettingsNode != null)
3131
{
32-
testsettingsPath = testsettingsNode.InnerText;
32+
testSettingsPath = testSettingsNode.InnerText;
3333
}
34-
if (!string.IsNullOrWhiteSpace(testsettingsPath))
34+
if (!string.IsNullOrWhiteSpace(testSettingsPath))
3535
{
36-
// Expand path relative to runsettings location.
37-
if (!Path.IsPathRooted(testsettingsPath))
36+
// Expand path relative to runSettings location.
37+
if (!Path.IsPathRooted(testSettingsPath))
3838
{
39-
testsettingsPath = Path.Combine(oldRunsettingsPath, testsettingsPath);
39+
testSettingsPath = Path.Combine(oldRunSettingsPath, testSettingsPath);
4040
}
4141

42-
MigrateTestsettings(testsettingsPath, newRunsettingsPath, File.ReadAllText(oldRunsettingsPath));
42+
MigrateTestSettingsNodesToRunSettings(testSettingsPath, runSettingsXmlDoc);
43+
44+
runSettingsXmlDoc.Save(newRunSettingsPath);
4345
}
4446
else
4547
{
46-
Console.WriteLine("Runsettings does not contain an embedded testsettings, not migrating.");
48+
Console.WriteLine("RunSettings does not contain an embedded testSettings, not migrating.");
4749
}
4850
}
4951
}
5052

53+
54+
public void MigrateTestSettings(string oldTestSettingsPath, string newRunSettingsPath)
55+
{
56+
var runSettingsXmlDoc = new XmlDocument();
57+
runSettingsXmlDoc.LoadXml(sampleRunSettingsContent);
58+
59+
MigrateTestSettingsNodesToRunSettings(oldTestSettingsPath, runSettingsXmlDoc);
60+
61+
runSettingsXmlDoc.Save(newRunSettingsPath);
62+
}
63+
5164
/// <summary>
52-
/// Given a testsettings, converts it to runsettings
65+
/// Given a testSettings, converts it to runSettings
5366
/// </summary>
54-
/// <param name="testsettingsPath"></param>
55-
/// <param name="newRunsettingsPath"></param>
67+
/// <param name="testSettingsPath"></param>
68+
/// <param name="newRunSettingsPath"></param>
5669
/// <param name="oldRunSettingsContent"></param>
57-
public void MigrateTestsettings(string testsettingsPath, string newRunsettingsPath, string oldRunSettingsContent = null)
70+
public void MigrateTestSettingsNodesToRunSettings(string testSettingsPath, XmlDocument runSettingsXmlDoc)
5871
{
59-
using (XmlTextReader reader = new XmlTextReader(testsettingsPath))
72+
var testSettingsNodes = ReadTestSettingsNodes(testSettingsPath);
73+
74+
string testTimeout = null;
75+
if (testSettingsNodes.Timeout != null && testSettingsNodes.Timeout.Attributes[TestTimeoutAttributeName] != null)
6076
{
61-
reader.Namespaces = false;
77+
testTimeout = testSettingsNodes.Timeout.Attributes[TestTimeoutAttributeName].Value;
78+
}
6279

63-
var document = new XmlDocument();
64-
document.Load(reader);
65-
var root = document.DocumentElement;
80+
string runTimeout = null;
81+
if (testSettingsNodes.Timeout != null && testSettingsNodes.Timeout.Attributes[RunTimeoutAttributeName] != null)
82+
{
83+
runTimeout = testSettingsNodes.Timeout.Attributes[RunTimeoutAttributeName].Value;
84+
}
6685

67-
// Select the interesting nodes from the xml.
68-
var deploymentNode = root.SelectSingleNode(@"/TestSettings/Deployment");
69-
var scriptnode = root.SelectSingleNode(@"/TestSettings/Scripts");
70-
var websettingsNode = root.SelectSingleNode(@"/TestSettings/Execution/TestTypeSpecific/WebTestRunConfiguration");
71-
var oldDatacollectorNodes = root.SelectNodes(@"/TestSettings/AgentRule/DataCollectors/DataCollector");
72-
var timeoutNode = root.SelectSingleNode(@"/TestSettings/Execution/Timeouts");
73-
var assemblyresolutionNode = root.SelectSingleNode(@"/TestSettings/Execution/TestTypeSpecific/UnitTestRunConfig");
74-
var hostsNode = root.SelectSingleNode(@"/TestSettings/Execution/Hosts");
75-
var executionNode = root.SelectSingleNode(@"/TestSettings/Execution");
76-
77-
string testTimeout = null;
78-
if (timeoutNode != null && timeoutNode.Attributes[TestTimeoutAttributeName] != null)
79-
{
80-
testTimeout = timeoutNode.Attributes[TestTimeoutAttributeName].Value;
81-
}
86+
string parallelTestCount = null;
87+
if (testSettingsNodes.Execution != null && testSettingsNodes.Execution.Attributes[ParallelTestCountAttributeName] != null)
88+
{
89+
parallelTestCount = testSettingsNodes.Execution.Attributes[ParallelTestCountAttributeName].Value;
90+
}
8291

83-
string runTimeout = null;
84-
if (timeoutNode != null && timeoutNode.Attributes[RunTimeoutAttributeName] != null)
85-
{
86-
runTimeout = timeoutNode.Attributes[RunTimeoutAttributeName].Value;
87-
}
92+
// Remove the embedded testSettings node if it exists.
93+
RemoveEmbeddedTestSettings(runSettingsXmlDoc);
8894

89-
string parallelTestCount = null;
90-
if (executionNode != null && executionNode.Attributes[ParallelTestCountAttributeName] != null)
91-
{
92-
parallelTestCount = executionNode.Attributes[ParallelTestCountAttributeName].Value;
93-
}
95+
// WebTestRunConfiguration node.
96+
if (testSettingsNodes.WebSettings != null)
97+
{
98+
runSettingsXmlDoc.DocumentElement.AppendChild(runSettingsXmlDoc.ImportNode(testSettingsNodes.WebSettings, deep: true));
99+
}
94100

95-
if(string.IsNullOrEmpty(oldRunSettingsContent))
96-
{
97-
oldRunSettingsContent = sampleRunsettingsContent;
98-
}
99-
var newXmlDoc = new XmlDocument();
100-
newXmlDoc.LoadXml(oldRunSettingsContent);
101+
// LegacySettings node.
102+
if (testSettingsNodes.Deployment != null || testSettingsNodes.Script != null || testSettingsNodes.UnitTestConfig != null ||
103+
!string.IsNullOrEmpty(parallelTestCount) || !string.IsNullOrEmpty(testTimeout) || testSettingsNodes.Hosts != null)
104+
{
105+
AddLegacyNodes(testSettingsNodes, testTimeout, parallelTestCount, runSettingsXmlDoc);
106+
}
101107

102-
// Remove the embedded testsettings node if it exists.
103-
RemoveEmbeddedTestsettings(newXmlDoc);
108+
// TestSessionTimeout node.
109+
if (!string.IsNullOrEmpty(runTimeout))
110+
{
111+
AddRunTimeoutNode(runTimeout, runSettingsXmlDoc);
112+
}
104113

105-
// WebTestRunConfiguration node.
106-
if (websettingsNode != null)
107-
{
108-
newXmlDoc.DocumentElement.AppendChild(newXmlDoc.ImportNode(websettingsNode, deep: true));
109-
}
114+
// DataCollectors node.
115+
if (testSettingsNodes.Datacollectors != null && testSettingsNodes.Datacollectors.Count > 0)
116+
{
117+
AddDataCollectorNodes(testSettingsNodes.Datacollectors, runSettingsXmlDoc);
118+
}
119+
}
110120

111-
// LegacySettings node.
112-
if (deploymentNode != null || scriptnode != null || assemblyresolutionNode != null ||
113-
!string.IsNullOrEmpty(parallelTestCount) || !string.IsNullOrEmpty(testTimeout) || hostsNode != null)
114-
{
115-
AddLegacyNodes(deploymentNode, scriptnode, testTimeout, assemblyresolutionNode, hostsNode, parallelTestCount, newXmlDoc);
116-
}
121+
private TestSettingsNodes ReadTestSettingsNodes(string testSettingsPath)
122+
{
123+
var testSettingsNodes = new TestSettingsNodes();
117124

118-
// TestSessionTimeout node.
119-
if (!string.IsNullOrEmpty(runTimeout))
120-
{
121-
AddRunTimeoutNode(runTimeout, newXmlDoc);
122-
}
125+
using (XmlTextReader reader = new XmlTextReader(testSettingsPath))
126+
{
127+
reader.Namespaces = false;
123128

124-
// DataCollectors node.
125-
if (oldDatacollectorNodes != null && oldDatacollectorNodes.Count > 0)
126-
{
127-
AddDataCollectorNodes(oldDatacollectorNodes, newXmlDoc);
128-
}
129+
var testSettingsXmlDoc = new XmlDocument();
130+
testSettingsXmlDoc.Load(reader);
131+
var testSettingsRoot = testSettingsXmlDoc.DocumentElement;
129132

130-
newXmlDoc.Save(newRunsettingsPath);
133+
// Select the interesting nodes from the xml.
134+
testSettingsNodes.Deployment = testSettingsRoot.SelectSingleNode(@"/TestSettings/Deployment");
135+
testSettingsNodes.Script = testSettingsRoot.SelectSingleNode(@"/TestSettings/Scripts");
136+
testSettingsNodes.WebSettings = testSettingsRoot.SelectSingleNode(@"/TestSettings/Execution/TestTypeSpecific/WebTestRunConfiguration");
137+
testSettingsNodes.Datacollectors = testSettingsRoot.SelectNodes(@"/TestSettings/AgentRule/DataCollectors/DataCollector");
138+
testSettingsNodes.Timeout = testSettingsRoot.SelectSingleNode(@"/TestSettings/Execution/Timeouts");
139+
testSettingsNodes.UnitTestConfig = testSettingsRoot.SelectSingleNode(@"/TestSettings/Execution/TestTypeSpecific/UnitTestRunConfig");
140+
testSettingsNodes.Hosts = testSettingsRoot.SelectSingleNode(@"/TestSettings/Execution/Hosts");
141+
testSettingsNodes.Execution = testSettingsRoot.SelectSingleNode(@"/TestSettings/Execution");
131142
}
143+
144+
return testSettingsNodes;
132145
}
133146

134147
/// <summary>
135-
/// Removes the embedded testsettings node if present.
148+
/// Removes the embedded testSettings node if present.
136149
/// </summary>
137150
/// <param name="newXmlDoc"></param>
138-
private void RemoveEmbeddedTestsettings(XmlDocument newXmlDoc)
151+
private void RemoveEmbeddedTestSettings(XmlDocument newXmlDoc)
139152
{
140-
var testsettingsNode = newXmlDoc.DocumentElement.SelectSingleNode(@"/RunSettings/MSTest/SettingsFile");
141-
if (testsettingsNode != null)
153+
var testSettingsNode = newXmlDoc.DocumentElement.SelectSingleNode(@"/RunSettings/MSTest/SettingsFile");
154+
if (testSettingsNode != null)
142155
{
143-
testsettingsNode.ParentNode.RemoveChild(testsettingsNode);
156+
testSettingsNode.ParentNode.RemoveChild(testSettingsNode);
144157
}
145158
}
146159

147-
148160
/// <summary>
149-
/// Adds the legacy nodes to runsettings xml.
161+
/// Adds the legacy nodes to runSettings xml.
150162
/// </summary>
151163
/// <param name="deploymentNode"></param>
152164
/// <param name="scriptnode"></param>
@@ -155,7 +167,7 @@ private void RemoveEmbeddedTestsettings(XmlDocument newXmlDoc)
155167
/// <param name="hostsNode"></param>
156168
/// <param name="parallelTestCount"></param>
157169
/// <param name="newXmlDoc"></param>
158-
private void AddLegacyNodes(XmlNode deploymentNode, XmlNode scriptnode, string testTimeout, XmlNode assemblyresolutionNode, XmlNode hostsNode, string parallelTestCount, XmlDocument newXmlDoc)
170+
private void AddLegacyNodes(TestSettingsNodes testSettingsNodes, string testTimeout, string parallelTestCount, XmlDocument newXmlDoc)
159171
{
160172
//Remove if the legacy node already exists.
161173
var legacyNode = newXmlDoc.DocumentElement.SelectSingleNode(@"/RunSettings/LegacySettings");
@@ -166,17 +178,17 @@ private void AddLegacyNodes(XmlNode deploymentNode, XmlNode scriptnode, string t
166178

167179
legacyNode = newXmlDoc.CreateNode(XmlNodeType.Element, LegacySettingsNodeName, null);
168180

169-
if (deploymentNode != null)
181+
if (testSettingsNodes.Deployment != null)
170182
{
171-
legacyNode.AppendChild(newXmlDoc.ImportNode(deploymentNode, deep: true));
183+
legacyNode.AppendChild(newXmlDoc.ImportNode(testSettingsNodes.Deployment, deep: true));
172184
}
173-
if (scriptnode != null)
185+
if (testSettingsNodes.Script != null)
174186
{
175-
legacyNode.AppendChild(newXmlDoc.ImportNode(scriptnode, deep: true));
187+
legacyNode.AppendChild(newXmlDoc.ImportNode(testSettingsNodes.Script, deep: true));
176188
}
177189

178190
// Execution node.
179-
if (assemblyresolutionNode != null || !string.IsNullOrEmpty(parallelTestCount) || !string.IsNullOrEmpty(testTimeout) || hostsNode != null)
191+
if (testSettingsNodes.UnitTestConfig != null || !string.IsNullOrEmpty(parallelTestCount) || !string.IsNullOrEmpty(testTimeout) || testSettingsNodes.Hosts != null)
180192
{
181193
var newExecutionNode = newXmlDoc.CreateNode(XmlNodeType.Element, ExecutionNodeName, null);
182194

@@ -194,24 +206,23 @@ private void AddLegacyNodes(XmlNode deploymentNode, XmlNode scriptnode, string t
194206
newTimeoutsNode.Attributes.Append(testtimeoutattribute);
195207
newExecutionNode.AppendChild(newXmlDoc.ImportNode(newTimeoutsNode, deep: true));
196208
}
197-
if (hostsNode != null)
209+
if (testSettingsNodes.Hosts != null)
198210
{
199-
newExecutionNode.AppendChild(newXmlDoc.ImportNode(hostsNode, deep: true));
211+
newExecutionNode.AppendChild(newXmlDoc.ImportNode(testSettingsNodes.Hosts, deep: true));
200212
}
201-
if (assemblyresolutionNode != null)
213+
if (testSettingsNodes.UnitTestConfig != null)
202214
{
203215
var testTypeSpecificNode = newXmlDoc.CreateNode(XmlNodeType.Element, TestTypeSpecificNodeName, null);
204-
testTypeSpecificNode.AppendChild(newXmlDoc.ImportNode(assemblyresolutionNode, deep: true));
216+
testTypeSpecificNode.AppendChild(newXmlDoc.ImportNode(testSettingsNodes.UnitTestConfig, deep: true));
205217
newExecutionNode.AppendChild(newXmlDoc.ImportNode(testTypeSpecificNode, deep: true));
206218
}
207219
legacyNode.AppendChild(newXmlDoc.ImportNode(newExecutionNode, deep: true));
208220
}
209221
newXmlDoc.DocumentElement.AppendChild(legacyNode);
210222
}
211223

212-
213224
/// <summary>
214-
/// Adds the datacollector nodes to the runsettings xml.
225+
/// Adds the datacollector nodes to the runSettings xml.
215226
/// </summary>
216227
/// <param name="oldDatacollectorNodes"></param>
217228
/// <param name="newXmlDoc"></param>
@@ -269,7 +280,7 @@ private void AddRunTimeoutNode(string runTimeout, XmlDocument newXmlDoc)
269280
const string TestSessionTimeoutNodeName = "TestSessionTimeout";
270281
const string DataCollectionRunSettingsNodeName = "DataCollectionRunSettings";
271282
const string DataCollectorsNodeName = "DataCollectors";
272-
const string sampleRunsettingsContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
283+
const string sampleRunSettingsContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
273284
"<RunSettings></RunSettings>";
274285
}
275286
}

src/SettingsMigrator/Program.cs

+12-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System;
5+
using System.Globalization;
56
using System.IO;
7+
using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources;
68

79
namespace Microsoft.VisualStudio.TestPlatform.CommandLine
810
{
@@ -12,38 +14,38 @@ static void Main(string[] args)
1214
{
1315
if (args.Length != 2)
1416
{
15-
Console.WriteLine("Valid usage:\nSettingsMigrator.exe [Full path to testsettings file/runsettings file to be migrated] [Full path to new runsettings file]");
17+
Console.WriteLine(string.Format(CultureInfo.CurrentCulture, CommandLineResources.ValidUsage));
1618
return;
1719
}
1820

1921
string oldFilePath = args[0];
2022
string newFilePath = args[1];
2123

22-
if (!Path.IsPathRooted(oldFilePath) || !Path.IsPathRooted(newFilePath) || !string.Equals(Path.GetExtension(newFilePath), RunsettingsExtension))
24+
if (!Path.IsPathRooted(oldFilePath) || !Path.IsPathRooted(newFilePath) || !string.Equals(Path.GetExtension(newFilePath), RunSettingsExtension))
2325
{
24-
Console.WriteLine("Valid usage:\nSettingsMigrator.exe [Full path to testsettings file/runsettings file to be migrated] [Full path to new runsettings file]");
26+
Console.WriteLine(string.Format(CultureInfo.CurrentCulture, CommandLineResources.ValidUsage));
2527
return;
2628
}
2729

2830
var migrator = new Migrator();
2931

30-
if (string.Equals(Path.GetExtension(oldFilePath), TestsettingsExtension))
32+
if (string.Equals(Path.GetExtension(oldFilePath), TestSettingsExtension))
3133
{
32-
migrator.MigrateTestsettings(oldFilePath, newFilePath);
34+
migrator.MigrateTestSettings(oldFilePath, newFilePath);
3335
}
34-
else if (string.Equals(Path.GetExtension(oldFilePath), RunsettingsExtension))
36+
else if (string.Equals(Path.GetExtension(oldFilePath), RunSettingsExtension))
3537
{
36-
migrator.MigrateRunsettings(oldFilePath, newFilePath);
38+
migrator.MigrateRunSettings(oldFilePath, newFilePath);
3739
}
3840
else
3941
{
40-
Console.WriteLine("Valid usage:\nSettingsMigrator.exe [Full path to testsettings file/runsettings file to be migrated] [Full path to new runsettings file]");
42+
Console.WriteLine(string.Format(CultureInfo.CurrentCulture, CommandLineResources.ValidUsage));
4143
return;
4244
}
4345
}
4446

45-
const string RunsettingsExtension = ".runsettings";
46-
const string TestsettingsExtension = ".testsettings";
47+
const string RunSettingsExtension = ".runsettings";
48+
const string TestSettingsExtension = ".testsettings";
4749
}
4850
}
4951

0 commit comments

Comments
 (0)