Module: Coverage::Helpers
- Defined in:
- lib/coverage/helpers.rb
Constant Summary collapse
- VERSION =
"1.0.0"
Class Method Summary collapse
-
.diff(cov1, cov2) ⇒ Object
Extract the coverage results that is covered by
cov1but not covered bycov2. -
.load(path) ⇒ Object
Load the coverage result from a file.
-
.merge(*covs) ⇒ Object
Sum up all coverage results.
-
.sanitize(cov) ⇒ Object
Make the covearge result able to marshal.
-
.save(path, cov) ⇒ Object
Save the coverage result to a file.
-
.to_lcov_info(cov, out: "", test_name: nil) ⇒ Object
Translate the result into LCOV info format.
Class Method Details
.diff(cov1, cov2) ⇒ Object
Extract the coverage results that is covered by cov1 but not covered by cov2.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/coverage/helpers.rb', line 114 def diff(cov1, cov2) ncov = {} old_format = true cov1.each do |path1, runs1| if cov2[path1] runs2 = cov2[path1] if runs1.is_a?(Array) && runs2.is_a?(Array) && old_format # diff two old-format (ruby 2.4 or before) coverage results ncov[path1] = diff_lines(runs1, runs2) next end # promotes from old format to new one if runs1.is_a?(Array) runs1 = { :lines => runs1 } end if runs2.is_a?(Array) runs2 = { :lines => runs2 } end if old_format old_format = false old2new!(ncov) end # diff two new-format (ruby 2.5 or later) coverage results ncov[path1] = {} [ [:lines, :diff_lines], [:branches, :diff_branches], [:methods, :diff_methods], ].each do |type, diff_func| if runs1[type] if runs2[type] ncov[path1][type] = send(diff_func, runs1[type], runs2[type]) else ncov[path1][type] = runs1[type] end end end else if runs1.is_a?(Array) && old_format ncov[path1] = runs1 next end # promotes from old format to new one if runs1.is_a?(Array) runs1 = { :lines => runs1 } end if old_format old_format = false old2new!(ncov) end ncov[path1] = runs1 end end ncov end |
.load(path) ⇒ Object
Load the coverage result from a file.
212 213 214 |
# File 'lib/coverage/helpers.rb', line 212 def load(path) Marshal.load(File.binread(path)) end |
.merge(*covs) ⇒ Object
Sum up all coverage results.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/coverage/helpers.rb', line 75 def self.merge(*covs) ncov = {} old_format = true covs.each do |cov| cov.each do |path, runs| if runs.is_a?(Array) && old_format # merge two old-format (ruby 2.4 or before) coverage results merge_lines(ncov[path] ||= [], runs) next end # promotes from old format to new one if runs.is_a?(Array) runs = { :lines => runs } end if old_format old_format = false old2new!(ncov) end # merge two new-format (ruby 2.5 or later) coverage results ncov[path] ||= {} [ [:lines, :merge_lines, []], [:branches, :merge_branches, {}], [:methods, :merge_methods, {}], ].each do |type, merge_func, default| if runs[type] send(merge_func, ncov[path][type] ||= default, runs[type]) end end end end ncov end |
.sanitize(cov) ⇒ Object
Make the covearge result able to marshal.
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/coverage/helpers.rb', line 177 def sanitize(cov) ncov = {} cov.each do |path, runs| if runs.is_a?(Array) ncov[path] = runs next end ncov[path] = {} ncov[path][:lines] = runs[:lines] if runs[:lines] ncov[path][:branches] = runs[:branches] if runs[:branches] if runs[:methods] ncov[path][:methods] = methods = {} runs[:methods].each do |mthd, run| klass = begin Marshal.dump(mthd[0]) mthd[0] rescue mthd[0].to_s end methods[[klass] + mthd.drop(1)] = run end end end ncov end |
.save(path, cov) ⇒ Object
Save the coverage result to a file.
207 208 209 |
# File 'lib/coverage/helpers.rb', line 207 def save(path, cov) File.binwrite(path, Marshal.dump(sanitize(cov))) end |
.to_lcov_info(cov, out: "", test_name: nil) ⇒ Object
Translate the result into LCOV info format.
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/coverage/helpers.rb', line 217 def to_lcov_info(cov, out: "", test_name: nil) out << "TN:#{ test_name }\n" cov.each do |path, runs| out << "SF:#{ path }\n" # function coverage if runs.is_a?(Hash) && runs[:methods] total = covered = 0 runs[:methods].each do |(klass, name, lineno), run| out << "FN:#{ lineno },#{ klass }##{ name }\n" total += 1 covered += 1 if run > 0 end out << "FNF:#{ total }\n" out << "FNF:#{ covered }\n" runs[:methods].each do |(klass, name, _), run| out << "FNDA:#{ run },#{ klass }##{ name }\n" end end # line coverage if runs.is_a?(Array) || (runs.is_a?(Hash) && runs[:lines]) total = covered = 0 lines = runs.is_a?(Array) ? runs : runs[:lines] lines.each_with_index do |run, lineno| next unless run out << "DA:#{ lineno + 1 },#{ run }\n" total += 1 covered += 1 if run > 0 end out << "LF:#{ total }\n" out << "LH:#{ covered }\n" end # branch coverage if runs.is_a?(Hash) && runs[:branches] total = covered = 0 id = 0 runs[:branches].each do |(_base_type, _, base_lineno), targets| i = 0 targets.each do |(_target_type, _target_lineno), run| out << "BRDA:#{ base_lineno },#{ id },#{ i },#{ run }\n" total += 1 covered += 1 if run > 0 i += 1 end id += 1 end out << "BRF:#{ total }\n" out << "BRH:#{ covered }\n" end out << "end_of_record\n" end out end |