简体   繁体   中英

Custom mix task not loading test environment

I am attempting to put together a custom mix task that will execute the mix test task in a specialized manner. My mix task is mix test.pretty .

So far, I've written the world's most basic custom mix task:

defmodule Mix.Tasks.Test.Pretty do
  use Mix.Task

  @shortdoc "Simply runs the Hello.say/0 function"
  def run(_) do
    Mix.env(:test)

    Mix.Task.run("test", ["--formatter", "ExPrettyTest.Formatter"])
  end
end

Unfortunately, when I run the task ( mix test.pretty ) I get this error:

** (RuntimeError) cannot invoke sandbox operation with pool DBConnection.ConnectionPool. To use the SQL Sandbox, configure your repository pool as:

 pool: Ecto.Adapters.SQL.Sandbox (ecto_sql) lib/ecto/adapters/sql/sandbox.ex:491: Ecto.Adapters.SQL.Sandbox.lookup_meta!/1 (ecto_sql) lib/ecto/adapters/sql/sandbox.ex:389: Ecto.Adapters.SQL.Sandbox.mode/2 (elixir) lib/code.ex:767: Code.require_file/2 (elixir) lib/enum.ex:765: Enum."-each/2-lists^foreach/1-0-"/2 (elixir) lib/enum.ex:765: Enum.each/2 (mix) lib/mix/tasks/test.ex:369: Mix.Tasks.Test.run/1 (mix) lib/mix/task.ex:316: Mix.Task.run_task/3 (mix) lib/mix/cli.ex:79: Mix.CLI.run_task/2 (elixir) lib/code.ex:767: Code.require_file/2

It appears that the test environment isn't being loaded up--perhaps test_helper.exs hasn't been loaded?

I can run the task like so: MIX_ENV=test mix test.pretty and it works fine. I'd like to get this working w/o having to put MIX_ENV=test in there every time I run the task. In my head, the entire point of having a custom test was to avoid having to specify that environment and the formatter argument manually.

My workflow is to run mix test , if there are errors, I think run mix test.pretty which gives me sweet, awesome test output using the custom formatter mechanism in ExUnit. Once I've nailed down the errors, I can choose to run the file pretty or not, the individual test pretty or not, etc.

Thanks!

Have you tried setting preferred_cli_env in your mix.exs file?

From: https://hexdocs.pm/mix/master/Mix.Task.html

Probably something like:

preferred_cli_env: [
  "test.pretty": :test
]

in your def project do ... end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM