PROPOSAL: Allow passing ExUnit custom formatter options when configuring ExUnit

34 views
Skip to first unread message

Devon Estes

unread,
Aug 17, 2018, 7:07:52 AM8/17/18
to elixir-lang-core
I'm writing a custom formatter for ExUnit and there is some configuration I would like the user to be able to do. However, it would be much better for the configuration to be able to happen by passing arguments to the `init/1` callback so we don't need to rely on compile time application config for this. Right now, since all custom formatters are just passed the ExUnit configuration when they're started in `ex_unit/lib/ex_unit/event_manager.ex`, and the user can't control how these custom formatters are started, I don't see a way to do this.

My proposal is to make a small change to the ExUnit configuration for formatters. Instead of only allowing a list of module names representing the GenServer we're going to use as formatters, we can also accept a tuple that contains the module name of our GenServer as well as configuration for that formatter. For example:

ExUnit.configure(formatters: [ExUnit.CLIFormatter, {CustomFormatter, %{print_results: false, email_results: true, api_key: System.get_env("API_KEY")}}])

Since then these options would be passed to our CustomFormatter when it's started, it would be easy enough for me to pick them up by doing:

def init(opts) do
  {_, my_opts} = Enum.find(opts[:formatters], {nil, %{}}, fn
    {__MODULE__, _} -> true
    _ -> false
  end)

  {:ok, my_opts}
end

José Valim

unread,
Aug 17, 2018, 7:14:45 AM8/17/18
to elixir-l...@googlegroups.com
I believe currently we pass all of the options given to configure. So you could do:

ExUnit.configure custom_formatter: %{print_results: ...}

Would this work? Is this acceptable?



José Valim
Skype: jv.ptec
Founder and Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/5e0ad079-0941-4fc9-b71f-02cb4f641c1f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Devon Estes

unread,
Aug 17, 2018, 9:01:07 AM8/17/18
to elixir-lang-core
Yeah, that it would work since the options are just a keyword list. B I worry that because that behavior isn't documented it could be confusing to folks. That's why I was thinking there could be an official, documented way of passing these options to custom formatters.

Alternatively, we could just document that `ExUnit.configure/1` accepts the currently documented options, but can also accept any other options that might be needed as configuration for cases like this.


On Friday, August 17, 2018 at 1:14:45 PM UTC+2, José Valim wrote:
I believe currently we pass all of the options given to configure. So you could do:

ExUnit.configure custom_formatter: %{print_results: ...}

Would this work? Is this acceptable?



José Valim
Skype: jv.ptec
Founder and Director of R&D

On Fri, Aug 17, 2018 at 1:07 PM, Devon Estes <devon....@gmail.com> wrote:
I'm writing a custom formatter for ExUnit and there is some configuration I would like the user to be able to do. However, it would be much better for the configuration to be able to happen by passing arguments to the `init/1` callback so we don't need to rely on compile time application config for this. Right now, since all custom formatters are just passed the ExUnit configuration when they're started in `ex_unit/lib/ex_unit/event_manager.ex`, and the user can't control how these custom formatters are started, I don't see a way to do this.

My proposal is to make a small change to the ExUnit configuration for formatters. Instead of only allowing a list of module names representing the GenServer we're going to use as formatters, we can also accept a tuple that contains the module name of our GenServer as well as configuration for that formatter. For example:

ExUnit.configure(formatters: [ExUnit.CLIFormatter, {CustomFormatter, %{print_results: false, email_results: true, api_key: System.get_env("API_KEY")}}])

Since then these options would be passed to our CustomFormatter when it's started, it would be easy enough for me to pick them up by doing:

def init(opts) do
  {_, my_opts} = Enum.find(opts[:formatters], {nil, %{}}, fn
    {__MODULE__, _} -> true
    _ -> false
  end)

  {:ok, my_opts}
end

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.

José Valim

unread,
Aug 17, 2018, 9:16:25 AM8/17/18
to elixir-l...@googlegroups.com
Yes, we should document that the formatters receive all configuration and custom keys can be added on configure. +1
--
Reply all
Reply to author
Forward
0 new messages