Class: Rcov::TextCoverageDiff
Overview
Constant Summary
collapse
- FORMAT_VERSION =
[0, 1, 0]
- DEFAULT_OPTS =
{:textmode => :coverage_diff,
:coverage_diff_mode => :record,
:coverage_diff_file => "coverage.info",
:diff_cmd => "diff", :comments_run_by_default => true}
/@@ -\d+,\d+ \+(\d+),(\d+) @@/
Instance Method Summary
collapse
Methods inherited from Formatter
#add_file, #code_coverage, #each_file_pair_sorted, #mangle_filename, #normalize_filename, #num_code_lines, #num_lines, #sorted_file_pairs, #total_coverage
Constructor Details
Returns a new instance of TextCoverageDiff.
346
347
348
349
350
351
352
353
354
355
|
# File 'lib/rcov/report.rb', line 346
def initialize(opts = {})
options = DEFAULT_OPTS.clone.update(opts)
@textmode = options[:textmode]
@color = options[:color]
@mode = options[:coverage_diff_mode]
@state_file = options[:coverage_diff_file]
@diff_cmd = options[:diff_cmd]
@gcc_output = options[:gcc_output]
super(options)
end
|
Instance Method Details
#compare_state ⇒ Object
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
# File 'lib/rcov/report.rb', line 385
def compare_state
return unless verify_diff_available
begin
format, prev_state = File.open(@state_file){|f| self.SERIALIZER.load(f) }
rescue
$stderr.puts <<-EOF
Couldn't load coverage data from #{@state_file}.
EOF
return end
if !(Array === format) or
FORMAT_VERSION[0] != format[0] || FORMAT_VERSION[1] < format[1]
$stderr.puts <<-EOF
Couldn't load coverage data from #{@state_file}.
The file is saved in the format #{format.inspect[0..20]}.
This rcov executable understands #{FORMAT_VERSION.inspect}.
EOF
return end
each_file_pair_sorted do |filename, fileinfo|
old_data = Tempfile.new("#{mangle_filename(filename)}-old")
new_data = Tempfile.new("#{mangle_filename(filename)}-new")
if prev_state.has_key? filename
old_code, old_cov = prev_state[filename].values_at(:lines, :coverage)
old_code.each_with_index do |line, i|
prefix = old_cov[i] ? " " : "!! "
old_data.write "#{prefix}#{line}"
end
else
old_data.write ""
end
old_data.close
SCRIPT_LINES__[filename].each_with_index do |line, i|
prefix = fileinfo.coverage[i] ? " " : "!! "
new_data.write "#{prefix}#{line}"
end
new_data.close
diff = `#{@diff_cmd} -u "#{old_data.path}" "#{new_data.path}"`
new_uncovered_hunks = process_unified_diff(filename, diff)
old_data.close!
new_data.close!
display_hunks(filename, new_uncovered_hunks)
end
end
|
#display_hunks(filename, hunks) ⇒ Object
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
|
# File 'lib/rcov/report.rb', line 431
def display_hunks(filename, hunks)
return if hunks.empty?
puts
puts "=" * 80
puts <<EOF
!!!!! Uncovered code introduced in #{filename}
EOF
hunks.each do |offset, lines|
if @gcc_output
lines.each_with_index do |line,i|
lineno = offset + i
flag = (/^!! / !~ line) ? "-" : ":"
prefix = "#{filename}#{flag}#{lineno}#{flag}"
puts "#{prefix}#{line[3..-1]}"
end
elsif @color
puts "### #{filename}:#{offset}"
lines.each do |line|
prefix = (/^!! / !~ line) ? "\e[32;40m" : "\e[31;40m"
puts "#{prefix}#{line[3..-1].chomp}\e[37;40m"
end
else
puts "### #{filename}:#{offset}"
puts lines
end
end
end
|
#execute ⇒ Object
357
358
359
360
361
362
363
364
365
366
|
# File 'lib/rcov/report.rb', line 357
def execute
case @mode
when :record
record_state
when :compare
compare_state
else
raise "Unknown TextCoverageDiff mode: #{mode.inspect}."
end
end
|
#process_unified_diff(filename, diff) ⇒ Object
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
|
# File 'lib/rcov/report.rb', line 489
def process_unified_diff(filename, diff)
current_hunk = []
current_hunk_start = 0
keep_current_hunk = false
state = :init
interesting_hunks = []
diff.each_with_index do |line, i|
case state
when :init
if md = HUNK_HEADER.match(line)
current_hunk = []
current_hunk_start = md[1].to_i
state = :body
end
when :body
case line
when HUNK_HEADER
new_start = $1.to_i
if keep_current_hunk
interesting_hunks << [current_hunk_start, current_hunk]
end
current_hunk_start = new_start
current_hunk = []
keep_current_hunk = false
when /^-/
when /^\+!! /
keep_current_hunk = true
current_hunk << line[1..-1]
else
current_hunk << line[1..-1]
end
end
end
if keep_current_hunk
interesting_hunks << [current_hunk_start, current_hunk]
end
interesting_hunks
end
|
#record_state ⇒ Object
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
# File 'lib/rcov/report.rb', line 368
def record_state
state = {}
each_file_pair_sorted do |filename, fileinfo|
state[filename] = {:lines => SCRIPT_LINES__[filename],
:coverage => fileinfo.coverage.to_a,
:counts => fileinfo.counts}
end
File.open(@state_file, "w") do |f|
self.SERIALIZER.dump([FORMAT_VERSION, state], f)
end
rescue
$stderr.puts <<-EOF
Couldn't save coverage data to #{@state_file}.
EOF
end
|
#SERIALIZER ⇒ Object
339
340
341
342
343
344
|
# File 'lib/rcov/report.rb', line 339
def SERIALIZER
Marshal
end
|
#verify_diff_available ⇒ Object
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
|
# File 'lib/rcov/report.rb', line 460
def verify_diff_available
old_stderr = STDERR.dup
old_stdout = STDOUT.dup
new_stderr = Tempfile.new("rcov_check_diff")
STDERR.reopen new_stderr.path
STDOUT.reopen new_stderr.path
retval = system "#{@diff_cmd} --version"
unless retval
old_stderr.puts <<EOF
The '#{@diff_cmd}' executable seems not to be available.
You can specify which diff executable should be used with --diff-cmd.
If your system doesn't have one, you might want to use Diff::LCS's:
gem install diff-lcs
and use --diff-cmd=ldiff.
EOF
return false
end
true
ensure
STDOUT.reopen old_stdout
STDERR.reopen old_stderr
new_stderr.close!
end
|