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.


355
356
357
358
359
360
361
362
363
364
# File 'lib/rcov/report.rb', line 355

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


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
433
434
435
436
437
438
# File 'lib/rcov/report.rb', line 394

def compare_state
    return unless verify_diff_available
    begin
        format, prev_state = File.open(@state_file){|f| self.SERIALIZER.load(f) }
    rescue
        $stderr.puts "Couldn't load coverage data from \#{@state_file}.\n"
        return              # '
    end
    if !(Array === format) or
        FORMAT_VERSION[0] != format[0] || FORMAT_VERSION[1] < format[1]
        $stderr.puts "Couldn't load coverage data from \#{@state_file}.\nThe file is saved in the format  \#{format.inspect[0..20]}.\nThis rcov executable understands \#{FORMAT_VERSION.inspect}.\n"
        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


440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/rcov/report.rb', line 440

def display_hunks(filename, hunks)
    return if hunks.empty?
    puts
    puts "=" * 80
    puts "!!!!! Uncovered code introduced in \#{filename}\n\n"
    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


366
367
368
369
370
371
372
373
374
375
# File 'lib/rcov/report.rb', line 366

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


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
533
534
535
536
537
538
# File 'lib/rcov/report.rb', line 498

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


377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/rcov/report.rb', line 377

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 "Couldn't save coverage data to \#{@state_file}.\n"
end

#SERIALIZERObject


348
349
350
351
352
353
# File 'lib/rcov/report.rb', line 348

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


469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/rcov/report.rb', line 469

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 "\nThe '\#{@diff_cmd}' executable seems not to be available.\nYou can specify which diff executable should be used with --diff-cmd.\nIf your system doesn't have one, you might want to use Diff::LCS's:\n  gem install diff-lcs\nand use --diff-cmd=ldiff.\n"
        return false
    end
    true
ensure
    STDOUT.reopen old_stdout
    STDERR.reopen old_stderr
    new_stderr.close!
end