Skip to content

Commit 5dab337

Browse files
author
José Valim
committed
Ensure Mix.Shell.IO do not puke on :eof
1 parent 12f113f commit 5dab337

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

lib/mix/lib/mix/shell/io.ex

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,17 @@ defmodule Mix.Shell.IO do
3838
"""
3939
def yes?(message) do
4040
put_app
41-
IO.gets(message <> IO.ANSI.escape(" [Yn] ")) =~ %r/^(Y(es)?)?$/i
41+
got_yes? IO.gets(message <> IO.ANSI.escape(" [Yn] "))
4242
end
4343

44-
def put_app do
44+
defp got_yes?(answer) when is_binary(answer) do
45+
answer =~ %r/^(Y(es)?)?$/i
46+
end
47+
48+
# The io server may return :eof or :error
49+
defp got_yes?(_), do: false
50+
51+
defp put_app do
4552
if Mix.Shell.output_app? do
4653
IO.puts "==> #{Mix.project[:app]}"
4754
end

lib/mix/test/mix/shell/io_test.exs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Code.require_file "../../test_helper.exs", __DIR__
2+
3+
defmodule Mix.Shell.IOTest do
4+
use MixTest.Case, async: true
5+
6+
import ExUnit.CaptureIO
7+
import Mix.Shell.IO
8+
9+
test "prints info message to stdio" do
10+
assert capture_io(fn ->
11+
info "hello"
12+
end) == "hello\n"
13+
end
14+
15+
test "prints error message to stderr" do
16+
assert capture_io(:stderr, fn ->
17+
error "hello"
18+
end) =~ "hello"
19+
end
20+
21+
test "asks the user with yes?" do
22+
assert capture_io("\n", fn -> yes?("Ok?") end) == "Ok? [Yn] "
23+
assert capture_io("\n", fn -> assert yes?("Ok?") end)
24+
assert capture_io("Yes", fn -> assert yes?("Ok?") end)
25+
assert capture_io("yes", fn -> assert yes?("Ok?") end)
26+
assert capture_io("y", fn -> assert yes?("Ok?") end)
27+
28+
assert capture_io("n", fn -> refute yes?("Ok?") end)
29+
assert capture_io("", fn -> refute yes?("Ok?") end)
30+
end
31+
end

0 commit comments

Comments
 (0)