@@ -6,11 +6,13 @@ module SimpleCovJSONFormatter
6
6
class ResultHashFormatter
7
7
def initialize ( result )
8
8
@result = result
9
+ @parent_path = "#{ Dir . pwd } /"
9
10
end
10
11
11
12
def format
12
13
format_files
13
14
format_groups
15
+ format_total
14
16
15
17
formatted_result
16
18
end
@@ -19,34 +21,65 @@ def format
19
21
20
22
def format_files
21
23
@result . files . each do |source_file |
22
- formatted_result [ :coverage ] [ source_file . filename ] =
23
- format_source_file ( source_file )
24
+ formatted_file = format_source_file ( source_file )
25
+
26
+ formatted_result [ :coverage ] [ source_file . filename ] = formatted_file
27
+ minimum_coverage_check_for_file! ( source_file , formatted_file )
24
28
end
25
29
end
26
30
27
31
def format_groups
28
32
@result . groups . each do |name , file_list |
29
33
formatted_result [ :groups ] [ name ] = {
30
34
lines : {
31
- covered_percent : file_list . covered_percent
35
+ covered_percent : file_list . covered_percent ,
36
+ covered_lines : file_list . covered_lines ,
37
+ missed_lines : file_list . missed_lines ,
38
+ lines_of_code : file_list . lines_of_code
32
39
}
33
40
}
34
41
end
35
42
end
36
43
44
+ def format_total
45
+ formatted_result [ :total ] = {
46
+ covered_percent : @result . covered_percent ,
47
+ covered_lines : @result . covered_lines ,
48
+ missed_lines : @result . missed_lines ,
49
+ lines_of_code : @result . total_lines
50
+ }
51
+ end
52
+
37
53
def formatted_result
38
54
@formatted_result ||= {
39
55
meta : {
40
- simplecov_version : SimpleCov ::VERSION
56
+ simplecov_version : SimpleCov ::VERSION , config : config
41
57
} ,
42
58
coverage : { } ,
43
- groups : { }
59
+ groups : { } ,
60
+ errors : { less_than_minimum_coverage : { } } ,
61
+ total : { }
44
62
}
45
63
end
46
64
47
65
def format_source_file ( source_file )
48
66
source_file_formatter = SourceFileFormatter . new ( source_file )
49
67
source_file_formatter . format
50
68
end
69
+
70
+ def config
71
+ @config ||= {
72
+ minimum_coverage : SimpleCov . minimum_coverage [ :line ] ,
73
+ minimum_coverage_by_file : SimpleCov . minimum_coverage_by_file [ :line ]
74
+ }
75
+ end
76
+
77
+ def minimum_coverage_check_for_file! ( source_file , formatted_file )
78
+ return nil unless config [ :minimum_coverage_by_file ]
79
+ return nil unless formatted_file [ :percent ] < config [ :minimum_coverage_by_file ]
80
+
81
+ file_name = source_file . filename . delete_prefix ( @parent_path )
82
+ formatted_result [ :errors ] [ :less_than_minimum_coverage ] [ file_name ] = formatted_file [ :percent ]
83
+ end
51
84
end
52
85
end
0 commit comments