-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #264 from satoren/minimum_lcov_support2
feat: minimum support for lcov (re)
- Loading branch information
Showing
5 changed files
with
169 additions
and
0 deletions.
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
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,73 @@ | ||
defmodule ExCoveralls.Lcov do | ||
@moduledoc """ | ||
Generate lcov output for results. | ||
""" | ||
|
||
@file_name "lcov.info" | ||
|
||
@doc """ | ||
Provides an entry point for the module. | ||
""" | ||
def execute(stats, options \\ []) do | ||
generate_lcov(stats, Enum.into(options, %{})) |> write_file(options[:output_dir]) | ||
|
||
ExCoveralls.Local.print_summary(stats) | ||
end | ||
|
||
def generate_lcov(stats, _options) do | ||
lcov = Enum.map(stats, fn stat -> generate_lcov_file(stat) end) |> Enum.join("\n") | ||
lcov <> "\n" | ||
end | ||
|
||
def generate_lcov_file(stat) do | ||
da = | ||
stat.coverage | ||
|> Enum.with_index(1) | ||
|> Enum.filter(fn {k, _v} -> k != nil end) | ||
|> Enum.map(fn {k, v} -> {Integer.to_string(v), Integer.to_string(k)} end) | ||
|> Enum.map(fn {line, count} -> "DA:" <> line <> "," <> count end) | ||
|
||
foundlines = | ||
stat.coverage | ||
|> Enum.filter(fn v -> v != nil end) | ||
|
||
lf = foundlines |> Enum.count() | ||
lh = foundlines |> Enum.filter(fn v -> v > 0 end) |> Enum.count() | ||
|
||
lines = | ||
["TN:", "SF:" <> stat.name] ++ | ||
da ++ | ||
[ | ||
"LF:" <> Integer.to_string(lf), | ||
"LH:" <> Integer.to_string(lh), | ||
"end_of_record" | ||
] | ||
|
||
Enum.join(lines, "\n") | ||
end | ||
|
||
defp output_dir(output_dir) do | ||
cond do | ||
output_dir -> | ||
output_dir | ||
|
||
true -> | ||
options = ExCoveralls.Settings.get_coverage_options() | ||
|
||
case Map.fetch(options, "output_dir") do | ||
{:ok, val} -> val | ||
_ -> "cover/" | ||
end | ||
end | ||
end | ||
|
||
defp write_file(content, output_dir) do | ||
file_path = output_dir(output_dir) | ||
|
||
unless File.exists?(file_path) do | ||
File.mkdir_p!(file_path) | ||
end | ||
|
||
File.write!(Path.expand(@file_name, file_path), content) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
defmodule ExCoveralls.LcovTest do | ||
use ExUnit.Case | ||
import Mock | ||
import ExUnit.CaptureIO | ||
alias ExCoveralls.Lcov | ||
|
||
@file_name "lcov.info" | ||
@file_size 67 | ||
@test_output_dir "cover_test/" | ||
|
||
@content "defmodule Test do\n def test do\n end\nend\n" | ||
@counts [0, 1, nil, nil] | ||
@source_info [%{name: "test/fixtures/test.ex", | ||
source: @content, | ||
coverage: @counts | ||
}] | ||
|
||
@stats_result "" <> | ||
"----------------\n" <> | ||
"COV FILE LINES RELEVANT MISSED\n" <> | ||
" 50.0% test/fixtures/test.ex 4 2 1\n" <> | ||
"[TOTAL] 50.0%\n" <> | ||
"----------------\n" | ||
|
||
setup do | ||
path = Path.expand(@file_name, @test_output_dir) | ||
|
||
# Assert does not exist prior to write | ||
assert(File.exists?(path) == false) | ||
on_exit fn -> | ||
if File.exists?(path) do | ||
# Ensure removed after test | ||
File.rm!(path) | ||
File.rmdir!(@test_output_dir) | ||
end | ||
end | ||
|
||
{:ok, report: path} | ||
end | ||
|
||
test_with_mock "generate lcov file", %{report: report}, 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 | ||
|
||
assert capture_io(fn -> | ||
Lcov.execute(@source_info) | ||
end) =~ @stats_result | ||
|
||
assert(File.read!(report) =~ ~s(TN:\nSF:test/fixtures/test.ex\nDA:1,0\nDA:2,1\nLF:2\nLH:1\nend_of_record\n)) | ||
%{size: size} = File.stat! report | ||
assert(size == @file_size) | ||
end | ||
|
||
test "generate json file with output_dir parameter", %{report: report} do | ||
assert capture_io(fn -> | ||
Lcov.execute(@source_info, [output_dir: @test_output_dir]) | ||
end) =~ @stats_result | ||
|
||
assert(File.read!(report) =~ ~s(TN:\nSF:test/fixtures/test.ex\nDA:1,0\nDA:2,1\nLF:2\nLH:1\nend_of_record\n)) | ||
%{size: size} = File.stat! report | ||
assert(size == @file_size) | ||
end | ||
end |