diff options
Diffstat (limited to 'src/test/WixToolsetTest.WixCop/ConverterFixture.cs')
-rw-r--r-- | src/test/WixToolsetTest.WixCop/ConverterFixture.cs | 452 |
1 files changed, 452 insertions, 0 deletions
diff --git a/src/test/WixToolsetTest.WixCop/ConverterFixture.cs b/src/test/WixToolsetTest.WixCop/ConverterFixture.cs new file mode 100644 index 00000000..86931d5a --- /dev/null +++ b/src/test/WixToolsetTest.WixCop/ConverterFixture.cs | |||
@@ -0,0 +1,452 @@ | |||
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.WixCop | ||
4 | { | ||
5 | using System; | ||
6 | using System.IO; | ||
7 | using System.Text; | ||
8 | using System.Xml.Linq; | ||
9 | using WixToolset.Data; | ||
10 | using WixToolset.Extensibility; | ||
11 | using WixToolset.Extensibility.Services; | ||
12 | using WixToolset.Tools.WixCop; | ||
13 | using Xunit; | ||
14 | |||
15 | public class ConverterFixture | ||
16 | { | ||
17 | private static readonly XNamespace Wix4Namespace = "http://wixtoolset.org/schemas/v4/wxs"; | ||
18 | |||
19 | [Fact] | ||
20 | public void EnsuresDeclaration() | ||
21 | { | ||
22 | var parse = String.Join(Environment.NewLine, | ||
23 | "<Wix xmlns='http://wixtoolset.org/schemas/v4/wxs'>", | ||
24 | " <Fragment />", | ||
25 | "</Wix>"); | ||
26 | |||
27 | var expected = String.Join(Environment.NewLine, | ||
28 | "<?xml version=\"1.0\" encoding=\"utf-16\"?>", | ||
29 | "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">", | ||
30 | " <Fragment />", | ||
31 | "</Wix>"); | ||
32 | |||
33 | var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); | ||
34 | |||
35 | var messaging = new DummyMessaging(); | ||
36 | var converter = new Converter(messaging, 2, null, null); | ||
37 | |||
38 | var errors = converter.ConvertDocument(document); | ||
39 | |||
40 | var actual = UnformattedDocumentString(document); | ||
41 | |||
42 | Assert.Equal(1, errors); | ||
43 | Assert.Equal(expected, actual); | ||
44 | } | ||
45 | |||
46 | [Fact] | ||
47 | public void EnsuresUtf8Declaration() | ||
48 | { | ||
49 | var parse = String.Join(Environment.NewLine, | ||
50 | "<?xml version='1.0'?>", | ||
51 | "<Wix xmlns='http://wixtoolset.org/schemas/v4/wxs'>", | ||
52 | " <Fragment />", | ||
53 | "</Wix>"); | ||
54 | |||
55 | var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); | ||
56 | |||
57 | var messaging = new DummyMessaging(); | ||
58 | var converter = new Converter(messaging, 4, null, null); | ||
59 | |||
60 | var errors = converter.ConvertDocument(document); | ||
61 | |||
62 | Assert.Equal(1, errors); | ||
63 | Assert.Equal("1.0", document.Declaration.Version); | ||
64 | Assert.Equal("utf-8", document.Declaration.Encoding); | ||
65 | } | ||
66 | |||
67 | [Fact] | ||
68 | public void CanFixWhitespace() | ||
69 | { | ||
70 | var parse = String.Join(Environment.NewLine, | ||
71 | "<?xml version='1.0' encoding='utf-8'?>", | ||
72 | "<Wix xmlns='http://wixtoolset.org/schemas/v4/wxs'>", | ||
73 | " <Fragment>", | ||
74 | " <Property Id='Prop'", | ||
75 | " Value='Val'>", | ||
76 | " </Property>", | ||
77 | " </Fragment>", | ||
78 | "</Wix>"); | ||
79 | |||
80 | var expected = String.Join(Environment.NewLine, | ||
81 | "<?xml version=\"1.0\" encoding=\"utf-16\"?>", | ||
82 | "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">", | ||
83 | " <Fragment>", | ||
84 | " <Property Id=\"Prop\" Value=\"Val\" />", | ||
85 | " </Fragment>", | ||
86 | "</Wix>"); | ||
87 | |||
88 | var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); | ||
89 | |||
90 | var messaging = new DummyMessaging(); | ||
91 | var converter = new Converter(messaging, 4, null, null); | ||
92 | |||
93 | var errors = converter.ConvertDocument(document); | ||
94 | |||
95 | var actual = UnformattedDocumentString(document); | ||
96 | |||
97 | Assert.Equal(4, errors); | ||
98 | Assert.Equal(expected, actual); | ||
99 | } | ||
100 | |||
101 | [Fact] | ||
102 | public void CanFixCdataWhitespace() | ||
103 | { | ||
104 | var parse = String.Join(Environment.NewLine, | ||
105 | "<?xml version='1.0' encoding='utf-8'?>", | ||
106 | "<Wix xmlns='http://wixtoolset.org/schemas/v4/wxs'>", | ||
107 | " <Fragment>", | ||
108 | " <Property Id='Prop'>", | ||
109 | " <![CDATA[1<2]]>", | ||
110 | " </Property>", | ||
111 | " </Fragment>", | ||
112 | "</Wix>"); | ||
113 | |||
114 | var expected = String.Join(Environment.NewLine, | ||
115 | "<?xml version=\"1.0\" encoding=\"utf-16\"?>", | ||
116 | "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">", | ||
117 | " <Fragment>", | ||
118 | " <Property Id=\"Prop\"><![CDATA[1<2]]></Property>", | ||
119 | " </Fragment>", | ||
120 | "</Wix>"); | ||
121 | |||
122 | var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); | ||
123 | |||
124 | var messaging = new DummyMessaging(); | ||
125 | var converter = new Converter(messaging, 2, null, null); | ||
126 | |||
127 | var errors = converter.ConvertDocument(document); | ||
128 | |||
129 | var actual = UnformattedDocumentString(document); | ||
130 | |||
131 | Assert.Equal(expected, actual); | ||
132 | Assert.Equal(2, errors); | ||
133 | } | ||
134 | |||
135 | [Fact] | ||
136 | public void CanFixCdataWithWhitespace() | ||
137 | { | ||
138 | var parse = String.Join(Environment.NewLine, | ||
139 | "<?xml version='1.0' encoding='utf-8'?>", | ||
140 | "<Wix xmlns='http://wixtoolset.org/schemas/v4/wxs'>", | ||
141 | " <Fragment>", | ||
142 | " <Property Id='Prop'>", | ||
143 | " <![CDATA[", | ||
144 | " 1<2", | ||
145 | " ]]>", | ||
146 | " </Property>", | ||
147 | " </Fragment>", | ||
148 | "</Wix>"); | ||
149 | |||
150 | var expected = String.Join(Environment.NewLine, | ||
151 | "<?xml version=\"1.0\" encoding=\"utf-16\"?>", | ||
152 | "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">", | ||
153 | " <Fragment>", | ||
154 | " <Property Id=\"Prop\"><![CDATA[1<2]]></Property>", | ||
155 | " </Fragment>", | ||
156 | "</Wix>"); | ||
157 | |||
158 | var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); | ||
159 | |||
160 | var messaging = new DummyMessaging(); | ||
161 | var converter = new Converter(messaging, 2, null, null); | ||
162 | |||
163 | var errors = converter.ConvertDocument(document); | ||
164 | |||
165 | var actual = UnformattedDocumentString(document); | ||
166 | |||
167 | Assert.Equal(expected, actual); | ||
168 | Assert.Equal(2, errors); | ||
169 | } | ||
170 | |||
171 | [Fact] | ||
172 | public void CanConvertMainNamespace() | ||
173 | { | ||
174 | var parse = String.Join(Environment.NewLine, | ||
175 | "<?xml version='1.0' encoding='utf-8'?>", | ||
176 | "<Wix xmlns='http://schemas.microsoft.com/wix/2006/wi'>", | ||
177 | " <Fragment />", | ||
178 | "</Wix>"); | ||
179 | |||
180 | var expected = String.Join(Environment.NewLine, | ||
181 | "<?xml version=\"1.0\" encoding=\"utf-16\"?>", | ||
182 | "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">", | ||
183 | " <Fragment />", | ||
184 | "</Wix>"); | ||
185 | |||
186 | var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); | ||
187 | |||
188 | var messaging = new DummyMessaging(); | ||
189 | var converter = new Converter(messaging, 2, null, null); | ||
190 | |||
191 | var errors = converter.ConvertDocument(document); | ||
192 | |||
193 | var actual = UnformattedDocumentString(document); | ||
194 | |||
195 | Assert.Equal(1, errors); | ||
196 | //Assert.Equal(Wix4Namespace, document.Root.GetDefaultNamespace()); | ||
197 | Assert.Equal(expected, actual); | ||
198 | } | ||
199 | |||
200 | [Fact] | ||
201 | public void CanConvertNamedMainNamespace() | ||
202 | { | ||
203 | var parse = String.Join(Environment.NewLine, | ||
204 | "<?xml version='1.0' encoding='utf-8'?>", | ||
205 | "<w:Wix xmlns:w='http://schemas.microsoft.com/wix/2006/wi'>", | ||
206 | " <w:Fragment />", | ||
207 | "</w:Wix>"); | ||
208 | |||
209 | var expected = String.Join(Environment.NewLine, | ||
210 | "<?xml version=\"1.0\" encoding=\"utf-16\"?>", | ||
211 | "<w:Wix xmlns:w=\"http://wixtoolset.org/schemas/v4/wxs\">", | ||
212 | " <w:Fragment />", | ||
213 | "</w:Wix>"); | ||
214 | |||
215 | var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); | ||
216 | |||
217 | var messaging = new DummyMessaging(); | ||
218 | var converter = new Converter(messaging, 2, null, null); | ||
219 | |||
220 | var errors = converter.ConvertDocument(document); | ||
221 | |||
222 | var actual = UnformattedDocumentString(document); | ||
223 | |||
224 | Assert.Equal(1, errors); | ||
225 | Assert.Equal(expected, actual); | ||
226 | Assert.Equal(Wix4Namespace, document.Root.GetNamespaceOfPrefix("w")); | ||
227 | } | ||
228 | |||
229 | [Fact] | ||
230 | public void CanConvertNonWixDefaultNamespace() | ||
231 | { | ||
232 | var parse = String.Join(Environment.NewLine, | ||
233 | "<?xml version='1.0' encoding='utf-8'?>", | ||
234 | "<w:Wix xmlns:w='http://schemas.microsoft.com/wix/2006/wi' xmlns='http://schemas.microsoft.com/wix/UtilExtension'>", | ||
235 | " <w:Fragment>", | ||
236 | " <Test />", | ||
237 | " </w:Fragment>", | ||
238 | "</w:Wix>"); | ||
239 | |||
240 | var expected = String.Join(Environment.NewLine, | ||
241 | "<?xml version=\"1.0\" encoding=\"utf-16\"?>", | ||
242 | "<w:Wix xmlns:w=\"http://wixtoolset.org/schemas/v4/wxs\" xmlns=\"http://wixtoolset.org/schemas/v4/wxs/util\">", | ||
243 | " <w:Fragment>", | ||
244 | " <Test />", | ||
245 | " </w:Fragment>", | ||
246 | "</w:Wix>"); | ||
247 | |||
248 | var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); | ||
249 | |||
250 | var messaging = new DummyMessaging(); | ||
251 | var converter = new Converter(messaging, 2, null, null); | ||
252 | |||
253 | var errors = converter.ConvertDocument(document); | ||
254 | |||
255 | var actual = UnformattedDocumentString(document); | ||
256 | |||
257 | Assert.Equal(expected, actual); | ||
258 | Assert.Equal(2, errors); | ||
259 | Assert.Equal(Wix4Namespace, document.Root.GetNamespaceOfPrefix("w")); | ||
260 | Assert.Equal("http://wixtoolset.org/schemas/v4/wxs/util", document.Root.GetDefaultNamespace()); | ||
261 | } | ||
262 | |||
263 | [Fact] | ||
264 | public void CanConvertExtensionNamespace() | ||
265 | { | ||
266 | var parse = String.Join(Environment.NewLine, | ||
267 | "<?xml version='1.0' encoding='utf-8'?>", | ||
268 | "<Wix xmlns='http://schemas.microsoft.com/wix/2006/wi' xmlns:util='http://schemas.microsoft.com/wix/UtilExtension'>", | ||
269 | " <Fragment />", | ||
270 | "</Wix>"); | ||
271 | |||
272 | var expected = String.Join(Environment.NewLine, | ||
273 | "<?xml version=\"1.0\" encoding=\"utf-16\"?>", | ||
274 | "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\" xmlns:util=\"http://wixtoolset.org/schemas/v4/wxs/util\">", | ||
275 | " <Fragment />", | ||
276 | "</Wix>"); | ||
277 | |||
278 | var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); | ||
279 | |||
280 | var messaging = new DummyMessaging(); | ||
281 | var converter = new Converter(messaging, 2, null, null); | ||
282 | |||
283 | var errors = converter.ConvertDocument(document); | ||
284 | |||
285 | var actual = UnformattedDocumentString(document); | ||
286 | |||
287 | Assert.Equal(2, errors); | ||
288 | Assert.Equal(expected, actual); | ||
289 | Assert.Equal(Wix4Namespace, document.Root.GetDefaultNamespace()); | ||
290 | } | ||
291 | |||
292 | [Fact] | ||
293 | public void CanConvertMissingNamespace() | ||
294 | { | ||
295 | var parse = String.Join(Environment.NewLine, | ||
296 | "<?xml version='1.0' encoding='utf-8'?>", | ||
297 | "<Wix>", | ||
298 | " <Fragment />", | ||
299 | "</Wix>"); | ||
300 | |||
301 | var expected = String.Join(Environment.NewLine, | ||
302 | "<?xml version=\"1.0\" encoding=\"utf-16\"?>", | ||
303 | "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">", | ||
304 | " <Fragment />", | ||
305 | "</Wix>"); | ||
306 | |||
307 | var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); | ||
308 | |||
309 | var messaging = new DummyMessaging(); | ||
310 | var converter = new Converter(messaging, 2, null, null); | ||
311 | |||
312 | var errors = converter.ConvertDocument(document); | ||
313 | |||
314 | var actual = UnformattedDocumentString(document); | ||
315 | |||
316 | Assert.Equal(1, errors); | ||
317 | Assert.Equal(expected, actual); | ||
318 | Assert.Equal(Wix4Namespace, document.Root.GetDefaultNamespace()); | ||
319 | } | ||
320 | |||
321 | [Fact] | ||
322 | public void CanConvertAnonymousFile() | ||
323 | { | ||
324 | var parse = String.Join(Environment.NewLine, | ||
325 | "<?xml version='1.0' encoding='utf-8'?>", | ||
326 | "<Wix xmlns='http://wixtoolset.org/schemas/v4/wxs'>", | ||
327 | " <File Source='path\\to\\foo.txt' />", | ||
328 | "</Wix>"); | ||
329 | |||
330 | var expected = String.Join(Environment.NewLine, | ||
331 | "<?xml version=\"1.0\" encoding=\"utf-16\"?>", | ||
332 | "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">", | ||
333 | " <File Id=\"foo.txt\" Source=\"path\\to\\foo.txt\" />", | ||
334 | "</Wix>"); | ||
335 | |||
336 | var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); | ||
337 | |||
338 | var messaging = new DummyMessaging(); | ||
339 | var converter = new Converter(messaging, 2, null, null); | ||
340 | |||
341 | var errors = converter.ConvertDocument(document); | ||
342 | |||
343 | var actual = UnformattedDocumentString(document); | ||
344 | |||
345 | Assert.Equal(1, errors); | ||
346 | Assert.Equal(expected, actual); | ||
347 | } | ||
348 | |||
349 | [Fact] | ||
350 | public void CanConvertSuppressSignatureValidationNo() | ||
351 | { | ||
352 | var parse = String.Join(Environment.NewLine, | ||
353 | "<?xml version='1.0' encoding='utf-8'?>", | ||
354 | "<Wix xmlns='http://wixtoolset.org/schemas/v4/wxs'>", | ||
355 | " <MsiPackage SuppressSignatureValidation='no' />", | ||
356 | "</Wix>"); | ||
357 | |||
358 | var expected = String.Join(Environment.NewLine, | ||
359 | "<?xml version=\"1.0\" encoding=\"utf-16\"?>", | ||
360 | "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">", | ||
361 | " <MsiPackage EnableSignatureValidation=\"yes\" />", | ||
362 | "</Wix>"); | ||
363 | |||
364 | var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); | ||
365 | |||
366 | var messaging = new DummyMessaging(); | ||
367 | var converter = new Converter(messaging, 2, null, null); | ||
368 | |||
369 | var errors = converter.ConvertDocument(document); | ||
370 | |||
371 | var actual = UnformattedDocumentString(document); | ||
372 | |||
373 | Assert.Equal(1, errors); | ||
374 | Assert.Equal(expected, actual); | ||
375 | } | ||
376 | |||
377 | [Fact] | ||
378 | public void CanConvertSuppressSignatureValidationYes() | ||
379 | { | ||
380 | var parse = String.Join(Environment.NewLine, | ||
381 | "<?xml version='1.0' encoding='utf-8'?>", | ||
382 | "<Wix xmlns='http://wixtoolset.org/schemas/v4/wxs'>", | ||
383 | " <Payload SuppressSignatureValidation='yes' />", | ||
384 | "</Wix>"); | ||
385 | |||
386 | var expected = String.Join(Environment.NewLine, | ||
387 | "<?xml version=\"1.0\" encoding=\"utf-16\"?>", | ||
388 | "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">", | ||
389 | " <Payload />", | ||
390 | "</Wix>"); | ||
391 | |||
392 | var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); | ||
393 | |||
394 | var messaging = new DummyMessaging(); | ||
395 | var converter = new Converter(messaging, 2, null, null); | ||
396 | |||
397 | var errors = converter.ConvertDocument(document); | ||
398 | |||
399 | var actual = UnformattedDocumentString(document); | ||
400 | |||
401 | Assert.Equal(1, errors); | ||
402 | Assert.Equal(expected, actual); | ||
403 | } | ||
404 | |||
405 | private static string UnformattedDocumentString(XDocument document) | ||
406 | { | ||
407 | var sb = new StringBuilder(); | ||
408 | |||
409 | using (var writer = new StringWriter(sb)) | ||
410 | { | ||
411 | document.Save(writer, SaveOptions.DisableFormatting); | ||
412 | } | ||
413 | |||
414 | return sb.ToString(); | ||
415 | } | ||
416 | |||
417 | private class DummyMessaging : IMessaging | ||
418 | { | ||
419 | public bool EncounteredError { get; set; } | ||
420 | |||
421 | public int LastErrorNumber { get; set; } | ||
422 | |||
423 | public bool ShowVerboseMessages { get; set; } | ||
424 | |||
425 | public bool SuppressAllWarnings { get; set; } | ||
426 | |||
427 | public bool WarningsAsError { get; set; } | ||
428 | |||
429 | public void ElevateWarningMessage(int warningNumber) | ||
430 | { | ||
431 | } | ||
432 | |||
433 | public string FormatMessage(Message message) => String.Empty; | ||
434 | |||
435 | public void SetListener(IMessageListener listener) | ||
436 | { | ||
437 | } | ||
438 | |||
439 | public void SuppressWarningMessage(int warningNumber) | ||
440 | { | ||
441 | } | ||
442 | |||
443 | public void Write(Message message) | ||
444 | { | ||
445 | } | ||
446 | |||
447 | public void Write(string message, bool verbose = false) | ||
448 | { | ||
449 | } | ||
450 | } | ||
451 | } | ||
452 | } | ||