Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support :suite_finished on elixir 1.12+ #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions config/config.exs

This file was deleted.

16 changes: 14 additions & 2 deletions lib/tapex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,16 @@ defmodule Tapex do
{:ok, config}
end

def handle_cast({:suite_finished, run_time, load_time}, %{test_count: count, seed: seed}=config) do
# Elixir <1.12
def handle_cast({:suite_finished, run, load}, config) do
handle_cast({:suite_finished, %{run: run, load: load, async: nil}}, config)
end

# Elixir >=1.12
def handle_cast({:suite_finished, times_us}, %{test_count: count, seed: seed}=config) do
IO.puts(Tap.format_plan(count))
IO.puts("")
IO.puts(Formatter.format_time(run_time, load_time))
IO.puts(format_times(times_us))
IO.puts(format_counts(config))

IO.puts("\nRandomized with seed #{seed}")
Expand Down Expand Up @@ -107,4 +113,10 @@ defmodule Tapex do
defp print_filters(filters, type) do
Formatter.format_filters(filters, type) |> IO.puts
end

if Version.compare(System.version(), "1.12.0-0") == :gt do
defp format_times(times_us), do: Formatter.format_times(times_us)
else
defp format_times(%{run: run, load: load} = _), do: Formatter.format_time(run, load)
end
end
16 changes: 11 additions & 5 deletions lib/tapex/diagnostic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ defmodule Tapex.Diagnostic do
alias ExUnit.{Test,TestCase}

import ExUnit.Formatter, only: [
{:format_test_failure, 5},
{:format_test_case_failure, 5}
{:format_test_failure, 5}
]
import Tapex.Tap, only: [{:color_wrap, 3}]

Expand All @@ -20,9 +19,16 @@ defmodule Tapex.Diagnostic do
|> diagnosticify()
end

def format_diagnostic(%TestCase{state: {:failed, failures}}=case, number, color) do
format_test_case_failure(case, failures, number, :infinity, &formatter(&1, &2, color))
|> diagnosticify()
if Version.compare(System.version(), "1.6.0-0") == :gt do
def format_diagnostic(%TestCase{state: {:failed, failures}}=case, number, color) do
ExUnit.Formatter.format_test_all_failure(case, failures, number, :infinity, &formatter(&1, &2, color))
|> diagnosticify()
end
else
def format_diagnostic(%TestCase{state: {:failed, failures}}=case, number, color) do
ExUnit.Formatter.format_test_case_failure(case, failures, number, :infinity, &formatter(&1, &2, color))
|> diagnosticify()
end
end

def format_diagnostic(_test, _number, _color), do: nil
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Tapex.Mixfile do
[app: :tapex,
deps: deps(),
description: description(),
elixir: "~> 1.2",
elixir: "~> 1.4",
package: package(),
version: "0.1.1"]
end
Expand Down
12 changes: 8 additions & 4 deletions test/tapex/diagnostic_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ defmodule Tapex.DiagnosticTest do

import Tapex.Diagnostic

def version do
Application.spec(:tapex, :vsn) |> Kernel.to_string()
end

test "format_diagnostic for test without color" do
trace = [{Tapex, :awesome, 1, [file: 'awesome.ex', line: 42]}]
failure = %ExUnit.AssertionError{
Expand All @@ -20,9 +24,9 @@ defmodule Tapex.DiagnosticTest do
"# 2) everything is awesome (nil)\n" <>
"# awesome.ex:12\n" <>
"# not awesome\n" <>
"# code: Awesum!()\n" <>
"# code: \"Awesum!()\"\n" <>
"# stacktrace:\n" <>
"# (tapex) awesome.ex:42: Tapex.awesome/1"
"# (tapex #{version()}) awesome.ex:42: Tapex.awesome/1"

assert expected == actual
end
Expand All @@ -37,10 +41,10 @@ defmodule Tapex.DiagnosticTest do
actual = format_diagnostic(case, 5, true)

expected =
"# 5) nil: failure on setup_all callback, test invalidated\n" <>
"# 5) nil: failure on setup_all callback, all tests have been invalidated\n" <>
"# \e[31m** (RuntimeError) BOOM!\e[0m\n" <>
"# \e[36mstacktrace:\e[0m\n" <>
"# (tapex) my_module.ex:20: Tapex.my_method/1"
"# (tapex #{version()}) my_module.ex:20: Tapex.my_method/1"

assert expected == actual
end
Expand Down
25 changes: 24 additions & 1 deletion test/tapex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ defmodule TapexTest do
assert Regex.match?(~r/^not ok/, output)
end

test ":suite_finished prints a report" do
test ":suite_finished prints a report (Elixir <1.12)" do
config = %{
colorize: false,
test_count: 5,
Expand All @@ -85,4 +85,27 @@ defmodule TapexTest do
assert String.contains?(output, "5 tests, 4 passed, 1 failed")
assert String.contains?(output, "Randomized with seed 12345")
end

test ":suite_finished prints a report (Elixir >=1.12)" do
config = %{
colorize: false,
test_count: 5,
seed: "12345",
state_counter: %{
passed: 4,
failed: 1
},
tag_counter: %{}
}

output = capture_io fn ->
time = 500000
{:noreply, _} = Tapex.handle_cast({:suite_finished, %{run: time, load: time, async: nil}}, config)
end

assert String.contains?(output, "1..5\n")
assert String.contains?(output, "Finished in 1.0 seconds")
assert String.contains?(output, "5 tests, 4 passed, 1 failed")
assert String.contains?(output, "Randomized with seed 12345")
end
end