Get mix task output so far for tests

I am writing a new (sub)task for the hex client, which I have some trouble testing.

The task has some output, using which I need to make a HTTP POST request (in the test).
The task will only complete after I make the HTTP request.

Now, how do I get the mix output so far, so that I can write tests for this task.

I have noticed that in the hex tests that prompt inputs are mocked as follows:

Perhaps I can send a :mix_shell_output msg and receive a response?

PS: I also didn’t find any docs for this idiom.

It seems to be Mix-specific thing:

def prompt(message) do
  print_app()
  send(process(), {:mix_shell, :prompt, [message]})

  receive do
    {:mix_shell_input, :prompt, response} -> response
  after
    0 -> raise "no shell process input given for prompt/1"
  end
end

Not sure how internal this is, but seems like something that you can use to mock user input.

I think you misunderstood. I want the mix shell output. Thanks anyway!

I talked to Eric (@ericmj) about this.

Turns out that all mix shell output is sent to the test process.
That means I can for example do things like this:

recieve do
  {:mix_shell, :info, [msg | _ ]} -> IO.inspect(msg)
end

Or an assert_recieved:

assert_received {:mix_shell, :info, ["Some description\n"]}

Do note that I am unsure if this solution is hex specific or not.

1 Like

Sorry for the confusion. I did misread the question. Seems Mix.Shell.Process is exactly what you need:

This is mainly useful in tests, allowing us to assert if given messages were received or not instead of performing checks on some captured IO. Since we need to guarantee a clean slate between tests, there is also a flush/1 function responsible for flushing all :mix_shell related messages from the process inbox.

It’s not Hex-specific at all.

2 Likes