檢視原始碼 Mix.Shell.Process (Mix v1.16.2)
使用目前處理程序郵件信箱進行通訊的 Mix shell。
此模組提供 Mix shell 實作,使用目前處理程序郵件信箱進行通訊,而非 IO。
例如,當呼叫 Mix.shell().info("hello")
時,以下訊息會傳送至呼叫處理程序
{:mix_shell, :info, ["hello"]}
這在測試中特別有用,讓我們可以確認是否收到特定訊息,而非對擷取的 IO 執行檢查。還有一個 flush/1
函式,負責清除處理程序收件匣中所有與 :mix_shell
相關的訊息。
範例
第一步是將 Mix shell 設定為此模組
Mix.shell(Mix.Shell.Process)
然後,如果 Mix 任務呼叫
Mix.shell().info("hello")
您應該可以在測試中收到它,只要它們在同一個處理程序中執行
assert_receive {:mix_shell, :info, [msg]}
assert msg == "hello"
您也可以在測試中回應提示
send(self(), {:mix_shell_input, :prompt, "Pretty cool"})
Mix.shell().prompt("How cool was that?!")
#=> "Pretty cool"
摘要
函式
執行指定的指令,並將其訊息轉發至目前的處理程序。
將錯誤轉發至目前的處理程序。
清除目前的處理程序中所有 :mix_shell
和 :mix_shell_input
訊息。
將訊息轉發至目前的處理程序。
如果尚未列印目前的應用程式,則列印它。
將訊息轉發至目前的處理程序。
將訊息轉發至目前的處理程序。
函式
執行指定的指令,並將其訊息轉發至目前的處理程序。
將錯誤轉發至目前的處理程序。
清除目前的處理程序中所有 :mix_shell
和 :mix_shell_input
訊息。
如果給定回呼,則會針對每個接收到的訊息呼叫它。
範例
flush(&IO.inspect/1)
將訊息轉發至目前的處理程序。
如果尚未列印目前的應用程式,則列印它。
將訊息轉發至目前的處理程序。
它也會檢查收件匣中是否有符合的輸入訊息
{:mix_shell_input, :prompt, value}
如果沒有,它會中止,因為沒有提供 shell 程序輸入。 value
必須是字串。
範例
以下會以 "Meg"
回應提示 "你叫什麼名字?"
# The response is sent before calling prompt/1 so that prompt/1 can read it
send(self(), {:mix_shell_input, :prompt, "Meg"})
Mix.shell().prompt("What's your name?")
將訊息轉發至目前的處理程序。
它也會檢查收件匣中是否有符合的輸入訊息
{:mix_shell_input, :yes?, value}
如果沒有,它會中止,因為沒有提供 shell 程序輸入。 value
必須是 true
或 false
。
範例
# Send the response to self() first so that yes?/2 will be able to read it
send(self(), {:mix_shell_input, :yes?, true})
Mix.shell().yes?("Are you sure you want to continue?")