Trebejo.Runner behaviour (Trebejo v2.0.0)

Copy Markdown View Source

Behaviour for executing external commands.

The default runner is Trebejo.Util.run_cmd/3 (via Arrea.Command). Tests can swap in Trebejo.Mox.Runner to stub specific calls without touching real binaries.

Implementing

defmodule MyApp.MockRunner do
  @behaviour Trebejo.Runner

  @impl true
  def run(_cmd_name, _args, _opts) do
    {:ok, "mocked output\n", 0}
  end
end

Then set Trebejo.run_with(MyApp.MockRunner) in your test setup.

Contract

run/3 returns:

  • {:ok, stdout, exit_code} on completion
  • {:error, reason} on failure (:timeout, :enoent, ...)

The runner must be safe to call from any process; it owns the port lifecycle.

Summary

Functions

Run a command using the active runner.

Swap the active runner. Use only in tests.

Callbacks

run(binary, list, keyword)

@callback run(binary(), [binary()], keyword()) ::
  {:ok, binary(), non_neg_integer()} | {:error, term()}

Functions

run(cmd_name, args, opts \\ [])

@spec run(binary(), [binary()], keyword()) ::
  {:ok, binary(), non_neg_integer()} | {:error, term()}

Run a command using the active runner.

The active runner defaults to Trebejo.Util.Runner (a thin adapter that delegates to Trebejo.Util.run_cmd/3). Tests can swap in Trebejo.Mox.Runner by calling set_runner/1 in their setup.

runner()

@spec runner() :: module()

set_runner(module)

@spec set_runner(module()) :: :ok

Swap the active runner. Use only in tests.