@joebigelow / wix / commits / 240f3594

Fix WixCop namespaces and some C# modernization

Rob Mensching committed Oct 4, 2018 at 13:33 UTC 240f3594db6f633ece6818afd28dde1e1ef6b36c
16 files changed +316 -245
src/test/WixToolsetTest.WixCop/ConverterFixture.cs renamed
+115 -81
@@ -1,16 +1,15 @@
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
3 +namespace WixToolsetTest.WixCop
4 {
5 using System;
6 using System.IO;
7 using System.Text;
8 using System.Xml.Linq;
9 - using WixCop;
10 - using WixToolset;
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
@@ -20,25 +19,25 @@ namespace WixTest.WixUnitTest
19 [Fact]
20 public void EnsuresDeclaration()
21 {
23 - string parse = String.Join(Environment.NewLine,
22 + var parse = String.Join(Environment.NewLine,
23 "<Wix xmlns='http://wixtoolset.org/schemas/v4/wxs'>",
24 " <Fragment />",
25 "</Wix>");
26
28 - string expected = String.Join(Environment.NewLine,
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
34 - XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
33 + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
34
35 var messaging = new DummyMessaging();
37 - Converter converter = new Converter(messaging, 2, null, null);
36 + var converter = new Converter(messaging, 2, null, null);
37
39 - int errors = converter.ConvertDocument(document);
38 + var errors = converter.ConvertDocument(document);
39
41 - string actual = UnformattedDocumentString(document);
40 + var actual = UnformattedDocumentString(document);
41
42 Assert.Equal(1, errors);
43 Assert.Equal(expected, actual);
@@ -47,18 +46,18 @@ namespace WixTest.WixUnitTest
46 [Fact]
47 public void EnsuresUtf8Declaration()
48 {
50 - string parse = String.Join(Environment.NewLine,
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
56 - XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
55 + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
56
57 var messaging = new DummyMessaging();
59 - Converter converter = new Converter(messaging, 4, null, null);
58 + var converter = new Converter(messaging, 4, null, null);
59
61 - int errors = converter.ConvertDocument(document);
60 + var errors = converter.ConvertDocument(document);
61
62 Assert.Equal(1, errors);
63 Assert.Equal("1.0", document.Declaration.Version);
@@ -68,7 +67,7 @@ namespace WixTest.WixUnitTest
67 [Fact]
68 public void CanFixWhitespace()
69 {
71 - string parse = String.Join(Environment.NewLine,
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>",
@@ -78,7 +77,7 @@ namespace WixTest.WixUnitTest
77 " </Fragment>",
78 "</Wix>");
79
81 - string expected = String.Join(Environment.NewLine,
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>",
@@ -86,14 +85,14 @@ namespace WixTest.WixUnitTest
85 " </Fragment>",
86 "</Wix>");
87
89 - XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
88 + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
89
90 var messaging = new DummyMessaging();
92 - Converter converter = new Converter(messaging, 4, null, null);
91 + var converter = new Converter(messaging, 4, null, null);
92
94 - int errors = converter.ConvertDocument(document);
93 + var errors = converter.ConvertDocument(document);
94
96 - string actual = UnformattedDocumentString(document);
95 + var actual = UnformattedDocumentString(document);
96
97 Assert.Equal(4, errors);
98 Assert.Equal(expected, actual);
@@ -102,7 +101,7 @@ namespace WixTest.WixUnitTest
101 [Fact]
102 public void CanFixCdataWhitespace()
103 {
105 - string parse = String.Join(Environment.NewLine,
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>",
@@ -112,7 +111,7 @@ namespace WixTest.WixUnitTest
111 " </Fragment>",
112 "</Wix>");
113
115 - string expected = String.Join(Environment.NewLine,
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>",
@@ -120,42 +119,78 @@ namespace WixTest.WixUnitTest
119 " </Fragment>",
120 "</Wix>");
121
123 - XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
122 + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
123
124 var messaging = new DummyMessaging();
126 - Converter converter = new Converter(messaging, 2, null, null);
125 + var converter = new Converter(messaging, 2, null, null);
126
128 - int errors = converter.ConvertDocument(document);
127 + var errors = converter.ConvertDocument(document);
128
130 - string actual = UnformattedDocumentString(document);
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 {
139 - string parse = String.Join(Environment.NewLine,
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
145 - string expected = String.Join(Environment.NewLine,
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
151 - XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
186 + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
187
188 var messaging = new DummyMessaging();
154 - Converter converter = new Converter(messaging, 2, null, null);
189 + var converter = new Converter(messaging, 2, null, null);
190
156 - int errors = converter.ConvertDocument(document);
191 + var errors = converter.ConvertDocument(document);
192
158 - string actual = UnformattedDocumentString(document);
193 + var actual = UnformattedDocumentString(document);
194
195 Assert.Equal(1, errors);
196 //Assert.Equal(Wix4Namespace, document.Root.GetDefaultNamespace());
@@ -165,26 +200,26 @@ namespace WixTest.WixUnitTest
200 [Fact]
201 public void CanConvertNamedMainNamespace()
202 {
168 - string parse = String.Join(Environment.NewLine,
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
174 - string expected = String.Join(Environment.NewLine,
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
180 - XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
215 + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
216
217 var messaging = new DummyMessaging();
183 - Converter converter = new Converter(messaging, 2, null, null);
218 + var converter = new Converter(messaging, 2, null, null);
219
185 - int errors = converter.ConvertDocument(document);
220 + var errors = converter.ConvertDocument(document);
221
187 - string actual = UnformattedDocumentString(document);
222 + var actual = UnformattedDocumentString(document);
223
224 Assert.Equal(1, errors);
225 Assert.Equal(expected, actual);
@@ -194,7 +229,7 @@ namespace WixTest.WixUnitTest
229 [Fact]
230 public void CanConvertNonWixDefaultNamespace()
231 {
197 - string parse = String.Join(Environment.NewLine,
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>",
@@ -202,7 +237,7 @@ namespace WixTest.WixUnitTest
237 " </w:Fragment>",
238 "</w:Wix>");
239
205 - string expected = String.Join(Environment.NewLine,
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>",
@@ -210,17 +245,17 @@ namespace WixTest.WixUnitTest
245 " </w:Fragment>",
246 "</w:Wix>");
247
213 - XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
248 + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
249
250 var messaging = new DummyMessaging();
216 - Converter converter = new Converter(messaging, 2, null, null);
251 + var converter = new Converter(messaging, 2, null, null);
252
218 - int errors = converter.ConvertDocument(document);
253 + var errors = converter.ConvertDocument(document);
254
220 - string actual = UnformattedDocumentString(document);
255 + var actual = UnformattedDocumentString(document);
256
222 - Assert.Equal(2, errors);
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 }
@@ -228,26 +263,26 @@ namespace WixTest.WixUnitTest
263 [Fact]
264 public void CanConvertExtensionNamespace()
265 {
231 - string parse = String.Join(Environment.NewLine,
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
237 - string expected = String.Join(Environment.NewLine,
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
243 - XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
278 + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
279
280 var messaging = new DummyMessaging();
246 - Converter converter = new Converter(messaging, 2, null, null);
281 + var converter = new Converter(messaging, 2, null, null);
282
248 - int errors = converter.ConvertDocument(document);
283 + var errors = converter.ConvertDocument(document);
284
250 - string actual = UnformattedDocumentString(document);
285 + var actual = UnformattedDocumentString(document);
286
287 Assert.Equal(2, errors);
288 Assert.Equal(expected, actual);
@@ -257,26 +292,26 @@ namespace WixTest.WixUnitTest
292 [Fact]
293 public void CanConvertMissingNamespace()
294 {
260 - string parse = String.Join(Environment.NewLine,
295 + var parse = String.Join(Environment.NewLine,
296 "<?xml version='1.0' encoding='utf-8'?>",
297 "<Wix>",
298 " <Fragment />",
299 "</Wix>");
300
266 - string expected = String.Join(Environment.NewLine,
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
272 - XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
307 + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
308
309 var messaging = new DummyMessaging();
275 - Converter converter = new Converter(messaging, 2, null, null);
310 + var converter = new Converter(messaging, 2, null, null);
311
277 - int errors = converter.ConvertDocument(document);
312 + var errors = converter.ConvertDocument(document);
313
279 - string actual = UnformattedDocumentString(document);
314 + var actual = UnformattedDocumentString(document);
315
316 Assert.Equal(1, errors);
317 Assert.Equal(expected, actual);
@@ -286,26 +321,26 @@ namespace WixTest.WixUnitTest
321 [Fact]
322 public void CanConvertAnonymousFile()
323 {
289 - string parse = String.Join(Environment.NewLine,
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
295 - string expected = String.Join(Environment.NewLine,
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
301 - XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
336 + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
337
338 var messaging = new DummyMessaging();
304 - Converter converter = new Converter(messaging, 2, null, null);
339 + var converter = new Converter(messaging, 2, null, null);
340
306 - int errors = converter.ConvertDocument(document);
341 + var errors = converter.ConvertDocument(document);
342
308 - string actual = UnformattedDocumentString(document);
343 + var actual = UnformattedDocumentString(document);
344
345 Assert.Equal(1, errors);
346 Assert.Equal(expected, actual);
@@ -314,26 +349,26 @@ namespace WixTest.WixUnitTest
349 [Fact]
350 public void CanConvertSuppressSignatureValidationNo()
351 {
317 - string parse = String.Join(Environment.NewLine,
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
323 - string expected = String.Join(Environment.NewLine,
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
329 - XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
364 + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
365
366 var messaging = new DummyMessaging();
332 - Converter converter = new Converter(messaging, 2, null, null);
367 + var converter = new Converter(messaging, 2, null, null);
368
334 - int errors = converter.ConvertDocument(document);
369 + var errors = converter.ConvertDocument(document);
370
336 - string actual = UnformattedDocumentString(document);
371 + var actual = UnformattedDocumentString(document);
372
373 Assert.Equal(1, errors);
374 Assert.Equal(expected, actual);
@@ -342,26 +377,26 @@ namespace WixTest.WixUnitTest
377 [Fact]
378 public void CanConvertSuppressSignatureValidationYes()
379 {
345 - string parse = String.Join(Environment.NewLine,
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
351 - string expected = String.Join(Environment.NewLine,
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
357 - XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
392 + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
393
394 var messaging = new DummyMessaging();
360 - Converter converter = new Converter(messaging, 2, null, null);
395 + var converter = new Converter(messaging, 2, null, null);
396
362 - int errors = converter.ConvertDocument(document);
397 + var errors = converter.ConvertDocument(document);
398
364 - string actual = UnformattedDocumentString(document);
399 + var actual = UnformattedDocumentString(document);
400
401 Assert.Equal(1, errors);
402 Assert.Equal(expected, actual);
@@ -369,11 +404,11 @@ namespace WixTest.WixUnitTest
404
405 private static string UnformattedDocumentString(XDocument document)
406 {
372 - StringBuilder sb = new StringBuilder();
407 + var sb = new StringBuilder();
408
374 - using (StringWriter writer = new StringWriter(sb))
409 + using (var writer = new StringWriter(sb))
410 {
376 - document.Save(writer, SaveOptions.DisableFormatting | SaveOptions.OmitDuplicateNamespaces);
411 + document.Save(writer, SaveOptions.DisableFormatting);
412 }
413
414 return sb.ToString();
@@ -386,17 +421,16 @@ namespace WixTest.WixUnitTest
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
396 - public string FormatMessage(Message message)
397 - {
398 - return "";
399 - }
433 + public string FormatMessage(Message message) => String.Empty;
434
435 public void SetListener(IMessageListener listener)
436 {
src/test/WixToolsetTest.WixCop/TestData/Preprocessor/ConvertedPreprocessor.wxs renamed
src/test/WixToolsetTest.WixCop/TestData/Preprocessor/Preprocessor.wxs renamed
src/test/WixToolsetTest.WixCop/TestData/Preprocessor/wixcop.settings.xml renamed
src/test/WixToolsetTest.WixCop/TestData/SingleFile/ConvertedSingleFile.wxs renamed
src/test/WixToolsetTest.WixCop/TestData/SingleFile/SingleFile.wxs renamed
src/test/WixToolsetTest.WixCop/WixCopFixture.cs renamed
+21 -75
@@ -1,18 +1,11 @@
1 -using System;
2 -using System.Collections.Generic;
3 -using System.IO;
4 -using System.Linq;
5 -using WixBuildTools.TestSupport;
6 -using WixCop.CommandLine;
7 -using WixCop.Interfaces;
8 -using WixToolset.Core;
9 -using WixToolset.Core.TestPackage;
10 -using WixToolset.Extensibility;
11 -using WixToolset.Extensibility.Services;
12 -using Xunit;
13 -
14 -namespace WixCopTests
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.IO;
6 + using WixBuildTools.TestSupport;
7 + using Xunit;
8 +
9 public class WixCopFixture
10 {
11 [Fact]
@@ -37,13 +30,18 @@ namespace WixCopTests
30 },
31 };
32
40 - var result = runner.Execute(out var messages);
33 + var result = runner.Execute();
34
42 - Assert.Equal(2, result);
35 + Assert.Equal(2, result.ExitCode);
36
37 var actualLines = File.ReadAllLines(targetFile);
38 var expectedLines = File.ReadAllLines(Path.Combine(folder, afterFileName));
46 - Assert.Equal(expectedLines, actualLines);
39 +
40 + for (var i = 0; i < actualLines.Length && i < expectedLines.Length; ++i)
41 + {
42 + Assert.Equal(expectedLines[i], actualLines[i]);
43 + }
44 + Assert.Equal(expectedLines.Length, actualLines.Length);
45
46 var runner2 = new WixCopRunner
47 {
@@ -54,9 +52,9 @@ namespace WixCopTests
52 },
53 };
54
57 - var result2 = runner2.Execute(out var messages2);
55 + var result2 = runner2.Execute();
56
59 - Assert.Equal(0, result2);
57 + Assert.Equal(0, result2.ExitCode);
58 }
59 }
60
@@ -83,9 +81,9 @@ namespace WixCopTests
81 },
82 };
83
86 - var result = runner.Execute(out var messages);
84 + var result = runner.Execute();
85
88 - Assert.Equal(2, result);
86 + Assert.Equal(2, result.ExitCode);
87
88 var actualLines = File.ReadAllLines(targetFile);
89 var expectedLines = File.ReadAllLines(Path.Combine(folder, afterFileName));
@@ -101,61 +99,9 @@ namespace WixCopTests
99 },
100 };
101
104 - var result2 = runner2.Execute(out var messages2);
105 -
106 - Assert.Equal(0, result2);
107 - }
108 - }
102 + var result2 = runner2.Execute();
103
110 - private class WixCopRunner
111 - {
112 - public bool FixErrors { get; set; }
113 -
114 - public List<string> SearchPatterns { get; } = new List<string>();
115 -
116 - public string SettingFile1 { get; set; }
117 -
118 - public int Execute(out List<string> messages)
119 - {
120 - var argList = new List<string>();
121 -
122 - if (this.FixErrors)
123 - {
124 - argList.Add("-f");
125 - }
126 -
127 - if (!String.IsNullOrEmpty(this.SettingFile1))
128 - {
129 - argList.Add($"-set1{this.SettingFile1}");
130 - }
131 -
132 - foreach (string searchPattern in this.SearchPatterns)
133 - {
134 - argList.Add(searchPattern);
135 - }
136 -
137 - return WixCopRunner.Execute(argList.ToArray(), out messages);
138 - }
139 -
140 - public static int Execute(string[] args, out List<string> messages)
141 - {
142 - var listener = new TestMessageListener();
143 -
144 - var serviceProvider = new WixToolsetServiceProvider();
145 - serviceProvider.AddService<IMessageListener>((x, y) => listener);
146 - serviceProvider.AddService<IWixCopCommandLineParser>((x, y) => new WixCopCommandLineParser(x));
147 -
148 - var result = Execute(serviceProvider, args);
149 -
150 - var messaging = serviceProvider.GetService<IMessaging>();
151 - messages = listener.Messages.Select(x => messaging.FormatMessage(x)).ToList();
152 - return result;
153 - }
154 -
155 - public static int Execute(IServiceProvider serviceProvider, string[] args)
156 - {
157 - var wixcop = new WixCop.Program();
158 - return wixcop.Run(serviceProvider, args);
104 + Assert.Equal(0, result2.ExitCode);
105 }
106 }
107 }
src/test/WixToolsetTest.WixCop/WixCopRunner.cs new
+67
@@ -0,0 +1,67 @@
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.Collections.Generic;
7 + using WixToolset.Core;
8 + using WixToolset.Core.TestPackage;
9 + using WixToolset.Extensibility;
10 + using WixToolset.Tools.WixCop;
11 + using WixToolset.Tools.WixCop.CommandLine;
12 + using WixToolset.Tools.WixCop.Interfaces;
13 +
14 + public class WixCopRunner
15 + {
16 + public bool FixErrors { get; set; }
17 +
18 + public List<string> SearchPatterns { get; } = new List<string>();
19 +
20 + public string SettingFile1 { get; set; }
21 +
22 + public WixCopRunnerResult Execute()
23 + {
24 + var argList = new List<string>();
25 +
26 + if (this.FixErrors)
27 + {
28 + argList.Add("-f");
29 + }
30 +
31 + if (!String.IsNullOrEmpty(this.SettingFile1))
32 + {
33 + argList.Add($"-set1{this.SettingFile1}");
34 + }
35 +
36 + foreach (var searchPattern in this.SearchPatterns)
37 + {
38 + argList.Add(searchPattern);
39 + }
40 +
41 + return WixCopRunner.Execute(argList.ToArray());
42 + }
43 +
44 + public static WixCopRunnerResult Execute(string[] args)
45 + {
46 + var listener = new TestMessageListener();
47 +
48 + var serviceProvider = new WixToolsetServiceProvider();
49 + serviceProvider.AddService<IMessageListener>((x, y) => listener);
50 + serviceProvider.AddService<IWixCopCommandLineParser>((x, y) => new WixCopCommandLineParser(x));
51 +
52 + var exitCode = Execute(serviceProvider, args);
53 +
54 + return new WixCopRunnerResult
55 + {
56 + ExitCode = exitCode,
57 + Messages = listener.Messages.ToArray()
58 + };
59 + }
60 +
61 + public static int Execute(IServiceProvider serviceProvider, string[] args)
62 + {
63 + var wixcop = new Program();
64 + return wixcop.Run(serviceProvider, args);
65 + }
66 + }
67 +}
src/test/WixToolsetTest.WixCop/WixCopRunnerResult.cs new
+22
@@ -0,0 +1,22 @@
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.Linq;
7 + using WixToolset.Data;
8 + using Xunit;
9 +
10 + public class WixCopRunnerResult
11 + {
12 + public int ExitCode { get; set; }
13 +
14 + public Message[] Messages { get; set; }
15 +
16 + public WixCopRunnerResult AssertSuccess()
17 + {
18 + Assert.True(0 == this.ExitCode, $"WixCop failed unexpectedly. Output:\r\n{String.Join("\r\n", this.Messages.Select(m => m.ToString()).ToArray())}");
19 + return this;
20 + }
21 + }
22 +}
src/test/WixToolsetTest.WixCop/WixToolsetTest.WixCop.csproj renamed
+1
@@ -7,6 +7,7 @@
7 <IsPackable>false</IsPackable>
8 <DebugType>embedded</DebugType>
9 </PropertyGroup>
10 +
11 <ItemGroup>
12 <None Remove="TestData\SingleFile\ConvertedSingleFile.wxs" />
13 <None Remove="TestData\SingleFile\SingleFile.wxs" />
src/wixcop/CommandLine/ConvertCommand.cs
+11 -10
@@ -1,13 +1,14 @@
1 -using System;
2 -using System.Collections.Generic;
3 -using System.IO;
4 -using System.Linq;
5 -using System.Xml;
6 -using WixToolset.Extensibility.Data;
7 -using WixToolset.Extensibility.Services;
8 -
9 -namespace WixCop.CommandLine
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 WixToolset.Tools.WixCop.CommandLine
4 {
5 + using System;
6 + using System.Collections.Generic;
7 + using System.IO;
8 + using System.Xml;
9 + using WixToolset.Extensibility.Data;
10 + using WixToolset.Extensibility.Services;
11 +
12 internal class ConvertCommand : ICommandLineCommand
13 {
14 private const string SettingsFileDefault = "wixcop.settings.xml";
@@ -69,7 +70,7 @@ namespace WixCop.CommandLine
70
71 var errors = this.InspectSubDirectories(converter, Path.GetFullPath("."));
72
72 - foreach (string searchPattern in this.SearchPatterns)
73 + foreach (var searchPattern in this.SearchPatterns)
74 {
75 if (!this.SearchPatternResults.Contains(searchPattern))
76 {
src/wixcop/CommandLine/HelpCommand.cs
+5 -3
@@ -1,8 +1,10 @@
1 -using System;
2 -using WixToolset.Extensibility.Data;
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
4 -namespace WixCop.CommandLine
3 +namespace WixToolset.Tools.WixCop.CommandLine
4 {
5 + using System;
6 + using WixToolset.Extensibility.Data;
7 +
8 internal class HelpCommand : ICommandLineCommand
9 {
10 public int Execute()
src/wixcop/CommandLine/WixCopCommandLineParser.cs
+42 -40
@@ -1,12 +1,14 @@
1 -using System;
2 -using System.Collections.Generic;
3 -using WixCop.Interfaces;
4 -using WixToolset.Core;
5 -using WixToolset.Extensibility.Data;
6 -using WixToolset.Extensibility.Services;
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
8 -namespace WixCop.CommandLine
3 +namespace WixToolset.Tools.WixCop.CommandLine
4 {
5 + using System;
6 + using System.Collections.Generic;
7 + using WixToolset.Core;
8 + using WixToolset.Extensibility.Data;
9 + using WixToolset.Extensibility.Services;
10 + using WixToolset.Tools.WixCop.Interfaces;
11 +
12 public sealed class WixCopCommandLineParser : IWixCopCommandLineParser
13 {
14 private bool fixErrors;
@@ -89,43 +91,43 @@ namespace WixCop.CommandLine
91
92 switch (parameter.ToLowerInvariant())
93 {
92 - case "?":
93 - this.showHelp = true;
94 - return true;
95 - case "f":
96 - this.fixErrors = true;
97 - return true;
98 - case "nologo":
99 - this.showLogo = false;
100 - return true;
101 - case "s":
102 - this.subDirectories = true;
103 - return true;
104 - default: // other parameters
105 - if (parameter.StartsWith("set1", StringComparison.Ordinal))
106 - {
107 - this.settingsFile1 = parameter.Substring(4);
108 - }
109 - else if (parameter.StartsWith("set2", StringComparison.Ordinal))
110 - {
111 - this.settingsFile2 = parameter.Substring(4);
112 - }
113 - else if (parameter.StartsWith("indent:", StringComparison.Ordinal))
94 + case "?":
95 + this.showHelp = true;
96 + return true;
97 + case "f":
98 + this.fixErrors = true;
99 + return true;
100 + case "nologo":
101 + this.showLogo = false;
102 + return true;
103 + case "s":
104 + this.subDirectories = true;
105 + return true;
106 + default: // other parameters
107 + if (parameter.StartsWith("set1", StringComparison.Ordinal))
108 + {
109 + this.settingsFile1 = parameter.Substring(4);
110 + }
111 + else if (parameter.StartsWith("set2", StringComparison.Ordinal))
112 + {
113 + this.settingsFile2 = parameter.Substring(4);
114 + }
115 + else if (parameter.StartsWith("indent:", StringComparison.Ordinal))
116 + {
117 + try
118 {
115 - try
116 - {
117 - this.indentationAmount = Convert.ToInt32(parameter.Substring(7));
118 - }
119 - catch
120 - {
121 - throw new ArgumentException("Invalid numeric argument.", parameter);
122 - }
119 + this.indentationAmount = Convert.ToInt32(parameter.Substring(7));
120 }
124 - else
121 + catch
122 {
126 - throw new ArgumentException("Invalid argument.", parameter);
123 + throw new ArgumentException("Invalid numeric argument.", parameter);
124 }
128 - return true;
125 + }
126 + else
127 + {
128 + throw new ArgumentException("Invalid argument.", parameter);
129 + }
130 + return true;
131 }
132 }
133 }
src/wixcop/Converter.cs
+25 -31
@@ -1,6 +1,6 @@
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 WixCop
3 +namespace WixToolset.Tools.WixCop
4 {
5 using System;
6 using System.Collections.Generic;
@@ -141,7 +141,7 @@ namespace WixCop
141 {
142 try
143 {
144 - using (StreamWriter writer = File.CreateText(this.SourceFile))
144 + using (var writer = File.CreateText(this.SourceFile))
145 {
146 document.Save(writer, SaveOptions.DisableFormatting | SaveOptions.OmitDuplicateNamespaces);
147 }
@@ -162,7 +162,7 @@ namespace WixCop
162 /// <returns>The number of errors found.</returns>
163 public int ConvertDocument(XDocument document)
164 {
165 - XDeclaration declaration = document.Declaration;
165 + var declaration = document.Declaration;
166
167 // Convert the declaration.
168 if (null != declaration)
@@ -206,7 +206,7 @@ namespace WixCop
206 }
207
208 // Convert this node if it is an element.
209 - XElement element = node as XElement;
209 + var element = node as XElement;
210
211 if (null != element)
212 {
@@ -215,7 +215,7 @@ namespace WixCop
215 // Convert all children of this element.
216 IEnumerable<XNode> children = element.Nodes().ToList();
217
218 - foreach (XNode child in children)
218 + foreach (var child in children)
219 {
220 this.ConvertNode(child, level + 1);
221 }
@@ -225,9 +225,9 @@ namespace WixCop
225 private void ConvertElement(XElement element)
226 {
227 // Gather any deprecated namespaces, then update this element tree based on those deprecations.
228 - Dictionary<XNamespace, XNamespace> deprecatedToUpdatedNamespaces = new Dictionary<XNamespace, XNamespace>();
228 + var deprecatedToUpdatedNamespaces = new Dictionary<XNamespace, XNamespace>();
229
230 - foreach (XAttribute declaration in element.Attributes().Where(a => a.IsNamespaceDeclaration))
230 + foreach (var declaration in element.Attributes().Where(a => a.IsNamespaceDeclaration))
231 {
232 XNamespace ns;
233
@@ -258,7 +258,7 @@ namespace WixCop
258 {
259 if (null == element.Attribute("Id"))
260 {
261 - XAttribute attribute = element.Attribute("Name");
261 + var attribute = element.Attribute("Name");
262
263 if (null == attribute)
264 {
@@ -267,7 +267,7 @@ namespace WixCop
267
268 if (null != attribute)
269 {
270 - string name = Path.GetFileName(attribute.Value);
270 + var name = Path.GetFileName(attribute.Value);
271
272 if (this.OnError(ConverterTestType.AssignAnonymousFileId, element, "The file id is being updated to '{0}' to ensure it remains the same as the default", name))
273 {
@@ -282,7 +282,7 @@ namespace WixCop
282
283 private void ConvertSuppressSignatureValidation(XElement element)
284 {
285 - XAttribute suppressSignatureValidation = element.Attribute("SuppressSignatureValidation");
285 + var suppressSignatureValidation = element.Attribute("SuppressSignatureValidation");
286
287 if (null != suppressSignatureValidation)
288 {
@@ -311,7 +311,7 @@ namespace WixCop
311
312 element.Add(new XAttribute("xmlns", WixNamespace.NamespaceName)); // set the default namespace.
313
314 - foreach (XElement elementWithoutNamespace in element.Elements().Where(e => XNamespace.None == e.Name.Namespace))
314 + foreach (var elementWithoutNamespace in element.Elements().Where(e => XNamespace.None == e.Name.Namespace))
315 {
316 elementWithoutNamespace.Name = WixNamespace.GetName(elementWithoutNamespace.Name.LocalName);
317 }
@@ -326,9 +326,7 @@ namespace WixCop
326 private void ConvertWhitespace(XNode node, int level)
327 {
328 // Fix the whitespace before this node.
329 - XText whitespace = node.PreviousNode as XText;
330 -
331 - if (null != whitespace)
329 + if (node.PreviousNode is XText whitespace)
330 {
331 if (XmlNodeType.CDATA == node.NodeType)
332 {
@@ -351,9 +349,7 @@ namespace WixCop
349 }
350
351 // Fix the whitespace after CDATA nodes.
354 - XCData cdata = node as XCData;
355 -
356 - if (null != cdata)
352 + if (node is XCData cdata)
353 {
354 whitespace = cdata.NextNode as XText;
355
@@ -368,9 +364,7 @@ namespace WixCop
364 else
365 {
366 // Fix the whitespace inside and after this node (except for Error which may contain just whitespace).
371 - XElement element = node as XElement;
372 -
373 - if (null != element && "Error" != element.Name.LocalName)
367 + if (node is XElement element && "Error" != element.Name.LocalName)
368 {
369 if (!element.HasElements && !element.IsEmpty && String.IsNullOrEmpty(element.Value.Trim()))
370 {
@@ -403,7 +397,7 @@ namespace WixCop
397 {
398 if (null != types)
399 {
406 - foreach (string type in types)
400 + foreach (var type in types)
401 {
402 ConverterTestType itt;
403
@@ -421,7 +415,7 @@ namespace WixCop
415
416 private static void UpdateElementsWithDeprecatedNamespaces(IEnumerable<XElement> elements, Dictionary<XNamespace, XNamespace> deprecatedToUpdatedNamespaces)
417 {
424 - foreach (XElement element in elements)
418 + foreach (var element in elements)
419 {
420 XNamespace ns;
421
@@ -434,9 +428,9 @@ namespace WixCop
428 IEnumerable<XAttribute> attributes = element.Attributes().ToList();
429 element.RemoveAttributes();
430
437 - foreach (XAttribute attribute in attributes)
431 + foreach (var attribute in attributes)
432 {
439 - XAttribute convertedAttribute = attribute;
433 + var convertedAttribute = attribute;
434
435 if (attribute.IsNamespaceDeclaration)
436 {
@@ -477,7 +471,7 @@ namespace WixCop
471 }
472
473 // check the spaces
480 - foreach (char character in whitespace)
474 + foreach (var character in whitespace)
475 {
476 if (' ' != character)
477 {
@@ -496,9 +490,9 @@ namespace WixCop
490 /// <param name="whitespace">The whitespace node to fix.</param>
491 private static void FixWhitespace(int indentationAmount, int level, XText whitespace)
492 {
499 - int newLineCount = 0;
493 + var newLineCount = 0;
494
501 - for (int i = 0; i + 1 < whitespace.Value.Length; ++i)
495 + for (var i = 0; i + 1 < whitespace.Value.Length; ++i)
496 {
497 if (XDocumentNewLine == whitespace.Value.Substring(i, 2))
498 {
@@ -516,7 +510,7 @@ namespace WixCop
510 whitespace.Value = String.Empty;
511
512 // add the correct number of newlines
519 - for (int i = 0; i < newLineCount; ++i)
513 + for (var i = 0; i < newLineCount; ++i)
514 {
515 whitespace.Value = String.Concat(whitespace.Value, XDocumentNewLine);
516 }
@@ -543,9 +537,9 @@ namespace WixCop
537 // Increase the error count.
538 this.Errors++;
539
546 - SourceLineNumber sourceLine = (null == node) ? new SourceLineNumber(this.SourceFile ?? "wixcop.exe") : new SourceLineNumber(this.SourceFile, ((IXmlLineInfo)node).LineNumber);
547 - bool warning = this.ErrorsAsWarnings.Contains(converterTestType);
548 - string display = String.Format(CultureInfo.CurrentCulture, message, args);
540 + var sourceLine = (null == node) ? new SourceLineNumber(this.SourceFile ?? "wixcop.exe") : new SourceLineNumber(this.SourceFile, ((IXmlLineInfo)node).LineNumber);
541 + var warning = this.ErrorsAsWarnings.Contains(converterTestType);
542 + var display = String.Format(CultureInfo.CurrentCulture, message, args);
543
544 var msg = new Message(sourceLine, warning ? MessageLevel.Warning : MessageLevel.Error, (int)converterTestType, "{0} ({1})", display, converterTestType.ToString());
545
src/wixcop/Interfaces/IWixCopCommandLineParser.cs
+4 -2
@@ -1,7 +1,9 @@
1 -using WixToolset.Extensibility.Data;
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 WixCop.Interfaces
3 +namespace WixToolset.Tools.WixCop.Interfaces
4 {
5 + using WixToolset.Extensibility.Data;
6 +
7 public interface IWixCopCommandLineParser
8 {
9 ICommandLineArguments Arguments { get; set; }
src/wixcop/Program.cs
+3 -3
@@ -1,15 +1,15 @@
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 WixCop
3 +namespace WixToolset.Tools.WixCop
4 {
5 using System;
6 - using WixCop.CommandLine;
7 - using WixCop.Interfaces;
6 using WixToolset.Core;
7 using WixToolset.Extensibility;
8 using WixToolset.Extensibility.Data;
9 using WixToolset.Extensibility.Services;
10 using WixToolset.Tools.Core;
11 + using WixToolset.Tools.WixCop.CommandLine;
12 + using WixToolset.Tools.WixCop.Interfaces;
13
14 /// <summary>
15 /// Wix source code style inspector and converter.