Class: Rcov::TextCoverageDiff

Inherits:
Formatter show all
Defined in:
lib/rcov/report.rb

Overview

:nodoc:

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}
HUNK_HEADER =
/@@ -\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

#initialize(opts = {}) ⇒ TextCoverageDiff

Returns a new instance of TextCoverageDiff.



349
350
351
352
353
354
355
356
357
358
# File 'lib/rcov/report.rb', line 349

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_stateObject



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
430
431
432
# File 'lib/rcov/report.rb', line 388

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



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
459
460
461
# File 'lib/rcov/report.rb', line 434

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

#executeObject



360
361
362
363
364
365
366
367
368
369
# File 'lib/rcov/report.rb', line 360

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



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
530
531
532
# File 'lib/rcov/report.rb', line 492

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|
        #puts "#{state} %5d #{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 /^-/
                # ignore
            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_stateObject



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/rcov/report.rb', line 371

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

#SERIALIZERObject



342
343
344
345
346
347
# File 'lib/rcov/report.rb', line 342

def SERIALIZER
    # mfp> this was going to be YAML but I caught it failing at basic
    # round-tripping, turning "\n" into "" and corrupting the data, so
    # it must be Marshal for now
    Marshal
end

#verify_diff_availableObject



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/rcov/report.rb', line 463

def verify_diff_available
    old_stderr = STDERR.dup
    old_stdout = STDOUT.dup
    # TODO: should use /dev/null or NUL(?), but I don't want to add the
    # win32 check right now
    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