Skip to content

Commit

Permalink
Import and export coverdata if needed (parroty#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
albertored authored Jan 24, 2023
1 parent 2dc17fd commit 5cbf759
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ Usage: mix coveralls <Options>
and your git repo resides in "app", then the root path should be: "/home/runs/app/" (from
coveralls.io)
--flagname Job flag name which will be shown in the Coveralls UI
--import_cover Directory from where '.coverdata' files should be imported and their results added to the report

Usage: mix coveralls.detail [--filter file-name-pattern]
Used to display coverage with detail
Expand Down
10 changes: 8 additions & 2 deletions lib/excoveralls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ defmodule ExCoveralls do
"""
def start(compile_path, opts) do
Cover.compile(compile_path)

options = ConfServer.get()
if options[:import_cover] do
Cover.import(options[:import_cover])
end

fn() ->
execute(ConfServer.get, compile_path, opts)
end
Expand Down Expand Up @@ -65,7 +71,7 @@ defmodule ExCoveralls do
output = Keyword.get(opts, :output, "cover")
File.mkdir_p!(output)

case :cover.export('#{output}/#{name}.coverdata') do
case Cover.export("#{output}/#{name}.coverdata") do
:ok ->
Mix.shell().info("Coverage data exported.")

Expand All @@ -77,7 +83,7 @@ defmodule ExCoveralls do
defp store_stats(stats, options, compile_path) do
{sub_app_name, _sub_app_path} =
ExCoveralls.SubApps.find(options[:sub_apps], compile_path)

Stats.append_sub_app_name(stats, sub_app_name, options[:apps_path]) |>
Stats.update_paths(options) |>
Enum.each(fn(stat) -> StatServer.add(stat) end)
Expand Down
15 changes: 14 additions & 1 deletion lib/excoveralls/cover.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ defmodule ExCoveralls.Cover do
"""
def compile(compile_path) do
:cover.stop
:cover.start
{:ok, pid} = :cover.start()

# Silence analyse import messages emitted by cover
{:ok, string_io} = StringIO.open("")
Process.group_leader(pid, string_io)

:cover.compile_beam_directory(compile_path |> string_to_charlist)
end

Expand All @@ -26,6 +31,14 @@ defmodule ExCoveralls.Cover do
:cover.modules |> Enum.filter(&has_compile_info?/1)
end

def import(base_path) do
Path.wildcard("#{base_path}/*.coverdata") |> Enum.map(&to_charlist/1) |> Enum.each(&:cover.import/1)
end

def export(path) do
path |> to_charlist() |> :cover.export()
end

def has_compile_info?(module) do
with info when not is_nil(info) <- module.module_info(:compile),
path when not is_nil(path) <- Keyword.get(info, :source) do
Expand Down
1 change: 1 addition & 0 deletions lib/excoveralls/task/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Usage: mix coveralls <Options>
and your git repo resides in "app", then the root path should be: "/home/runs/app/" (from
coveralls.io)
--flagname Job flag name which will be shown in the Coveralls UI
--import_cover Directory from where '.coverdata' files should be imported and their results added to the report
Usage: mix coveralls.detail [--filter file-name-pattern]
Used to display coverage with detail
Expand Down
6 changes: 4 additions & 2 deletions lib/mix/tasks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule Mix.Tasks.Coveralls do
message: "Please specify 'test_coverage: [tool: ExCoveralls]' in the 'project' section of mix.exs"
end

switches = [filter: :string, umbrella: :boolean, verbose: :boolean, pro: :boolean, parallel: :boolean, sort: :string, output_dir: :string, subdir: :string, rootdir: :string, flagname: :string]
switches = [filter: :string, umbrella: :boolean, verbose: :boolean, pro: :boolean, parallel: :boolean, sort: :string, output_dir: :string, subdir: :string, rootdir: :string, flagname: :string, import_cover: :string]
aliases = [f: :filter, u: :umbrella, v: :verbose, o: :output_dir]
{args, common_options} = parse_common_options(args, switches: switches, aliases: aliases)
all_options = options ++ common_options
Expand Down Expand Up @@ -269,6 +269,7 @@ defmodule Mix.Tasks.Coveralls do
rootdir: :string,
subdir: :string,
build: :string,
import_cover: :string
]
aliases = [f: :filter, u: :umbrella, v: :verbose]
{remaining, options} = Mix.Tasks.Coveralls.parse_common_options(
Expand All @@ -292,7 +293,8 @@ defmodule Mix.Tasks.Coveralls do
parallel: options[:parallel],
flag_name: options[:flagname] || "",
rootdir: options[:rootdir] || "",
subdir: options[:subdir] || ""
subdir: options[:subdir] || "",
import_cover: options[:string]
])
end

Expand Down
4 changes: 2 additions & 2 deletions test/mix/tasks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ defmodule Mix.Tasks.CoverallsTest do
[type: "post", endpoint: nil, token: "dummy_token",
service_name: "dummy_service_name", service_number: "1", branch: "branch",
committer: "committer", sha: "asdf", message: "message",
umbrella: nil, verbose: nil, parallel: nil, flag_name: "arbitrary_value", rootdir: "umbrella0/", subdir: "", args: []])
umbrella: nil, verbose: nil, parallel: nil, flag_name: "arbitrary_value", rootdir: "umbrella0/", subdir: "", import_cover: nil, args: []])

System.put_env("COVERALLS_REPO_TOKEN", org_token)
System.put_env("COVERALLS_SERVICE_NAME", org_name)
Expand All @@ -175,7 +175,7 @@ defmodule Mix.Tasks.CoverallsTest do
[type: "post", endpoint: nil, token: "token",
service_name: "excoveralls", service_number: "", branch: "",
committer: "", sha: "", message: "[no commit message]",
umbrella: nil, verbose: nil, parallel: nil, flag_name: "", rootdir: "", subdir: "", args: []])
umbrella: nil, verbose: nil, parallel: nil, flag_name: "", rootdir: "", subdir: "", import_cover: nil, args: []])

if org_token != nil do
System.put_env("COVERALLS_REPO_TOKEN", org_token)
Expand Down

0 comments on commit 5cbf759

Please sign in to comment.