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

GH-3279: Cake runner arguments as build script arguments #4142

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Cake.Cli/Cake.Cli.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyName>Cake.Cli</AssemblyName>
<OutputType>Library</OutputType>
Expand Down
45 changes: 43 additions & 2 deletions src/Cake.Frosting.Tests/CakeHostTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// 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 System.Linq;
using Autofac.Core;
using Cake.Cli;
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Core.Packaging;
using Cake.Frosting.Tests.Fakes;
using Cake.Testing;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
Expand Down Expand Up @@ -316,7 +320,7 @@ public void Should_Install_Tools()
}

[Fact]
public void Should_pass_target_within_cakeContext_arguments()
public void Should_Pass_Target_Within_CakeContext_Arguments()
{
// Given
var fixture = new CakeHostFixture();
Expand Down Expand Up @@ -359,5 +363,42 @@ public void Should_Execute_Multiple_Targets_In_Correct_Order(string task0, strin
fixture.Strategy.ExecuteAsync(Arg.Is<CakeTask>(t => t.Name == task2), Arg.Any<ICakeContext>());
});
}

[Fact]
public void The_Version_Option_Should_Call_Version_Feature()
{
// Given
var fixture = new CakeHostFixture();
fixture.RegisterTask<DummyTask>();
fixture.Strategy = Substitute.For<IExecutionStrategy>();

// Replace the Cake.Cli.VersionResolver that CakeHost adds to its service collection
// with a fake so that the call to VersionFeature can be asserted below
fixture.Host.ConfigureServices(services => services.Remove(services.Single(sd => sd.ServiceType == typeof(IVersionResolver))));
fixture.Host.ConfigureServices(services => services.AddSingleton<IVersionResolver, FakeVersionResolver>());

// When
fixture.Run("--version");

// Then
Assert.Collection(fixture.Console.Messages, s => s.Equals("FakeVersion"));
}

[Fact]
public void Should_Pass_Cake_Runner_Argument_And_Value_To_Build_Script()
{
// Given
var fixture = new CakeHostFixture();
fixture.RegisterTask<DummyTask>();
fixture.Strategy = Substitute.For<IExecutionStrategy>();

// When
fixture.Run("--target", "dummytask", "--version=1.2.3");

// Then
fixture.Strategy.Received(1).ExecuteAsync(
Arg.Any<CakeTask>(),
Arg.Is<ICakeContext>(cc => cc.Arguments.HasArgument("version") && cc.Arguments.GetArgument("version") == "1.2.3"));
}
}
}
25 changes: 25 additions & 0 deletions src/Cake.Frosting.Tests/Fakes/FakeVersionResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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.Cli;

namespace Cake.Frosting.Tests.Fakes
{
internal sealed class FakeVersionResolver : IVersionResolver
{
public FakeVersionResolver()
{
}

public string GetVersion()
{
return "FakeVersion";
}

public string GetProductVersion()
{
return "ProductVersion";
}
}
}
5 changes: 5 additions & 0 deletions src/Cake.Frosting/CakeHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public int Run(IEnumerable<string> args)
config.AddExample(Array.Empty<string>());
config.AddExample(new[] { "--verbosity", "quiet" });
config.AddExample(new[] { "--tree" });

// Allow the Cake runner arguments, when found on the command line with a value,
// to be passed directly to the Cake build script as an argument
// eg. "--version=1.2.3" is accessable in the build script by calling Argument("version")
config.Settings.ConvertFlagsToRemainingArguments = true;
});

return app.Run(args);
Expand Down
18 changes: 18 additions & 0 deletions src/Cake.Tests/Unit/ProgramTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using Autofac;
using Cake.Cli;
using Cake.Core;
Expand Down Expand Up @@ -130,5 +131,22 @@ public async Task The_Info_Option_Should_Call_Info_Feature(params string[] args)
// Then
feature.Received(1).Run(fixture.Console);
}

[Fact]
public async void Should_Pass_Cake_Runner_Argument_And_Value_To_Build_Script()
{
// Given
var fixture = new ProgramFixture();
var feature = Substitute.For<IBuildFeature>();
fixture.Overrides.Add(builder => builder.RegisterInstance(feature));

// When
var result = await fixture.Run("--version=1.2.3");

// Then
feature.Received(1).Run(
Arg.Is<ICakeArguments>(arguments => arguments.HasArgument("version") && arguments.GetArguments("version").Contains("1.2.3")),
Arg.Is<BuildFeatureSettings>(settings => settings.BuildHostKind == BuildHostKind.Build));
}
}
}
5 changes: 5 additions & 0 deletions src/Cake/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public async Task<int> Run(string[] args)
config.AddExample(Array.Empty<string>());
config.AddExample(new[] { "build.cake", "--verbosity", "quiet" });
config.AddExample(new[] { "build.cake", "--tree" });

// Allow the Cake runner arguments, when found on the command line with a value,
// to be passed directly to the Cake build script as an argument
// eg. "--version=1.2.3" is accessable in the build script by calling Argument("version")
config.Settings.ConvertFlagsToRemainingArguments = true;
});

return await app.RunAsync(args);
Expand Down