Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I avoid the "warning: redefining module Foo" when running ExUnit tests multiple times in one process

I wish to be able to run the ExUnit tests multiple times from within a running process, such as iex.

To do this I have code that looks somewhat like this:

def test do
  # Unload test files
  ["test"]
  |> Mix.Utils.extract_files("*")
  |> Enum.map(&Path.expand/1)
  |> Code.unload_files

  # Reenable tasks
  ~w(loadpaths deps.loadpaths test)
  |> Enum.map(&Mix.Task.reenable/1)

  # Run the test suite
  Mix.Task.run("test", args)

  # Cleanup
  :elixir_config.put(:at_exit, [])
end

This works, but prints the test/my_app/foo_test.exs:1 warning: redefining module FooTest for each module defined in my test files.

I thought that as I had unloaded those files from the :elixir_code_server these warnings would not be raised, but this is not the case.

How might I silence or avoid these warnings without resorting to methods such as silencing stderr?

It seems there is a compiler flag that I can use to suppress these warnings, but there is no clear public API for setting this flag. It seems we can disable these warning messages, there there is no clear API for doing so.

See elixir_compiler:get_opt/1
https://github.com/elixir-lang/elixir/blob/master/lib/elixir/src/elixir_compiler.erl#L8-L13

See elixir_module:check_module_availability/3 where it checks elixir_compiler:get_opt(ignore_module_conflict)
https://github.com/elixir-lang/elixir/blob/master/lib/elixir/src/elixir_module.erl#L408-L418

like image 247
lpil Avatar asked Apr 28 '16 22:04

lpil


1 Answers

After some trial-and-error storming, this is the solution that works for me:

Code.compiler_options(ignore_module_conflict: true)

Cheers!

like image 186
mindreframer Avatar answered Sep 28 '22 18:09

mindreframer