lpil
lpil

Reputation: 1857

Loading mix dependencies marked as test only when the MIX_ENV was not originally set to "test"

I wish to programmatically run the mix test task within my Elixir application. This can be done with Mix.Tasks.Test.run/1, though attempting to do so without first setting the MIX_ENV environmental variable results in the task refusing to run.

We can set the env with System.put_env/2, but then the application will crash once it finds a reference to a module defined in a dependancy marked as test only.

How can I load these dependancies in this situation?

Upvotes: 4

Views: 676

Answers (1)

José Valim
José Valim

Reputation: 51439

We can set the env with System.put_env/2, but then the application will crash once it finds a reference to a module defined in a dependancy marked as test only.

That's on purpose. You need to set the environment variable before Mix is started, otherwise Mix will load the wrong dependencies.

If you are creating a new task, you can tell Mix what is the preferred environment to run it by setting [preferred_cli_env: [my_task: :test]] in your project function. Other than that, you have no option besides setting MIX_ENV explicitly.

Upvotes: 4

Related Questions