-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Extract run summary into its own module
Also, refactor for less string concatentation.
- Loading branch information
1 parent
23d344b
commit cf5fa1b
Showing
4 changed files
with
162 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
defmodule MixTestInteractive.RunSummary do | ||
@moduledoc false | ||
alias MixTestInteractive.Settings | ||
|
||
@doc """ | ||
Return a text summary of the current interactive mode settings. | ||
""" | ||
@spec from_settings(Settings.t()) :: String.t() | ||
def from_settings(%Settings{} = settings) do | ||
[&base_summary/1, &all_tag_filters/1, &max_failures/1, &repeat_count/1, &seed/1, &tracing/1] | ||
|> Enum.flat_map(fn fun -> List.wrap(fun.(settings)) end) | ||
|> Enum.join("\n") | ||
end | ||
|
||
defp all_tag_filters(%Settings{} = settings) do | ||
Enum.reject( | ||
[ | ||
tag_filters("Excluding tags", settings.excludes), | ||
tag_filters("Including tags", settings.includes), | ||
tag_filters("Only tags", settings.only) | ||
], | ||
&is_nil/1 | ||
) | ||
end | ||
|
||
defp base_summary(%Settings{} = settings) do | ||
cond do | ||
settings.failed? -> | ||
"Ran only failed tests" | ||
|
||
settings.stale? -> | ||
"Ran only stale tests" | ||
|
||
!Enum.empty?(settings.patterns) -> | ||
"Ran all test files matching #{Enum.join(settings.patterns, ", ")}" | ||
|
||
true -> | ||
"Ran all tests" | ||
end | ||
end | ||
|
||
defp max_failures(%Settings{max_failures: nil}), do: nil | ||
|
||
defp max_failures(%Settings{} = settings) do | ||
"Max failures: #{settings.max_failures}" | ||
end | ||
|
||
defp repeat_count(%Settings{repeat_count: nil}), do: nil | ||
|
||
defp repeat_count(%Settings{} = settings) do | ||
"Repeat until failure: #{settings.repeat_count}" | ||
end | ||
|
||
def seed(%Settings{seed: nil}), do: nil | ||
|
||
def seed(%Settings{} = settings) do | ||
"Seed: #{settings.seed}" | ||
end | ||
|
||
defp tracing(%Settings{tracing?: false}), do: nil | ||
|
||
defp tracing(%Settings{}) do | ||
"Tracing: ON" | ||
end | ||
|
||
defp tag_filters(_label, []), do: nil | ||
|
||
defp tag_filters(label, tags) do | ||
label <> ": " <> inspect(tags) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
defmodule MixTestInteractive.RunSummaryTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias MixTestInteractive.RunSummary | ||
alias MixTestInteractive.Settings | ||
|
||
describe "summarizing a test run" do | ||
test "ran all tests" do | ||
settings = %Settings{} | ||
|
||
assert RunSummary.from_settings(settings) == "Ran all tests" | ||
end | ||
|
||
test "ran failed tests" do | ||
settings = Settings.only_failed(%Settings{}) | ||
|
||
assert RunSummary.from_settings(settings) == "Ran only failed tests" | ||
end | ||
|
||
test "ran stale tests" do | ||
settings = Settings.only_stale(%Settings{}) | ||
|
||
assert RunSummary.from_settings(settings) == "Ran only stale tests" | ||
end | ||
|
||
test "ran specific patterns" do | ||
settings = | ||
Settings.only_patterns(%Settings{}, ["p1", "p2"]) | ||
|
||
assert RunSummary.from_settings(settings) == "Ran all test files matching p1, p2" | ||
end | ||
|
||
test "appends max failures" do | ||
settings = Settings.with_max_failures(%Settings{}, "6") | ||
|
||
assert RunSummary.from_settings(settings) =~ "Max failures: 6" | ||
end | ||
|
||
test "appends repeat count" do | ||
settings = Settings.with_repeat_count(%Settings{}, "150") | ||
|
||
assert RunSummary.from_settings(settings) =~ "Repeat until failure: 150" | ||
end | ||
|
||
test "appends seed" do | ||
settings = Settings.with_seed(%Settings{}, "4242") | ||
|
||
assert RunSummary.from_settings(settings) =~ "Seed: 4242" | ||
end | ||
|
||
test "appends tag filters" do | ||
settings = | ||
%Settings{} | ||
|> Settings.with_excludes(["tag1", "tag2"]) | ||
|> Settings.with_includes(["tag3", "tag4"]) | ||
|> Settings.with_only(["tag5", "tag6"]) | ||
|
||
summary = RunSummary.from_settings(settings) | ||
|
||
assert summary =~ ~s(Excluding tags: ["tag1", "tag2"]) | ||
assert summary =~ ~s(Including tags: ["tag3", "tag4"]) | ||
assert summary =~ ~s(Only tags: ["tag5", "tag6"]) | ||
end | ||
|
||
test "appends tracing" do | ||
settings = Settings.toggle_tracing(%Settings{}) | ||
|
||
assert RunSummary.from_settings(settings) =~ "Tracing: ON" | ||
end | ||
|
||
test "includes only relevant information with no extra blank lines" do | ||
settings = | ||
%Settings{} | ||
|> Settings.only_stale() | ||
|> Settings.toggle_tracing() | ||
|> Settings.with_only(["tag1", "tag2"]) | ||
|> Settings.with_seed("4258") | ||
|
||
expected = """ | ||
Ran only stale tests | ||
Only tags: ["tag1", "tag2"] | ||
Seed: 4258 | ||
Tracing: ON | ||
""" | ||
|
||
assert RunSummary.from_settings(settings) == String.trim(expected) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters