Skip to content

Commit 1449d71

Browse files
committed
Change some tests to use @tag :capture_io
1 parent 2d4e2b0 commit 1449d71

File tree

2 files changed

+78
-88
lines changed

2 files changed

+78
-88
lines changed

lib/elixir/test/elixir/kernel/cli_test.exs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,28 @@ end
3939
defmodule Kernel.CLITest do
4040
use ExUnit.Case, async: true
4141

42-
import ExUnit.CaptureIO
43-
4442
defp run(argv) do
4543
{config, argv} = Kernel.CLI.parse_argv(Enum.map(argv, &String.to_charlist/1))
4644
assert Kernel.CLI.process_commands(config) == []
4745
Enum.map(argv, &IO.chardata_to_string/1)
4846
end
4947

50-
test "argv handling" do
51-
assert capture_io(fn ->
52-
assert run(["-e", "IO.puts :ok", "sample.exs", "-o", "1", "2"]) ==
53-
["sample.exs", "-o", "1", "2"]
54-
end) == "ok\n"
55-
56-
assert capture_io(fn ->
57-
assert run(["-e", "IO.puts :ok", "--", "sample.exs", "-o", "1", "2"]) ==
58-
["sample.exs", "-o", "1", "2"]
59-
end) == "ok\n"
60-
61-
assert capture_io(fn ->
62-
assert run(["-e", "", "--", "sample.exs", "-o", "1", "2"]) ==
63-
["sample.exs", "-o", "1", "2"]
64-
end)
48+
@tag :capture_io
49+
test "argv handling", %{capture_io: io} do
50+
assert run(["-e", "IO.puts :ok1", "sample.exs", "-o", "1", "2"]) ==
51+
["sample.exs", "-o", "1", "2"]
52+
53+
assert StringIO.flush(io) == "ok1\n"
54+
55+
assert run(["-e", "IO.puts :ok2", "--", "sample.exs", "-o", "1", "2"]) ==
56+
["sample.exs", "-o", "1", "2"]
57+
58+
assert StringIO.flush(io) == "ok2\n"
59+
60+
assert run(["-e", "", "--", "sample.exs", "-o", "1", "2"]) ==
61+
["sample.exs", "-o", "1", "2"]
62+
63+
assert StringIO.flush(io) == ""
6564
end
6665
end
6766

lib/mix/test/mix/tasks/compile.erlang_test.exs

Lines changed: 62 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ Code.require_file("../../test_helper.exs", __DIR__)
66

77
defmodule Mix.Tasks.Compile.ErlangTest do
88
use MixTest.Case
9-
import ExUnit.CaptureIO
109

1110
defmacro position(line, column), do: {line, column}
1211

@@ -18,12 +17,11 @@ defmodule Mix.Tasks.Compile.ErlangTest do
1817
end
1918

2019
@tag erlc_options: [{:d, ~c"foo", ~c"bar"}]
20+
@tag :capture_io
2121
test "raises on invalid erlc_options" do
2222
in_fixture("compile_erlang", fn ->
2323
assert_raise Mix.Error, ~r/Compiling Erlang file ".*" failed/, fn ->
24-
capture_io(fn ->
25-
Mix.Tasks.Compile.Erlang.run([])
26-
end)
24+
Mix.Tasks.Compile.Erlang.run([])
2725
end
2826
end)
2927
end
@@ -84,6 +82,7 @@ defmodule Mix.Tasks.Compile.ErlangTest do
8482
end)
8583
end
8684

85+
@tag :capture_io
8786
test "continues even if one file fails to compile" do
8887
in_fixture("compile_erlang", fn ->
8988
file = Path.absname("src/zzz.erl")
@@ -94,24 +93,23 @@ defmodule Mix.Tasks.Compile.ErlangTest do
9493
def zzz(), do: b
9594
""")
9695

97-
capture_io(fn ->
98-
assert {:error, [diagnostic]} = Mix.Tasks.Compile.Erlang.run([])
99-
100-
assert %Mix.Task.Compiler.Diagnostic{
101-
compiler_name: "erl_parse",
102-
file: ^source,
103-
source: ^source,
104-
message: "syntax error before: zzz",
105-
position: position(2, 5),
106-
severity: :error
107-
} = diagnostic
108-
end)
96+
assert {:error, [diagnostic]} = Mix.Tasks.Compile.Erlang.run([])
97+
98+
assert %Mix.Task.Compiler.Diagnostic{
99+
compiler_name: "erl_parse",
100+
file: ^source,
101+
source: ^source,
102+
message: "syntax error before: zzz",
103+
position: position(2, 5),
104+
severity: :error
105+
} = diagnostic
109106

110107
assert File.regular?("_build/dev/lib/sample/ebin/b.beam")
111108
assert File.regular?("_build/dev/lib/sample/ebin/c.beam")
112109
end)
113110
end
114111

112+
@tag :capture_io
115113
test "saves warnings between builds" do
116114
in_fixture("compile_erlang", fn ->
117115
file = Path.absname("src/has_warning.erl")
@@ -122,41 +120,40 @@ defmodule Mix.Tasks.Compile.ErlangTest do
122120
my_fn() -> ok.
123121
""")
124122

125-
capture_io(fn ->
126-
assert {:ok, [diagnostic]} = Mix.Tasks.Compile.Erlang.run([])
127-
128-
assert %Mix.Task.Compiler.Diagnostic{
129-
file: ^source,
130-
source: ^source,
131-
compiler_name: "erl_lint",
132-
message: "function my_fn/0 is unused",
133-
position: position(2, 1),
134-
severity: :warning
135-
} = diagnostic
136-
137-
capture_io(:stderr, fn ->
138-
# Should return warning without recompiling file
139-
assert {:noop, [^diagnostic]} = Mix.Tasks.Compile.Erlang.run(["--verbose"])
140-
refute_received {:mix_shell, :info, ["Compiled src/has_warning.erl"]}
141-
142-
assert [^diagnostic] = Mix.Tasks.Compile.Erlang.diagnostics()
143-
assert [^diagnostic] = Mix.Task.Compiler.diagnostics()
144-
145-
# Should not return warning after changing file
146-
File.write!(file, """
147-
-module(has_warning).
148-
-export([my_fn/0]).
149-
my_fn() -> ok.
150-
""")
151-
152-
ensure_touched(file)
153-
assert {:ok, []} = Mix.Tasks.Compile.Erlang.run([])
154-
end)
123+
assert {:ok, [diagnostic]} = Mix.Tasks.Compile.Erlang.run([])
124+
125+
assert %Mix.Task.Compiler.Diagnostic{
126+
file: ^source,
127+
source: ^source,
128+
compiler_name: "erl_lint",
129+
message: "function my_fn/0 is unused",
130+
position: position(2, 1),
131+
severity: :warning
132+
} = diagnostic
133+
134+
ExUnit.CaptureIO.capture_io(:stderr, fn ->
135+
# Should return warning without recompiling file
136+
assert {:noop, [^diagnostic]} = Mix.Tasks.Compile.Erlang.run(["--verbose"])
137+
refute_received {:mix_shell, :info, ["Compiled src/has_warning.erl"]}
138+
139+
assert [^diagnostic] = Mix.Tasks.Compile.Erlang.diagnostics()
140+
assert [^diagnostic] = Mix.Task.Compiler.diagnostics()
141+
142+
# Should not return warning after changing file
143+
File.write!(file, """
144+
-module(has_warning).
145+
-export([my_fn/0]).
146+
my_fn() -> ok.
147+
""")
148+
149+
ensure_touched(file)
150+
assert {:ok, []} = Mix.Tasks.Compile.Erlang.run([])
155151
end)
156152
end)
157153
end
158154

159-
test "prints warnings from stale files with --all-warnings" do
155+
@tag :capture_io
156+
test "prints warnings from stale files with --all-warnings", %{capture_io: io} do
160157
in_fixture("compile_erlang", fn ->
161158
file = Path.absname("src/has_warning.erl")
162159

@@ -165,13 +162,14 @@ defmodule Mix.Tasks.Compile.ErlangTest do
165162
my_fn() -> ok.
166163
""")
167164

168-
capture_io(fn -> Mix.Tasks.Compile.Erlang.run([]) end)
165+
Mix.Tasks.Compile.Erlang.run([])
166+
assert StringIO.flush(io) =~ "Warning: function my_fn/0 is unused"
169167

170-
assert capture_io(:stderr, fn ->
168+
assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
171169
assert {:noop, _} = Mix.Tasks.Compile.Erlang.run([])
172170
end) =~ ~r"has_warning.erl:2:(1:)? warning: function my_fn/0 is unused\n"
173171

174-
assert capture_io(:stderr, fn ->
172+
assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
175173
assert {:noop, _} = Mix.Tasks.Compile.Erlang.run([])
176174
end) =~ ~r"has_warning.erl:2:(1:)? warning: function my_fn/0 is unused\n"
177175

@@ -182,19 +180,14 @@ defmodule Mix.Tasks.Compile.ErlangTest do
182180

183181
ensure_touched(file)
184182

185-
output =
186-
capture_io(fn ->
187-
Mix.Tasks.Compile.Erlang.run(["--all-warnings"])
188-
end)
189-
190-
assert output == ""
183+
Mix.Tasks.Compile.Erlang.run(["--all-warnings"])
184+
assert StringIO.flush(io) == ""
191185
end)
192186
end
193187

188+
@tag :capture_io
194189
test "returns syntax error from an Erlang file when --return-errors is set" do
195190
in_fixture("no_mixfile", fn ->
196-
import ExUnit.CaptureIO
197-
198191
file = Path.absname("src/a.erl")
199192
source = deterministic_source(file)
200193

@@ -205,19 +198,17 @@ defmodule Mix.Tasks.Compile.ErlangTest do
205198
def b(), do: b
206199
""")
207200

208-
capture_io(fn ->
209-
assert {:error, [diagnostic]} =
210-
Mix.Tasks.Compile.Erlang.run(["--force", "--return-errors"])
211-
212-
assert %Mix.Task.Compiler.Diagnostic{
213-
compiler_name: "erl_parse",
214-
file: ^source,
215-
source: ^source,
216-
message: "syntax error before: b",
217-
position: position(2, 5),
218-
severity: :error
219-
} = diagnostic
220-
end)
201+
assert {:error, [diagnostic]} =
202+
Mix.Tasks.Compile.Erlang.run(["--force", "--return-errors"])
203+
204+
assert %Mix.Task.Compiler.Diagnostic{
205+
compiler_name: "erl_parse",
206+
file: ^source,
207+
source: ^source,
208+
message: "syntax error before: b",
209+
position: position(2, 5),
210+
severity: :error
211+
} = diagnostic
221212

222213
refute File.regular?("ebin/Elixir.A.beam")
223214
refute File.regular?("ebin/Elixir.B.beam")

0 commit comments

Comments
 (0)