By default, `mix test` requires no command-line arg

From my understanding, writing a custom Mix task requires this implementation:

def run(arg) do
...
end

Without this above implementation, Mix raises:
** (Mix) The task "xyz" does not export run/1

So studying, mix test module, it implemented the same function but I could safely run mix test in my project without command-line option. Why is that?

Thanks! :smiley:

It implements run/1 and then parses the options, with an option parser that knows what to do when args is empty.

2 Likes

the arg is whatever comes after the name, as a string, so the arg for mix test is just "" an empty string. the arity of the function doesn’t define if you have args or not, it’s a behaviour the expected arity is always 1 no matter what.

what you can do is just implement it as

def run(_arg) do
...
end

you can find more details here:
https://hexdocs.pm/mix/Mix.Task.html

2 Likes

Thanks for the clarification! @cevado @NobbZ
It makes so much sense now!

1 Like