From 08a397e051f16b54b18870fa1a3777664a216673 Mon Sep 17 00:00:00 2001 From: Alberto Sartori Date: Wed, 1 Mar 2023 16:08:01 +0100 Subject: [PATCH] Add coveralls.multiple command --- lib/excoveralls.ex | 5 +++-- lib/excoveralls/conf_server.ex | 14 ++++++++++++++ lib/excoveralls/local.ex | 7 +++++-- lib/excoveralls/task/util.ex | 7 +++++++ lib/mix/tasks.ex | 35 ++++++++++++++++++++++++++++++++++ test/html_test.exs | 3 +++ test/json_test.exs | 3 +++ test/lcov_test.exs | 3 +++ test/local_test.exs | 6 ++++++ test/mix/tasks_test.exs | 7 +++++++ test/xml_test.exs | 3 +++ 11 files changed, 89 insertions(+), 4 deletions(-) diff --git a/lib/excoveralls.ex b/lib/excoveralls.ex index 7769481b..a1e2bef2 100644 --- a/lib/excoveralls.ex +++ b/lib/excoveralls.ex @@ -58,8 +58,9 @@ defmodule ExCoveralls do if options[:umbrella] do store_stats(stats, options, compile_path) else - Stats.update_paths(stats, options) |> - analyze(options[:type] || "local", options) + types = List.wrap(options[:type] || "local") + stats = Stats.update_paths(stats, options) + Enum.each(types, &analyze(stats, &1, options)) end after if name = opts[:export] do diff --git a/lib/excoveralls/conf_server.ex b/lib/excoveralls/conf_server.ex index 5a1ee3f6..1a0a8713 100644 --- a/lib/excoveralls/conf_server.ex +++ b/lib/excoveralls/conf_server.ex @@ -5,6 +5,7 @@ defmodule ExCoveralls.ConfServer do @ets_table :excoveralls_conf_server @ets_key :config_key + @ets_summary_key :summary_key @doc """ Initialize the data-store table. @@ -41,4 +42,17 @@ defmodule ExCoveralls.ConfServer do :ets.insert(@ets_table, {@ets_key, value}) value end + + def summary_printed do + start() + :ets.insert(@ets_table, {@ets_summary_key, true}) + end + + def summary_printed? do + start() + + @ets_table + |> :ets.lookup(@ets_summary_key) + |> Keyword.get(@ets_summary_key, false) + end end diff --git a/lib/excoveralls/local.ex b/lib/excoveralls/local.ex index cfc7ee7e..2317f719 100644 --- a/lib/excoveralls/local.ex +++ b/lib/excoveralls/local.ex @@ -2,6 +2,8 @@ defmodule ExCoveralls.Local do @moduledoc """ Locally displays the result to screen. """ + + defmodule Count do @moduledoc """ @@ -44,8 +46,9 @@ defmodule ExCoveralls.Local do """ def print_summary(stats, options \\ []) do enabled = ExCoveralls.Settings.get_print_summary - if enabled do - coverage(stats, options) |> IO.puts + if enabled and not ExCoveralls.ConfServer.summary_printed?() do + coverage(stats, options) |> IO.puts() + ExCoveralls.ConfServer.summary_printed() end end diff --git a/lib/excoveralls/task/util.ex b/lib/excoveralls/task/util.ex index d473e7f2..5c97ad41 100644 --- a/lib/excoveralls/task/util.ex +++ b/lib/excoveralls/task/util.ex @@ -56,6 +56,13 @@ Usage: mix coveralls.post -s (--sha) Commit SHA (required when not using Travis) --build Service number ('BUILDS' column at coveralls.io page) --parallel coveralls.io 'parallel' option (See coveralls.io API Reference) + +Usage: mix coveralls.multiple + Used to perform multiple coveralls task at once without need of re-running tests. + + + --type Coveralls task to execute (can be given multiple times) + e.g. 'mix coveralls.multiple --type html --type json' """ end end diff --git a/lib/mix/tasks.ex b/lib/mix/tasks.ex index 55020b0b..d5fa5040 100644 --- a/lib/mix/tasks.ex +++ b/lib/mix/tasks.ex @@ -310,4 +310,39 @@ defmodule Mix.Tasks.Coveralls do end end end + + defmodule Multiple do + @moduledoc """ + Provides an entry point for executing multiple coveralls + task at once without re-running tests + """ + + use Mix.Task + + @preferred_cli_env :test + + def run(args) do + {parsed, _, _} = OptionParser.parse(args, strict: [type: :keep]) + + args = remove_type_args(args) + + case Keyword.get_values(parsed, :type) do + [] -> raise ExCoveralls.InvalidOptionError, message: "type argument is required" + types -> Mix.Tasks.Coveralls.do_run(args, type: types) + end + end + + defp remove_type_args(args) do + {res, _} = + Enum.reduce(args, {[], nil}, fn entry, {buffer, acc} -> + case {entry, acc} do + {_, "--type" }-> {buffer, nil} + {"--type", _} -> {buffer, "--type"} + {el, _} -> {[el | buffer], nil} + end + end) + + Enum.reverse(res) + end + end end diff --git a/test/html_test.exs b/test/html_test.exs index bb29e3f3..571ad244 100644 --- a/test/html_test.exs +++ b/test/html_test.exs @@ -24,6 +24,7 @@ defmodule ExCoveralls.HtmlTest do "----------------\n" setup do + ExCoveralls.ConfServer.clear() path = Path.expand(@file_name, @test_output_dir) # Assert does not exist prior to write @@ -34,6 +35,8 @@ defmodule ExCoveralls.HtmlTest do File.rm!(path) File.rmdir!(@test_output_dir) end + + ExCoveralls.ConfServer.clear() end {:ok, report: path} diff --git a/test/json_test.exs b/test/json_test.exs index b4508a57..b129281d 100644 --- a/test/json_test.exs +++ b/test/json_test.exs @@ -23,6 +23,7 @@ defmodule ExCoveralls.JsonTest do "----------------\n" setup do + ExCoveralls.ConfServer.clear() path = Path.expand(@file_name, @test_output_dir) # Assert does not exist prior to write @@ -33,6 +34,8 @@ defmodule ExCoveralls.JsonTest do File.rm!(path) File.rmdir!(@test_output_dir) end + + ExCoveralls.ConfServer.clear() end {:ok, report: path} diff --git a/test/lcov_test.exs b/test/lcov_test.exs index ce3710bf..9146b8f8 100644 --- a/test/lcov_test.exs +++ b/test/lcov_test.exs @@ -23,6 +23,7 @@ defmodule ExCoveralls.LcovTest do "----------------\n" setup do + ExCoveralls.ConfServer.clear() path = Path.expand(@file_name, @test_output_dir) # Assert does not exist prior to write @@ -33,6 +34,8 @@ defmodule ExCoveralls.LcovTest do File.rm!(path) File.rmdir!(@test_output_dir) end + + ExCoveralls.ConfServer.clear() end {:ok, report: path} diff --git a/test/local_test.exs b/test/local_test.exs index cf3ffc80..8d13f637 100644 --- a/test/local_test.exs +++ b/test/local_test.exs @@ -37,6 +37,12 @@ defmodule ExCoveralls.LocalTest do "\e[31mdefmodule Test do\e[m\n\e[32m def test do\e[m\n" <> " end\n" <> "end" + + setup do + ExCoveralls.ConfServer.clear() + on_exit(fn -> ExCoveralls.ConfServer.clear() end) + :ok + end test "display source information" do assert(Local.source(@source_info) =~ @source_result) diff --git a/test/mix/tasks_test.exs b/test/mix/tasks_test.exs index 99557dba..e4ed1d7d 100644 --- a/test/mix/tasks_test.exs +++ b/test/mix/tasks_test.exs @@ -87,6 +87,13 @@ defmodule Mix.Tasks.CoverallsTest do assert(ExCoveralls.ConfServer.get == [type: "html", args: []]) end + test_with_mock "multiple", Runner, [run: fn(_, _) -> nil end] do + Mix.Tasks.Coveralls.Multiple.run(["--type", "html", "--type", "json", "--export-coverage", "cover", "test/foo_test.exs"]) + assert(called Runner.run("test", ["--cover", "--export-coverage", "cover", "test/foo_test.exs"])) + assert(ExCoveralls.ConfServer.get()[:type] == ["html", "json"]) + assert(ExCoveralls.ConfServer.get()[:args] == ["--export-coverage", "cover", "test/foo_test.exs"]) + end + test_with_mock "json", Runner, [run: fn(_, _) -> nil end] do Mix.Tasks.Coveralls.Json.run([]) assert(called Runner.run("test", ["--cover"])) diff --git a/test/xml_test.exs b/test/xml_test.exs index 707ac89d..ad0e6019 100644 --- a/test/xml_test.exs +++ b/test/xml_test.exs @@ -22,6 +22,7 @@ defmodule ExCoveralls.XmlTest do "----------------\n" setup do + ExCoveralls.ConfServer.clear() path = Path.expand(@file_name, @test_output_dir) # Assert does not exist prior to write @@ -32,6 +33,8 @@ defmodule ExCoveralls.XmlTest do File.rm!(path) File.rmdir!(@test_output_dir) end + + ExCoveralls.ConfServer.clear() end {:ok, report: path}