Skip to content

Commit

Permalink
Cobertura now handles defprotocol and defimpl definitions (#306)
Browse files Browse the repository at this point in the history
* Cobertura now handles defprotocol and defimpl definitions

* Cobertura, catch all if module type is unknown
  • Loading branch information
gorghoa authored Mar 27, 2023
1 parent 3341fd6 commit 8212269
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
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

0 comments on commit 8212269

Please sign in to comment.