From a001090c343e0d1cf2c76c9f24460e8f32ee0a79 Mon Sep 17 00:00:00 2001 From: Rodrigue Villetard Date: Tue, 21 Mar 2023 18:18:31 +0100 Subject: [PATCH 1/2] Cobertura now handles defprotocol and defimpl definitions --- lib/excoveralls/cobertura.ex | 5 +++-- test/cobertura_test.exs | 40 ++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/lib/excoveralls/cobertura.ex b/lib/excoveralls/cobertura.ex index 228c2aea..c4405d85 100644 --- a/lib/excoveralls/cobertura.ex +++ b/lib/excoveralls/cobertura.ex @@ -159,12 +159,13 @@ 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 + case Regex.run(~r/^def(?:module|protocol|impl)\s+(.*)\s+do$/m, source, capture: :all_but_first) do + [module] -> module _ -> - [module] = Regex.run(~r/^-module\((.*)\)\.$/m, source, capture: :all_but_first) + [module] = Regex.run(~r/^-(?:module|protocol|impl)\((.*)\)\.$/m, source, capture: :all_but_first) module end end diff --git a/test/cobertura_test.exs b/test/cobertura_test.exs index 92d01a64..4a78234a 100644 --- a/test/cobertura_test.exs +++ b/test/cobertura_test.exs @@ -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 From 689d1369835e50bd16b98cb860d0d04353d16613 Mon Sep 17 00:00:00 2001 From: Rodrigue Villetard Date: Tue, 21 Mar 2023 23:32:35 +0100 Subject: [PATCH 2/2] Cobertura, catch all if module type is unknown --- lib/excoveralls/cobertura.ex | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/excoveralls/cobertura.ex b/lib/excoveralls/cobertura.ex index c4405d85..2cd0bd56 100644 --- a/lib/excoveralls/cobertura.ex +++ b/lib/excoveralls/cobertura.ex @@ -159,15 +159,12 @@ defmodule ExCoveralls.Cobertura do end defp module_name(source) do - case Regex.run(~r/^def(?:module|protocol|impl)\s+(.*)\s+do$/m, source, capture: :all_but_first) do - - [module] -> - module - - _ -> - [module] = Regex.run(~r/^-(?:module|protocol|impl)\((.*)\)\.$/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