Last active
September 22, 2019 15:06
-
-
Save ishikawa/01214a8c1275c43c0821c2724a614bac to your computer and use it in GitHub Desktop.
Compile different function on Mix env and test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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