From 240f3594db6f633ece6818afd28dde1e1ef6b36c Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Thu, 4 Oct 2018 13:33:08 -0700 Subject: Fix WixCop namespaces and some C# modernization --- src/test/WixToolsetTest.WixCop/ConverterFixture.cs | 452 +++++++++++++++++++++ .../Preprocessor/ConvertedPreprocessor.wxs | 62 +++ .../TestData/Preprocessor/Preprocessor.wxs | 63 +++ .../TestData/Preprocessor/wixcop.settings.xml | 9 + .../TestData/SingleFile/ConvertedSingleFile.wxs | 60 +++ .../TestData/SingleFile/SingleFile.wxs | 61 +++ src/test/WixToolsetTest.WixCop/WixCopFixture.cs | 108 +++++ src/test/WixToolsetTest.WixCop/WixCopRunner.cs | 67 +++ .../WixToolsetTest.WixCop/WixCopRunnerResult.cs | 22 + .../WixToolsetTest.WixCop.csproj | 41 ++ 10 files changed, 945 insertions(+) create mode 100644 src/test/WixToolsetTest.WixCop/ConverterFixture.cs create mode 100644 src/test/WixToolsetTest.WixCop/TestData/Preprocessor/ConvertedPreprocessor.wxs create mode 100644 src/test/WixToolsetTest.WixCop/TestData/Preprocessor/Preprocessor.wxs create mode 100644 src/test/WixToolsetTest.WixCop/TestData/Preprocessor/wixcop.settings.xml create mode 100644 src/test/WixToolsetTest.WixCop/TestData/SingleFile/ConvertedSingleFile.wxs create mode 100644 src/test/WixToolsetTest.WixCop/TestData/SingleFile/SingleFile.wxs create mode 100644 src/test/WixToolsetTest.WixCop/WixCopFixture.cs create mode 100644 src/test/WixToolsetTest.WixCop/WixCopRunner.cs create mode 100644 src/test/WixToolsetTest.WixCop/WixCopRunnerResult.cs create mode 100644 src/test/WixToolsetTest.WixCop/WixToolsetTest.WixCop.csproj (limited to 'src/test/WixToolsetTest.WixCop') 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 @@ +// 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. + +namespace WixToolsetTest.WixCop +{ + using System; + using System.IO; + using System.Text; + using System.Xml.Linq; + using WixToolset.Data; + using WixToolset.Extensibility; + using WixToolset.Extensibility.Services; + using WixToolset.Tools.WixCop; + using Xunit; + + public class ConverterFixture + { + private static readonly XNamespace Wix4Namespace = "http://wixtoolset.org/schemas/v4/wxs"; + + [Fact] + public void EnsuresDeclaration() + { + var parse = String.Join(Environment.NewLine, + "", + " ", + ""); + + var expected = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + var converter = new Converter(messaging, 2, null, null); + + var errors = converter.ConvertDocument(document); + + var actual = UnformattedDocumentString(document); + + Assert.Equal(1, errors); + Assert.Equal(expected, actual); + } + + [Fact] + public void EnsuresUtf8Declaration() + { + var parse = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + var converter = new Converter(messaging, 4, null, null); + + var errors = converter.ConvertDocument(document); + + Assert.Equal(1, errors); + Assert.Equal("1.0", document.Declaration.Version); + Assert.Equal("utf-8", document.Declaration.Encoding); + } + + [Fact] + public void CanFixWhitespace() + { + var parse = String.Join(Environment.NewLine, + "", + "", + " ", + " ", + " ", + " ", + ""); + + var expected = String.Join(Environment.NewLine, + "", + "", + " ", + " ", + " ", + ""); + + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + var converter = new Converter(messaging, 4, null, null); + + var errors = converter.ConvertDocument(document); + + var actual = UnformattedDocumentString(document); + + Assert.Equal(4, errors); + Assert.Equal(expected, actual); + } + + [Fact] + public void CanFixCdataWhitespace() + { + var parse = String.Join(Environment.NewLine, + "", + "", + " ", + " ", + " ", + " ", + " ", + ""); + + var expected = String.Join(Environment.NewLine, + "", + "", + " ", + " ", + " ", + ""); + + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + var converter = new Converter(messaging, 2, null, null); + + var errors = converter.ConvertDocument(document); + + var actual = UnformattedDocumentString(document); + + Assert.Equal(expected, actual); + Assert.Equal(2, errors); + } + + [Fact] + public void CanFixCdataWithWhitespace() + { + var parse = String.Join(Environment.NewLine, + "", + "", + " ", + " ", + " ", + " ", + " ", + ""); + + var expected = String.Join(Environment.NewLine, + "", + "", + " ", + " ", + " ", + ""); + + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + var converter = new Converter(messaging, 2, null, null); + + var errors = converter.ConvertDocument(document); + + var actual = UnformattedDocumentString(document); + + Assert.Equal(expected, actual); + Assert.Equal(2, errors); + } + + [Fact] + public void CanConvertMainNamespace() + { + var parse = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + var expected = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + var converter = new Converter(messaging, 2, null, null); + + var errors = converter.ConvertDocument(document); + + var actual = UnformattedDocumentString(document); + + Assert.Equal(1, errors); + //Assert.Equal(Wix4Namespace, document.Root.GetDefaultNamespace()); + Assert.Equal(expected, actual); + } + + [Fact] + public void CanConvertNamedMainNamespace() + { + var parse = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + var expected = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + var converter = new Converter(messaging, 2, null, null); + + var errors = converter.ConvertDocument(document); + + var actual = UnformattedDocumentString(document); + + Assert.Equal(1, errors); + Assert.Equal(expected, actual); + Assert.Equal(Wix4Namespace, document.Root.GetNamespaceOfPrefix("w")); + } + + [Fact] + public void CanConvertNonWixDefaultNamespace() + { + var parse = String.Join(Environment.NewLine, + "", + "", + " ", + " ", + " ", + ""); + + var expected = String.Join(Environment.NewLine, + "", + "", + " ", + " ", + " ", + ""); + + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + var converter = new Converter(messaging, 2, null, null); + + var errors = converter.ConvertDocument(document); + + var actual = UnformattedDocumentString(document); + + Assert.Equal(expected, actual); + Assert.Equal(2, errors); + Assert.Equal(Wix4Namespace, document.Root.GetNamespaceOfPrefix("w")); + Assert.Equal("http://wixtoolset.org/schemas/v4/wxs/util", document.Root.GetDefaultNamespace()); + } + + [Fact] + public void CanConvertExtensionNamespace() + { + var parse = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + var expected = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + var converter = new Converter(messaging, 2, null, null); + + var errors = converter.ConvertDocument(document); + + var actual = UnformattedDocumentString(document); + + Assert.Equal(2, errors); + Assert.Equal(expected, actual); + Assert.Equal(Wix4Namespace, document.Root.GetDefaultNamespace()); + } + + [Fact] + public void CanConvertMissingNamespace() + { + var parse = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + var expected = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + var converter = new Converter(messaging, 2, null, null); + + var errors = converter.ConvertDocument(document); + + var actual = UnformattedDocumentString(document); + + Assert.Equal(1, errors); + Assert.Equal(expected, actual); + Assert.Equal(Wix4Namespace, document.Root.GetDefaultNamespace()); + } + + [Fact] + public void CanConvertAnonymousFile() + { + var parse = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + var expected = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + var converter = new Converter(messaging, 2, null, null); + + var errors = converter.ConvertDocument(document); + + var actual = UnformattedDocumentString(document); + + Assert.Equal(1, errors); + Assert.Equal(expected, actual); + } + + [Fact] + public void CanConvertSuppressSignatureValidationNo() + { + var parse = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + var expected = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + var converter = new Converter(messaging, 2, null, null); + + var errors = converter.ConvertDocument(document); + + var actual = UnformattedDocumentString(document); + + Assert.Equal(1, errors); + Assert.Equal(expected, actual); + } + + [Fact] + public void CanConvertSuppressSignatureValidationYes() + { + var parse = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + var expected = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + var converter = new Converter(messaging, 2, null, null); + + var errors = converter.ConvertDocument(document); + + var actual = UnformattedDocumentString(document); + + Assert.Equal(1, errors); + Assert.Equal(expected, actual); + } + + private static string UnformattedDocumentString(XDocument document) + { + var sb = new StringBuilder(); + + using (var writer = new StringWriter(sb)) + { + document.Save(writer, SaveOptions.DisableFormatting); + } + + return sb.ToString(); + } + + private class DummyMessaging : IMessaging + { + public bool EncounteredError { get; set; } + + public int LastErrorNumber { get; set; } + + public bool ShowVerboseMessages { get; set; } + + public bool SuppressAllWarnings { get; set; } + + public bool WarningsAsError { get; set; } + + public void ElevateWarningMessage(int warningNumber) + { + } + + public string FormatMessage(Message message) => String.Empty; + + public void SetListener(IMessageListener listener) + { + } + + public void SuppressWarningMessage(int warningNumber) + { + } + + public void Write(Message message) + { + } + + public void Write(string message, bool verbose = false) + { + } + } + } +} diff --git a/src/test/WixToolsetTest.WixCop/TestData/Preprocessor/ConvertedPreprocessor.wxs b/src/test/WixToolsetTest.WixCop/TestData/Preprocessor/ConvertedPreprocessor.wxs new file mode 100644 index 00000000..d6280185 --- /dev/null +++ b/src/test/WixToolsetTest.WixCop/TestData/Preprocessor/ConvertedPreprocessor.wxs @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/WixToolsetTest.WixCop/TestData/Preprocessor/Preprocessor.wxs b/src/test/WixToolsetTest.WixCop/TestData/Preprocessor/Preprocessor.wxs new file mode 100644 index 00000000..2eb908c2 --- /dev/null +++ b/src/test/WixToolsetTest.WixCop/TestData/Preprocessor/Preprocessor.wxs @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/WixToolsetTest.WixCop/TestData/Preprocessor/wixcop.settings.xml b/src/test/WixToolsetTest.WixCop/TestData/Preprocessor/wixcop.settings.xml new file mode 100644 index 00000000..9d3ad496 --- /dev/null +++ b/src/test/WixToolsetTest.WixCop/TestData/Preprocessor/wixcop.settings.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/test/WixToolsetTest.WixCop/TestData/SingleFile/ConvertedSingleFile.wxs b/src/test/WixToolsetTest.WixCop/TestData/SingleFile/ConvertedSingleFile.wxs new file mode 100644 index 00000000..aacb68fa --- /dev/null +++ b/src/test/WixToolsetTest.WixCop/TestData/SingleFile/ConvertedSingleFile.wxs @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/WixToolsetTest.WixCop/TestData/SingleFile/SingleFile.wxs b/src/test/WixToolsetTest.WixCop/TestData/SingleFile/SingleFile.wxs new file mode 100644 index 00000000..310ae811 --- /dev/null +++ b/src/test/WixToolsetTest.WixCop/TestData/SingleFile/SingleFile.wxs @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/WixToolsetTest.WixCop/WixCopFixture.cs b/src/test/WixToolsetTest.WixCop/WixCopFixture.cs new file mode 100644 index 00000000..1025eac8 --- /dev/null +++ b/src/test/WixToolsetTest.WixCop/WixCopFixture.cs @@ -0,0 +1,108 @@ +// 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. + +namespace WixToolsetTest.WixCop +{ + using System.IO; + using WixBuildTools.TestSupport; + using Xunit; + + public class WixCopFixture + { + [Fact] + public void CanConvertSingleFile() + { + const string beforeFileName = "SingleFile.wxs"; + const string afterFileName = "ConvertedSingleFile.wxs"; + var folder = TestData.Get(@"TestData\SingleFile"); + + using (var fs = new DisposableFileSystem()) + { + var baseFolder = fs.GetFolder(true); + var targetFile = Path.Combine(baseFolder, beforeFileName); + File.Copy(Path.Combine(folder, beforeFileName), Path.Combine(baseFolder, beforeFileName)); + + var runner = new WixCopRunner + { + FixErrors = true, + SearchPatterns = + { + targetFile, + }, + }; + + var result = runner.Execute(); + + Assert.Equal(2, result.ExitCode); + + var actualLines = File.ReadAllLines(targetFile); + var expectedLines = File.ReadAllLines(Path.Combine(folder, afterFileName)); + + for (var i = 0; i < actualLines.Length && i < expectedLines.Length; ++i) + { + Assert.Equal(expectedLines[i], actualLines[i]); + } + Assert.Equal(expectedLines.Length, actualLines.Length); + + var runner2 = new WixCopRunner + { + FixErrors = true, + SearchPatterns = + { + targetFile, + }, + }; + + var result2 = runner2.Execute(); + + Assert.Equal(0, result2.ExitCode); + } + } + + [Fact] + public void RetainsPreprocessorInstructions() + { + const string beforeFileName = "Preprocessor.wxs"; + const string afterFileName = "ConvertedPreprocessor.wxs"; + var folder = TestData.Get(@"TestData\Preprocessor"); + + using (var fs = new DisposableFileSystem()) + { + var baseFolder = fs.GetFolder(true); + var targetFile = Path.Combine(baseFolder, beforeFileName); + File.Copy(Path.Combine(folder, beforeFileName), Path.Combine(baseFolder, beforeFileName)); + + var runner = new WixCopRunner + { + FixErrors = true, + SettingFile1 = Path.Combine(folder, "wixcop.settings.xml"), + SearchPatterns = + { + targetFile, + }, + }; + + var result = runner.Execute(); + + Assert.Equal(2, result.ExitCode); + + var actualLines = File.ReadAllLines(targetFile); + var expectedLines = File.ReadAllLines(Path.Combine(folder, afterFileName)); + Assert.Equal(expectedLines, actualLines); + + var runner2 = new WixCopRunner + { + FixErrors = true, + SettingFile1 = Path.Combine(folder, "wixcop.settings.xml"), + SearchPatterns = + { + targetFile, + }, + }; + + var result2 = runner2.Execute(); + + Assert.Equal(0, result2.ExitCode); + } + } + } +} diff --git a/src/test/WixToolsetTest.WixCop/WixCopRunner.cs b/src/test/WixToolsetTest.WixCop/WixCopRunner.cs new file mode 100644 index 00000000..b831baa7 --- /dev/null +++ b/src/test/WixToolsetTest.WixCop/WixCopRunner.cs @@ -0,0 +1,67 @@ +// 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. + +namespace WixToolsetTest.WixCop +{ + using System; + using System.Collections.Generic; + using WixToolset.Core; + using WixToolset.Core.TestPackage; + using WixToolset.Extensibility; + using WixToolset.Tools.WixCop; + using WixToolset.Tools.WixCop.CommandLine; + using WixToolset.Tools.WixCop.Interfaces; + + public class WixCopRunner + { + public bool FixErrors { get; set; } + + public List SearchPatterns { get; } = new List(); + + public string SettingFile1 { get; set; } + + public WixCopRunnerResult Execute() + { + var argList = new List(); + + if (this.FixErrors) + { + argList.Add("-f"); + } + + if (!String.IsNullOrEmpty(this.SettingFile1)) + { + argList.Add($"-set1{this.SettingFile1}"); + } + + foreach (var searchPattern in this.SearchPatterns) + { + argList.Add(searchPattern); + } + + return WixCopRunner.Execute(argList.ToArray()); + } + + public static WixCopRunnerResult Execute(string[] args) + { + var listener = new TestMessageListener(); + + var serviceProvider = new WixToolsetServiceProvider(); + serviceProvider.AddService((x, y) => listener); + serviceProvider.AddService((x, y) => new WixCopCommandLineParser(x)); + + var exitCode = Execute(serviceProvider, args); + + return new WixCopRunnerResult + { + ExitCode = exitCode, + Messages = listener.Messages.ToArray() + }; + } + + public static int Execute(IServiceProvider serviceProvider, string[] args) + { + var wixcop = new Program(); + return wixcop.Run(serviceProvider, args); + } + } +} diff --git a/src/test/WixToolsetTest.WixCop/WixCopRunnerResult.cs b/src/test/WixToolsetTest.WixCop/WixCopRunnerResult.cs new file mode 100644 index 00000000..1b35e491 --- /dev/null +++ b/src/test/WixToolsetTest.WixCop/WixCopRunnerResult.cs @@ -0,0 +1,22 @@ +// 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. + +namespace WixToolsetTest.WixCop +{ + using System; + using System.Linq; + using WixToolset.Data; + using Xunit; + + public class WixCopRunnerResult + { + public int ExitCode { get; set; } + + public Message[] Messages { get; set; } + + public WixCopRunnerResult AssertSuccess() + { + Assert.True(0 == this.ExitCode, $"WixCop failed unexpectedly. Output:\r\n{String.Join("\r\n", this.Messages.Select(m => m.ToString()).ToArray())}"); + return this; + } + } +} diff --git a/src/test/WixToolsetTest.WixCop/WixToolsetTest.WixCop.csproj b/src/test/WixToolsetTest.WixCop/WixToolsetTest.WixCop.csproj new file mode 100644 index 00000000..6b41b6b6 --- /dev/null +++ b/src/test/WixToolsetTest.WixCop/WixToolsetTest.WixCop.csproj @@ -0,0 +1,41 @@ + + + + + + net461 + false + embedded + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.2.3-55-g6feb