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

Pull notification logic; use generalized ding package. #15

Open
wants to merge 1 commit 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
47 changes: 21 additions & 26 deletions lib/ex_unit_notifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,13 @@ defmodule ExUnitNotifier do
To enable notifications, add `ExUnitNotifier` as a formatter in your `test_helper.exs`:

ExUnit.configure formatters: [ExUnit.CLIFormatter, ExUnitNotifier]

"""

use GenServer

alias ExUnitNotifier.Counter
alias ExUnitNotifier.MessageFormatter

@notifiers [
ExUnitNotifier.Notifiers.TerminalNotifier,
ExUnitNotifier.Notifiers.NotifySend,
ExUnitNotifier.Notifiers.TmuxNotifier,
ExUnitNotifier.Notifiers.TerminalTitle
]

def init(_opts), do: {:ok, %Counter{}}

def handle_cast({:test_finished, %ExUnit.Test{state: nil}}, counter),
Expand All @@ -39,40 +31,43 @@ defmodule ExUnitNotifier do

# Elixir version < 1.12.0
def handle_cast({:suite_finished, run_us, load_us}, counter) do
apply(notifier(), :notify, [
status(counter),
Ding.send(
"ExUnit",
MessageFormatter.format(counter, run_us, load_us),
opts()
])
message_opts(counter)
)

{:noreply, counter}
end

# Elixir version >= 1.12.0, see https://hexdocs.pm/ex_unit/1.12.0/ExUnit.Formatter.html
def handle_cast({:suite_finished, %{run: run_us, async: _async_us, load: load_us}}, counter) do
apply(notifier(), :notify, [
status(counter),
Ding.send(
"ExUnit",
MessageFormatter.format(counter, run_us, load_us),
opts()
])
message_opts(counter)
)

{:noreply, counter}
end

def handle_cast(_, counter), do: {:noreply, counter}

defp status(%Counter{failures: failures, invalid: invalid}) when failures > 0 or invalid > 0,
do: :error

defp status(
%Counter{failures: failures, invalid: invalid}
) when failures > 0 or invalid > 0, do: :error
defp status(_), do: :ok

defp opts,
do: %{
clear_history: Application.get_env(:ex_unit_notifier, :clear_history, false)
}
defp color(:error), do: "red"
defp color(_status), do: "green"

defp notifier, do: Application.get_env(:ex_unit_notifier, :notifier, first_available_notifier())
defp message_opts counter do %{
clear_history: Application.get_env(:ex_unit_notifier, :clear_history, false),
color: color(status(counter)),
icon_png: icon_png(status(counter)),
icon_icns: icon_icns(status(counter)),
} end

defp first_available_notifier,
do: @notifiers |> Enum.find(fn notifier -> notifier.available? end)
defp icon_png(status), do: Application.app_dir(:ex_unit_notifier, "priv/icons/#{status}.png")
defp icon_icns(status), do: Application.app_dir(:ex_unit_notifier, "priv/icons/#{status}.icns")
end
30 changes: 0 additions & 30 deletions lib/ex_unit_notifier/notifiers/notify_send.ex

This file was deleted.

28 changes: 0 additions & 28 deletions lib/ex_unit_notifier/notifiers/terminal_notifier.ex

This file was deleted.

7 changes: 0 additions & 7 deletions lib/ex_unit_notifier/notifiers/terminal_title.ex

This file was deleted.

29 changes: 0 additions & 29 deletions lib/ex_unit_notifier/notifiers/tmux_notifier.ex

This file was deleted.

3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ defmodule ExUnitNotifier.MixProject do
defp deps do
[
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false},
{:mix_test_watch, "~> 1.0", only: :dev}
{:mix_test_watch, "~> 1.0", only: :dev},
{:ding, "~> 0.0" },
]
end

Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
%{
"ding": {:hex, :ding, "0.0.1", "39544ed9d32f9ff9f20ea86566c1cee67dd4a5678f364591c70b8107df1969b5", [:mix], [], "hexpm", "e1791c3b89e52e70ad04583d1e181d185dffbeebe683e37cf734294d66a4e484"},
"earmark": {:hex, :earmark, "1.4.15", "2c7f924bf495ec1f65bd144b355d0949a05a254d0ec561740308a54946a67888", [:mix], [{:earmark_parser, ">= 1.4.13", [hex: :earmark_parser, repo: "hexpm", optional: false]}], "hexpm", "3b1209b85bc9f3586f370f7c363f6533788fb4e51db23aa79565875e7f9999ee"},
"earmark_parser": {:hex, :earmark_parser, "1.4.31", "a93921cdc6b9b869f519213d5bc79d9e218ba768d7270d46fdcf1c01bacff9e2", [:mix], [], "hexpm", "317d367ee0335ef037a87e46c91a2269fef6306413f731e8ec11fc45a7efd059"},
"ex_doc": {:hex, :ex_doc, "0.29.4", "6257ecbb20c7396b1fe5accd55b7b0d23f44b6aa18017b415cb4c2b91d997729", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "2c6699a737ae46cb61e4ed012af931b57b699643b24dabe2400a8168414bc4f5"},
Expand Down