main
cs 546 lines 19.7 KB
Raw
1 // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
3 namespace WixToolsetTest.Converters
4 {
5 using System;
6 using System.IO;
7 using System.Linq;
8 using System.Xml.Linq;
9 using WixInternal.TestSupport;
10 using WixToolset.Converters;
11 using WixToolsetTest.Converters.Mocks;
12 using Xunit;
13
14 public class ConverterFixture : BaseConverterFixture
15 {
16 private static readonly XNamespace Wix4Namespace = "http://wixtoolset.org/schemas/v4/wxs";
17
18 [Fact]
19 public void EnsuresNoDeclaration()
20 {
21 var parse = String.Join(Environment.NewLine,
22 "<?xml version='1.0' encoding='utf-8'?>",
23 "<Wix xmlns='http://wixtoolset.org/schemas/v4/wxs'>",
24 " <Fragment />",
25 "</Wix>");
26
27 var expected = new[]
28 {
29 "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">",
30 " <Fragment />",
31 "</Wix>",
32 };
33
34 var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
35
36 var messaging = new MockMessaging();
37 var converter = new WixConverter(messaging, 2, null, null);
38
39 var errors = converter.ConvertDocument(document);
40
41 var actual = UnformattedDocumentLines(document);
42
43 Assert.Equal(1, errors);
44 WixAssert.CompareLineByLine(expected, actual);
45 }
46
47 [Fact]
48 public void EnsuresDeclarationWhenIgnored()
49 {
50 var parse = String.Join(Environment.NewLine,
51 "<?xml version='1.0' encoding='utf-16'?>",
52 "<Wix xmlns='http://wixtoolset.org/schemas/v4/wxs'>",
53 " <Fragment />",
54 "</Wix>");
55
56 var expected = new[]
57 {
58 "<?xml version=\"1.0\" encoding=\"utf-16\"?>",
59 "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">",
60 " <Fragment />",
61 "</Wix>",
62 };
63
64 var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
65
66 var messaging = new MockMessaging();
67 var converter = new WixConverter(messaging, 2, ignoreErrors: new[] { "DeclarationPresent" });
68
69 var errors = converter.ConvertDocument(document);
70
71 var actual = UnformattedDocumentLines(document, omitXmlDeclaration: false);
72
73 Assert.Equal(0, errors);
74 WixAssert.CompareLineByLine(expected, actual);
75 }
76
77 [Fact]
78 public void CanConvertMainNamespace()
79 {
80 var parse = String.Join(Environment.NewLine,
81 "<?xml version='1.0' encoding='utf-8'?>",
82 "<Wix xmlns='http://schemas.microsoft.com/wix/2006/wi'>",
83 " <Fragment />",
84 "</Wix>");
85
86 var expected = new[]
87 {
88 "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">",
89 " <Fragment />",
90 "</Wix>",
91 };
92
93 var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
94
95 var messaging = new MockMessaging();
96 var converter = new WixConverter(messaging, 2, null, null);
97
98 var errors = converter.ConvertDocument(document);
99
100 var actual = UnformattedDocumentLines(document);
101
102 Assert.Equal(2, errors);
103 //Assert.Equal(Wix4Namespace, document.Root.GetDefaultNamespace());
104 WixAssert.CompareLineByLine(expected, actual);
105 }
106
107 [Fact]
108 public void CanConvertMainNamespaceFromDisk()
109 {
110 var dataFolder = TestData.Get("TestData", "FixDeclarationAndNamespace");
111
112 var expected = new[]
113 {
114 "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">",
115 " <Fragment />",
116 "</Wix>",
117 };
118
119 using (var fs = new TestDataFolderFileSystem())
120 {
121 fs.Initialize(dataFolder);
122 var path = Path.Combine(fs.BaseFolder, "FixDeclarationAndNamespace.wxs");
123
124 var messaging = new MockMessaging();
125 var converter = new WixConverter(messaging, 2, null, null);
126
127 var errors = converter.ConvertFile(path, true);
128
129 var messages = messaging.Messages.Select(m => $"{Path.GetFileName(m.SourceLineNumbers.FileName)}({m.SourceLineNumbers.LineNumber}) {m.ToString()}").ToArray();
130 WixAssert.CompareLineByLine(new[]
131 {
132 "FixDeclarationAndNamespace.wxs() [Converted] This file contains an XML declaration on the first line. (DeclarationPresent)",
133 "FixDeclarationAndNamespace.wxs(1) [Converted] The namespace 'http://schemas.microsoft.com/wix/2006/wi' is out of date. It must be 'http://wixtoolset.org/schemas/v4/wxs'. (XmlnsValueWrong)"
134 }, messages);
135
136 var actual = File.ReadAllLines(path);
137 WixAssert.CompareLineByLine(expected, actual);
138 }
139 }
140
141 [Fact]
142 public void CanConvertNamedMainNamespace()
143 {
144 var parse = String.Join(Environment.NewLine,
145 "<?xml version='1.0' encoding='utf-8'?>",
146 "<w:Wix xmlns:w='http://schemas.microsoft.com/wix/2006/wi'>",
147 " <w:Fragment />",
148 "</w:Wix>");
149
150 var expected = new[]
151 {
152 "<w:Wix xmlns:w=\"http://wixtoolset.org/schemas/v4/wxs\">",
153 " <w:Fragment />",
154 "</w:Wix>",
155 };
156
157 var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
158
159 var messaging = new MockMessaging();
160 var converter = new WixConverter(messaging, 2, null, null);
161
162 var errors = converter.ConvertDocument(document);
163
164 var actual = UnformattedDocumentLines(document);
165
166 Assert.Equal(2, errors);
167 WixAssert.CompareLineByLine(expected, actual);
168 Assert.Equal(Wix4Namespace, document.Root.GetNamespaceOfPrefix("w"));
169 }
170
171 [Fact]
172 public void CanConvertNonWixDefaultNamespace()
173 {
174 var parse = String.Join(Environment.NewLine,
175 "<?xml version='1.0' encoding='utf-8'?>",
176 "<w:Wix xmlns:w='http://schemas.microsoft.com/wix/2006/wi' xmlns='http://schemas.microsoft.com/wix/UtilExtension'>",
177 " <w:Fragment>",
178 " <Test />",
179 " </w:Fragment>",
180 "</w:Wix>");
181
182 var expected = new[]
183 {
184 "<w:Wix xmlns:w=\"http://wixtoolset.org/schemas/v4/wxs\" xmlns=\"http://wixtoolset.org/schemas/v4/wxs/util\">",
185 " <w:Fragment>",
186 " <Test />",
187 " </w:Fragment>",
188 "</w:Wix>",
189 };
190
191 var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
192
193 var messaging = new MockMessaging();
194 var converter = new WixConverter(messaging, 2, null, null);
195
196 var errors = converter.ConvertDocument(document);
197
198 var actual = UnformattedDocumentLines(document);
199
200 WixAssert.CompareLineByLine(expected, actual);
201 Assert.Equal(3, errors);
202 Assert.Equal(Wix4Namespace, document.Root.GetNamespaceOfPrefix("w"));
203 Assert.Equal("http://wixtoolset.org/schemas/v4/wxs/util", document.Root.GetDefaultNamespace());
204 }
205
206 [Fact]
207 public void CanRemoveUnusedNamespaces()
208 {
209 var parse = String.Join(Environment.NewLine,
210 "<?xml version='1.0' encoding='utf-8'?>",
211 "<Wix xmlns='http://schemas.microsoft.com/wix/2006/wi' xmlns:util='http://schemas.microsoft.com/wix/UtilExtension'>",
212 " <Fragment />",
213 "</Wix>");
214
215 var expected = new[]
216 {
217 "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">",
218 " <Fragment />",
219 "</Wix>",
220 };
221
222 var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
223
224 var messaging = new MockMessaging();
225 var converter = new WixConverter(messaging, 2, null, null);
226
227 var errors = converter.ConvertDocument(document);
228
229 var actual = UnformattedDocumentLines(document);
230
231 Assert.Equal(4, errors);
232 WixAssert.CompareLineByLine(expected, actual);
233 Assert.Equal(Wix4Namespace, document.Root.GetDefaultNamespace());
234 }
235
236 [Fact]
237 public void CanConvertMissingWixNamespace()
238 {
239 var parse = String.Join(Environment.NewLine,
240 "<?xml version='1.0' encoding='utf-8'?>",
241 "<Wix>",
242 " <Fragment />",
243 "</Wix>");
244
245 var expected = new[]
246 {
247 "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">",
248 " <Fragment />",
249 "</Wix>",
250 };
251
252 var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
253
254 var messaging = new MockMessaging();
255 var converter = new WixConverter(messaging, 2, null, null);
256
257 var errors = converter.ConvertDocument(document);
258
259 var actual = UnformattedDocumentLines(document);
260
261 Assert.Equal(2, errors);
262 WixAssert.CompareLineByLine(expected, actual);
263 Assert.Equal(Wix4Namespace, document.Root.GetDefaultNamespace());
264 }
265
266 [Fact]
267 public void CanConvertMissingIncludeNamespace()
268 {
269 var parse = String.Join(Environment.NewLine,
270 "<?xml version='1.0' encoding='utf-8'?>",
271 "<Include>",
272 " <?define Version = 1.2.3 ?>",
273 " <Fragment>",
274 " <DirectoryRef Id='TARGETDIR'>",
275 " <Directory Id='ANOTHERDIR' Name='Another' />",
276 " </DirectoryRef>",
277 " </Fragment>",
278 "</Include>");
279
280 var expected = new[]
281 {
282 "<Include xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">",
283 " <?define Version = 1.2.3 ?>",
284 " <Fragment>",
285 " <Directory Id=\"ANOTHERDIR\" Name=\"Another\" />",
286 " </Fragment>",
287 "</Include>",
288 };
289
290 var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
291
292 var messaging = new MockMessaging();
293 var converter = new WixConverter(messaging, 2, null, null);
294
295 var errors = converter.ConvertDocument(document);
296
297 var actual = UnformattedDocumentLines(document);
298
299 WixAssert.CompareLineByLine(expected, actual);
300 Assert.Equal(Wix4Namespace, document.Root.GetDefaultNamespace());
301 Assert.Equal(4, errors);
302 }
303
304 [Fact]
305 public void CanConvertAnonymousFile()
306 {
307 var parse = String.Join(Environment.NewLine,
308 "<?xml version='1.0' encoding='utf-8'?>",
309 "<Wix xmlns='http://schemas.microsoft.com/wix/2006/wi'>",
310 " <File Source='path\\to\\foo.txt' />",
311 "</Wix>");
312
313 var expected = new[]
314 {
315 "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">",
316 " <File Id=\"foo.txt\" Source=\"path\\to\\foo.txt\" />",
317 "</Wix>",
318 };
319
320 var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
321
322 var messaging = new MockMessaging();
323 var converter = new WixConverter(messaging, 2, null, null);
324
325 var errors = converter.ConvertDocument(document);
326
327 var actual = UnformattedDocumentLines(document);
328
329 Assert.Equal(3, errors);
330 WixAssert.CompareLineByLine(expected, actual);
331 }
332
333 [Fact]
334 public void CanConvertShortNameDirectoryWithoutName()
335 {
336 var parse = String.Join(Environment.NewLine,
337 "<?xml version='1.0' encoding='utf-8'?>",
338 "<Wix xmlns='http://wixtoolset.org/schemas/v4/wxs'>",
339 " <Directory ShortName='iamshort' />",
340 "</Wix>");
341
342 var expected = new[]
343 {
344 "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">",
345 " <Directory Name=\"iamshort\" />",
346 "</Wix>",
347 };
348
349 var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
350
351 var messaging = new MockMessaging();
352 var converter = new WixConverter(messaging, 2, null, null);
353
354 var errors = converter.ConvertDocument(document);
355
356 var actual = UnformattedDocumentLines(document);
357
358 Assert.Equal(2, errors);
359 WixAssert.CompareLineByLine(expected, actual);
360 }
361
362 [Fact]
363 public void CanConvertCatalogElement()
364 {
365 var parse = String.Join(Environment.NewLine,
366 "<Wix xmlns='http://wixtoolset.org/schemas/v4/wxs'>",
367 " <Catalog Id='idCatalog' SourceFile='path\\to\\catalog.cat' />",
368 "</Wix>");
369
370 var expected = new[]
371 {
372 "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">",
373 " ",
374 "</Wix>",
375 };
376
377 var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
378
379 var messaging = new MockMessaging();
380 var converter = new WixConverter(messaging, 2, null, null);
381
382 var errors = converter.ConvertDocument(document);
383
384 var actual = UnformattedDocumentLines(document);
385
386 Assert.Equal(1, errors);
387 WixAssert.CompareLineByLine(expected, actual);
388 }
389
390 [Fact]
391 public void CanConvertSuppressSignatureVerificationNo()
392 {
393 var parse = String.Join(Environment.NewLine,
394 "<Wix xmlns='http://wixtoolset.org/schemas/v4/wxs'>",
395 " <MsiPackage SuppressSignatureVerification='no' />",
396 "</Wix>");
397
398 var expected = new[]
399 {
400 "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">",
401 " <MsiPackage />",
402 "</Wix>",
403 };
404
405 var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
406
407 var messaging = new MockMessaging();
408 var converter = new WixConverter(messaging, 2, null, null);
409
410 var errors = converter.ConvertDocument(document);
411
412 var actual = UnformattedDocumentLines(document);
413
414 Assert.Equal(1, errors);
415 WixAssert.CompareLineByLine(expected, actual);
416 }
417
418 [Fact]
419 public void CanConvertSuppressSignatureVerificationYes()
420 {
421 var parse = String.Join(Environment.NewLine,
422 "<Wix xmlns='http://wixtoolset.org/schemas/v4/wxs'>",
423 " <Payload SuppressSignatureVerification='yes' />",
424 "</Wix>");
425
426 var expected = new[]
427 {
428 "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">",
429 " <Payload />",
430 "</Wix>",
431 };
432
433 var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
434
435 var messaging = new MockMessaging();
436 var converter = new WixConverter(messaging, 2, null, null);
437
438 var errors = converter.ConvertDocument(document);
439
440 var actual = UnformattedDocumentLines(document);
441
442 Assert.Equal(1, errors);
443 WixAssert.CompareLineByLine(expected, actual);
444 }
445
446 [Fact]
447 public void CantConvertVerbTarget()
448 {
449 var parse = String.Join(Environment.NewLine,
450 "<Wix xmlns='http://schemas.microsoft.com/wix/2006/wi'>",
451 " <Verb Target='anything' />",
452 "</Wix>");
453
454 var expected = new[]
455 {
456 "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">",
457 " <Verb Target=\"anything\" />",
458 "</Wix>",
459 };
460
461 var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
462
463 var messaging = new MockMessaging();
464 var converter = new WixConverter(messaging, 2, null, null);
465
466 var errors = converter.ConvertDocument(document);
467
468 var actual = UnformattedDocumentLines(document);
469
470 Assert.Equal(2, errors);
471 WixAssert.CompareLineByLine(expected, actual);
472 }
473
474 [Fact]
475 public void CanConvertDeprecatedPrefix()
476 {
477 var parse = String.Join(Environment.NewLine,
478 "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">",
479 "<Fragment>",
480 "<ComponentGroup Id=\"$(loc.Variable)\" />",
481 "<ComponentGroup Id=\"$$(loc.Variable)\" />",
482 "<ComponentGroup Id=\"$$$(loc.Variable)\" />",
483 "<ComponentGroup Id=\"$$$$(loc.Variable)\" />",
484 "<ComponentGroup Id=\"$$$$$(loc.Variable)\" />",
485 "</Fragment>",
486 "</Wix>");
487
488 var expected = new[]
489 {
490 "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">",
491 "<Fragment>",
492 "<ComponentGroup Id=\"!(loc.Variable)\" />",
493 "<ComponentGroup Id=\"$$(loc.Variable)\" />",
494 "<ComponentGroup Id=\"$$!(loc.Variable)\" />",
495 "<ComponentGroup Id=\"$$$$(loc.Variable)\" />",
496 "<ComponentGroup Id=\"$$$$!(loc.Variable)\" />",
497 "</Fragment>",
498 "</Wix>",
499 };
500
501 var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
502
503 var messaging = new MockMessaging();
504 var converter = new WixConverter(messaging, 2, null, null);
505
506 var errors = converter.ConvertDocument(document);
507
508 var actual = UnformattedDocumentLines(document);
509
510 Assert.Equal(3, errors);
511 WixAssert.CompareLineByLine(expected, actual);
512 }
513
514 [Fact]
515 public void CantConvertStandardCustomActionRescheduling()
516 {
517 var parse = String.Join(Environment.NewLine,
518 "<Wix xmlns='http://schemas.microsoft.com/wix/2006/wi'>",
519 " <InstallExecuteSequence>",
520 " <Custom Action='WixCloseApplications' Before='StopServices' />",
521 " </InstallExecuteSequence>",
522 "</Wix>");
523
524 var expected = new[]
525 {
526 "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">",
527 " <InstallExecuteSequence>",
528 " <Custom Action=\"WixCloseApplications\" Before=\"StopServices\" />",
529 " </InstallExecuteSequence>",
530 "</Wix>",
531 };
532
533 var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
534
535 var messaging = new MockMessaging();
536 var converter = new WixConverter(messaging, 2, null, null);
537
538 var errors = converter.ConvertDocument(document);
539
540 var actual = UnformattedDocumentLines(document);
541
542 Assert.Equal(2, errors);
543 WixAssert.CompareLineByLine(expected, actual);
544 }
545 }
546 }