-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test Coverage Reporting #14343
Merged
+211
−14
Merged
Test Coverage Reporting #14343
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -15,3 +15,4 @@ | |
/.eunit | ||
.elixir.plt | ||
erl_crash.dump | ||
/cover/ |
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
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,30 @@ | ||
#!bin/elixir | ||
|
||
# SPDX-License-Identifier: Apache-2.0 | ||
# SPDX-FileCopyrightText: 2021 The Elixir Team | ||
|
||
Code.require_file("cover_record.exs", __DIR__) | ||
cover_pid = CoverageRecorder.enable_coverage() | ||
|
||
coverdata_inputs = | ||
CoverageRecorder.cover_dir() |> Path.join("ex_unit_*.coverdata") |> Path.wildcard() | ||
|
||
coverdata_output = Path.join(CoverageRecorder.cover_dir(), "combined.coverdata") | ||
|
||
for file <- coverdata_inputs do | ||
:ok = :cover.import(String.to_charlist(file)) | ||
end | ||
|
||
:ok = :cover.export(String.to_charlist(coverdata_output)) | ||
|
||
{:ok, _} = Application.ensure_all_started(:mix) | ||
|
||
# Silence analyse import messages emitted by cover | ||
{:ok, string_io} = StringIO.open("") | ||
Process.group_leader(cover_pid, string_io) | ||
|
||
:ok = | ||
Mix.Tasks.Test.Coverage.generate_cover_results( | ||
output: CoverageRecorder.cover_dir(), | ||
summary: [threshold: 0] | ||
) |
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,77 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# SPDX-FileCopyrightText: 2021 The Elixir Team | ||
|
||
defmodule CoverageRecorder do | ||
def maybe_record(suite_name) do | ||
if enabled?() do | ||
record(suite_name) | ||
|
||
true | ||
else | ||
false | ||
end | ||
end | ||
|
||
def enable_coverage do | ||
_ = :cover.stop() | ||
{:ok, pid} = :cover.start() | ||
|
||
cover_compile_ebins() | ||
|
||
pid | ||
end | ||
|
||
def cover_dir, do: Path.join(root_dir(), "cover") | ||
|
||
defp enabled? do | ||
case System.fetch_env("COVER") do | ||
{:ok, truthy} when truthy in ~w[1 true yes y] -> | ||
true | ||
|
||
_ -> | ||
false | ||
end | ||
end | ||
|
||
defp root_dir, do: Path.join(__DIR__, "../../..") | ||
defp ebins, do: root_dir() |> Path.join("lib/*/ebin") |> Path.wildcard() | ||
|
||
defp record(suite_name) do | ||
file = Path.join(cover_dir(), "ex_unit_#{suite_name}.coverdata") | ||
|
||
enable_coverage() | ||
|
||
System.at_exit(fn _status -> | ||
File.mkdir_p!(cover_dir()) | ||
|
||
:ok = :cover.export(String.to_charlist(file)) | ||
end) | ||
end | ||
|
||
defp cover_compile_ebins do | ||
relevant_beam_files() | ||
|> Enum.map(&String.to_charlist/1) | ||
|> :cover.compile_beam() | ||
|> Enum.each(fn | ||
{:ok, _module} -> | ||
:ok | ||
|
||
{:error, reason} -> | ||
raise "Failed to cover compile with reason: #{inspect(reason)}" | ||
end) | ||
end | ||
|
||
defp relevant_beam_files do | ||
ebins() | ||
|> Enum.flat_map(fn ebin -> | ||
ebin |> Path.join("*.beam") |> Path.wildcard() | ||
end) | ||
|> Enum.reject(&deprecated/1) | ||
end | ||
|
||
defp deprecated(file) do | ||
mod = file |> Path.basename(".beam") |> String.to_atom() | ||
|
||
match?({:docs_v1, _, _, _, _, %{deprecated: _}, _}, Code.fetch_docs(mod)) | ||
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a left-over?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, this is intentionally as a dependency of the
cover
target.The idea is that you can call
make cover
and it will run all the tests with coverage. (I tried to make it simple for non-regular contributors as well to get the coverage report.)