Skip to content

Commit a064a00

Browse files
committed
Fix CI and improve ExUnit results
1 parent 352bc46 commit a064a00

8 files changed

Lines changed: 104 additions & 127 deletions

File tree

lib/ex_unit/lib/ex_unit/cli_formatter.ex

Lines changed: 37 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ defmodule ExUnit.CLIFormatter do
4343
end
4444

4545
def handle_cast({:suite_finished, times_us}, config) do
46-
test_type_counts = collect_test_type_counts(config)
46+
test_counter = collect_test_counter(config)
4747

48-
if test_type_counts == 0 and config.excluded_counter > 0 do
48+
if test_counter == 0 and config.excluded_counter > 0 do
4949
IO.puts(invalid("All tests have been excluded.", config))
5050
end
5151

@@ -83,10 +83,7 @@ defmodule ExUnit.CLIFormatter do
8383
def handle_cast({:test_finished, %ExUnit.Test{state: {:excluded, reason}} = test}, config)
8484
when is_binary(reason) do
8585
if config.trace, do: IO.puts(trace_test_excluded(test))
86-
87-
test_counter = update_test_counter(config.test_counter, test)
88-
config = %{config | test_counter: test_counter, excluded_counter: config.excluded_counter + 1}
89-
86+
config = %{config | excluded_counter: config.excluded_counter + 1}
9087
{:noreply, config}
9188
end
9289

@@ -98,10 +95,7 @@ defmodule ExUnit.CLIFormatter do
9895
IO.write(skipped("*", config))
9996
end
10097

101-
test_counter = update_test_counter(config.test_counter, test)
102-
config = %{config | test_counter: test_counter, skipped_counter: config.skipped_counter + 1}
103-
104-
{:noreply, config}
98+
{:noreply, %{config | skipped_counter: config.skipped_counter + 1}}
10599
end
106100

107101
def handle_cast(
@@ -115,10 +109,7 @@ defmodule ExUnit.CLIFormatter do
115109
IO.write(invalid("?", config))
116110
end
117111

118-
test_counter = update_test_counter(config.test_counter, test)
119-
config = %{config | test_counter: test_counter, invalid_counter: config.invalid_counter + 1}
120-
121-
{:noreply, config}
112+
{:noreply, %{config | invalid_counter: config.invalid_counter + 1}}
122113
end
123114

124115
def handle_cast({:test_finished, %ExUnit.Test{state: {:failed, failures}} = test}, config) do
@@ -287,10 +278,6 @@ defmodule ExUnit.CLIFormatter do
287278
end
288279
end
289280

290-
defp update_test_counter(test_counter, %{state: {:excluded, _reason}}) do
291-
test_counter
292-
end
293-
294281
defp update_test_counter(test_counter, %{tags: %{test_type: test_type}}) do
295282
Map.update(test_counter, test_type, 1, &(&1 + 1))
296283
end
@@ -366,26 +353,22 @@ defmodule ExUnit.CLIFormatter do
366353
## Printing
367354

368355
defp print_summary(config, force_failures?) do
369-
test_type_counts = collect_test_type_counts(config)
370-
test_counter = test_counter_or_default(config, test_type_counts)
371-
372-
passed_total =
373-
test_type_counts - config.failure_counter - config.skipped_counter - config.invalid_counter
356+
test_counter = collect_test_counter(config)
357+
passed_counter = test_counter - config.failure_counter
374358

375-
# Passed line: "Passed: 447/455 (53/54 doctests, 393/403 tests)" or
376-
# "Passed: 455 (70 tests, 14 properties)" when all pass
377-
all_passed? = passed_total == test_type_counts
359+
# Passed line: "Result: 447/455 passed (53/54 doctests, 393/403 tests)" or
360+
# "Result: 455 passed (70 tests, 14 properties)" when all pass
361+
all_passed? = config.failure_counter == 0
378362

379363
passed_breakdown =
380-
format_passed_breakdown(test_counter, config.failure_type_counter, all_passed?)
364+
format_passed_breakdown(config.test_counter, config.failure_type_counter, all_passed?)
381365

382366
passed_line =
383-
if all_passed? do
384-
"Passed: #{passed_total}"
385-
else
386-
"Passed: #{passed_total}/#{test_type_counts}"
387-
end
388-
|> if_true(passed_breakdown != "", &(&1 <> " (#{passed_breakdown})"))
367+
cond do
368+
test_counter == 0 -> "Result: 0 tests"
369+
all_passed? -> "Result: #{passed_counter} passed"
370+
true -> "Result: #{passed_counter}/#{test_counter} passed"
371+
end <> passed_breakdown
389372

390373
# Failed line: "Failed: 8 tests, 1 property"
391374
failed_line =
@@ -408,7 +391,7 @@ defmodule ExUnit.CLIFormatter do
408391
)
409392
|> if_true(
410393
config.excluded_counter > 0,
411-
&(&1 <> " (#{config.excluded_counter} excluded)")
394+
&(&1 <> ", #{config.excluded_counter} excluded")
412395
)
413396

