From 244b46cf7f3252d6dc3884ce184be901d1d173e5 Mon Sep 17 00:00:00 2001 From: Sean Hall Date: Sun, 2 Sep 2018 16:12:29 -0500 Subject: Migrate WixCop into Tools from wix4. --- src/test/wixcop/ConverterFixture.cs | 418 +++++++++++++++++++++ .../TestData/SingleFile/ConvertedSingleFile.wxs | 60 +++ src/test/wixcop/TestData/SingleFile/SingleFile.wxs | 61 +++ src/test/wixcop/WixCopFixture.cs | 107 ++++++ src/test/wixcop/WixCopTests.csproj | 41 ++ 5 files changed, 687 insertions(+) create mode 100644 src/test/wixcop/ConverterFixture.cs create mode 100644 src/test/wixcop/TestData/SingleFile/ConvertedSingleFile.wxs create mode 100644 src/test/wixcop/TestData/SingleFile/SingleFile.wxs create mode 100644 src/test/wixcop/WixCopFixture.cs create mode 100644 src/test/wixcop/WixCopTests.csproj (limited to 'src/test/wixcop') 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 @@ +// 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 WixTest.WixUnitTest +{ + using System; + using System.IO; + using System.Text; + using System.Xml.Linq; + using WixCop; + using WixToolset; + using WixToolset.Data; + using WixToolset.Extensibility; + using WixToolset.Extensibility.Services; + using Xunit; + + public class ConverterFixture + { + private static readonly XNamespace Wix4Namespace = "http://wixtoolset.org/schemas/v4/wxs"; + + [Fact] + public void EnsuresDeclaration() + { + string parse = String.Join(Environment.NewLine, + "", + " ", + ""); + + string expected = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + Converter converter = new Converter(messaging, 2, null, null); + + int errors = converter.ConvertDocument(document); + + string actual = UnformattedDocumentString(document); + + Assert.Equal(1, errors); + Assert.Equal(expected, actual); + } + + [Fact] + public void EnsuresUtf8Declaration() + { + string parse = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + Converter converter = new Converter(messaging, 4, null, null); + + int 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() + { + string parse = String.Join(Environment.NewLine, + "", + "", + " ", + " ", + " ", + " ", + ""); + + string expected = String.Join(Environment.NewLine, + "", + "", + " ", + " ", + " ", + ""); + + XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + Converter converter = new Converter(messaging, 4, null, null); + + int errors = converter.ConvertDocument(document); + + string actual = UnformattedDocumentString(document); + + Assert.Equal(4, errors); + Assert.Equal(expected, actual); + } + + [Fact] + public void CanFixCdataWhitespace() + { + string parse = String.Join(Environment.NewLine, + "", + "", + " ", + " ", + " ", + " ", + " ", + ""); + + string expected = String.Join(Environment.NewLine, + "", + "", + " ", + " ", + " ", + ""); + + XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + Converter converter = new Converter(messaging, 2, null, null); + + int errors = converter.ConvertDocument(document); + + string actual = UnformattedDocumentString(document); + + Assert.Equal(2, errors); + Assert.Equal(expected, actual); + } + + [Fact] + public void CanConvertMainNamespace() + { + string parse = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + string expected = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + Converter converter = new Converter(messaging, 2, null, null); + + int errors = converter.ConvertDocument(document); + + string actual = UnformattedDocumentString(document); + + Assert.Equal(1, errors); + //Assert.Equal(Wix4Namespace, document.Root.GetDefaultNamespace()); + Assert.Equal(expected, actual); + } + + [Fact] + public void CanConvertNamedMainNamespace() + { + string parse = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + string expected = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + Converter converter = new Converter(messaging, 2, null, null); + + int errors = converter.ConvertDocument(document); + + string actual = UnformattedDocumentString(document); + + Assert.Equal(1, errors); + Assert.Equal(expected, actual); + Assert.Equal(Wix4Namespace, document.Root.GetNamespaceOfPrefix("w")); + } + + [Fact] + public void CanConvertNonWixDefaultNamespace() + { + string parse = String.Join(Environment.NewLine, + "", + "", + " ", + " ", + " ", + ""); + + string expected = String.Join(Environment.NewLine, + "", + "", + " ", + " ", + " ", + ""); + + XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + Converter converter = new Converter(messaging, 2, null, null); + + int errors = converter.ConvertDocument(document); + + string actual = UnformattedDocumentString(document); + + Assert.Equal(2, errors); + Assert.Equal(expected, actual); + Assert.Equal(Wix4Namespace, document.Root.GetNamespaceOfPrefix("w")); + Assert.Equal("http://wixtoolset.org/schemas/v4/wxs/util", document.Root.GetDefaultNamespace()); + } + + [Fact] + public void CanConvertExtensionNamespace() + { + string parse = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + string expected = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + Converter converter = new Converter(messaging, 2, null, null); + + int errors = converter.ConvertDocument(document); + + string actual = UnformattedDocumentString(document); + + Assert.Equal(2, errors); + Assert.Equal(expected, actual); + Assert.Equal(Wix4Namespace, document.Root.GetDefaultNamespace()); + } + + [Fact] + public void CanConvertMissingNamespace() + { + string parse = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + string expected = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + Converter converter = new Converter(messaging, 2, null, null); + + int errors = converter.ConvertDocument(document); + + string actual = UnformattedDocumentString(document); + + Assert.Equal(1, errors); + Assert.Equal(expected, actual); + Assert.Equal(Wix4Namespace, document.Root.GetDefaultNamespace()); + } + + [Fact] + public void CanConvertAnonymousFile() + { + string parse = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + string expected = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + Converter converter = new Converter(messaging, 2, null, null); + + int errors = converter.ConvertDocument(document); + + string actual = UnformattedDocumentString(document); + + Assert.Equal(1, errors); + Assert.Equal(expected, actual); + } + + [Fact] + public void CanConvertSuppressSignatureValidationNo() + { + string parse = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + string expected = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + Converter converter = new Converter(messaging, 2, null, null); + + int errors = converter.ConvertDocument(document); + + string actual = UnformattedDocumentString(document); + + Assert.Equal(1, errors); + Assert.Equal(expected, actual); + } + + [Fact] + public void CanConvertSuppressSignatureValidationYes() + { + string parse = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + string expected = String.Join(Environment.NewLine, + "", + "", + " ", + ""); + + XDocument document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); + + var messaging = new DummyMessaging(); + Converter converter = new Converter(messaging, 2, null, null); + + int errors = converter.ConvertDocument(document); + + string actual = UnformattedDocumentString(document); + + Assert.Equal(1, errors); + Assert.Equal(expected, actual); + } + + private static string UnformattedDocumentString(XDocument document) + { + StringBuilder sb = new StringBuilder(); + + using (StringWriter writer = new StringWriter(sb)) + { + document.Save(writer, SaveOptions.DisableFormatting | SaveOptions.OmitDuplicateNamespaces); + } + + 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) + { + return ""; + } + + 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/wixcop/TestData/SingleFile/ConvertedSingleFile.wxs b/src/test/wixcop/TestData/SingleFile/ConvertedSingleFile.wxs new file mode 100644 index 00000000..aacb68fa --- /dev/null +++ b/src/test/wixcop/TestData/SingleFile/ConvertedSingleFile.wxs @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/wixcop/TestData/SingleFile/SingleFile.wxs b/src/test/wixcop/TestData/SingleFile/SingleFile.wxs new file mode 100644 index 00000000..310ae811 --- /dev/null +++ b/src/test/wixcop/TestData/SingleFile/SingleFile.wxs @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/wixcop/WixCopFixture.cs b/src/test/wixcop/WixCopFixture.cs new file mode 100644 index 00000000..12863959 --- /dev/null +++ b/src/test/wixcop/WixCopFixture.cs @@ -0,0 +1,107 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using WixBuildTools.TestSupport; +using WixCop.CommandLine; +using WixCop.Interfaces; +using WixToolset.Core; +using WixToolset.Core.TestPackage; +using WixToolset.Extensibility; +using WixToolset.Extensibility.Services; +using Xunit; + +namespace WixCopTests +{ + 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(out var messages); + + Assert.Equal(2, result); + + var actualLines = File.ReadAllLines(targetFile); + var expectedLines = File.ReadAllLines(Path.Combine(folder, afterFileName)); + Assert.Equal(expectedLines, actualLines); + + var runner2 = new WixCopRunner + { + FixErrors = true, + SearchPatterns = + { + targetFile, + }, + }; + + var result2 = runner2.Execute(out var messages2); + + Assert.Equal(0, result2); + } + } + + private class WixCopRunner + { + public bool FixErrors { get; set; } + + public List SearchPatterns { get; } = new List(); + + public int Execute(out List messages) + { + var argList = new List(); + if (this.FixErrors) + { + argList.Add("-f"); + } + + foreach (string searchPattern in this.SearchPatterns) + { + argList.Add(searchPattern); + } + + return WixCopRunner.Execute(argList.ToArray(), out messages); + } + + public static int Execute(string[] args, out List messages) + { + var listener = new TestMessageListener(); + + var serviceProvider = new WixToolsetServiceProvider(); + serviceProvider.AddService((x, y) => listener); + serviceProvider.AddService((x, y) => new WixCopCommandLineParser(x)); + + var result = Execute(serviceProvider, args); + + var messaging = serviceProvider.GetService(); + messages = listener.Messages.Select(x => messaging.FormatMessage(x)).ToList(); + return result; + } + + public static int Execute(IServiceProvider serviceProvider, string[] args) + { + var wixcop = new WixCop.Program(); + return wixcop.Run(serviceProvider, args); + } + } + } +} diff --git a/src/test/wixcop/WixCopTests.csproj b/src/test/wixcop/WixCopTests.csproj new file mode 100644 index 00000000..0ae50dc8 --- /dev/null +++ b/src/test/wixcop/WixCopTests.csproj @@ -0,0 +1,41 @@ + + + + + + net461 + false + embedded + + + + + + + + PreserveNewest + + + PreserveNewest + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.2.3-55-g6feb