Skip to content

Commit

Permalink
Fixed encoding issues (closes #71)
Browse files Browse the repository at this point in the history
The root cause was a weird char encoding in a authorship comment in
Rubygems' rubygems/package/tar_input.rb

Refactored the processing of skipped lines in simplecov to be more in line
with the other facilities in the library, specifically to use SimpleCov::SourceFile::Line
instead of raw source arrays.

Due to this, the processing of skipped lines in results only happens on the filtered
resultset, which won't include any gem/library files. This should also make result generation
faster.
  • Loading branch information
colszowka committed Sep 13, 2011
1 parent cea53d1 commit 2872cd2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 46 deletions.
31 changes: 19 additions & 12 deletions lib/simplecov/source_file.rb
Expand Up @@ -24,11 +24,12 @@ class Line
alias_method :line, :line_number
alias_method :number, :line_number

def initialize(src, line_number, coverage, skipped = false)
def initialize(src, line_number, coverage)
raise ArgumentError, "Only String accepted for source" unless src.kind_of?(String)
raise ArgumentError, "Only Fixnum accepted for line_number" unless line_number.kind_of?(Fixnum)
raise ArgumentError, "Only Fixnum and nil accepted for coverage" unless coverage.kind_of?(Fixnum) or coverage.nil?
@src, @line_number, @coverage, @skipped = src, line_number, coverage, skipped
@src, @line_number, @coverage = src, line_number, coverage
@skipped = false
end

# Returns true if this is a line that should have been covered, but was not
Expand All @@ -45,9 +46,16 @@ def covered?
def never?
not skipped? and coverage.nil?
end

# Flags this line as skipped
def skipped!
@skipped = true
end

# Returns true if this line was skipped, false otherwise. Lines are skipped if they are wrapped with
# # :nocov: comment lines.
def skipped?
@skipped
!!skipped
end
end

Expand All @@ -61,7 +69,6 @@ def skipped?

def initialize(filename, coverage)
@filename, @coverage, @src = filename, coverage, File.readlines(filename)
@skipped_line_numbers = process_skipped_lines
end

# Returns all source lines for this file as instances of SimpleCov::SourceFile::Line,
Expand All @@ -77,8 +84,9 @@ def lines
# Initialize lines
@lines = []
src.each_with_index do |src, i|
@lines << SimpleCov::SourceFile::Line.new(src, i+1, coverage[i], @skipped_line_numbers.include?(i + 1))
@lines << SimpleCov::SourceFile::Line.new(src, i+1, coverage[i])
end
process_skipped_lines!
@lines
end
alias_method :source_lines, :lines
Expand All @@ -94,7 +102,6 @@ def covered_percent
(covered_lines.count) * 100 / (lines.count - never_lines.count - skipped_lines.count).to_f
end

#
def covered_strength
return 0 if lines.length == 0 or lines.length == never_lines.count
lines_strength = 0
Expand Down Expand Up @@ -133,17 +140,17 @@ def lines_of_code
covered_lines.count + missed_lines.count
end

def process_skipped_lines
skipped_line_numbers = []
# Will go through all source files and mark lines that are wrapped within # :nocov: comment blocks
# as skipped.
def process_skipped_lines!
skipping = false
@src.each_with_index do |line, i|
if line =~ /^\s*#\:nocov\:/
lines.each do |line|
if line.src =~ /^\s*#\:nocov\:/
skipping = !skipping
else
skipped_line_numbers << i + 1 if skipping
line.skipped! if skipping
end
end
skipped_line_numbers
end

private
Expand Down
1 change: 1 addition & 0 deletions test/helper.rb
Expand Up @@ -13,6 +13,7 @@ def source_fixture(filename)
# Keep 1.8-rubies from complaining about missing tests in each file that covers only 1.9 functionality
def default_test
end

end

require 'shoulda_macros'
Expand Down
12 changes: 12 additions & 0 deletions test/shoulda_macros.rb
Expand Up @@ -8,4 +8,16 @@ def on_ruby(*ruby_versions)
yield
end if ruby_versions.any? {|v| RUBY_VERSION =~ /#{v}/ }
end

def should_be(boolean_flag)
should "be #{boolean_flag}" do
assert_equal true, subject.send(boolean_flag)
end
end

def should_not_be(boolean_flag)
should "not be #{boolean_flag}" do
assert_equal false, subject.send(boolean_flag)
end
end
end
62 changes: 28 additions & 34 deletions test/test_source_file_line.rb
@@ -1,11 +1,14 @@
require 'helper'

class TestSourceFileLine < Test::Unit::TestCase


on_ruby '1.9' do
context "A source line" do
setup do
@line = SimpleCov::SourceFile::Line.new('# the ruby source', 5, 3)
end
subject { @line }

should 'return "# the ruby source" as src' do
assert_equal '# the ruby source', @line.src
Expand All @@ -23,72 +26,63 @@ class TestSourceFileLine < Test::Unit::TestCase
assert_equal @line.line_number, @line.line
assert_equal @line.line_number, @line.number
end

context "flagged as skipped!" do
setup { @line.skipped! }

should_not_be :covered?
should_be :skipped?
should_not_be :missed?
should_not_be :never?
end
end

context "A source line with coverage" do
setup do
@line = SimpleCov::SourceFile::Line.new('# the ruby source', 5, 3)
end
subject { @line }

should "have coverage of 3" do
assert_equal 3, @line.coverage
end

should "be covered?" do
assert @line.covered?
end

should "not be never?" do
assert !@line.never?
end

should "not be missed?" do
assert !@line.missed?
end
should_be :covered?
should_not_be :skipped?
should_not_be :missed?
should_not_be :never?
end

context "A source line without coverage" do
setup do
@line = SimpleCov::SourceFile::Line.new('# the ruby source', 5, 0)
end
subject { @line }

should "have coverage of 0" do
assert_equal 0, @line.coverage
end

should "not be covered?" do
assert !@line.covered?
end

should "not be never?" do
assert !@line.never?
end

should "be missed?" do
assert @line.missed?
end
should_not_be :covered?
should_not_be :skipped?
should_be :missed?
should_not_be :never?
end

context "A source line with no code" do
setup do
@line = SimpleCov::SourceFile::Line.new('# the ruby source', 5, nil)
end
subject { @line }

should "have nil coverage" do
assert_nil @line.coverage
end

should "not be covered?" do
assert !@line.covered?
end

should "be never?" do
assert @line.never?
end

should "not be missed?" do
assert !@line.missed?
end
should_not_be :covered?
should_not_be :skipped?
should_not_be :missed?
should_be :never?
end

should "raise ArgumentError when initialized with invalid src" do
Expand Down

0 comments on commit 2872cd2

Please sign in to comment.