414397
cond do
@@ -418,7 +401,7 @@ defmodule ExUnit.CLIFormatter do
418401
config.invalid_counter > 0 ->
419402
IO.puts(invalid(message, config))
420403

421-
test_type_counts > 0 && config.excluded_counter == test_type_counts ->
404+
test_counter > 0 && config.excluded_counter == test_counter ->
422405
IO.puts(invalid(message, config))
423406

424407
true ->
@@ -455,41 +438,35 @@ defmodule ExUnit.CLIFormatter do
455438
end
456439

457440
defp format_passed_breakdown(test_counter, failure_type_counter, all_passed?) do
458-
# If there are no different test types, we just print "Passed: N/N"
459-
# without the type.
441+
# If there are no different test types, we just print "Result: N/N passed" without the type.
460442
if map_size(test_counter) in 0..1 do
461443
""
462444
else
463-
test_counter
464-
|> Map.keys()
465-
|> Enum.sort()
466-
|> Enum.map_join(", ", fn type ->
467-
total = Map.fetch!(test_counter, type)
468-
469-
if all_passed? do
470-
"#{total} #{pluralize_type(total, type)}"
471-
else
472-
failed = Map.get(failure_type_counter, type, 0)
473-
passed = total - failed
474-
"#{passed}/#{total} #{pluralize_type(total, type)}"
475-
end
476-
end)
445+
entries =
446+
test_counter
447+
|> Map.keys()
448+
|> Enum.sort()
449+
|> Enum.map_join(", ", fn type ->
450+
total = Map.fetch!(test_counter, type)
451+
452+
if all_passed? do
453+
"#{total} #{pluralize_type(total, type)}"
454+
else
455+
failed = Map.get(failure_type_counter, type, 0)
456+
passed = total - failed
457+
"#{passed}/#{total} #{pluralize_type(total, type)}"
458+
end
459+
end)
460+
461+
" (" <> entries <> ")"
477462
end
478463
end
479464

480465
defp pluralize_type(count, type) do
481466
pluralize(count, type, ExUnit.plural_rule(to_string(type)))
482467
end
483468

484-
defp test_counter_or_default(_config, 0) do
485-
%{test: 0}
486-
end
487-
488-
defp test_counter_or_default(%{test_counter: test_counter} = _config, _test_type_counts) do
489-
test_counter
490-
end
491-
492-
defp collect_test_type_counts(%{test_counter: test_counter} = _config) do
469+
defp collect_test_counter(%{test_counter: test_counter} = _config) do
493470
Enum.reduce(test_counter, 0, fn {_, count}, acc ->
494471
acc + count
495472
end)

lib/ex_unit/test/ex_unit/callbacks_test.exs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ defmodule ExUnit.CallbacksTest do
3636
end
3737
end
3838

39-
assert capture_io(fn -> ExUnit.run() end) =~ "Passed: 1"
39+
assert capture_io(fn -> ExUnit.run() end) =~ "Result: 1 passed"
4040
end
4141

4242
test "named callbacks run custom code in order" do
@@ -66,7 +66,7 @@ defmodule ExUnit.CallbacksTest do
6666
defp store_5(context), do: store(context, 5)
6767
end
6868

69-
assert capture_io(fn -> ExUnit.run() end) =~ "Passed: 1"
69+
assert capture_io(fn -> ExUnit.run() end) =~ "Result: 1 passed"
7070
end
7171

7272
test "named callbacks support {module, function} tuples" do
@@ -87,7 +87,7 @@ defmodule ExUnit.CallbacksTest do
8787
def setup_3(_), do: [setup_3: true]
8888
end
8989

90-
assert capture_io(fn -> ExUnit.run() end) =~ "Passed: 1"
90+
assert capture_io(fn -> ExUnit.run() end) =~ "Result: 1 passed"
9191
end
9292

9393
test "doesn't choke on setup errors" do
@@ -141,7 +141,7 @@ defmodule ExUnit.CallbacksTest do
141141
end
142142
end
143143

144-
assert capture_io(fn -> ExUnit.run() end) =~ "Passed: 0/1"
144+
assert capture_io(fn -> ExUnit.run() end) =~ "Result: 0 tests"
145145
end
146146

147147
test "doesn't choke on dead supervisor" do
@@ -253,7 +253,7 @@ defmodule ExUnit.CallbacksTest do
253253

254254
output = capture_io(fn -> ExUnit.run() end)
255255
assert output =~ "on_exit run"
256-
assert output =~ "Passed: 1"
256+
assert output =~ "Result: 1 passed"
257257
end
258258

259259
test "runs multiple on_exit exits and overrides by ref" do

lib/ex_unit/test/ex_unit/doc_test_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ defmodule ExUnit.DocTestTest do
10841084
doctest ExUnit.DocTestTest.Numbered
10851085
end
10861086

1087-
assert capture_io(fn -> ExUnit.run() end) =~ "Passed: 1"
1087+
assert capture_io(fn -> ExUnit.run() end) =~ "Result: 1 passed"
10881088
end
10891089

10901090
test "IEx prompt contains host" do
@@ -1253,7 +1253,7 @@ defmodule ExUnit.DocTestTest do
12531253
end
12541254

12551255
output = capture_io(fn -> ExUnit.run() end)
1256-
assert output =~ "Passed: 2"
1256+
assert output =~ "Result: 2 passed"
12571257
end
12581258

12591259
test "failing" do

lib/ex_unit/test/ex_unit/register_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ defmodule ExUnit.RegisterTest do
4242

4343
assert capture_io(fn ->
4444
assert ExUnit.run() == %{failures: 0, skipped: 0, total: 2, excluded: 0}
45-
end) =~ "Passed: 2 (1 property, 1 test)"
45+
end) =~ "Result: 2 passed (1 property, 1 test)"
4646
end
4747

4848
test "plural test types" do
@@ -96,6 +96,6 @@ defmodule ExUnit.RegisterTest do
9696

9797
assert capture_io(fn ->
9898
assert ExUnit.run() == %{failures: 0, skipped: 0, total: 4, excluded: 0}
99-
end) =~ "Passed: 4 (2 properties, 2 tests)"
99+
end) =~ "Result: 4 passed (2 properties, 2 tests)"
100100
end
101101
end

lib/ex_unit/test/ex_unit_test.exs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ defmodule ExUnitTest do
3333
assert capture_io(fn ->
3434
assert ExUnit.async_run() |> ExUnit.await_run() ==
3535
%{failures: 0, skipped: 0, total: 0, excluded: 0}
36-
end) =~ "\nPassed: 0\n"
36+
end) =~ "\nResult: 0 tests\n"
3737
end
3838

3939
test "supports rerunning given modules" do
@@ -139,7 +139,7 @@ defmodule ExUnitTest do
139139
assert result =~ ~r"\* test true \[.*test/ex_unit_test.exs:#{line}\]"
140140

141141
assert result =~ "Showing results so far..."
142-
assert result =~ "Passed: 0"
142+
assert result =~ "Result: 0 tests"
143143
end
144144

145145
test "doesn't hang on exits" do
@@ -332,11 +332,11 @@ defmodule ExUnitTest do
332332

333333
{result, output} = run_with_filter([exclude: [even: true]], [ParityTest])
334334
assert result == %{failures: 0, skipped: 0, excluded: 1, total: 4}
335-
assert output =~ "Passed: 3"
335+
assert output =~ "Result: 3 passed"
336336

337337
{result, output} = run_with_filter([exclude: :even], [ParityTest])
338338
assert result == %{failures: 0, skipped: 0, excluded: 3, total: 4}
339-
assert output =~ "Passed: 1"
339+
assert output =~ "Result: 1 passed"
340340

341341
{result, output} = run_with_filter([exclude: :even, include: [even: true]], [ParityTest])
342342
assert result == %{failures: 1, skipped: 0, excluded: 2, total: 4}
@@ -493,7 +493,7 @@ defmodule ExUnitTest do
493493
assert ExUnit.run() == %{failures: 0, skipped: 2, total: 2, excluded: 0}
494494
end)
495495

