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