Skip to content

Instantly share code, notes, and snippets.

@felegy
Last active May 24, 2020 14:06
Show Gist options
  • Save felegy/911f430a016dc5b8084a892a7f35e0ae to your computer and use it in GitHub Desktop.
Save felegy/911f430a016dc5b8084a892a7f35e0ae to your computer and use it in GitHub Desktop.

Cake Build FakeProcessRunner

The main purpose of this to dump the process command and arguments in string format for later execution or use it (for example use to run inside Docker). I would like to run the system related tests in a build process based on cake generated test commands in separated environments (like Docker swarm service in compose/swarm stack).

Cake dotnet Test command:

DotNetCoreTest(SolutionFile, new DotNetCoreTestSettings
{
    Configuration = configuration,
    Verbosity = DotNetCoreVerbosity.Normal,
    NoBuild = true,
    NoRestore = true
});

and the expected (or similar) output:

FilePath: /Users/felegy/homebrew/bin/dotnet

ProcessSettings.Arguments: test --configuration Release --verbosity normal --no-restore --no-build

How to use it

Clone the gist repo and run the commands in a gists folder.

# Install Cake
dotnet tool install -g Cake.Tool

# Execute build process // in standard mode
dotnet cake

# And run again to dump command
dotnet cake --fake-run=true

How it works

  1. Important to refer the Context in a task declaration:
Task("Default")
    .Does(ctx =>
  1. Use the command constructor can to change context elements:
var tester = new DotNetCoreTester(
        ctx.FileSystem,
        ctx.Environment,
        processRunner, // change the ctx.ProcessRunner => FakeProcessRunner 
        ctx.Tools
    );
  1. Command execution:
tester.Test("", settings);
  1. Dump the command and arguments:
Information($"FilePath:{fakeRunner.FilePath}");
Information($"ProcessSettings.Arguments:{fakeRunner.ProcessSettings.Arguments.Render()}");

In any kind of situation when logging ability is enough, this is a lighter and better solution (thanks a lot @petriashev):

https://stackoverflow.com/questions/50826394/how-to-print-tool-command-line-in-cake/50826495?r=SearchResults#50826495

var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
bool fakerun = Argument("fake-run", false);
using System.Diagnostics;
using System.Collections.Generic;
using Cake.Core.IO;
using Cake.Common.Tools.DotNetCore.Test;
public class ProcessWrapper : IProcess
{
public void Dispose(){}
public int GetExitCode()
{
return 0;
}
public IEnumerable<string> GetStandardError()
{
return new string[] { };
}
public IEnumerable<string> GetStandardOutput()
{
return new string[] { };
}
public void Kill() {}
public void WaitForExit() {}
public bool WaitForExit(int milliseconds)
{
return true;
}
}
public class FakeProcessRunner : IProcessRunner
{
public FilePath FilePath { get; private set; }
public ProcessSettings ProcessSettings { get; private set; }
public IProcess Start(FilePath filePath, ProcessSettings settings)
{
this.FilePath = filePath;
this.ProcessSettings = settings;
return new ProcessWrapper();
}
}
Task("Default")
.Does(ctx =>
{
IProcessRunner processRunner;
FakeProcessRunner fakeRunner = new FakeProcessRunner();
if(fakerun) // Fake it
{
processRunner = fakeRunner;
}
else // Run it
{
processRunner = ctx.ProcessRunner;
}
var tester = new DotNetCoreTester(
ctx.FileSystem,
ctx.Environment,
processRunner,
ctx.Tools
);
var settings = new DotNetCoreTestSettings
{
Configuration = configuration,
Verbosity = DotNetCoreVerbosity.Normal,
NoBuild = false,
NoRestore = false
};
tester.Test("", settings);
if(fakerun)
{
Information($"FilePath:{fakeRunner.FilePath}");
Information($"ProcessSettings.Arguments:{fakeRunner.ProcessSettings.Arguments.Render()}");
}
});
RunTarget(target);
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>
</Project>
namespace ProcessRunner
{
using System.Diagnostics;
using System.Collections.Generic;
using Xunit;
public class ProcessRunnerTests
{
[Fact]
public void Test1()
{
}
}
}
@felegy
Copy link
Author

felegy commented May 24, 2020

In any kind of situation when logging ability is enough, this is a lighter and better solution (thanks a lot @petriashev):

https://stackoverflow.com/questions/50826394/how-to-print-tool-command-line-in-cake/50826495?r=SearchResults#50826495

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment