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

WorkflowCommands and ExUnit reporter #14

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
6 changes: 4 additions & 2 deletions .github/workflows/elixir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ jobs:
name: Build and test
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
otp_version: ['24.0', '23.3', '22.3']
elixir_version: ['1.10.3', '1.11.4', '1.12.1']
otp_version: ['25', '24', '23']
elixir_version: ['1.12', '1.13', '1.14']
steps:
- uses: actions/checkout@v2
- name: Set up Elixir
uses: erlef/setup-beam@v1
with:
otp-version: ${{ matrix.otp_version }}
elixir-version: ${{ matrix.elixir_version }}
disable_problem_matchers: true
- name: Restore dependencies cache
uses: actions/cache@v2
with:
Expand Down
75 changes: 75 additions & 0 deletions lib/mix_machine/github/test_log.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
defmodule MixMachine.Github.TestLog do
use GenServer

def start_link(_), do: GenServer.start_link(__MODULE__, [])

def init(_) do
counter = :atomics.new(1, [])

{:ok, %{counter: counter}}
end

def handle_cast({:test_finished, test}, state) do
format_test(test, state)

{:noreply, state}
end

def handle_cast({event, _}, state) when event in ~w[case_started case_finished]a do
{:noreply, state}
end

def handle_cast(_msg, state) do
# IO.inspect(msg)
{:noreply, state}
end

alias MixMachine.Github.WorkflowCommands, as: C

defp format_test(%ExUnit.Test{state: {:failed, failures}} = test, %{counter: counter}) do
id = :atomics.add_get(counter, 1, 1)

C.error("#{test.name} failed", meta(test))

C.group("#{test.name} report", fn ->
IO.puts("")
IO.puts(
ExUnit.Formatter.format_test_failure(test, failures, id, 80, fn _, msg -> msg end)
)
end)
end

defp format_test(%ExUnit.Test{state: {:invalid, module}} = test, %{counter: counter}) do
_id = :atomics.add_get(counter, 1, 1)

C.error(
"#{test.name} is invalid because `setup_all` in #{inspect(module)} failed",
meta(test)
)
end

defp format_test(%ExUnit.Test{state: {:excluded, filter}} = test, %{counter: counter}) do
_id = :atomics.add_get(counter, 1, 1)

C.notice("#{test.name} excluded by #{filter}", meta(test))
end

defp format_test(%ExUnit.Test{state: {:skipped, reason}} = test, _state) do
C.notice("#{test.name} skipped because of #{reason}", meta(test))
end

defp format_test(%ExUnit.Test{name: name, state: nil}, _state) do
C.debug("#{name} passed")
end

defp meta(%ExUnit.Test{tags: tags}) do
tags
|> Map.take([:file, :line])
|> update(:file, &Path.relative_to_cwd/1)
end

defp update(map, key, func) when is_map_key(map, key),
do: %{map | key => func.(Map.fetch!(map, key))}

defp update(map, _key, _func), do: map
end
66 changes: 66 additions & 0 deletions lib/mix_machine/github/workflow_commands.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
defmodule MixMachine.Github.WorkflowCommands do
@doc """
Print debug message
"""
def debug(message), do: print(:debug, message)

@log_metadata [:title, :file, :col, :end_column, :line, :end_line]

@doc """
Log message using `notice` level
"""
def notice(message, meta \\ %{}),
do: print(:notice, Map.take(meta, @log_metadata), message)

@doc """
Log message using `warning` level
"""
def warning(message, meta \\ %{}),
do: print(:warning, Map.take(meta, @log_metadata), message)

@doc """
Log message using `error` level
"""
def error(message, meta \\ %{}),
do: print(:error, Map.take(meta, @log_metadata), message)

def group(name, callback) do
print(:group, name)
return = callback.()
print(:endgroup)

return
end

def set_output(name, value) do
print("set-output", %{name: name}, value)
end

def set_secret(value), do: print("add-mask", value)

defp print(command), do: print(command, %{}, "")

defp print(command, message), do: print(command, %{}, message)

defp print(command, meta, message) do
IO.puts(["::", to_string(command), format_meta(meta), "::", message])
end

defp format_meta(map) when map == %{}, do: ""

defp format_meta(map) do
values =
for {key, value} <- map do
[format_key(key), "=", to_string(value)]
end

[" " | Enum.intersperse(values, ",")]
end

# Change `key` to real camel case (first letter lowercase and rest mixed case)
defp format_key(key) do
<<c, rest::binary>> = Macro.camelize(to_string(key))

String.downcase(<<c>>) <> rest
end
end
8 changes: 8 additions & 0 deletions test/mix_machine_test.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
defmodule MixMachineTest do
use ExUnit.Case

test "foo" do
:ok
end

test "bar" do
assert false
end
end
2 changes: 1 addition & 1 deletion test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ExUnit.start()
ExUnit.start(formatters: [MixMachine.Github.TestLog])