Skip to content

Commit 162e71a

Browse files
authored
Add Custom Filename support for coveralls.json (#335)
* default case still working * add custom filename support to JSON * README updates
1 parent 610c538 commit 162e71a

File tree

4 files changed

+65
-15
lines changed

4 files changed

+65
-15
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,11 @@ Output to the shell is the same as running the command `mix coveralls` (to suppr
359359
Upload a coverage report to Codecov using their [bash uploader](https://docs.codecov.io/docs/about-the-codecov-bash-uploader)
360360
or to Code Climate using their [test-reporter](https://docs.codeclimate.com/docs/configuring-test-coverage).
361361
362-
Output reports are written to `cover/excoveralls.json` by default, however, the path can be specified by overwriting the `"output_dir"` coverage option.
362+
Output reports are written to `cover/excoveralls.json` by default, however, the path can be specified by overwriting the `"output_dir"` coverage option. Additionally, the filename can be specified using the `export` option under `test_coverage` in your `mix.exs` i.e.
363+
364+
`test_coverage: [tool: ExCoveralls, export: "my_awesome_report"]`
365+
366+
*Note:* the .json extension will be added automatically to the export name. Defaults to `excoveralls` if not specified.
363367
364368
### [mix coveralls.xml] Show coverage as XML report
365369
This task displays coverage information at the source-code level formatted as a XML document.

lib/excoveralls.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ defmodule ExCoveralls do
6363
else
6464
types = List.wrap(options[:type] || "local")
6565
stats = Stats.update_paths(stats, options)
66+
67+
# Push all available options down
68+
options = options ++ opts
6669
Enum.each(types, &analyze(stats, &1, options))
6770
end
6871
after

lib/excoveralls/json.ex

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ defmodule ExCoveralls.Json do
1010
Provides an entry point for the module.
1111
"""
1212
def execute(stats, options \\ []) do
13-
generate_json(stats, Enum.into(options, %{})) |> write_file(options[:output_dir])
13+
generate_json(stats, Enum.into(options, %{})) |> write_file(options)
1414

1515
ExCoveralls.Local.print_summary(stats)
1616
end
@@ -34,12 +34,22 @@ defmodule ExCoveralls.Json do
3434
end
3535
end
3636

37-
defp write_file(content, output_dir) do
37+
defp output_name(name) do
38+
case name do
39+
nil -> @file_name
40+
name -> "#{name}.json"
41+
end
42+
end
43+
44+
defp write_file(content, options) do
45+
output_dir = options[:output_dir]
46+
name = output_name(options[:export])
47+
3848
file_path = output_dir(output_dir)
3949
unless File.exists?(file_path) do
4050
File.mkdir_p!(file_path)
4151
end
42-
File.write!(Path.expand(@file_name, file_path), content)
52+
File.write!(Path.expand(name, file_path), content)
4353
end
4454

4555
end

test/json_test.exs

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule ExCoveralls.JsonTest do
44
import ExUnit.CaptureIO
55
alias ExCoveralls.Json
66

7-
@file_name "excoveralls.json"
7+
@file_name "excoveralls"
88
@file_size 136
99
@test_output_dir "cover_test/"
1010

@@ -24,31 +24,36 @@ defmodule ExCoveralls.JsonTest do
2424
"----------------\n"
2525

2626
setup do
27-
ExCoveralls.ConfServer.clear()
28-
path = Path.expand(@file_name, @test_output_dir)
27+
on_entry = fn name, path ->
28+
ExCoveralls.ConfServer.clear()
29+
path = Path.expand("#{name}.json", path)
30+
31+
# Assert does not exist prior to write
32+
assert(File.exists?(path) == false)
2933

30-
# Assert does not exist prior to write
31-
assert(File.exists?(path) == false)
32-
on_exit fn ->
34+
path
35+
end
36+
on_exit = fn path, dir ->
3337
if File.exists?(path) do
3438
# Ensure removed after test
3539
File.rm!(path)
36-
File.rmdir!(@test_output_dir)
40+
File.rmdir!(dir)
3741
end
3842

3943
ExCoveralls.ConfServer.clear()
4044
end
4145

42-
{:ok, report: path}
46+
{:ok, on_entry: on_entry, on_exit: on_exit}
4347
end
4448

45-
test_with_mock "generate json file", %{report: report}, ExCoveralls.Settings, [],
49+
test_with_mock "generate json file", %{on_entry: on_entry, on_exit: on_exit}, ExCoveralls.Settings, [],
4650
[
4751
get_coverage_options: fn -> %{"output_dir" => @test_output_dir} end,
4852
get_file_col_width: fn -> 40 end,
4953
get_print_summary: fn -> true end,
5054
get_print_files: fn -> true end
5155
] do
56+
report = on_entry.(@file_name, @test_output_dir)
5257

5358
assert capture_io(fn ->
5459
Json.execute(@source_info)
@@ -66,11 +71,37 @@ defmodule ExCoveralls.JsonTest do
6671
} = Jason.decode!(File.read!(report)))
6772
%{size: size} = File.stat! report
6873
assert(size == @file_size)
74+
75+
on_exit.(report, @test_output_dir)
76+
end
77+
78+
test "generate json file with output_dir parameter", %{on_entry: on_entry, on_exit: on_exit} do
79+
report = on_entry.(@file_name, "coverme")
80+
assert capture_io(fn ->
81+
Json.execute(@source_info, [output_dir: "coverme"])
82+
end) =~ @stats_result
83+
84+
assert(
85+
%{
86+
"source_files" => [
87+
%{
88+
"coverage" => [0, 1, nil, nil],
89+
"name" => "test/fixtures/test.ex",
90+
"source" => "defmodule Test do\n def test do\n end\nend\n"
91+
}
92+
]
93+
} = Jason.decode!(File.read!(report)))
94+
%{size: size} = File.stat! report
95+
assert(size == @file_size)
96+
97+
on_exit.(report, "coverme")
6998
end
7099

71-
test "generate json file with output_dir parameter", %{report: report} do
100+
test "generate json file with custom filename", %{on_entry: on_entry, on_exit: on_exit} do
101+
report = on_entry.("custom", @test_output_dir)
102+
72103
assert capture_io(fn ->
73-
Json.execute(@source_info, [output_dir: @test_output_dir])
104+
Json.execute(@source_info, [output_dir: @test_output_dir, export: "custom"])
74105
end) =~ @stats_result
75106

76107
assert(
@@ -85,5 +116,7 @@ defmodule ExCoveralls.JsonTest do
85116
} = Jason.decode!(File.read!(report)))
86117
%{size: size} = File.stat! report
87118
assert(size == @file_size)
119+
120+
on_exit.(report, @test_output_dir)
88121
end
89122
end

0 commit comments

Comments
 (0)