From b673734cce44dd28c1d4d1810da3069324466166 Mon Sep 17 00:00:00 2001 From: Sean Hall Date: Sat, 2 Jan 2021 19:00:16 -0600 Subject: Implement command line for SuppressAllWarnings and WarningsAsError. Make WixRunner.Execute default to setting WarningsAsError to make sure tests are not accidentally causing warnings. --- src/WixToolset.Core.TestPackage/WixRunner.cs | 30 ++++++++-- src/WixToolset.Core/CommandLine/BuildCommand.cs | 68 +++++++++++++++++----- .../LinkerFixture.cs | 1 + .../WixToolsetTest.CoreIntegration/MsiFixture.cs | 1 + .../MsiQueryFixture.cs | 1 + .../MsiTransactionFixture.cs | 2 + .../PayloadFixture.cs | 2 +- .../PreprocessorFixture.cs | 2 +- .../WarningFixture.cs | 63 ++++++++++++++++++++ 9 files changed, 150 insertions(+), 20 deletions(-) create mode 100644 src/test/WixToolsetTest.CoreIntegration/WarningFixture.cs diff --git a/src/WixToolset.Core.TestPackage/WixRunner.cs b/src/WixToolset.Core.TestPackage/WixRunner.cs index a3883cd5..ed7c49b8 100644 --- a/src/WixToolset.Core.TestPackage/WixRunner.cs +++ b/src/WixToolset.Core.TestPackage/WixRunner.cs @@ -21,23 +21,36 @@ namespace WixToolset.Core.TestPackage /// /// /// + /// /// - public static int Execute(string[] args, out List messages) + public static int Execute(string[] args, out List messages, bool warningsAsErrors = true) { var serviceProvider = WixToolsetServiceProviderFactory.CreateServiceProvider(); - var task = Execute(args, serviceProvider, out messages); + var task = Execute(args, serviceProvider, out messages, warningsAsErrors: warningsAsErrors); return task.Result; } /// /// Emulates calling wix.exe with standard backends. + /// This overload always treats warnings as errors. /// /// /// public static WixRunnerResult Execute(params string[] args) + { + return Execute(true, args); + } + + /// + /// Emulates calling wix.exe with standard backends. + /// + /// + /// + /// + public static WixRunnerResult Execute(bool warningsAsErrors, params string[] args) { var serviceProvider = WixToolsetServiceProviderFactory.CreateServiceProvider(); - var exitCode = Execute(args, serviceProvider, out var messages); + var exitCode = Execute(args, serviceProvider, out var messages, warningsAsErrors: warningsAsErrors); return new WixRunnerResult { ExitCode = exitCode.Result, Messages = messages.ToArray() }; } @@ -47,8 +60,9 @@ namespace WixToolset.Core.TestPackage /// /// /// + /// /// - public static Task Execute(string[] args, IWixToolsetCoreServiceProvider coreProvider, out List messages) + public static Task Execute(string[] args, IWixToolsetCoreServiceProvider coreProvider, out List messages, bool warningsAsErrors = true) { coreProvider.AddWindowsInstallerBackend() .AddBundleBackend(); @@ -60,8 +74,14 @@ namespace WixToolset.Core.TestPackage var messaging = coreProvider.GetService(); messaging.SetListener(listener); + var arguments = new List(args); + if (warningsAsErrors) + { + arguments.Add("-wx"); + } + var commandLine = coreProvider.GetService(); - var command = commandLine.CreateCommand(args); + var command = commandLine.CreateCommand(arguments.ToArray()); return command?.ExecuteAsync(CancellationToken.None) ?? Task.FromResult(1); } } diff --git a/src/WixToolset.Core/CommandLine/BuildCommand.cs b/src/WixToolset.Core/CommandLine/BuildCommand.cs index 4064a23c..072accc3 100644 --- a/src/WixToolset.Core/CommandLine/BuildCommand.cs +++ b/src/WixToolset.Core/CommandLine/BuildCommand.cs @@ -549,8 +549,8 @@ namespace WixToolset.Core.CommandLine { if (parser.IsSwitch(arg)) { - var parameter = arg.Substring(1); - switch (parameter.ToLowerInvariant()) + var parameter = arg.Substring(1).ToLowerInvariant(); + switch (parameter) { case "?": case "h": @@ -583,7 +583,7 @@ namespace WixToolset.Core.CommandLine this.BindPaths.Add(bindPath); return true; } - break; + return false; } case "cc": @@ -649,7 +649,7 @@ namespace WixToolset.Core.CommandLine this.PdbType = pdbType; return true; } - break; + return false; } case "nologo": @@ -664,16 +664,22 @@ namespace WixToolset.Core.CommandLine case "sval": // todo: implement return true; + } - case "sw": - case "suppresswarning": - var warning = parser.GetNextArgumentOrError(arg); - if (!String.IsNullOrEmpty(warning)) - { - var warningNumber = Convert.ToInt32(warning); - this.Messaging.SuppressWarningMessage(warningNumber); - } - return true; + if (parameter.StartsWith("sw")) + { + this.ParseSuppressWarning(parameter, "sw".Length, parser); + return true; + } + else if (parameter.StartsWith("suppresswarning")) + { + this.ParseSuppressWarning(parameter, "suppresswarning".Length, parser); + return true; + } + else if (parameter.StartsWith("wx")) + { + this.ParseWarningAsError(parameter, "wx".Length, parser); + return true; } return false; @@ -821,6 +827,42 @@ namespace WixToolset.Core.CommandLine return true; } + + private void ParseSuppressWarning(string parameter, int offset, ICommandLineParser parser) + { + var paramArg = parameter.Substring(offset); + if (paramArg.Length == 0) + { + this.Messaging.SuppressAllWarnings = true; + } + else if (Int32.TryParse(paramArg, out var suppressWarning) && suppressWarning > 0) + { + this.Messaging.SuppressWarningMessage(suppressWarning); + } + else + { + this.Messaging.Write(ErrorMessages.IllegalSuppressWarningId(paramArg)); + parser.ErrorArgument = parameter; + } + } + + private void ParseWarningAsError(string parameter, int offset, ICommandLineParser parser) + { + var paramArg = parameter.Substring(offset); + if (paramArg.Length == 0) + { + this.Messaging.WarningsAsError = true; + } + else if (Int32.TryParse(paramArg, out var elevateWarning) && elevateWarning > 0) + { + this.Messaging.SuppressWarningMessage(elevateWarning); + } + else + { + this.Messaging.Write(ErrorMessages.IllegalSuppressWarningId(paramArg)); + parser.ErrorArgument = parameter; + } + } } } } diff --git a/src/test/WixToolsetTest.CoreIntegration/LinkerFixture.cs b/src/test/WixToolsetTest.CoreIntegration/LinkerFixture.cs index 5e08ca58..d85eb3bf 100644 --- a/src/test/WixToolsetTest.CoreIntegration/LinkerFixture.cs +++ b/src/test/WixToolsetTest.CoreIntegration/LinkerFixture.cs @@ -55,6 +55,7 @@ namespace WixToolsetTest.CoreIntegration var result = WixRunner.Execute(new[] { "build", + "-sw1008", // this is expected for this test Path.Combine(folder, "Package.wxs"), Path.Combine(folder, "PackageComponents.wxs"), "-loc", Path.Combine(folder, "Package.en-us.wxl"), diff --git a/src/test/WixToolsetTest.CoreIntegration/MsiFixture.cs b/src/test/WixToolsetTest.CoreIntegration/MsiFixture.cs index 740d58c7..e26e197f 100644 --- a/src/test/WixToolsetTest.CoreIntegration/MsiFixture.cs +++ b/src/test/WixToolsetTest.CoreIntegration/MsiFixture.cs @@ -162,6 +162,7 @@ namespace WixToolsetTest.CoreIntegration var result = WixRunner.Execute(new[] { "build", + "-sw1079", // TODO: why does this test need to create a second cab which is empty? Path.Combine(folder, "Package.wxs"), Path.Combine(folder, "PackageComponents.wxs"), "-loc", Path.Combine(folder, "Package.en-us.wxl"), diff --git a/src/test/WixToolsetTest.CoreIntegration/MsiQueryFixture.cs b/src/test/WixToolsetTest.CoreIntegration/MsiQueryFixture.cs index 11b1703c..b71b62cb 100644 --- a/src/test/WixToolsetTest.CoreIntegration/MsiQueryFixture.cs +++ b/src/test/WixToolsetTest.CoreIntegration/MsiQueryFixture.cs @@ -357,6 +357,7 @@ namespace WixToolsetTest.CoreIntegration var result = WixRunner.Execute(new[] { "build", + "-sw1031", // this is expected for this test Path.Combine(folder, "DefaultDir", "DefaultDir.wxs"), Path.Combine(folder, "ProductWithComponentGroupRef", "Product.wxs"), "-bindpath", Path.Combine(folder, "SingleFile", "data"), diff --git a/src/test/WixToolsetTest.CoreIntegration/MsiTransactionFixture.cs b/src/test/WixToolsetTest.CoreIntegration/MsiTransactionFixture.cs index 5a29eb9e..7ec0ea93 100644 --- a/src/test/WixToolsetTest.CoreIntegration/MsiTransactionFixture.cs +++ b/src/test/WixToolsetTest.CoreIntegration/MsiTransactionFixture.cs @@ -26,6 +26,7 @@ namespace WixToolsetTest.CoreIntegration var result = WixRunner.Execute(new[] { "build", + "-sw1151", // this is expected for this test Path.Combine(folder, "MsiTransaction", "X64AfterX86Bundle.wxs"), Path.Combine(folder, "BundleWithPackageGroupRef", "Bundle.wxs"), "-bindpath", Path.Combine(folder, "SimpleBundle", "data"), @@ -55,6 +56,7 @@ namespace WixToolsetTest.CoreIntegration var result = WixRunner.Execute(new[] { "build", + "-sw1151", // this is expected for this test Path.Combine(folder, "MsiTransaction", "X86AfterX64Bundle.wxs"), Path.Combine(folder, "BundleWithPackageGroupRef", "Bundle.wxs"), "-bindpath", Path.Combine(folder, "SimpleBundle", "data"), diff --git a/src/test/WixToolsetTest.CoreIntegration/PayloadFixture.cs b/src/test/WixToolsetTest.CoreIntegration/PayloadFixture.cs index 4a9344b9..4fc57c76 100644 --- a/src/test/WixToolsetTest.CoreIntegration/PayloadFixture.cs +++ b/src/test/WixToolsetTest.CoreIntegration/PayloadFixture.cs @@ -61,7 +61,7 @@ namespace WixToolsetTest.CoreIntegration var intermediateFolder = Path.Combine(baseFolder, "obj"); var wixlibPath = Path.Combine(intermediateFolder, @"test.wixlib"); - var result = WixRunner.Execute(new[] + var result = WixRunner.Execute(warningsAsErrors: false, new[] { "build", Path.Combine(folder, "CanonicalizeName.wxs"), diff --git a/src/test/WixToolsetTest.CoreIntegration/PreprocessorFixture.cs b/src/test/WixToolsetTest.CoreIntegration/PreprocessorFixture.cs index e18990d3..a6504cb9 100644 --- a/src/test/WixToolsetTest.CoreIntegration/PreprocessorFixture.cs +++ b/src/test/WixToolsetTest.CoreIntegration/PreprocessorFixture.cs @@ -49,7 +49,7 @@ namespace WixToolsetTest.CoreIntegration var baseFolder = fs.GetFolder(); var intermediateFolder = Path.Combine(baseFolder, "obj"); - var result = WixRunner.Execute(new[] + var result = WixRunner.Execute(warningsAsErrors: false, new[] { "build", Path.Combine(folder, "Package.wxs"), diff --git a/src/test/WixToolsetTest.CoreIntegration/WarningFixture.cs b/src/test/WixToolsetTest.CoreIntegration/WarningFixture.cs new file mode 100644 index 00000000..c5b6c261 --- /dev/null +++ b/src/test/WixToolsetTest.CoreIntegration/WarningFixture.cs @@ -0,0 +1,63 @@ +// 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.CoreIntegration +{ + using System.IO; + using WixBuildTools.TestSupport; + using WixToolset.Core.TestPackage; + using WixToolset.Data; + using Xunit; + + public class WarningFixture + { + [Fact] + public void SuppressedWarningsWithWarningAsErrorsAreNotErrors() + { + var folder = TestData.Get(@"TestData\Payload"); + + using (var fs = new DisposableFileSystem()) + { + var baseFolder = fs.GetFolder(); + var intermediateFolder = Path.Combine(baseFolder, "obj"); + var wixlibPath = Path.Combine(intermediateFolder, @"test.wixlib"); + + var result = WixRunner.Execute(warningsAsErrors: true, new[] + { + "build", + "-sw1152", + Path.Combine(folder, "CanonicalizeName.wxs"), + "-intermediateFolder", intermediateFolder, + "-o", wixlibPath, + }); + + result.AssertSuccess(); + } + } + + [Fact] + public void WarningsAsErrorsTreatsWarningsAsErrors() + { + var folder = TestData.Get(@"TestData\Payload"); + + using (var fs = new DisposableFileSystem()) + { + var baseFolder = fs.GetFolder(); + var intermediateFolder = Path.Combine(baseFolder, "obj"); + var wixlibPath = Path.Combine(intermediateFolder, @"test.wixlib"); + + var result = WixRunner.Execute(warningsAsErrors: true, new[] + { + "build", + Path.Combine(folder, "CanonicalizeName.wxs"), + "-intermediateFolder", intermediateFolder, + "-o", wixlibPath, + }); + + Assert.Equal((int)WarningMessages.Ids.PathCanonicalized, result.ExitCode); + + var message = Assert.Single(result.Messages); + Assert.Equal(MessageLevel.Warning, message.Level); // TODO: is this right? + } + } + } +} -- cgit v1.2.3-55-g6feb