Benjamin-Philip

Benjamin-Philip

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:

https://github.com/hexpm/hex/blob/main/test/mix/tasks/hex.user_test.exs#L53-L57

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

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

Marked As Solved

stefanchrobot

stefanchrobot

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.

Also Liked

Benjamin-Philip

Benjamin-Philip

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.

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement