@@ -10,143 +10,155 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine
10
10
public class Migrator
11
11
{
12
12
/// <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 .
14
14
/// </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 )
18
18
{
19
- string testsettingsPath = null ;
20
- using ( XmlTextReader reader = new XmlTextReader ( oldRunsettingsPath ) )
19
+ string testSettingsPath = null ;
20
+ using ( XmlTextReader reader = new XmlTextReader ( oldRunSettingsPath ) )
21
21
{
22
22
reader . Namespaces = false ;
23
23
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 ;
27
27
28
- var testsettingsNode = root . SelectSingleNode ( @"/RunSettings/MSTest/SettingsFile" ) ;
28
+ var testSettingsNode = root . SelectSingleNode ( @"/RunSettings/MSTest/SettingsFile" ) ;
29
29
30
- if ( testsettingsNode != null )
30
+ if ( testSettingsNode != null )
31
31
{
32
- testsettingsPath = testsettingsNode . InnerText ;
32
+ testSettingsPath = testSettingsNode . InnerText ;
33
33
}
34
- if ( ! string . IsNullOrWhiteSpace ( testsettingsPath ) )
34
+ if ( ! string . IsNullOrWhiteSpace ( testSettingsPath ) )
35
35
{
36
- // Expand path relative to runsettings location.
37
- if ( ! Path . IsPathRooted ( testsettingsPath ) )
36
+ // Expand path relative to runSettings location.
37
+ if ( ! Path . IsPathRooted ( testSettingsPath ) )
38
38
{
39
- testsettingsPath = Path . Combine ( oldRunsettingsPath , testsettingsPath ) ;
39
+ testSettingsPath = Path . Combine ( oldRunSettingsPath , testSettingsPath ) ;
40
40
}
41
41
42
- MigrateTestsettings ( testsettingsPath , newRunsettingsPath , File . ReadAllText ( oldRunsettingsPath ) ) ;
42
+ MigrateTestSettingsNodesToRunSettings ( testSettingsPath , runSettingsXmlDoc ) ;
43
+
44
+ runSettingsXmlDoc . Save ( newRunSettingsPath ) ;
43
45
}
44
46
else
45
47
{
46
- Console . WriteLine ( "Runsettings does not contain an embedded testsettings , not migrating." ) ;
48
+ Console . WriteLine ( "RunSettings does not contain an embedded testSettings , not migrating." ) ;
47
49
}
48
50
}
49
51
}
50
52
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
+
51
64
/// <summary>
52
- /// Given a testsettings , converts it to runsettings
65
+ /// Given a testSettings , converts it to runSettings
53
66
/// </summary>
54
- /// <param name="testsettingsPath "></param>
55
- /// <param name="newRunsettingsPath "></param>
67
+ /// <param name="testSettingsPath "></param>
68
+ /// <param name="newRunSettingsPath "></param>
56
69
/// <param name="oldRunSettingsContent"></param>
57
- public void MigrateTestsettings ( string testsettingsPath , string newRunsettingsPath , string oldRunSettingsContent = null )
70
+ public void MigrateTestSettingsNodesToRunSettings ( string testSettingsPath , XmlDocument runSettingsXmlDoc )
58
71
{
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 )
60
76
{
61
- reader . Namespaces = false ;
77
+ testTimeout = testSettingsNodes . Timeout . Attributes [ TestTimeoutAttributeName ] . Value ;
78
+ }
62
79
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
+ }
66
85
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
+ }
82
91
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 ) ;
88
94
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
+ }
94
100
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
+ }
101
107
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
+ }
104
113
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
+ }
110
120
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 ( ) ;
117
124
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 ;
123
128
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 ;
129
132
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" ) ;
131
142
}
143
+
144
+ return testSettingsNodes ;
132
145
}
133
146
134
147
/// <summary>
135
- /// Removes the embedded testsettings node if present.
148
+ /// Removes the embedded testSettings node if present.
136
149
/// </summary>
137
150
/// <param name="newXmlDoc"></param>
138
- private void RemoveEmbeddedTestsettings ( XmlDocument newXmlDoc )
151
+ private void RemoveEmbeddedTestSettings ( XmlDocument newXmlDoc )
139
152
{
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 )
142
155
{
143
- testsettingsNode . ParentNode . RemoveChild ( testsettingsNode ) ;
156
+ testSettingsNode . ParentNode . RemoveChild ( testSettingsNode ) ;
144
157
}
145
158
}
146
159
147
-
148
160
/// <summary>
149
- /// Adds the legacy nodes to runsettings xml.
161
+ /// Adds the legacy nodes to runSettings xml.
150
162
/// </summary>
151
163
/// <param name="deploymentNode"></param>
152
164
/// <param name="scriptnode"></param>
@@ -155,7 +167,7 @@ private void RemoveEmbeddedTestsettings(XmlDocument newXmlDoc)
155
167
/// <param name="hostsNode"></param>
156
168
/// <param name="parallelTestCount"></param>
157
169
/// <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 )
159
171
{
160
172
//Remove if the legacy node already exists.
161
173
var legacyNode = newXmlDoc . DocumentElement . SelectSingleNode ( @"/RunSettings/LegacySettings" ) ;
@@ -166,17 +178,17 @@ private void AddLegacyNodes(XmlNode deploymentNode, XmlNode scriptnode, string t
166
178
167
179
legacyNode = newXmlDoc . CreateNode ( XmlNodeType . Element , LegacySettingsNodeName , null ) ;
168
180
169
- if ( deploymentNode != null )
181
+ if ( testSettingsNodes . Deployment != null )
170
182
{
171
- legacyNode . AppendChild ( newXmlDoc . ImportNode ( deploymentNode , deep : true ) ) ;
183
+ legacyNode . AppendChild ( newXmlDoc . ImportNode ( testSettingsNodes . Deployment , deep : true ) ) ;
172
184
}
173
- if ( scriptnode != null )
185
+ if ( testSettingsNodes . Script != null )
174
186
{
175
- legacyNode . AppendChild ( newXmlDoc . ImportNode ( scriptnode , deep : true ) ) ;
187
+ legacyNode . AppendChild ( newXmlDoc . ImportNode ( testSettingsNodes . Script , deep : true ) ) ;
176
188
}
177
189
178
190
// 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 )
180
192
{
181
193
var newExecutionNode = newXmlDoc . CreateNode ( XmlNodeType . Element , ExecutionNodeName , null ) ;
182
194
@@ -194,24 +206,23 @@ private void AddLegacyNodes(XmlNode deploymentNode, XmlNode scriptnode, string t
194
206
newTimeoutsNode . Attributes . Append ( testtimeoutattribute ) ;
195
207
newExecutionNode . AppendChild ( newXmlDoc . ImportNode ( newTimeoutsNode , deep : true ) ) ;
196
208
}
197
- if ( hostsNode != null )
209
+ if ( testSettingsNodes . Hosts != null )
198
210
{
199
- newExecutionNode . AppendChild ( newXmlDoc . ImportNode ( hostsNode , deep : true ) ) ;
211
+ newExecutionNode . AppendChild ( newXmlDoc . ImportNode ( testSettingsNodes . Hosts , deep : true ) ) ;
200
212
}
201
- if ( assemblyresolutionNode != null )
213
+ if ( testSettingsNodes . UnitTestConfig != null )
202
214
{
203
215
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 ) ) ;
205
217
newExecutionNode . AppendChild ( newXmlDoc . ImportNode ( testTypeSpecificNode , deep : true ) ) ;
206
218
}
207
219
legacyNode . AppendChild ( newXmlDoc . ImportNode ( newExecutionNode , deep : true ) ) ;
208
220
}
209
221
newXmlDoc . DocumentElement . AppendChild ( legacyNode ) ;
210
222
}
211
223
212
-
213
224
/// <summary>
214
- /// Adds the datacollector nodes to the runsettings xml.
225
+ /// Adds the datacollector nodes to the runSettings xml.
215
226
/// </summary>
216
227
/// <param name="oldDatacollectorNodes"></param>
217
228
/// <param name="newXmlDoc"></param>
@@ -269,7 +280,7 @@ private void AddRunTimeoutNode(string runTimeout, XmlDocument newXmlDoc)
269
280
const string TestSessionTimeoutNodeName = "TestSessionTimeout" ;
270
281
const string DataCollectionRunSettingsNodeName = "DataCollectionRunSettings" ;
271
282
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\" ?>" +
273
284
"<RunSettings></RunSettings>" ;
274
285
}
275
286
}
0 commit comments