dcrck

dcrck

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!

Showing Posts 1 to 6

Asd

Asd

Why do you have a specific error in the first place?

Consider you have an optional dependency named ABC and you have an adapter called ABCAdapter, then you can just do

if Code.ensure_loaded?(ABC) do
  defmodule ABCAdapter do
    ...
  end
end

If your library user writes code like ABCAdapter.call(...) in their own project, where your library is installed as a dependency, but abc is not installed, there will be no compile-time error, only a runtime one, and this error would be self-descriptive, saying something like “Module ABCAdapter is undefined”. Why do you raise a special error and why do you want to test it?

dcrck

dcrck OP

What you described is exactly what I’m doing at the moment. In this case I’d want to write this test case in the library:

test “fails to run if ABC is missing” do
  assert_raise UndefinedFunctionError, fn → ABCAdapter.call(…) end
end

I’d want to test this because I’d want to make sure that optional dependencies are truly optional, as well as automate this in CI. Maybe it’s not a common use case since I don’t see any other “adapter” pattern libraries that have a test like this.

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

LostKobrakai

LostKobrakai

You might be able to work with the --no-optional-deps flag on mix compile. You cannot toggle that out at runtime though. I generally only use that to test for compile time errors and warnings, but running tests might be possible as well.

mudasobwa

mudasobwa

Creator of Cure

Introduce the separate env :ci, exclude this library from it, mark the tests with @tag :ci and run MIX_ENV=ci mix test –-only ci.

dcrck

dcrck OP

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
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
psy-q
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New

Other Trending Topics Top

mudasobwa
Since there is no ability to insert an actionable poll (or I am banned from it), here is the text version of it, please reply in comments...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews