File tree 2 files changed +40
-2
lines changed
2 files changed +40
-2
lines changed Original file line number Diff line number Diff line change @@ -38,10 +38,17 @@ defmodule Mix.Shell.IO do
38
38
"""
39
39
def yes? ( message ) do
40
40
put_app
41
- IO . gets ( message <> IO.ANSI . escape ( " [Yn] " ) ) =~ % r /^ ( Y ( es ) ?) ?$ / i
41
+ got_yes? IO . gets ( message <> IO.ANSI . escape ( " [Yn] " ) )
42
42
end
43
43
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
45
52
if Mix.Shell . output_app? do
46
53
IO . puts "==> #{ Mix . project [ :app ] } "
47
54
end
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments