Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Prevent libraries that tamper with ARGV (I'm looking at you, rake)

from disabling SimpleCov's ability to guess test suite / framework names
based upon the command line call that invoked the tests.

Fixes #110 and probably also #45

Cleaned up and refactored the command guessing along the way.
  • Loading branch information...
commit 23a768d10e61e7045e3bde0c6c29b029682891e2 1 parent a83260d
@colszowka authored
View
61 lib/simplecov/command_guesser.rb
@@ -2,27 +2,48 @@
# Helper that tries to find out what test suite is running (for SimpleCov.command_name)
#
module SimpleCov::CommandGuesser
- def self.guess(command)
- case command
- when /#{'test/functional/'}/
- "Functional Tests"
- when /#{'test/integration/'}/
- "Integration Tests"
- when /#{'test/'}/
- "Unit Tests"
- when /cucumber/, /features/
- "Cucumber Features"
- when /spec/
+ class << self
+ # Storage for the original command line call that invoked the test suite.
+ # This has got to be stored as early as possible because i.e. rake and test/unit 2
+ # have a habit of tampering with ARGV, which makes i.e. the automatic distinction
+ # between rails unit/functional/integration tests impossible without this cached
+ # item.
+ attr_accessor :original_run_command
+
+ def guess
+ from_command_line_options || from_defined_constants
+ end
+
+ private
+
+ def from_command_line_options
+ case original_run_command
+ when /#{'test/functional/'}/
+ "Functional Tests"
+ when /#{'test/integration/'}/
+ "Integration Tests"
+ when /#{'test/'}/
+ "Unit Tests"
+ when /cucumber/, /features/
+ "Cucumber Features"
+ when /spec/
+ "RSpec"
+ else
+ nil
+ end
+ end
+
+ def from_defined_constants
+ # If the command regexps fail, let's try checking defined constants.
+ if defined?(RSpec)
"RSpec"
+ elsif defined?(Test::Unit)
+ "Unit Tests"
else
- # If the command regexps fail, let's try checking defined constants.
- if defined?(RSpec)
- return "RSpec"
- elsif defined?(Test::Unit)
- return "Unit Tests"
- else
- return command
- end
+ # TODO: Provide link to docs/wiki article
+ warn "SimpleCov failed to recognize the test framework and/or suite used. Please specify manually using SimpleCov.command_name 'Unit Tests'."
+ 'Unknown Test Framework'
+ end
end
end
-end
+end
View
4 lib/simplecov/configuration.rb
@@ -57,10 +57,10 @@ def filters
# also check out the corresponding section in README.rdoc
def command_name(name=nil)
@name = name unless name.nil?
- @name ||= SimpleCov::CommandGuesser.guess("#{$0} #{ARGV.join(" ")}")
+ @name ||= SimpleCov::CommandGuesser.guess
@name
end
-
+
#
# Gets or sets the configured formatter.
#
View
4 lib/simplecov/defaults.rb
@@ -36,6 +36,10 @@
# Exclude files outside of SimpleCov.root
load_adapter 'root_filter'
end
+
+# Gotta stash this a-s-a-p, see the CommandGuesser class and i.e. #110 for further info
+SimpleCov::CommandGuesser.original_run_command = "#{$0} #{ARGV.join(" ")}"
+
at_exit do
# Store the exit status of the test run since it goes away after calling the at_exit proc...
if $! #was an exception thrown?
View
2  test/fixtures/deleted_source_sample.rb
@@ -1,6 +1,6 @@
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..'))
require 'lib/simplecov'
-SimpleCov.start
+SimpleCov.start { command_name "Test" }
dir = File.expand_path(File.dirname(__FILE__))
file = File.join(dir, "generated_buddha.rb")
View
3  test/test_command_guesser.rb
@@ -5,7 +5,8 @@ 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)
+ SimpleCov::CommandGuesser.original_run_command = args
+ assert_equal expectation, SimpleCov::CommandGuesser.guess
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.