Skip to content

Commit

Permalink
Added version checks to unit tests so they only get executed on
Browse files Browse the repository at this point in the history
applicable ruby versions.

Also added Rake Task 'multitest', which will run rake test
on 1.9.2, 1.8.7 and 1.8.6 via rvm.

Use `rake multitest:bundle` to bundle install on all multitest
rubies.

Also added a fix that made test suites fail on 1.8.6 in a mysterious
fashion. See issue #18
  • Loading branch information
colszowka committed Jan 6, 2011
1 parent 78da00c commit a7b582a
Show file tree
Hide file tree
Showing 12 changed files with 430 additions and 377 deletions.
13 changes: 13 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,16 @@ Rake::RDocTask.new do |rdoc|
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
end

TestedRubyVersions = %w(1.9.2 1.8.7-p302 1.8.6)

desc "Perorms bundle install on all tested ruby versions (#{TestedRubyVersions.join(', ')})"
task :"multitest:bundle" do
system "rvm #{TestedRubyVersions.join(',')} exec bundle install"
end

desc "Runs tests using rvm for: #{TestedRubyVersions.join(', ')}"
task :multitest do
system "rvm #{TestedRubyVersions.join(',')} rake test"
end

17 changes: 13 additions & 4 deletions lib/simplecov.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ class << self
# Please check out the RDoc for SimpleCov::Configuration to find about available config options
#
def start(adapter=nil, &block)
unless "1.9".respond_to?(:encoding)
warn "WARNING: SimpleCov is activated, but you're not running Ruby 1.9 - no coverage analysis will happen"
return false
end
return false unless SimpleCov.usable?

require 'coverage'
load_adapter(adapter) unless adapter.nil?
Coverage.start
Expand Down Expand Up @@ -90,6 +88,17 @@ def load_adapter(name)
adapters.load(name)
end

#
# Checks whether we're on a proper version of ruby (1.9+) and returns false if this is not the case,
# also printing an appropriate warning
#
def usable?
unless "1.9".respond_to?(:encoding)
warn "WARNING: SimpleCov is activated, but you're not running Ruby 1.9+ - no coverage analysis will happen"
return false
end
true
end
end
end

Expand Down
1 change: 1 addition & 0 deletions lib/simplecov/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def adapters
# options at once.
#
def configure(&block)
return false unless SimpleCov.usable?
instance_exec(&block)
end

Expand Down
9 changes: 8 additions & 1 deletion test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ class Test::Unit::TestCase
def source_fixture(filename)
File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', filename))
end
end

# 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'
Test::Unit::TestCase.send :extend, ShouldaMacros
9 changes: 9 additions & 0 deletions test/shoulda_macros.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module ShouldaMacros
#
# Simple block helper for running certain tests only on specific ruby versions.
# The given strings will be regexp-matched against RUBY_VERSION
#
def on_ruby(*ruby_versions)
yield if ruby_versions.any? {|v| RUBY_VERSION =~ /#{v}/ }
end
end
24 changes: 13 additions & 11 deletions test/test_command_guesser.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
require 'helper'

class TestCommandGuesser < Test::Unit::TestCase
def self.should_guess_command_name(expectation, *argv)
argv.each do |args|
should "return '#{expectation}' for '#{args}'" do
assert_equal expectation, SimpleCov::CommandGuesser.guess(args)
on_ruby '1.9' do
def self.should_guess_command_name(expectation, *argv)
argv.each do |args|
should "return '#{expectation}' for '#{args}'" do
assert_equal expectation, SimpleCov::CommandGuesser.guess(args)
end
end
end
end

should_guess_command_name "Unit Tests", '/some/path/test/units/foo_bar_test.rb', 'test/units/foo.rb', 'test/foo.rb'
should_guess_command_name "Functional Tests", '/some/path/test/functional/foo_bar_controller_test.rb'
should_guess_command_name "Integration Tests", '/some/path/test/integration/foo_bar_controller_test.rb'
should_guess_command_name "Cucumber Features", 'features', 'cucumber', 'cucumber features'
should_guess_command_name "RSpec", '/some/path/spec/foo.rb'
should_guess_command_name "some_arbitrary_command with arguments", 'some_arbitrary_command with arguments'
should_guess_command_name "Unit Tests", '/some/path/test/units/foo_bar_test.rb', 'test/units/foo.rb', 'test/foo.rb'
should_guess_command_name "Functional Tests", '/some/path/test/functional/foo_bar_controller_test.rb'
should_guess_command_name "Integration Tests", '/some/path/test/integration/foo_bar_controller_test.rb'
should_guess_command_name "Cucumber Features", 'features', 'cucumber', 'cucumber features'
should_guess_command_name "RSpec", '/some/path/spec/foo.rb'
should_guess_command_name "some_arbitrary_command with arguments", 'some_arbitrary_command with arguments'
end
end
104 changes: 53 additions & 51 deletions test/test_filters.rb
Original file line number Diff line number Diff line change
@@ -1,73 +1,75 @@
require 'helper'

class TestFilters < Test::Unit::TestCase
context "A source file initialized with some coverage data" do
setup do
@source_file = SimpleCov::SourceFile.new(source_fixture('sample.rb'), [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil])
end
on_ruby '1.9' do
context "A source file initialized with some coverage data" do
setup do
@source_file = SimpleCov::SourceFile.new(source_fixture('sample.rb'), [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil])
end

should "pass a new SimpleCov::StringFilter 'foobar'" do
assert SimpleCov::StringFilter.new('foobar').passes?(@source_file)
end
should "pass a new SimpleCov::StringFilter 'foobar'" do
assert SimpleCov::StringFilter.new('foobar').passes?(@source_file)
end

should "pass a new SimpleCov::StringFilter 'some/path'" do
assert SimpleCov::StringFilter.new('some/path').passes?(@source_file)
end
should "pass a new SimpleCov::StringFilter 'some/path'" do
assert SimpleCov::StringFilter.new('some/path').passes?(@source_file)
end

should "not pass a new SimpleCov::StringFilter 'test/fixtures'" do
assert !SimpleCov::StringFilter.new('test/fixtures').passes?(@source_file)
end
should "not pass a new SimpleCov::StringFilter 'test/fixtures'" do
assert !SimpleCov::StringFilter.new('test/fixtures').passes?(@source_file)
end

should "not pass a new SimpleCov::StringFilter 'test/fixtures/sample.rb'" do
assert !SimpleCov::StringFilter.new('test/fixtures/sample.rb').passes?(@source_file)
end
should "not pass a new SimpleCov::StringFilter 'test/fixtures/sample.rb'" do
assert !SimpleCov::StringFilter.new('test/fixtures/sample.rb').passes?(@source_file)
end

should "not pass a new SimpleCov::StringFilter 'sample.rb'" do
assert !SimpleCov::StringFilter.new('sample.rb').passes?(@source_file)
end
should "not pass a new SimpleCov::StringFilter 'sample.rb'" do
assert !SimpleCov::StringFilter.new('sample.rb').passes?(@source_file)
end

should "pass a new SimpleCov::BlockFilter that is not applicable" do
assert SimpleCov::BlockFilter.new(Proc.new {|s| File.basename(s.filename) == 'foo.rb'}).passes?(@source_file)
end
should "pass a new SimpleCov::BlockFilter that is not applicable" do
assert SimpleCov::BlockFilter.new(Proc.new {|s| File.basename(s.filename) == 'foo.rb'}).passes?(@source_file)
end

should "not pass a new SimpleCov::BlockFilter that is applicable" do
assert !SimpleCov::BlockFilter.new(Proc.new {|s| File.basename(s.filename) == 'sample.rb'}).passes?(@source_file)
should "not pass a new SimpleCov::BlockFilter that is applicable" do
assert !SimpleCov::BlockFilter.new(Proc.new {|s| File.basename(s.filename) == 'sample.rb'}).passes?(@source_file)
end
end
end

context "with no filters set up and a basic source file in an array" do
setup do
SimpleCov.filters = []
@files = [SimpleCov::SourceFile.new(source_fixture('sample.rb'), [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil])]
end
context "with no filters set up and a basic source file in an array" do
setup do
SimpleCov.filters = []
@files = [SimpleCov::SourceFile.new(source_fixture('sample.rb'), [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil])]
end

should "return 0 items after executing SimpleCov.filtered on files when using a 'sample' string filter" do
SimpleCov.add_filter "sample"
assert_equal 0, SimpleCov.filtered(@files).count
end
should "return 0 items after executing SimpleCov.filtered on files when using a 'sample' string filter" do
SimpleCov.add_filter "sample"
assert_equal 0, SimpleCov.filtered(@files).count
end

should "return 0 items after executing SimpleCov.filtered on files when using a 'test/fixtures/' string filter" do
SimpleCov.add_filter "test/fixtures"
assert_equal 0, SimpleCov.filtered(@files).count
end
should "return 0 items after executing SimpleCov.filtered on files when using a 'test/fixtures/' string filter" do
SimpleCov.add_filter "test/fixtures"
assert_equal 0, SimpleCov.filtered(@files).count
end

should "return 1 item after executing SimpleCov.filtered on files when using a 'fooo' string filter" do
SimpleCov.add_filter "fooo"
assert_equal 1, SimpleCov.filtered(@files).count
end
should "return 1 item after executing SimpleCov.filtered on files when using a 'fooo' string filter" do
SimpleCov.add_filter "fooo"
assert_equal 1, SimpleCov.filtered(@files).count
end

should "return 0 items after executing SimpleCov.filtered on files when using a block filter that returns true" do
SimpleCov.add_filter do |src_file|
true
should "return 0 items after executing SimpleCov.filtered on files when using a block filter that returns true" do
SimpleCov.add_filter do |src_file|
true
end
assert_equal 0, SimpleCov.filtered(@files).count
end
assert_equal 0, SimpleCov.filtered(@files).count
end

should "return 1 item after executing SimpleCov.filtered on files when using an always-false block filter" do
SimpleCov.add_filter do |src_file|
false
should "return 1 item after executing SimpleCov.filtered on files when using an always-false block filter" do
SimpleCov.add_filter do |src_file|
false
end
assert_equal 1, SimpleCov.filtered(@files).count
end
assert_equal 1, SimpleCov.filtered(@files).count
end
end
end
Loading

0 comments on commit a7b582a

Please sign in to comment.