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 ++ src/test/wixcop/ConverterFixture.cs | 418 ------------------- .../Preprocessor/ConvertedPreprocessor.wxs | 62 --- .../wixcop/TestData/Preprocessor/Preprocessor.wxs | 63 --- .../TestData/Preprocessor/wixcop.settings.xml | 9 - .../TestData/SingleFile/ConvertedSingleFile.wxs | 60 --- src/test/wixcop/TestData/SingleFile/SingleFile.wxs | 61 --- src/test/wixcop/WixCopFixture.cs | 162 -------- src/test/wixcop/WixCopTests.csproj | 40 -- 18 files changed, 945 insertions(+), 875 deletions(-) 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 delete mode 100644 src/test/wixcop/ConverterFixture.cs delete mode 100644 src/test/wixcop/TestData/Preprocessor/ConvertedPreprocessor.wxs delete mode 100644 src/test/wixcop/TestData/Preprocessor/Preprocessor.wxs delete mode 100644 src/test/wixcop/TestData/Preprocessor/wixcop.settings.xml delete mode 100644 src/test/wixcop/TestData/SingleFile/ConvertedSingleFile.wxs delete mode 100644 src/test/wixcop/TestData/SingleFile/SingleFile.wxs delete mode 100644 src/test/wixcop/WixCopFixture.cs delete mode 100644 src/test/wixcop/WixCopTests.csproj (limited to 'src/test') 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 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/wixcop/ConverterFixture.cs b/src/test/wixcop/ConverterFixture.cs deleted file mode 100644 index 45ccc33e..00000000 --- a/src/test/wixcop/ConverterFixture.cs +++ /dev/null @@ -1,418 +0,0 @@ -// 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/Preprocessor/ConvertedPreprocessor.wxs b/src/test/wixcop/TestData/Preprocessor/ConvertedPreprocessor.wxs deleted file mode 100644 index d6280185..00000000 --- a/src/test/wixcop/TestData/Preprocessor/ConvertedPreprocessor.wxs +++ /dev/null @@ -1,62 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/test/wixcop/TestData/Preprocessor/Preprocessor.wxs b/src/test/wixcop/TestData/Preprocessor/Preprocessor.wxs deleted file mode 100644 index 2eb908c2..00000000 --- a/src/test/wixcop/TestData/Preprocessor/Preprocessor.wxs +++ /dev/null @@ -1,63 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/test/wixcop/TestData/Preprocessor/wixcop.settings.xml b/src/test/wixcop/TestData/Preprocessor/wixcop.settings.xml deleted file mode 100644 index 9d3ad496..00000000 --- a/src/test/wixcop/TestData/Preprocessor/wixcop.settings.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/src/test/wixcop/TestData/SingleFile/ConvertedSingleFile.wxs b/src/test/wixcop/TestData/SingleFile/ConvertedSingleFile.wxs deleted file mode 100644 index aacb68fa..00000000 --- a/src/test/wixcop/TestData/SingleFile/ConvertedSingleFile.wxs +++ /dev/null @@ -1,60 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/test/wixcop/TestData/SingleFile/SingleFile.wxs b/src/test/wixcop/TestData/SingleFile/SingleFile.wxs deleted file mode 100644 index 310ae811..00000000 --- a/src/test/wixcop/TestData/SingleFile/SingleFile.wxs +++ /dev/null @@ -1,61 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/test/wixcop/WixCopFixture.cs b/src/test/wixcop/WixCopFixture.cs deleted file mode 100644 index 1fa49be9..00000000 --- a/src/test/wixcop/WixCopFixture.cs +++ /dev/null @@ -1,162 +0,0 @@ -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); - } - } - - [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(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, - SettingFile1 = Path.Combine(folder, "wixcop.settings.xml"), - 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 string SettingFile1 { get; set; } - - public int Execute(out List messages) - { - var argList = new List(); - - if (this.FixErrors) - { - argList.Add("-f"); - } - - if (!String.IsNullOrEmpty(this.SettingFile1)) - { - argList.Add($"-set1{this.SettingFile1}"); - } - - 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 deleted file mode 100644 index 012ef4db..00000000 --- a/src/test/wixcop/WixCopTests.csproj +++ /dev/null @@ -1,40 +0,0 @@ - - - - - - net461 - false - embedded - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- cgit v1.2.3-55-g6feb