Skip to content

Commit

Permalink
add XML capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrien Moreau committed Mar 13, 2020
1 parent 0a1aa01 commit 22c9a3c
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/excoveralls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule ExCoveralls do
alias ExCoveralls.Html
alias ExCoveralls.Json
alias ExCoveralls.Post
alias ExCoveralls.Xml

@type_travis "travis"
@type_github "github"
Expand All @@ -26,6 +27,7 @@ defmodule ExCoveralls do
@type_html "html"
@type_json "json"
@type_post "post"
@type_xml "xml"

@doc """
This method will be called from mix to trigger coverage analysis.
Expand Down Expand Up @@ -110,6 +112,13 @@ defmodule ExCoveralls do
Json.execute(stats, options)
end

@doc """
Logic for XML output, without posting server
"""
def analyze(stats, @type_xml, options) do
Xml.execute(stats, options)
end

@doc """
Logic for posting from general CI server with token.
"""
Expand Down
7 changes: 7 additions & 0 deletions lib/excoveralls/settings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ defmodule ExCoveralls.Settings do
end
end

@doc """
Get xml base dir
"""
def get_xml_base_dir do
Map.get(get_coverage_options(), "xml_base_dir", "")
end

@doc """
Get skip files from the json file.
"""
Expand Down
55 changes: 55 additions & 0 deletions lib/excoveralls/xml.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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 = IO.inspect(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
{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 = 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
15 changes: 15 additions & 0 deletions lib/mix/tasks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,21 @@ defmodule Mix.Tasks.Coveralls do
end
end

defmodule Xml do
@moduledoc """
Provides an entry point for outputting coveralls information
as a XML file.
"""
use Mix.Task

@shortdoc "Output the test coverage as a XML file"
@preferred_cli_env :test

def run(args) do
Mix.Tasks.Coveralls.do_run(args, [ type: "xml" ])
end
end

defmodule Json do
@moduledoc """
Provides an entry point for outputting coveralls information
Expand Down

0 comments on commit 22c9a3c

Please sign in to comment.