Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH1102: Implements AddMessage on the AppVeyorProvider #1103

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions src/Cake.Common.Tests/Unit/Build/AppVeyor/AppVeyorProviderTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<CakeException>(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<FilePath>(p => p.FullPath == "appveyor"),
Arg.Is<ProcessSettings>(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<FilePath>(p => p.FullPath == "appveyor"),
Arg.Is<ProcessSettings>(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<FilePath>(p => p.FullPath == "appveyor"),
Arg.Is<ProcessSettings>(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<FilePath>(p => p.FullPath == "appveyor"),
Arg.Is<ProcessSettings>(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<FilePath>(p => p.FullPath == "appveyor"),
Arg.Is<ProcessSettings>(p => p.Arguments.Render() == string.Format("AddMessage \"{0}\" -Category \"Error\" -Details \"{1}\"", message, exception.ToString())));
}
}
}
}
26 changes: 26 additions & 0 deletions src/Cake.Common/Build/AppVeyor/AppVeyorMessageCategoryType.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// AppVeyor AddMessage categories
/// </summary>
public enum AppVeyorMessageCategoryType
{
/// <summary>
/// Informational message
/// </summary>
Information,

/// <summary>
/// Warning message
/// </summary>
Warning,

/// <summary>
/// Error message
/// </summary>
Error
}
}
38 changes: 38 additions & 0 deletions src/Cake.Common/Build/AppVeyor/AppVeyorProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,43 @@ public void UpdateBuildVersion(string version)
// Start the process.
_processRunner.Start("appveyor", new ProcessSettings { Arguments = arguments });
}

/// <summary>
/// Adds a message to the AppVeyor build. Messages can be categorised as: Information, Warning or Error
/// </summary>
/// <param name="message">A short message to display</param>
/// <param name="category">The category of the message</param>
/// <param name="details">Additional message details</param>
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 });
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;

namespace Cake.Common.Build.AppVeyor
{
/// <summary>
/// AddMessage extension methods for the IAppVeyorProvider
/// </summary>
public static class AppVeyorProviderAddMessageExtensions
{
/// <summary>
/// Adds an informational message to the AppVeyor build log
/// </summary>
/// <param name="provider">The AppVeyor provider</param>
/// <param name="format">the message</param>
/// <param name="args">The args</param>
public static void AddInformationalMessage(this IAppVeyorProvider provider, string format, params object[] args)
{
provider.AddMessage(string.Format(format, args));
}

/// <summary>
/// Adds a warning message to the AppVeyor build log
/// </summary>
/// <param name="provider">The AppVeyor provider</param>
/// <param name="format">the message</param>
/// <param name="args">The args</param>
public static void AddWarningMessage(this IAppVeyorProvider provider, string format, params object[] args)
{
provider.AddMessage(string.Format(format, args), AppVeyorMessageCategoryType.Warning);
}

/// <summary>
/// Adds a warning message to the AppVeyor build log
/// </summary>
/// <param name="provider">The AppVeyor provider</param>
/// <param name="format">the message</param>
/// <param name="args">The args</param>
public static void AddErrorMessage(this IAppVeyorProvider provider, string format, params object[] args)
{
provider.AddMessage(string.Format(format, args), AppVeyorMessageCategoryType.Error);
}

/// <summary>
/// Adds a warning message to the AppVeyor build log
/// </summary>
/// <param name="provider">The AppVeyor provider</param>
/// <param name="message">the message</param>
/// <param name="exception">The exception</param>
public static void AddErrorMessage(this IAppVeyorProvider provider, string message, Exception exception)
{
provider.AddMessage(message, AppVeyorMessageCategoryType.Error, exception.ToString());
}
}
}
9 changes: 9 additions & 0 deletions src/Cake.Common/Build/AppVeyor/IAppVeyorProvider.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -45,5 +46,13 @@ public interface IAppVeyorProvider
/// </summary>
/// <param name="version">The new build version.</param>
void UpdateBuildVersion(string version);

/// <summary>
/// Adds a message to the AppVeyor build log. Messages can be categorised as: Information, Warning or Error
/// </summary>
/// <param name="message">A short message to display</param>
/// <param name="category">The category of the message</param>
/// <param name="details">Additional message details</param>
void AddMessage(string message, AppVeyorMessageCategoryType category = AppVeyorMessageCategoryType.Information, string details = null);
}
}
2 changes: 2 additions & 0 deletions src/Cake.Common/Cake.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
<Reference Include="System.IO.Compression.FileSystem" />
</ItemGroup>
<ItemGroup>
<Compile Include="Build\AppVeyor\AppVeyorMessageCategoryType.cs" />
<Compile Include="Build\AppVeyor\AppVeyorProviderAddMessageExtensions.cs" />
<Compile Include="Build\AppVeyor\AppVeyorTestResultsType.cs" />
<Compile Include="Build\AppVeyor\Data\AppVeyorEnvironmentInfo.cs" />
<Compile Include="Build\AppVeyor\AppVeyorInfo.cs" />
Expand Down