Skip to content

Commit

Permalink
Prevent libraries that tamper with ARGV (I'm looking at you, rake)
Browse files Browse the repository at this point in the history
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
colszowka committed Feb 22, 2012
1 parent a83260d commit 23a768d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 24 deletions.
61 changes: 41 additions & 20 deletions lib/simplecov/command_guesser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/simplecov/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand Down
4 changes: 4 additions & 0 deletions lib/simplecov/defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/deleted_source_sample.rb
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
3 changes: 2 additions & 1 deletion test/test_command_guesser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 23a768d

Please sign in to comment.