dcrck

dcrck

Testing the absence of optional dependencies within a library

I am adding various client adapters as optional dependencies for a library, much in the way :tesla does and as described here (wrap each adapter in Code.ensure_loaded?/1). I know the dependency is checked at compile time and always loaded in the library itself, but is there a way to “unload” the dependency during a unit test? I would like to ensure that an error is raised if a user has not added an optional dependency and tries to use its adapter. I have tried using :code.delete and :code.purge with the optional dependencies but that doesn’t seem to work as I thought it would. Maybe I have to delete the files themselves during a test run tagged with :dependency_missing, i.e. mix test –-only dependency_missing? I haven’t been able to find any examples so far about how to do this. Maybe what I’m asking for isn’t practical? Would I have to, say, create separate Mix projects within an umbrella without each optional dependency installed and run the tests I want there? Thanks!

Marked As Solved

dcrck

dcrck

These are all good suggestions! I’m sure they would all work. What I ended up doing is creating a script that runs once for each optional dependency, i.e. elixir check_optional_dependency.exs dependency_name, in CI. It calls Mix.install/2 to install the dependency, then runs a function to see if the adapter module is available. It also makes sure the adapters for the other optional dependencies throw errors when they are used:

try do
  {:ok, _} = adapter.fun(param1, param2)
  raise "#{adapter} should not be available!"
rescue
  UndefinedFunctionError -> :ok
  e -> raise e
end

Also Liked

Asd

Asd

If I were you, I’d just not test it, cause it is too much effort for a single and very simple if.

There is no “good” way to test this, only hacks. I love hacks, so I can suggest these two things

  1. Use mix_tester. Set up two mix projects. One which installs your dependency (you can specify it as {:my_library, path: "path/to/my_library"}) and abc. Another one which installs your library, but not abc. Then compile them and run some basic test in them.
    This approach is slow, since each test run will install abc, set up mix project and run a couple of binaries (hex usually caches dependencies locally, but still does network lookup to check for source hash changes), but it is the most real-world test you can get

  2. Use repatch. In the async: false test introduce a global (or maybe shared) patch on the Code.ensure_loaded?/1 function to return false for ABC argument. Then delete the module and call something like Code.compile_file(ABCAdapter.module_info()[:compile][:source]), then assert that it fails. Then remove the patch. Then call the compile_file again, to bring the module back

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement