Repository: parroty/excoveralls
Pull request #215 (2020-01-19 13:33:07)

Add terminal option to hide file coverage list
Solves https://github.com/parroty/excoveralls/issues/148


Patch:
diff --git a/README.md b/README.md
index c5e35f0..841d167 100644
--- a/README.md
+++ b/README.md
@@ -356,6 +356,18 @@ If you want to change the column width used for file names add the `file_column_
   }
 }
 ```
+
+If you want to see only the total coverage without a table of each file, set the `print_files` option
+to `false`:
+
+```javascript
+{
+  "terminal_options": {
+    "print_files": false
+  }
+}
+```
+
 #### Coverage Options
 - treat_no_relevant_lines_as_covered
   - By default, coverage for [files with no relevant lines] are displayed as 0% for aligning with coveralls.io behavior. But, if `treat_no_relevant_lines_as_covered` is set to `true`, it will be displayed as 100%.
diff --git a/lib/excoveralls/local.ex b/lib/excoveralls/local.ex
index 9ed6746..db01c39 100644
--- a/lib/excoveralls/local.ex
+++ b/lib/excoveralls/local.ex
@@ -44,13 +44,8 @@ defmodule ExCoveralls.Local do
   """
   def print_summary(stats, options \\ []) do
     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
 
@@ -77,9 +72,23 @@ defmodule ExCoveralls.Local do
   Format the source coverage stats into string.
   """
   def coverage(stats, options \\ []) do
+    file_width = ExCoveralls.Settings.get_file_col_width
+    print_files? = ExCoveralls.Settings.get_print_files
+
     count_info = Enum.map(stats, fn(stat) -> [stat, calculate_count(stat[:coverage])] end)
     count_info = sort(count_info, options)
-    Enum.join(format_body(count_info), "\n") <> "\n" <> format_total(count_info)
+
+    if print_files? do
+      """
+      ----------------
+      #{print_string("~-6s ~-#{file_width}s ~8s ~8s ~8s", ["COV", "FILE", "LINES", "RELEVANT", "MISSED"])}
+      #{Enum.join(format_body(count_info), "\n")}
+      #{format_total(count_info)}
+      ----------------\
+      """
+    else
+      "Test Coverage #{format_total(count_info)}\n"
+    end
   end
 
   defp sort(count_info, options) do
@@ -139,7 +148,6 @@ defmodule ExCoveralls.Local do
       [coverage, stat[:name], count.lines, count.relevant, count.relevant - count.covered])
   end
 
-
   defp format_total(info) do
     totals   = Enum.reduce(info, %Count{}, fn([_, count], acc) -> append(count, acc) end)
     coverage = get_coverage(totals)
diff --git a/lib/excoveralls/settings.ex b/lib/excoveralls/settings.ex
index 298bbce..01874da 100644
--- a/lib/excoveralls/settings.ex
+++ b/lib/excoveralls/settings.ex
@@ -58,6 +58,13 @@ defmodule ExCoveralls.Settings do
     end
   end
 
+  def get_print_files do
+    case Map.fetch(get_terminal_options(), "print_files") do
+      {:ok, val} when is_boolean(val) -> val
+      _ -> true
+    end
+  end
+
   defp read_config_file(file_name) do
     if File.exists?(file_name) do
       case File.read!(file_name) |> Jason.decode do


Test patch:
diff --git a/test/html_test.exs b/test/html_test.exs
index 3977224..ffc5a86 100644
--- a/test/html_test.exs
+++ b/test/html_test.exs
@@ -50,8 +50,12 @@ 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, get_print_summary: fn -> true 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,
+        get_print_files: fn -> true end
+      ] do
     assert capture_io(fn ->
       Html.execute(@source_info)
     end) =~ @stats_result
@@ -62,7 +66,12 @@ 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, get_print_summary: fn -> true 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,
+        get_print_files: fn -> true end
+      ] do
     output = capture_io(fn ->
       assert catch_exit(Html.execute(@source_info)) == {:shutdown, 1}
     end)
@@ -70,7 +79,12 @@ defmodule ExCoveralls.HtmlTest do
   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, get_print_summary: fn -> true 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,
+        get_print_files: fn -> true end
+      ] do
     assert capture_io(fn ->
       Html.execute(@source_info)
     end) =~ @stats_result
diff --git a/test/json_test.exs b/test/json_test.exs
index 56548c9..b4508a5 100644
--- a/test/json_test.exs
+++ b/test/json_test.exs
@@ -39,7 +39,12 @@ 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, get_print_summary: fn -> true 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,
+        get_print_files: fn -> true end
+      ] do
 
     assert capture_io(fn ->
       Json.execute(@source_info)
diff --git a/test/local_test.exs b/test/local_test.exs
index 1a27671..b994ce9 100644
--- a/test/local_test.exs
+++ b/test/local_test.exs
@@ -30,6 +30,8 @@ defmodule ExCoveralls.LocalTest do
       "[TOTAL]  50.0%\n" <>
       "----------------\n"
 
+  @stats_no_files_results "Test Coverage [TOTAL]  50.0%\n"
+
   @source_result "" <>
       "\n\e[33m--------test/fixtures/test.ex--------\e[m\n" <>
       "\e[31mdefmodule Test do\e[m\n\e[32m  def test do\e[m\n" <>
@@ -67,21 +69,34 @@ defmodule ExCoveralls.LocalTest do
   end
 
   test "Empty (no relevant lines) file is calculated as 0.0%" do
-    assert String.ends_with?(Local.coverage(@empty_source_info), "[TOTAL] 100.0%")
+    assert String.contains?(Local.coverage(@empty_source_info), "[TOTAL] 100.0%")
   end
 
   test_with_mock "Empty (no relevant lines) file with treat_no_relevant_lines_as_covered=true option is calculated as 100.0%",
-    ExCoveralls.Settings, [get_coverage_options: fn -> %{"treat_no_relevant_lines_as_covered" => true} end, get_file_col_width: fn -> 40 end] do
-    assert String.ends_with?(Local.coverage(@empty_source_info), "[TOTAL] 100.0%")
+    ExCoveralls.Settings, [
+      get_coverage_options: fn -> %{"treat_no_relevant_lines_as_covered" => true} end,
+      get_file_col_width: fn -> 40 end,
+      get_print_files: fn -> true end
+    ] do
+    assert String.contains?(Local.coverage(@empty_source_info), "[TOTAL] 100.0%")
   end
 
   test_with_mock "Empty (no relevant lines) file with treat_no_relevant_lines_as_covered=false option is calculated as 0.0%",
-    ExCoveralls.Settings, [get_coverage_options: fn -> %{"treat_no_relevant_lines_as_covered" => false} end, get_file_col_width: fn -> 40 end] do
-    assert String.ends_with?(Local.coverage(@empty_source_info), "[TOTAL]   0.0%")
+      ExCoveralls.Settings, [
+        get_coverage_options: fn -> %{"treat_no_relevant_lines_as_covered" => false} end,
+        get_file_col_width: fn -> 40 end,
+        get_print_files: fn -> true end
+      ] do
+    assert String.contains?(Local.coverage(@empty_source_info), "[TOTAL]   0.0%")
   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, get_print_summary: fn -> true 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,
+        get_print_files: fn -> true end
+      ] do
     output = capture_io(fn ->
       assert catch_exit(Local.execute(@source_info)) == {:shutdown, 1}
     end)
@@ -89,16 +104,38 @@ defmodule ExCoveralls.LocalTest do
   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, get_print_summary: fn -> true 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,
+        get_print_files: 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
+      ExCoveralls.Settings, [
+        get_coverage_options: fn -> %{"minimum_coverage" => 49.9} end,
+        get_file_col_width: fn -> 40 end,
+        get_print_summary: fn -> true end,
+        get_print_files: fn -> true end
+      ] do
     assert capture_io(fn ->
       Local.execute(@source_info)
     end) =~ ""
   end
+
+  test_with_mock "Do not output table if print_files 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,
+        get_print_files: fn -> false end
+      ] do
+    assert capture_io(fn ->
+      Local.execute(@source_info)
+    end) =~ @stats_no_files_results
+  end
 end