fireproofsocks
Is it possible to test the version of Elixir being used? How to support both use Mix.Config and import Config?
I recently submitted a PR for an open source Elixir project and the tests failed for older versions of Elixir because I (prematurely) changed the config files to use import Config instead of the older form use Mix.Config.
Elsewhere I have seen code that tests the version of Erlang:
if System.otp_release() >= "22" do
# do something
else
# do something the old way
end
but is there something similar to testing the version of Elixir being used? Or is there some better way to write config files so they can run on older and newer versions of Elixir? The following results in a compile error when I use it inside config/config.exs:
if System.version() >= "1.9.0" do
import Config
else
use Mix.Config
end
Most Liked
cmo
You aware of the version module?
eksperimental
Best way to check if a module is available is to do this IMO:
iex> Code.ensure_loaded(Config)
{:module, Config}
iex> function_exported? Config, :__info__, 1
true
or you can just rely on the result of the first function call.
cmo
if Code.ensure_loaded?(Config), do: ...
fireproofsocks
Unfortunately, this doesn’t work in config/config.exs:
if Code.ensure_loaded?(Config), do: import(Config)
if Code.ensure_loaded?(Mix.Config), do: use(Mix.Config)
results in:
** (CompileError) config/config.exs:39: undefined function config/2 (there is no such import)
I suspect this is because this is inside the config.exs and the app is bootstrapping…
I guess the easy answer is to pin the old version of the app to a tag and get on with life using newer versions…








