Skip to content

Commit

Permalink
Adds integration tests for Cake Frosting
Browse files Browse the repository at this point in the history
ecampidoglio committed Dec 4, 2024
1 parent 86fd49e commit 787f4b7
Showing 3 changed files with 204 additions and 0 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -153,3 +153,58 @@ jobs:
string-parameter: 'value'
numeric-parameter: 3
boolean-parameter: true
test-with-frosting:
name: Test with Cake Frosting
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-latest, ubuntu-latest]
env:
project-directory: integrationtests/frosting
steps:
- name: Get the sources
uses: actions/checkout@v1
- name: Install Node 20
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install the .NET 8 SDK
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'
- name: Run a specific Cake Frosting project
uses: ./
with:
project-path: ${{ env.project-directory}}/Build.csproj
- name: Run a specific target
uses: ./
with:
project-path: ${{ env.project-directory}}/Build.csproj
target: Successful-Task
- name: Run with a specific verbosity level
uses: ./
env:
EXPECTED_VERBOSITY: Diagnostic
with:
verbosity: Diagnostic
project-path: ${{ env.project-directory}}/Build.csproj
target: Test-Verbosity
- name: Do a dry run
uses: ./
with:
dry-run: true
project-path: ${{ env.project-directory }}/Build.csproj
target: Test-Dry-Run
- name: Run with custom script parameters
uses: ./
env:
EXPECTED_STRING_ARGUMENT: '''value'''
EXPECTED_NUMERIC_ARGUMENT: '3'
EXPECTED_BOOLEAN_ARGUMENT: 'true'
with:
project-path: ${{ env.project-directory }}/Build.csproj
target: Test-Script-Parameters
arguments: |
string-parameter: 'value'
numeric-parameter: 3
boolean-parameter: true
10 changes: 10 additions & 0 deletions integrationtests/frosting/Build.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RunWorkingDirectory>$(MSBuildProjectDirectory)</RunWorkingDirectory>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Cake.Frosting" Version="4.0.0" />
</ItemGroup>
</Project>
139 changes: 139 additions & 0 deletions integrationtests/frosting/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using System;
using Cake.Common;
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Frosting;

public static class Program
{
public static int Main(string[] args)
{
return new CakeHost()
.UseContext<BuildContext>()
.Run(args);
}
}

public class BuildContext : FrostingContext
{
public BuildContext(ICakeContext context)
: base(context)
{
StringParameter = context.Argument<string>("String-Parameter", null);
NumericParameter = context.Argument<int?>("Numeric-Parameter", null);
BooleanParameter = context.Argument<bool?>("Boolean-Parameter", null);
}

public string StringParameter { get; }

public int? NumericParameter { get; }

public bool? BooleanParameter { get; }

}

[TaskName("Successful-Task")]
public sealed class SuccessfulTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
context.Log.Information("✓ Passed");
}
}

[TaskName("Test-Verbosity")]
public sealed class TestVerbosity : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
var hasExpectedVerbosity = Enum.TryParse(
context.EnvironmentVariable("EXPECTED_VERBOSITY"),
ignoreCase: true,
out Verbosity expectedVerbosity);

if (!hasExpectedVerbosity)
{
throw new Exception(
"✕ The EXPECTED_VERBOSITY environment variable is not set or it doesn't contain a verbosity level");
}

var actualVerbosity = context.Log.Verbosity;

if (expectedVerbosity != actualVerbosity)
{
throw new Exception($"✕ Expected verbosity {expectedVerbosity} but got {actualVerbosity}");
}

context.Log.Information("✓ Passed");
}
}

[TaskName("Test-Dry-Run")]
public sealed class TestDryRun : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
if (!context.IsDryRun())
{
throw new Exception("✕ Expected this to be a dry run, but it isn't");
}

context.Log.Information("✓ Passed");
}
}

[TaskName("Test-Script-Parameters")]
public sealed class TestScriptParameters : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
var expectedStringArgument = context.EnvironmentVariable("EXPECTED_STRING_ARGUMENT");

var hasExpectedNumericArgument = int.TryParse(
context.EnvironmentVariable("EXPECTED_NUMERIC_ARGUMENT"),
out int expectedNumericArgument);

var hasExpectedBooleanArgument = bool.TryParse(
context.EnvironmentVariable("EXPECTED_BOOLEAN_ARGUMENT"),
out bool expectedBooleanArgument);

if (string.IsNullOrEmpty(expectedStringArgument))
{
throw new Exception(
"✕ The EXPECTED_STRING_ARGUMENT environment variable is not set");
}

if (!hasExpectedNumericArgument)
{
throw new Exception(
"✕ The EXPECTED_NUMERIC_ARGUMENT environment variable is not set");
}

if (!hasExpectedBooleanArgument)
{
throw new Exception(
"✕ The EXPECTED_BOOLEAN_ARGUMENT environment variable is not set");
}

if (expectedStringArgument != context.StringParameter)
{
throw new Exception($"✕ Expected string argument {expectedStringArgument} but got {context.StringParameter}");
}

if (expectedNumericArgument != context.NumericParameter)
{
throw new Exception($"✕ Expected numeric argument {expectedNumericArgument} but got {context.NumericParameter}");
}

if (expectedBooleanArgument != context.BooleanParameter)
{
throw new Exception($"✕ Expected boolean argument {expectedBooleanArgument} but got {context.BooleanParameter}");
}

context.Log.Information("✓ Passed");
}
}

[TaskName("Default")]
[IsDependentOn(typeof(SuccessfulTask))]
public class DefaultTask : FrostingTask;

0 comments on commit 787f4b7

Please sign in to comment.