Skip to content

Commit 8212269

Browse files
author
Rodrigue Villetard
authored
Cobertura now handles defprotocol and defimpl definitions (#306)
* Cobertura now handles defprotocol and defimpl definitions * Cobertura, catch all if module type is unknown
1 parent 3341fd6 commit 8212269

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

lib/excoveralls/cobertura.ex

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,12 @@ defmodule ExCoveralls.Cobertura do
159159
end
160160

161161
defp module_name(source) do
162-
case Regex.run(~r/^defmodule\s+(.*)\s+do$/m, source, capture: :all_but_first) do
163-
[module] ->
164-
module
165-
166-
_ ->
167-
[module] = Regex.run(~r/^-module\((.*)\)\.$/m, source, capture: :all_but_first)
168-
module
169-
end
162+
with nil <- Regex.run(~r/^def(?:module|protocol|impl)\s+(.*)\s+do$/m, source, capture: :all_but_first),
163+
nil <- Regex.run(~r/^-module\((.*)\)\.$/m, source, capture: :all_but_first) do
164+
"UNKNOWN_MODULE"
165+
else
166+
[module] -> module
167+
end
170168
end
171169

172170
defp package_name(path, c_paths) do

test/cobertura_test.exs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,44 @@ defmodule ExCoveralls.CoberturaTest do
198198
assert String.ends_with?(source1, "/lib")
199199
assert String.ends_with?(source2, "/test/fixtures")
200200
end
201+
202+
test_with_mock "generate cobertura file with defprotocol", _, ExCoveralls.Settings, [],
203+
get_coverage_options: fn -> %{"output_dir" => @test_output_dir} end,
204+
get_file_col_width: fn -> 40 end,
205+
get_print_summary: fn -> true end,
206+
get_print_files: fn -> true end do
207+
content = "defprotocol TestProtocol do\n def test(value)\nend\n"
208+
counts = [0, 1, nil, nil]
209+
source_info = [%{name: "test/fixtures/test_protocol.ex", source: content, coverage: counts}]
210+
211+
stats_result =
212+
"" <>
213+
"----------------\n" <>
214+
"COV FILE LINES RELEVANT MISSED\n" <>
215+
" 50.0% test/fixtures/test_protocol.ex 4 2 1\n" <>
216+
"[TOTAL] 50.0%\n" <>
217+
"----------------\n"
218+
219+
assert capture_io(fn -> Cobertura.execute(source_info) end) =~ stats_result
220+
end
221+
222+
test_with_mock "generate cobertura file with defimpl", _, ExCoveralls.Settings, [],
223+
get_coverage_options: fn -> %{"output_dir" => @test_output_dir} end,
224+
get_file_col_width: fn -> 40 end,
225+
get_print_summary: fn -> true end,
226+
get_print_files: fn -> true end do
227+
content = "defimpl TestProtocol, for: Integer do\n def test(value), do: \"integer!\" \nend\n"
228+
counts = [0, 1, nil, nil]
229+
source_info = [%{name: "test/fixtures/test_impl.ex", source: content, coverage: counts}]
230+
231+
stats_result =
232+
"" <>
233+
"----------------\n" <>
234+
"COV FILE LINES RELEVANT MISSED\n" <>
235+
" 50.0% test/fixtures/test_impl.ex 4 2 1\n" <>
236+
"[TOTAL] 50.0%\n" <>
237+
"----------------\n"
238+
239+
assert capture_io(fn -> Cobertura.execute(source_info) end) =~ stats_result
240+
end
201241
end

0 commit comments

Comments
 (0)