Skip to content

Instantly share code, notes, and snippets.

@garthk
Created July 10, 2020 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garthk/b14af4dec0e00ee0e181ebc19e81d484 to your computer and use it in GitHub Desktop.
Save garthk/b14af4dec0e00ee0e181ebc19e81d484 to your computer and use it in GitHub Desktop.
Elixir test PID capture
defmodule PidSnitchFormatter do
@moduledoc """
Report test PIDs?
```bash
mix test --formatter PidSnitchFormatter
```
See `ExUnit.CLIFormatter` for protocol details.
"""
use GenServer
@impl GenServer
def init(opts), do: {:ok, opts}
def handle_trace({:trace, _pid, :call, {ExUnit.Runner, :exec_test, [_test]}}, fmt_pid) do
# ignore
fmt_pid
end
def handle_trace({:trace, pid, :return_from, {ExUnit.Runner, :exec_test, 1}, test}, fmt_pid) do
IO.puts("#{inspect(pid)} at #{test.tags[:file]}:#{test.tags[:line]}")
fmt_pid
end
@impl GenServer
def handle_cast(message, opts)
# test protocol; see ExUnit.CLIFormatter
def handle_cast({:suite_started, _opts}, opts) do
:dbg.stop_clear() |> IO.inspect(label: "#{__ENV__.file}:#{__ENV__.line}")
:dbg.tracer(:process, {&handle_trace/2, self()})
:dbg.tpl(ExUnit.Runner, :exec_test, [{:_, [], [{:return_trace}]}])
:dbg.p(:all, :call)
{:noreply, opts}
end
def handle_cast({:suite_finished, _, _}, opts) do
:dbg.stop_clear()
{:noreply, opts}
end
def handle_cast(_message, opts) do
{:noreply, opts}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment