From bb8f326e4704a22e0e645308d6960cbfa36af768 Mon Sep 17 00:00:00 2001 From: philo Date: Fri, 22 Jul 2016 12:07:03 +0100 Subject: [PATCH] Implements AddMessage on the AppVeyorProvider This supports adding messages to the AppVeyor build log, messages can be added as Information, Warning or Error --- .../Build/AppVeyor/AppVeyorProviderTests.cs | 142 ++++++++++++++++++ .../AppVeyor/AppVeyorMessageCategoryType.cs | 26 ++++ .../Build/AppVeyor/AppVeyorProvider.cs | 38 +++++ .../AppVeyorProviderAddMessageExtensions.cs | 54 +++++++ .../Build/AppVeyor/IAppVeyorProvider.cs | 9 ++ src/Cake.Common/Cake.Common.csproj | 2 + 6 files changed, 271 insertions(+) create mode 100644 src/Cake.Common/Build/AppVeyor/AppVeyorMessageCategoryType.cs create mode 100644 src/Cake.Common/Build/AppVeyor/AppVeyorProviderAddMessageExtensions.cs diff --git a/src/Cake.Common.Tests/Unit/Build/AppVeyor/AppVeyorProviderTests.cs b/src/Cake.Common.Tests/Unit/Build/AppVeyor/AppVeyorProviderTests.cs index e4d6c4ac09..441b6309f9 100644 --- a/src/Cake.Common.Tests/Unit/Build/AppVeyor/AppVeyorProviderTests.cs +++ b/src/Cake.Common.Tests/Unit/Build/AppVeyor/AppVeyorProviderTests.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. + +using System; using Cake.Common.Build.AppVeyor; using Cake.Common.Tests.Fixtures.Build; using Cake.Core; @@ -233,5 +235,145 @@ public void Should_Throw_If_Not_Running_On_AppVeyor() "The current build is not running on AppVeyor."); } } + + public sealed class TheAddMessageMethod + { + [Fact] + public void Should_Throw_If_Message_Is_Null() + { + // Given + var fixture = new AppVeyorFixture(); + var appVeyor = fixture.CreateAppVeyorService(); + + // When + var result = Record.Exception(() => appVeyor.AddMessage(null)); + + // Then + Assert.IsArgumentNullException(result, "message"); + } + + [Fact] + public void Should_Throw_If_Message_Is_Empty() + { + // Given + var fixture = new AppVeyorFixture(); + var appVeyor = fixture.CreateAppVeyorService(); + + // When + var result = Record.Exception(() => appVeyor.AddMessage("")); + + // Then + Assert.IsCakeException(result, "The message cannot be empty."); + } + + [Fact] + public void Should_Throw_If_Not_Running_On_AppVeyor() + { + // Given + var fixture = new AppVeyorFixture(); + var appVeyor = fixture.CreateAppVeyorService(); + + // When + var result = Record.Exception(() => appVeyor.AddMessage("Hello world")); + + // Then + Assert.IsExceptionWithMessage(result, + "The current build is not running on AppVeyor."); + } + + [Theory] + [InlineData("Hello world", AppVeyorMessageCategoryType.Information, null, "\"Hello world\" -Category \"Information\"")] + [InlineData("Hello world", AppVeyorMessageCategoryType.Warning, null, "\"Hello world\" -Category \"Warning\"")] + [InlineData("Hello world", AppVeyorMessageCategoryType.Error, null, "\"Hello world\" -Category \"Error\"")] + [InlineData("Hello world", AppVeyorMessageCategoryType.Error, "Details of message", "\"Hello world\" -Category \"Error\" -Details \"Details of message\"")] + public void Should_Add_Message(string message, AppVeyorMessageCategoryType category, string details, string args) + { + // Given + var fixture = new AppVeyorFixture(); + fixture.IsRunningOnAppVeyor(); + var appVeyor = fixture.CreateAppVeyorService(); + + // When + appVeyor.AddMessage(message, category, details); + + // Then + fixture.ProcessRunner.Received(1).Start( + Arg.Is(p => p.FullPath == "appveyor"), + Arg.Is(p => p.Arguments.Render() == string.Format("AddMessage {0}", args))); + } + + [Fact] + public void Should_Add_InformationalMessage() + { + // Given + var fixture = new AppVeyorFixture(); + fixture.IsRunningOnAppVeyor(); + var appVeyor = fixture.CreateAppVeyorService(); + const string message = "Hello world"; + + // When + appVeyor.AddInformationalMessage(message); + + // Then + fixture.ProcessRunner.Received(1).Start( + Arg.Is(p => p.FullPath == "appveyor"), + Arg.Is(p => p.Arguments.Render() == string.Format("AddMessage \"{0}\" -Category \"Information\"", message))); + } + + [Fact] + public void Should_Add_WarningMessage() + { + // Given + var fixture = new AppVeyorFixture(); + fixture.IsRunningOnAppVeyor(); + var appVeyor = fixture.CreateAppVeyorService(); + const string message = "Hello world"; + + // When + appVeyor.AddWarningMessage(message); + + // Then + fixture.ProcessRunner.Received(1).Start( + Arg.Is(p => p.FullPath == "appveyor"), + Arg.Is(p => p.Arguments.Render() == string.Format("AddMessage \"{0}\" -Category \"Warning\"", message))); + } + + [Fact] + public void Should_Add_ErrorMessage() + { + // Given + var fixture = new AppVeyorFixture(); + fixture.IsRunningOnAppVeyor(); + var appVeyor = fixture.CreateAppVeyorService(); + const string message = "Hello world"; + + // When + appVeyor.AddErrorMessage(message); + + // Then + fixture.ProcessRunner.Received(1).Start( + Arg.Is(p => p.FullPath == "appveyor"), + Arg.Is(p => p.Arguments.Render() == string.Format("AddMessage \"{0}\" -Category \"Error\"", message))); + } + + [Fact] + public void Should_Add_ErrorMessageWithException() + { + // Given + var fixture = new AppVeyorFixture(); + fixture.IsRunningOnAppVeyor(); + var appVeyor = fixture.CreateAppVeyorService(); + const string message = "Hello world"; + var exception = new CakeException("This is an exception", new ArgumentException()); + + // When + appVeyor.AddErrorMessage(message, exception); + + // Then + fixture.ProcessRunner.Received(1).Start( + Arg.Is(p => p.FullPath == "appveyor"), + Arg.Is(p => p.Arguments.Render() == string.Format("AddMessage \"{0}\" -Category \"Error\" -Details \"{1}\"", message, exception.ToString()))); + } + } } } diff --git a/src/Cake.Common/Build/AppVeyor/AppVeyorMessageCategoryType.cs b/src/Cake.Common/Build/AppVeyor/AppVeyorMessageCategoryType.cs new file mode 100644 index 0000000000..7f9f3accf1 --- /dev/null +++ b/src/Cake.Common/Build/AppVeyor/AppVeyorMessageCategoryType.cs @@ -0,0 +1,26 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. +namespace Cake.Common.Build.AppVeyor +{ + /// + /// AppVeyor AddMessage categories + /// + public enum AppVeyorMessageCategoryType + { + /// + /// Informational message + /// + Information, + + /// + /// Warning message + /// + Warning, + + /// + /// Error message + /// + Error + } +} \ No newline at end of file diff --git a/src/Cake.Common/Build/AppVeyor/AppVeyorProvider.cs b/src/Cake.Common/Build/AppVeyor/AppVeyorProvider.cs index 81939f5286..c5ecdd7af8 100644 --- a/src/Cake.Common/Build/AppVeyor/AppVeyorProvider.cs +++ b/src/Cake.Common/Build/AppVeyor/AppVeyorProvider.cs @@ -155,5 +155,43 @@ public void UpdateBuildVersion(string version) // Start the process. _processRunner.Start("appveyor", new ProcessSettings { Arguments = arguments }); } + + /// + /// Adds a message to the AppVeyor build. Messages can be categorised as: Information, Warning or Error + /// + /// A short message to display + /// The category of the message + /// Additional message details + public void AddMessage(string message, AppVeyorMessageCategoryType category = AppVeyorMessageCategoryType.Information, string details = null) + { + if (message == null) + { + throw new ArgumentNullException("message"); + } + if (string.IsNullOrWhiteSpace(message)) + { + throw new CakeException("The message cannot be empty."); + } + + if (!IsRunningOnAppVeyor) + { + throw new CakeException("The current build is not running on AppVeyor."); + } + + // Build the arguments. + var arguments = new ProcessArgumentBuilder(); + arguments.Append("AddMessage"); + arguments.AppendQuoted(message); + arguments.Append("-Category"); + arguments.AppendQuoted(category.ToString()); + if (!string.IsNullOrWhiteSpace(details)) + { + arguments.Append("-Details"); + arguments.AppendQuoted(details); + } + + // Start the process. + _processRunner.Start("appveyor", new ProcessSettings { Arguments = arguments }); + } } } diff --git a/src/Cake.Common/Build/AppVeyor/AppVeyorProviderAddMessageExtensions.cs b/src/Cake.Common/Build/AppVeyor/AppVeyorProviderAddMessageExtensions.cs new file mode 100644 index 0000000000..31f7ed2e08 --- /dev/null +++ b/src/Cake.Common/Build/AppVeyor/AppVeyorProviderAddMessageExtensions.cs @@ -0,0 +1,54 @@ +using System; + +namespace Cake.Common.Build.AppVeyor +{ + /// + /// AddMessage extension methods for the IAppVeyorProvider + /// + public static class AppVeyorProviderAddMessageExtensions + { + /// + /// Adds an informational message to the AppVeyor build log + /// + /// The AppVeyor provider + /// the message + /// The args + public static void AddInformationalMessage(this IAppVeyorProvider provider, string format, params object[] args) + { + provider.AddMessage(string.Format(format, args)); + } + + /// + /// Adds a warning message to the AppVeyor build log + /// + /// The AppVeyor provider + /// the message + /// The args + public static void AddWarningMessage(this IAppVeyorProvider provider, string format, params object[] args) + { + provider.AddMessage(string.Format(format, args), AppVeyorMessageCategoryType.Warning); + } + + /// + /// Adds a warning message to the AppVeyor build log + /// + /// The AppVeyor provider + /// the message + /// The args + public static void AddErrorMessage(this IAppVeyorProvider provider, string format, params object[] args) + { + provider.AddMessage(string.Format(format, args), AppVeyorMessageCategoryType.Error); + } + + /// + /// Adds a warning message to the AppVeyor build log + /// + /// The AppVeyor provider + /// the message + /// The exception + public static void AddErrorMessage(this IAppVeyorProvider provider, string message, Exception exception) + { + provider.AddMessage(message, AppVeyorMessageCategoryType.Error, exception.ToString()); + } + } +} \ No newline at end of file diff --git a/src/Cake.Common/Build/AppVeyor/IAppVeyorProvider.cs b/src/Cake.Common/Build/AppVeyor/IAppVeyorProvider.cs index d77df9227a..f736f98a5f 100644 --- a/src/Cake.Common/Build/AppVeyor/IAppVeyorProvider.cs +++ b/src/Cake.Common/Build/AppVeyor/IAppVeyorProvider.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. + using Cake.Common.Build.AppVeyor.Data; using Cake.Core.IO; @@ -45,5 +46,13 @@ public interface IAppVeyorProvider /// /// The new build version. void UpdateBuildVersion(string version); + + /// + /// Adds a message to the AppVeyor build log. Messages can be categorised as: Information, Warning or Error + /// + /// A short message to display + /// The category of the message + /// Additional message details + void AddMessage(string message, AppVeyorMessageCategoryType category = AppVeyorMessageCategoryType.Information, string details = null); } } diff --git a/src/Cake.Common/Cake.Common.csproj b/src/Cake.Common/Cake.Common.csproj index a595aa3c9b..081f9cfd3e 100644 --- a/src/Cake.Common/Cake.Common.csproj +++ b/src/Cake.Common/Cake.Common.csproj @@ -53,6 +53,8 @@ + +