/
json_test.exs
88 lines (76 loc) · 2.4 KB
/
json_test.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
defmodule ExCoveralls.JsonTest do
use ExUnit.Case
import Mock
import ExUnit.CaptureIO
alias ExCoveralls.Json
@file_name "excoveralls.json"
@file_size 136
@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
ExCoveralls.ConfServer.clear()
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
ExCoveralls.ConfServer.clear()
end
{:ok, report: path}
end
test_with_mock "generate json 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 ->
Json.execute(@source_info)
end) =~ @stats_result
assert(
%{
"source_files" => [
%{
"coverage" => [0, 1, nil, nil],
"name" => "test/fixtures/test.ex",
"source" => "defmodule Test do\n def test do\n end\nend\n"
}
]
} = Jason.decode!(File.read!(report)))
%{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 ->
Json.execute(@source_info, [output_dir: @test_output_dir])
end) =~ @stats_result
assert(
%{
"source_files" => [
%{
"coverage" => [0, 1, nil, nil],
"name" => "test/fixtures/test.ex",
"source" => "defmodule Test do\n def test do\n end\nend\n"
}
]
} = Jason.decode!(File.read!(report)))
%{size: size} = File.stat! report
assert(size == @file_size)
end
end