496-
assert output =~ "Passed: 0/2"
496+
assert output =~ "Result: 0 tests"
497497
end
498498

499499
test "filtering cases with :module tag" do
@@ -511,7 +511,7 @@ defmodule ExUnitTest do
511511
{result, output} = run_with_filter([exclude: :module], [])
512512

513513
assert result == %{failures: 0, skipped: 0, excluded: 2, total: 2}
514-
assert output =~ "Passed: 0"
514+
assert output =~ "Result: 0 tests"
515515

516516
{result, output} =
517517
[exclude: :test, include: [module: "ExUnitTest.SecondTestModule"]]
@@ -644,7 +644,7 @@ defmodule ExUnitTest do
644644

645645
assert capture_io(fn ->
646646
assert ExUnit.run() == %{failures: 0, skipped: 0, total: 3, excluded: 0}
647-
end) =~ "Passed: 3"
647+
end) =~ "Result: 3 passed"
648648
end
649649

650650
# Skipped and excluded tests should be included in the stats
@@ -682,7 +682,7 @@ defmodule ExUnitTest do
682682
end)
683683

684684
refute output =~ max_failures_reached_msg()
685-
assert output =~ "Passed: 0/5"
685+
assert output =~ "Result: 0 tests"
686686
end
687687

688688
test "parameterized tests" do
@@ -727,7 +727,7 @@ defmodule ExUnitTest do
727727
end
728728

729729
configure_and_reload_on_exit(trace: true)
730-
assert capture_io(fn -> ExUnit.run() end) =~ "Passed: 0"
730+
assert capture_io(fn -> ExUnit.run() end) =~ "Result: 0 tests"
731731

732732
defmodule EmptyGroupedParameterizedTests do
733733
use ExUnit.Case, async: true, parameterize: [], group: :example
@@ -738,7 +738,7 @@ defmodule ExUnitTest do
738738
end
739739

740740
configure_and_reload_on_exit(trace: true)
741-
assert capture_io(fn -> ExUnit.run() end) =~ "Passed: 0"
741+
assert capture_io(fn -> ExUnit.run() end) =~ "Result: 0 tests"
742742
end
743743

744744
describe "after_suite/1" do
@@ -912,7 +912,7 @@ defmodule ExUnitTest do
912912
end)
913913

914914
assert output =~ max_failures_reached_msg()
915-
assert output =~ "Passed: 0/3"
915+
assert output =~ "Result: 0 tests"
916916
end
917917

918918
test ":max_failures flushes all async/sync cases" do
@@ -1103,7 +1103,7 @@ defmodule ExUnitTest do
11031103
end)
11041104

11051105
assert output =~ "All tests have been excluded.\n"
1106-
assert output =~ "Passed: 0"
1106+
assert output =~ "Result: 0 tests"
11071107
end
11081108

11091109
test "tests are run in compile order (FIFO)" do

lib/mix/lib/mix/tasks/test.coverage.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ defmodule Mix.Tasks.Test.Coverage do
8181
to compute coverage, leading to better performance when running a test suite
8282
with coverage enabled.
8383
84-
You can enable it by setting:
84+
You can enable it by running:
8585
86-
ERL_COMPILER_OPTIONS=line_coverage mix test.coverage
86+
ERL_COMPILER_OPTIONS=line_coverage mix test
8787
8888
## Exporting coverage
8989

lib/mix/test/mix/cli_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ defmodule Mix.CLITest do
253253

254254
output = mix(~w[test test/new_with_tests_test.exs --cover])
255255
assert File.regular?("_build/test/lib/new_with_tests/ebin/Elixir.NewWithTests.beam")
256-
assert output =~ "1 doctest, 1 test, 0 failures"
256+
assert output =~ "Result: 2 passed (1 doctest, 1 test)"
257257
assert output =~ "Generating cover results ..."
258258
assert File.regular?("cover/Elixir.NewWithTests.html")
259259
end)
@@ -267,7 +267,7 @@ defmodule Mix.CLITest do
267267

268268
output = mix(~w[test])
269269
assert File.regular?("_build/test/lib/sup_with_tests/ebin/Elixir.SupWithTests.beam")
270-
assert output =~ "1 doctest, 1 test, 0 failures"
270+
assert output =~ "Result: 2 passed (1 doctest, 1 test)"
271271
end)
272272
end
273273
end

0 commit comments

Comments
 (0)