Giter Site home page Giter Site logo

tmds.execfunction's Introduction

Travis NuGet

Tmds.ExecFunction

Tmds.ExecFunction is a library that makes it simple to execute a function in a separate process. This can be interesting for writing tests that require a separate process, or running some code with a different lifetime as the .NET application process. The library is based on the corefx RemoteExecutorTestBase class.

Supported platforms

This library supports .NET Core 2.0+ on Windows and Linux.

Usage

The main method of the library is ExecFunction.Run. It accepts a delegate that is the function to execute in the remote process. The function can have the same signature of a .NET Main: a void/string[] argument, and a void/int/Task/Task<int> return type.

For example:

ExecFunction.Run(() => Console.WriteLine("Hello from child process!"));

The ProcessStartInfo that is used to start the process can be configured, by adding a configuration delegate:

ExecFunction.Run(..., o => o.StartInfo.RedirectStandardOutput = true);

If you want to re-use the same configuration for multiple invocations you can use the FunctionExecutor class.

private FunctionExecutor FunctionExecutor = new FunctionExecutor(o.StartInfo.RedirectStandardOutput = true);

// Now call FunctionExecutor.Run(...).

The configuration allows you to add an OnExit action. For example, you can use this FunctionExecutor in an xunit project and Assert in the child process:

private FunctionExecutor FunctionExecutor = new FunctionExecutor(
    o =>
    {
        o.StartInfo.RedirectStandardError = true;
        o.OnExit = p =>
        {
            if (p.ExitCode != 0)
            {
                string message = $"Function exit code failed with exit code: {p.ExitCode}" + Environment.NewLine +
                                  p.StandardError.ReadToEnd();
                throw new Xunit.Sdk.XunitException(message);
            }
        };
    }
);

[Fact]
public void Test()
{
    FunctionExecutor.Run(
        (string[] args) => 
        {
            Assert.Equal("arg1", args[0]);
            Assert.Equal("arg2", args[1]);
        },
        new string[] { "arg1", "arg2" }
    );
}

When ExecFunction is used from the dotnet host, it will work out-of-the box. To make ExecFunction work from an application host (that is, when you've published your application as a native binary), you need to add a hook in the main function:

static int Main(string[] args)
{
    if (ExecFunction.IsExecFunctionCommand(args))
    {
        return ExecFunction.Program.Main(args);
    }
    else
    {
        ExecFunction.Run(() => System.Console.WriteLine("Hello world!"));
        return 0;
    }
}

NuGet feed

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="tmds" value="https://www.myget.org/F/tmds/api/v3/index.json" />
  </packageSources>
</configuration>

tmds.execfunction's People

Contributors

tmds avatar atifaziz avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.