Skip to content

Instantly share code, notes, and snippets.

@ishikawa
Last active September 22, 2019 15:06
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 ishikawa/01214a8c1275c43c0821c2724a614bac to your computer and use it in GitHub Desktop.
Save ishikawa/01214a8c1275c43c0821c2724a614bac to your computer and use it in GitHub Desktop.
Compile different function on Mix env and test
defmodule MyModule do
@moduledoc false
if Mix.env() == :prod do
@foo Application.fetch_env!(:myapp, :foo)
def foo, do: @foo
else
def foo, do: Application.fetch_env!(:myapp, :foo)
end
end
defmodule MyModuleTest do
use ExUnit.Case
import ExUnit.CaptureIO
@source_filepath __DIR__
|> Path.join("../../lib/myapp/my_module.ex")
|> Path.expand()
test "foo" do
on_exit(fn ->
Mix.env(:test)
end)
# suppress warnings
capture_io(:stderr, fn ->
Mix.env(:prod)
Code.compile_file(@source_filepath)
assert MyModule.foo(), "compile on Mix.env(:prod)"
Mix.env(:test)
Code.compile_file(@source_filepath)
assert MyModule.foo(), "compile on Mix.env(:test)"
end)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment