-
Notifications
You must be signed in to change notification settings - Fork 176
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 #218 from adrienmo/xml
Add support to generate XML files
- Loading branch information
Showing
6 changed files
with
162 additions
and
1 deletion.
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
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,56 @@ | ||
defmodule ExCoveralls.Xml do | ||
@moduledoc """ | ||
Generate XML output for results. | ||
""" | ||
|
||
alias ExCoveralls.Settings | ||
|
||
@file_name "excoveralls.xml" | ||
|
||
@doc """ | ||
Provides an entry point for the module. | ||
""" | ||
def execute(stats, options \\ []) do | ||
stats | ||
|> generate_xml(Enum.into(options, %{})) | ||
|> write_file(options[:output_dir]) | ||
|
||
ExCoveralls.Local.print_summary(stats) | ||
end | ||
|
||
def generate_xml(stats, _options) do | ||
base_dir = Settings.get_xml_base_dir() | ||
"<coverage version=\"1\">" <> Enum.map_join(stats, fn %{name: name, coverage: coverage} -> | ||
path = String.replace("#{base_dir}/#{name}", ~r/(\/)+/, "/", global: true) | ||
"<file path=\"#{path}\">" <> | ||
Enum.map_join(Enum.with_index(coverage), fn | ||
{nil, _line} -> "" | ||
{count, line} -> | ||
"<lineToCover lineNumber=\"#{line + 1}\" covered=\"#{count != 0}\"/>" | ||
end) | ||
<> "</file>" | ||
end) <> "</coverage>" | ||
end | ||
|
||
defp output_dir(output_dir) do | ||
cond do | ||
output_dir -> | ||
output_dir | ||
true -> | ||
options = 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,65 @@ | ||
defmodule ExCoveralls.XmlTest do | ||
use ExUnit.Case | ||
import Mock | ||
import ExUnit.CaptureIO | ||
alias ExCoveralls.Xml | ||
|
||
@file_name "excoveralls.xml" | ||
@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 xml 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, | ||
get_xml_base_dir: fn -> "base_dir" end | ||
] do | ||
|
||
assert capture_io(fn -> | ||
Xml.execute(@source_info) | ||
end) =~ @stats_result | ||
|
||
assert File.read!(report) =~ ~s(<coverage version="1"><file path="base_dir/test/fixtures/test.ex"><lineToCover lineNumber="1" covered="false"/><lineToCover lineNumber="2" covered="true"/></file></coverage>) | ||
assert %{size: 173} = File.stat! report | ||
end | ||
|
||
test "generate json file with output_dir parameter", %{report: report} do | ||
assert capture_io(fn -> | ||
Xml.execute(@source_info, [output_dir: @test_output_dir]) | ||
end) =~ @stats_result | ||
|
||
assert File.read!(report) =~ ~s(<coverage version="1"><file path="/test/fixtures/test.ex"><lineToCover lineNumber="1" covered="false"/><lineToCover lineNumber="2" covered="true"/></file></coverage>) | ||
assert %{size: 165} = File.stat! report | ||
end | ||
end |