Skip to content

Commit

Permalink
Allow repeat-until-failure to be used
Browse files Browse the repository at this point in the history
Mimic won't actually reset modules when repeat-until-failure is used

This means that modules are not cleaned up and coverage is not
calculated
  • Loading branch information
edgurgel committed Jul 27, 2024
1 parent 55ebadd commit 98daebb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/mimic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,12 @@ defmodule Mimic do
with :ok <- ensure_module_not_copied(module),
{:module, module} <- Code.ensure_compiled(module),
:ok <- Mimic.Server.mark_to_copy(module) do
ExUnit.after_suite(fn _ -> Mimic.Server.reset(module) end)
if ExUnit.configuration()[:repeat_until_failure] do
ExUnit.after_suite(fn _ -> Mimic.Server.soft_reset(module) end)
else
ExUnit.after_suite(fn _ -> Mimic.Server.reset(module) end)
end

:ok
else
{:error, {:module_already_copied, _module}} ->
Expand Down
10 changes: 10 additions & 0 deletions lib/mimic/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ defmodule Mimic.Server do
GenServer.call(__MODULE__, {:reset, module}, @long_timeout)
end

@spec soft_reset(module) :: :ok
def soft_reset(module) do
GenServer.call(__MODULE__, {:soft_reset, module}, @long_timeout)
end

@spec mark_to_copy(module) :: :ok | {:error, {:module_already_copied, module}}
def mark_to_copy(module) do
GenServer.call(__MODULE__, {:mark_to_copy, module}, @long_timeout)
Expand Down Expand Up @@ -429,6 +434,11 @@ defmodule Mimic.Server do
{:reply, :ok, %{state | verify_on_exit: MapSet.put(state.verify_on_exit, pid)}}
end

def handle_call({:soft_reset, _module}, _from, state) do
state = %{state | expectations: %{}, stubs: %{}, mode: :private, global_pid: nil}
{:reply, :ok, state}
end

def handle_call({:reset, module}, _from, state) do
state = %{state | modules_to_be_copied: MapSet.delete(state.modules_to_be_copied, module)}

Expand Down
4 changes: 3 additions & 1 deletion test/dsl_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
defmodule Mimic.DSLTest do
use ExUnit.Case, async: true
use ExUnit.Case, async: false
use Mimic.DSL

setup :set_mimic_private

test "basic example" do
stub(Calculator.add(_x, _y), do: :stub)
expect Calculator.add(x, y), do: x + y
Expand Down
2 changes: 1 addition & 1 deletion test/mimic_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Mimic.Test do
use ExUnit.Case, async: true
use ExUnit.Case, async: false
import Mimic

describe "no stub or expects private mode" do
Expand Down

0 comments on commit 98daebb

Please sign in to comment.