Skip to content

Commit

Permalink
Merge pull request #153 from naps62/mp/custom-file
Browse files Browse the repository at this point in the history
Custom config file path, and ability to silence output
  • Loading branch information
parroty committed Aug 26, 2018
2 parents a9fc241 + 5682a1a commit 1842b72
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 15 deletions.
14 changes: 9 additions & 5 deletions lib/excoveralls/local.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ defmodule ExCoveralls.Local do
Prints summary statistics for given coverage.
"""
def print_summary(stats, options \\ []) do
file_width = ExCoveralls.Settings.get_file_col_width
IO.puts "----------------"
IO.puts print_string("~-6s ~-#{file_width}s ~8s ~8s ~8s", ["COV", "FILE", "LINES", "RELEVANT", "MISSED"])
coverage(stats, options) |> IO.puts
IO.puts "----------------"
enabled = ExCoveralls.Settings.get_print_summary

if enabled do
file_width = ExCoveralls.Settings.get_file_col_width
IO.puts "----------------"
IO.puts print_string("~-6s ~-#{file_width}s ~8s ~8s ~8s", ["COV", "FILE", "LINES", "RELEVANT", "MISSED"])
coverage(stats, options) |> IO.puts
IO.puts "----------------"
end
end

defp format_source(stat) do
Expand Down
11 changes: 7 additions & 4 deletions lib/excoveralls/settings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule ExCoveralls.Settings do
defmodule Files do
@filename "coveralls.json"
def default_file, do: "#{Path.dirname(__ENV__.file)}/../conf/#{@filename}"
def custom_file, do: "#{System.cwd}/#{@filename}"
def custom_file, do: Application.get_env(:excoveralls, :config_file, "#{System.cwd}/#{@filename}")
def dot_file, do: Path.expand("~/.excoveralls/#{@filename}")
end

Expand Down Expand Up @@ -77,6 +77,10 @@ defmodule ExCoveralls.Settings do
|> Enum.map(&Regex.compile!/1)
end

def get_print_summary do
read_config("print_summary", true)
end

@doc """
Reads the value for the specified key defined in the json file.
"""
Expand All @@ -88,9 +92,8 @@ defmodule ExCoveralls.Settings do
end

defp read_merged_config(dot, custom) do
dot_config = read_config_file(dot)
project_config = read_config_file(custom)
merge(dot_config, project_config)
read_config_file(dot)
|> merge(read_config_file(custom))
end

defp merge(left, right) when is_map(left) and is_map(right) do
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/no_summary.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"print_summary": false
}
6 changes: 3 additions & 3 deletions test/html_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule ExCoveralls.HtmlTest do
end

test_with_mock "generate stats information", %{report: report}, ExCoveralls.Settings, [],
[get_coverage_options: fn -> %{"output_dir" => @test_output_dir, "template_path" => @test_template_path} end, get_file_col_width: fn -> 40 end] do
[get_coverage_options: fn -> %{"output_dir" => @test_output_dir, "template_path" => @test_template_path} end, get_file_col_width: fn -> 40 end, get_print_summary: fn -> true end] do

assert capture_io(fn ->
Html.execute(@source_info)
Expand All @@ -52,15 +52,15 @@ defmodule ExCoveralls.HtmlTest do
end

test_with_mock "Exit status code is 1 when actual coverage does not reach the minimum",
ExCoveralls.Settings, [get_coverage_options: fn -> coverage_options(100) end, get_file_col_width: fn -> 40 end] do
ExCoveralls.Settings, [get_coverage_options: fn -> coverage_options(100) end, get_file_col_width: fn -> 40 end, get_print_summary: fn -> true end] do
output = capture_io(fn ->
assert catch_exit(Html.execute(@source_info)) == {:shutdown, 1}
end)
assert String.ends_with?(output, "\e[31m\e[1mFAILED: Expected minimum coverage of 100%, got 50%.\e[0m\n")
end

test_with_mock "Exit status code is 0 when actual coverage reaches the minimum",
ExCoveralls.Settings, [get_coverage_options: fn -> coverage_options(49.9) end, get_file_col_width: fn -> 40 end] do
ExCoveralls.Settings, [get_coverage_options: fn -> coverage_options(49.9) end, get_file_col_width: fn -> 40 end, get_print_summary: fn -> true end] do
assert capture_io(fn ->
Html.execute(@source_info)
end) =~ @stats_result
Expand Down
2 changes: 1 addition & 1 deletion test/json_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule ExCoveralls.JsonTest do
end

test_with_mock "generate json file", %{report: report}, ExCoveralls.Settings, [],
[get_coverage_options: fn -> %{"output_dir" => @test_output_dir} end, get_file_col_width: fn -> 40 end] do
[get_coverage_options: fn -> %{"output_dir" => @test_output_dir} end, get_file_col_width: fn -> 40 end, get_print_summary: fn -> true end] do

assert capture_io(fn ->
Json.execute(@source_info)
Expand Down
11 changes: 9 additions & 2 deletions test/local_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,24 @@ defmodule ExCoveralls.LocalTest do
end

test_with_mock "Exit status code is 1 when actual coverage does not reach the minimum",
ExCoveralls.Settings, [get_coverage_options: fn -> %{"minimum_coverage" => 100} end, get_file_col_width: fn -> 40 end] do
ExCoveralls.Settings, [get_coverage_options: fn -> %{"minimum_coverage" => 100} end, get_file_col_width: fn -> 40 end, get_print_summary: fn -> true end] do
output = capture_io(fn ->
assert catch_exit(Local.execute(@source_info)) == {:shutdown, 1}
end)
assert String.ends_with?(output, "\e[31m\e[1mFAILED: Expected minimum coverage of 100%, got 50%.\e[0m\n")
end

test_with_mock "Exit status code is 0 when actual coverage reaches the minimum",
ExCoveralls.Settings, [get_coverage_options: fn -> %{"minimum_coverage" => 49.9} end, get_file_col_width: fn -> 40 end] do
ExCoveralls.Settings, [get_coverage_options: fn -> %{"minimum_coverage" => 49.9} end, get_file_col_width: fn -> 40 end, get_print_summary: fn -> true end] do
assert capture_io(fn ->
Local.execute(@source_info)
end) =~ @stats_result
end

test_with_mock "No output if print_summary is false",
ExCoveralls.Settings, [get_coverage_options: fn -> %{"minimum_coverage" => 49.9} end, get_file_col_width: fn -> 40 end, get_print_summary: fn -> true end] do
assert capture_io(fn ->
Local.execute(@source_info)
end) =~ ""
end
end
30 changes: 30 additions & 0 deletions test/settings_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Excoveralls.SettingsTest do
@fixture_covered Path.dirname(__ENV__.file) <> "/fixtures/no_relevant_lines_are_covered.json"
@fixture_column_default Path.dirname(__ENV__.file) <> "/fixtures/terminal_options1.json"
@fixture_column_integer Path.dirname(__ENV__.file) <> "/fixtures/terminal_options2.json"
@fixture_no_summary Path.dirname(__ENV__.file) <> "/fixtures/no_summary.json"

test "returns default file path" do
assert(Settings.Files.default_file
Expand All @@ -22,6 +23,15 @@ defmodule Excoveralls.SettingsTest do
|> Path.relative_to(File.cwd!) == "coveralls.json")
end

test "returns custom file path from config file" do
Application.put_env(:excoveralls, :config_file, "custom_coveralls.json")

assert(Settings.Files.custom_file
|> Path.relative_to(File.cwd!) == "custom_coveralls.json")

Application.delete_env(:excoveralls, :config_file)
end

test_with_mock "read config defined in default file", Settings.Files,
[default_file: fn -> @fixture_default end,
custom_file: fn -> @fixture_custom end,
Expand Down Expand Up @@ -108,5 +118,25 @@ defmodule Excoveralls.SettingsTest do
assert Settings.get_stop_words()== [~r/a/, ~r/b/, ~r/cc/, ~r/dd/, ~r/aa/, ~r/bb/]
end

test_with_mock "print_summary is true by default",
Settings.Files, [
default_file: fn -> "__invalid__" end,
custom_file: fn -> "__invalid__" end,
dot_file: fn -> "__invalid__" end
] do

assert Settings.get_print_summary
end

test_with_mock "print_summary can be set to false",
Settings.Files, [
default_file: fn -> "__invalid__" end,
custom_file: fn -> @fixture_no_summary end,
dot_file: fn -> "__invalid__" end
] do

refute Settings.get_print_summary
end

end

0 comments on commit 1842b72

Please sign in to comment.