Skip to content
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

Cobertura now handles defprotocol and defimpl definitions #306

Merged
merged 2 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions lib/excoveralls/cobertura.ex
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,12 @@ defmodule ExCoveralls.Cobertura do
end

defp module_name(source) do
case Regex.run(~r/^defmodule\s+(.*)\s+do$/m, source, capture: :all_but_first) do
[module] ->
module

_ ->
[module] = Regex.run(~r/^-module\((.*)\)\.$/m, source, capture: :all_but_first)
module
end
with nil <- Regex.run(~r/^def(?:module|protocol|impl)\s+(.*)\s+do$/m, source, capture: :all_but_first),
nil <- Regex.run(~r/^-module\((.*)\)\.$/m, source, capture: :all_but_first) do
"UNKNOWN_MODULE"
else
[module] -> module
end
end

defp package_name(path, c_paths) do
Expand Down
40 changes: 40 additions & 0 deletions test/cobertura_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,44 @@ defmodule ExCoveralls.CoberturaTest do
assert String.ends_with?(source1, "/lib")
assert String.ends_with?(source2, "/test/fixtures")
end

test_with_mock "generate cobertura file with defprotocol", _, 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,
get_print_files: fn -> true end do
content = "defprotocol TestProtocol do\n def test(value)\nend\n"
counts = [0, 1, nil, nil]
source_info = [%{name: "test/fixtures/test_protocol.ex", source: content, coverage: counts}]

stats_result =
"" <>
"----------------\n" <>
"COV FILE LINES RELEVANT MISSED\n" <>
" 50.0% test/fixtures/test_protocol.ex 4 2 1\n" <>
"[TOTAL] 50.0%\n" <>
"----------------\n"

assert capture_io(fn -> Cobertura.execute(source_info) end) =~ stats_result
end

test_with_mock "generate cobertura file with defimpl", _, 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,
get_print_files: fn -> true end do
content = "defimpl TestProtocol, for: Integer do\n def test(value), do: \"integer!\" \nend\n"
counts = [0, 1, nil, nil]
source_info = [%{name: "test/fixtures/test_impl.ex", source: content, coverage: counts}]

stats_result =
"" <>
"----------------\n" <>
"COV FILE LINES RELEVANT MISSED\n" <>
" 50.0% test/fixtures/test_impl.ex 4 2 1\n" <>
"[TOTAL] 50.0%\n" <>
"----------------\n"

assert capture_io(fn -> Cobertura.execute(source_info) end) =~ stats_result
end
end