Skip to content

Commit

Permalink
Cope with Rcov giving different lines for coverage with Ruby 1.8 and 1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
josephwilk committed Apr 3, 2010
1 parent 79b0104 commit 9b33a30
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 23 deletions.
3 changes: 3 additions & 0 deletions cucumber.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
default: --profile 1_8
1_8: --tags ~@ruby_1_9
1_9: --tags @ruby_1_9
37 changes: 37 additions & 0 deletions features/coverage_of.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Feature: Coverage Of
Background:
Given I am using the simple example app

@ruby_1_8
Scenario: Get coverage of a source file that's partly touched by the feature that was run
Given I have run cucover -- features/call_foo.feature
When I run cucover --coverage-of lib/foo.rb
Expand All @@ -23,6 +24,7 @@ Feature: Coverage Of
"""

@ruby_1_8
Scenario: Run another feature that also covers the same source file
Given I have run cucover -- features/call_foo.feature
And I have run cucover -- features/call_foo_and_bar_together.feature
Expand All @@ -41,4 +43,39 @@ Feature: Coverage Of
"""

@ruby_1_9
Scenario: Get coverage of a source file that's partly touched by the feature that was run
Given I have run cucover -- features/call_foo.feature
When I run cucover --coverage-of lib/foo.rb
Then it should pass with:
"""
1 class Foo
2 def execute features/call_foo.feature:3
3 true features/call_foo.feature:3
4 end features/call_foo.feature:3
5
6 def sloppy_me..
7 1/0
8 end
9 end
"""

@ruby_1_9
Scenario: Run another feature that also covers the same source file
Given I have run cucover -- features/call_foo.feature
And I have run cucover -- features/call_foo_and_bar_together.feature
When I run cucover --coverage-of lib/foo.rb
Then it should pass with:
"""
1 class Foo
2 def execute features/call_foo.feature:3, features/call_foo_and_bar_together.feature:3
3 true features/call_foo.feature:3, features/call_foo_and_bar_together.feature:3
4 end features/call_foo.feature:3, features/call_foo_and_bar_together.feature:3
5
6 def sloppy_me..
7 1/0
8 end
9 end
"""
20 changes: 11 additions & 9 deletions lib/cucover.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$:.unshift(File.dirname(__FILE__))
$:.unshift(File.dirname(__FILE__))
require 'dependencies'
require 'cucover/logging_config'
require 'cucover/cli_commands/coverage_of'
Expand All @@ -12,6 +12,8 @@
require 'cucover/recording'
require 'cucover/recorder'
require 'cucover/store'
require 'cucover/line_numbers'


require 'at_exit_hook'

Expand All @@ -20,11 +22,11 @@ class << self
def logger
Logging::Logger['Cucover']
end

def should_execute?(scenario_or_table_row)
controller(scenario_or_table_row).should_execute?
end

def start_recording!(scenario_or_table_row)
raise("Already recording. Please call stop first.") if recording?

Expand All @@ -40,24 +42,24 @@ def stop_recording!
store.keep!(@current_recorder.recording)
@current_recorder = nil
end

def record_file(source_file)
Cucover.logger.debug("Recording extra source file #{source_file}")
@current_recorder.record_file!(source_file)
end

private

def controller(scenario_or_table_row)
Controller.new(scenario_or_table_row.file_colon_line, store)
end

def recording?
!!@current_recorder
end

def store
@store ||= Store.new
end
end
end
end
10 changes: 10 additions & 0 deletions lib/cucover/line_numbers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'rbconfig'

module Cucover
module LineNumbers
# Different Ruby versions have slightly different line numbers with rcov
def self.offset
Config::CONFIG['MINOR'] == '9' ? 0 : 1
end
end
end
24 changes: 12 additions & 12 deletions lib/cucover/recording/covered_file.rb
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
module Cucover
class Recording::CoveredFile
attr_reader :file

def initialize(full_filename, rcov_marked_info, recording)
@full_filename = full_filename
@rcov_marked_info = rcov_marked_info
@recording = recording
@file = File.expand_path(full_filename).gsub(/^#{Dir.pwd}\//, '')

extend HasLineNumberDetail if @rcov_marked_info
end

def dirty?
Cucover.logger.debug("#{file} last modified at #{File.mtime(@full_filename)}")
Cucover.logger.debug("#{file} recording started at #{@recording.start_time}")
result = File.mtime(@full_filename).to_i >= @recording.start_time.to_i
Cucover.logger.debug("verdict: #{(result ? "dirty" : "not dirty")}")
result
end

def covers_line?(line_number)
covered_lines.include?(line_number)
end

def ==(other)
other == file
end

def to_s
"#{file}:#{covered_lines.join(':')}"
end

private

def covered_lines
['<unknown lines>']
end

module HasLineNumberDetail
def covered_lines
return @covered_lines if @covered_lines

@covered_lines = []
@rcov_marked_info.each_with_index do |covered, index|
line_number = index + 1
line_number = index + Cucover::LineNumbers.offset
@covered_lines << line_number if covered
end
@covered_lines
end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/dependencies.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require 'rubygems'

gem 'logging', '>= 1.1.4'
gem 'logging', '>= 1.4.1'
require 'logging'

gem 'cucumber', '>= 0.3.1'
require 'cucumber'

gem 'relevance-rcov','>= 0.8.3.4'
gem 'rcov', '>= 0.9.8'
require 'rcov'

0 comments on commit 9b33a30

Please sign in to comment.