Skip to content

Commit

Permalink
add doc and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrien Moreau committed Mar 16, 2020
1 parent 22c9a3c commit 20a3309
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ end
- [[mix coveralls.detail] Show coverage with detail](#mix-coverallsdetail-show-coverage-with-detail)
- [[mix coveralls.html] Show coverage as HTML report](#mix-coverallshtml-show-coverage-as-html-report)
- [[mix coveralls.json] Show coverage as JSON report](#mix-coverallsjson-show-coverage-as-json-report)
- [[mix coveralls.xml] Show coverage as XML report](#mix-coverallsjson-show-coverage-as-xml-report)
- [coveralls.json](#coverallsjson)
- [Stop Words](#stop-words)
- [Exclude Files](#exclude-files)
Expand Down Expand Up @@ -321,6 +322,13 @@ or to Code Climate using their [test-reporter](https://docs.codeclimate.com/docs

Output reports are written to `cover/excoveralls.json` by default, however, the path can be specified by overwriting the `"output_dir"` coverage option.

### [mix coveralls.xml] Show coverage as XML report
This task displays coverage information at the source-code level formatted as a XML document.
The report follows a format supported by several code coverage services like SonarQube.
Output to the shell is the same as running the command `mix coveralls` (to suppress this output, add `"print_summary": false` to your project's `coveralls.json` file). In a similar manner to `mix coveralls.detail`, reported source code can be filtered by specifying arguments using the `--filter` flag.

Output reports are written to `cover/excoveralls.xml` by default, however, the path can be specified by overwriting the `"output_dir"` coverage option.

## coveralls.json
`coveralls.json` provides settings for excoveralls.

Expand Down Expand Up @@ -394,7 +402,8 @@ to `false`:
"treat_no_relevant_lines_as_covered": true,
"output_dir": "cover/",
"template_path": "custom/path/to/template/",
"minimum_coverage": 90
"minimum_coverage": 90,
"xml_base_dir": "custom/path/for/xml/reports/"
}
}
```
Expand Down
4 changes: 2 additions & 2 deletions lib/excoveralls/xml.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule ExCoveralls.Xml do
end

def generate_xml(stats, _options) do
base_dir = IO.inspect(Settings.get_xml_base_dir())
base_dir = Settings.get_xml_base_dir()
"<coverage version=\"1\">" <> Enum.map_join(stats, fn %{name: name, coverage: coverage} ->
"<file path=\"#{base_dir}/#{name}\">" <>
Enum.map_join(Enum.with_index(coverage), fn
Expand All @@ -36,7 +36,7 @@ defmodule ExCoveralls.Xml do
output_dir ->
output_dir
true ->
options = ExCoveralls.Settings.get_coverage_options
options = Settings.get_coverage_options
case Map.fetch(options, "output_dir") do
{:ok, val} -> val
_ -> "cover/"
Expand Down
65 changes: 65 additions & 0 deletions test/xml_test.exs
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

0 comments on commit 20a3309

Please sign in to comment.