Skip to content

Not properly copying modules with NIFs #51

@frm

Description

@frm

Hey!

I have a project where we use Mimic to copy a few modules, worked perfectly until we tried to copy a module that was basically a NIF module:

# nif module
defmodule Core.Crypto.Ethereum do
  use Rustler, otp_app: :core, crate: :core_crypto_ethereum

  def ens_lookup_address(_), do: :erlang.nif_error(:nif_not_loaded)
end

# test_helpers.exs
# ...
# a bunch of Mimic.copy/1 calls here
Mimic.copy(Core.Crypto.Ethereum)

ExUnit.start()

# test:

defmodule MyTest do
  use Core.DataCase, async: true

  import Mimic

  describe "some function" do
    test "example" do
      expect(Core.Crypto.Ethereum, :ens_lookup_address, fn _ -> {:ok, "example.eth"} end)

      assert true
    end
  end
end

Output:

** (ArgumentError) Module Core.Crypto.Ethereum has not been copied.  See docs for Mimic.copy/1
     code: expect(Ethereum, :ens_lookup_address, fn _addr -> {:ok, "example.eth"} end)
     stacktrace:
       (mimic 1.7.4) lib/mimic.ex:498: Mimic.validate_server_response/2
       test/core/services/my_test.exs:49: (test)

# ...

18:43:45.063 [error] GenServer Mimic.Server terminating
** (stop) {:error_loading_module, Core.Crypto.Ethereum.Mimic.Original.Module, :on_load_failure}
    (mimic 1.7.4) lib/mimic/module.ex:87: Mimic.Module.load_binary/3
    (mimic 1.7.4) lib/mimic/module.ex:57: Mimic.Module.rename_module/2
    (mimic 1.7.4) lib/mimic/module.ex:34: Mimic.Module.replace!/1
    (mimic 1.7.4) lib/mimic/server.ex:504: Mimic.Server.ensure_module_copied/2
    (mimic 1.7.4) lib/mimic/server.ex:360: Mimic.Server.handle_call/3
    (stdlib 4.0.1) gen_server.erl:1146::gen_server.try_handle_call/4
    (stdlib 4.0.1) gen_server.erl:1175::gen_server.handle_msg/6
    (stdlib 4.0.1) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
Last message (from #PID<0.2767.0>): {:expect, {Core.Crypto.Ethereum, :ens_lookup_address, #Function<4.71929683/1 in Core.MyTest."test example"/1>, 1}, 1, #PID<0.2767.0>}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions