1
+ require 'slather/coverage_info'
2
+ require 'slather/coveralls_coverage'
3
+
1
4
module Slather
2
5
class CoverageFile
3
6
7
+ include CoverageInfo
8
+ include CoverallsCoverage
9
+
4
10
attr_accessor :project , :gcno_file_pathname
5
11
6
12
def initialize ( project , gcno_file_pathname )
@@ -11,7 +17,6 @@ def initialize(project, gcno_file_pathname)
11
17
def source_file_pathname
12
18
@source_file_pathname ||= begin
13
19
base_filename = gcno_file_pathname . basename . sub_ext ( "" )
14
- # TODO: Handle Swift
15
20
path = nil
16
21
if project . source_directory
17
22
path = Dir [ "#{ project . source_directory } /**/#{ base_filename } .{#{ supported_file_extensions . join ( "," ) } }" ] . first
@@ -36,10 +41,6 @@ def source_data
36
41
source_file . read
37
42
end
38
43
39
- def source_file_pathname_relative_to_repo_root
40
- source_file_pathname . realpath . relative_path_from ( Pathname ( "./" ) . realpath )
41
- end
42
-
43
44
def gcov_data
44
45
@gcov_data ||= begin
45
46
gcov_output = `gcov "#{ source_file_pathname } " --object-directory "#{ gcno_file_pathname . parent } " --branch-probabilities --branch-counts`
@@ -58,6 +59,24 @@ def gcov_data
58
59
end
59
60
end
60
61
62
+ def all_lines
63
+ unless cleaned_gcov_data . empty?
64
+ first_line_start = cleaned_gcov_data =~ /^\s +(-|#+|[0-9+]):\s +1:/
65
+ cleaned_gcov_data [ first_line_start ..-1 ] . split ( "\n " ) . map
66
+ else
67
+ [ ]
68
+ end
69
+ end
70
+
71
+ def cleaned_gcov_data
72
+ data = gcov_data . encode ( 'UTF-8' , 'binary' , invalid : :replace , undef : :replace , replace : '' ) . gsub ( /^function(.*) called [0-9]+ returned [0-9]+% blocks executed(.*)$\r ?\n / , '' )
73
+ data . gsub ( /^branch(.*)$\r ?\n / , '' )
74
+ end
75
+
76
+ def raw_data
77
+ self . gcov_data
78
+ end
79
+
61
80
def line_coverage_data
62
81
unless cleaned_gcov_data . empty?
63
82
first_line_start = cleaned_gcov_data =~ /^\s +(-|#+|[0-9+]):\s +1:/
@@ -70,9 +89,8 @@ def line_coverage_data
70
89
end
71
90
end
72
91
73
- def cleaned_gcov_data
74
- data = gcov_data . encode ( 'UTF-8' , 'binary' , invalid : :replace , undef : :replace , replace : '' ) . gsub ( /^function(.*) called [0-9]+ returned [0-9]+% blocks executed(.*)$\r ?\n / , '' )
75
- data . gsub ( /^branch(.*)$\r ?\n / , '' )
92
+ def line_number_in_line ( line )
93
+ line . split ( ':' ) [ 1 ] . strip . to_i
76
94
end
77
95
78
96
def coverage_for_line ( line )
@@ -89,102 +107,31 @@ def coverage_for_line(line)
89
107
end
90
108
end
91
109
92
- def num_lines_tested
93
- line_coverage_data . compact . select { |cd | cd > 0 } . count
94
- end
95
-
96
- def num_lines_testable
97
- line_coverage_data . compact . count
98
- end
99
-
100
- def rate_lines_tested
101
- if num_lines_testable > 0
102
- ( num_lines_tested / num_lines_testable . to_f )
103
- else
104
- 0
105
- end
106
- end
107
-
108
- def percentage_lines_tested
109
- if num_lines_testable == 0
110
- 100
111
- else
112
- rate_lines_tested * 100
113
- end
114
- end
115
-
116
110
def branch_coverage_data
117
111
@branch_coverage_data ||= begin
118
112
branch_coverage_data = Hash . new
119
113
120
- gcov_data . scan ( /(^(\s +(-|#+|[0-9]+):\s +[1-9]+:(.*)$\r ?\n )(^branch\s +[0-9]+\s +[a-zA-Z0-9]+\s +[a-zA-Z0-9]+$\r ?\n )+)+/ ) do |data |
121
- lines = data [ 0 ] . split ( "\n " )
122
- line_number = lines [ 0 ] . split ( ':' ) [ 1 ] . strip . to_i
123
- branch_coverage_data [ line_number ] = lines [ 1 ..-1 ] . map do |line |
124
- if line . split ( ' ' ) [ 2 ] . strip == "never"
125
- 0
126
- else
127
- line . split ( ' ' ) [ 3 ] . strip . to_i
128
- end
114
+ gcov_data . scan ( /(^(\s +(-|#+|[0-9]+):\s +[1-9]+:(.*)$\r ?\n )(^branch\s +[0-9]+\s +[a-zA-Z0-9]+\s +[a-zA-Z0-9]+$\r ?\n )+)+/ ) do |data |
115
+ lines = data [ 0 ] . split ( "\n " )
116
+ line_number = lines [ 0 ] . split ( ':' ) [ 1 ] . strip . to_i
117
+ branch_coverage_data [ line_number ] = lines [ 1 ..-1 ] . map do |line |
118
+ if line . split ( ' ' ) [ 2 ] . strip == "never"
119
+ 0
120
+ else
121
+ line . split ( ' ' ) [ 3 ] . strip . to_i
129
122
end
130
123
end
124
+ end
131
125
branch_coverage_data
132
126
end
133
127
end
134
128
135
- def branch_coverage_data_for_statement_on_line ( line_number )
136
- branch_coverage_data [ line_number ] || [ ]
137
- end
138
-
139
- def num_branches_for_statement_on_line ( line_number )
140
- branch_coverage_data_for_statement_on_line ( line_number ) . length
141
- end
142
-
143
- def num_branch_hits_for_statement_on_line ( line_number )
144
- branch_coverage_data_for_statement_on_line ( line_number ) . count { |hit_count | hit_count > 0 }
145
- end
146
-
147
- def rate_branch_coverage_for_statement_on_line ( line_number )
148
- branch_data = branch_coverage_data_for_statement_on_line ( line_number )
149
- if branch_data . empty?
150
- 0.0
151
- else
152
- ( num_branch_hits_for_statement_on_line ( line_number ) / branch_data . length . to_f )
153
- end
154
- end
155
-
156
- def percentage_branch_coverage_for_statement_on_line ( line_number )
157
- rate_branch_coverage_for_statement_on_line ( line_number ) * 100
158
- end
159
-
160
- def num_branches_testable
161
- branch_coverage_data . keys . reduce ( 0 ) do |sum , line_number |
162
- sum += num_branches_for_statement_on_line ( line_number )
163
- end
164
- end
165
-
166
- def num_branches_tested
167
- branch_coverage_data . keys . reduce ( 0 ) do |sum , line_number |
168
- sum += num_branch_hits_for_statement_on_line ( line_number )
169
- end
170
- end
171
-
172
- def rate_branches_tested
173
- if ( num_branches_testable > 0 )
174
- ( num_branches_tested / num_branches_testable . to_f )
175
- else
176
- 0.0
177
- end
178
- end
179
-
180
129
def source_file_basename
181
130
File . basename ( source_file_pathname , '.m' )
182
131
end
183
132
184
- def ignored?
185
- project . ignore_list . any? do |ignore |
186
- File . fnmatch ( ignore , source_file_pathname_relative_to_repo_root )
187
- end
133
+ def line_number_separator
134
+ ":"
188
135
end
189
136
190
137
def supported_file_extensions
0 commit comments