Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion lib/mox.ex
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ defmodule Mox do
`$callers` is used to determine the process that actually defined the
expectations.

> #### Parent PIDs {: .tip}
> [Since OTP 25](https://erlang.org/documentation/doc-15.0-rc2/erts-14.3/doc/html/notes.html#erts-13-0),
> `Process.info/2` supports a `:parent` key for retrieving the parent of the given PID.
> Mox started using this in v1.3.0 to determine process tree structures in case `$callers`
> is not available in the process dictionary. This means that even more allowance cases
> are taken care of automatically.

#### Explicit allowances as lazy/deferred functions

Under some circumstances, the process might not have been already started
Expand Down Expand Up @@ -941,7 +948,27 @@ defmodule Mox do

# Find the pid of the actual caller
defp caller_pids do
Process.get(:"$callers", [])
case Process.get(:"$callers") do
nil -> [self() | recursive_parents(self())]
pids when is_list(pids) -> pids
end
end

# A PID with no parent has :undefined as its parent.
defp recursive_parents(:undefined) do
[]
end

defp recursive_parents(pid) when is_pid(pid) do
Process.info(pid, :parent)
rescue
# erlang:process_info(Pid, parent) is not available, as it was released in
# ERTS 13.0 (https://erlang.org/documentation/doc-15.0-rc2/erts-14.3/doc/html/notes.html#erts-13-0)
ArgumentError ->
[]
else
{:parent, parent_pid} ->
[parent_pid | recursive_parents(parent_pid)]
end

## Ownership
Expand Down
6 changes: 0 additions & 6 deletions test/mox_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -846,8 +846,6 @@ defmodule MoxTest do

{:ok, child_pid} =
start_link_no_callers(fn ->
assert_raise Mox.UnexpectedCallError, fn -> CalcMock.add(1, 1) end

receive do
:call_mock ->
add_result = CalcMock.add(1, 1)
Expand Down Expand Up @@ -876,8 +874,6 @@ defmodule MoxTest do
|> stub(:mult, fn _, _ -> :stubbed end)

async_no_callers(fn ->
assert_raise Mox.UnexpectedCallError, fn -> CalcMock.add(1, 1) end

CalcMock
|> allow(parent_pid, self())

Expand All @@ -892,8 +888,6 @@ defmodule MoxTest do

{:ok, child_pid} =
start_link_no_callers(fn ->
assert_raise(Mox.UnexpectedCallError, fn -> CalcMock.add(1, 1) end)

receive do
:call_mock ->
add_result = CalcMock.add(1, 1)
Expand Down
Loading