Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

More specs running on 1.9

  • Loading branch information...
commit 983643e57af815534f204850a51a374e9e0a9137 1 parent 01fbc02
@josevalim josevalim authored
View
13 lib/thor/task.rb
@@ -1,8 +1,8 @@
class Thor
class Task < Struct.new(:name, :description, :usage, :options)
+ FILE_REGEXP = /^#{Regexp.escape(File.expand_path(__FILE__))}:[\w:]+ `run'$/
# A dynamic task that handles method missing scenarios.
- #
class Dynamic < Task
def initialize(name)
super(name.to_s, "A dynamically-generated task", name.to_s)
@@ -27,7 +27,6 @@ def initialize_copy(other) #:nodoc:
# By default, a task invokes a method in the thor class. You can change this
# implementation to create custom tasks.
- #
def run(instance, args=[])
raise UndefinedTaskError, "the '#{name}' task of #{instance.class} is private" unless public_method?(instance)
instance.send(name, *args)
@@ -78,16 +77,20 @@ def required_options
end
# Given a target, checks if this class name is not a private/protected method.
- #
def public_method?(instance) #:nodoc:
collection = instance.private_methods + instance.protected_methods
(collection & [name.to_s, name.to_sym]).empty?
end
- def parse_argument_error(instance, e, caller) #:nodoc:
+ # For Ruby <= 1.8.7, we have to match the method name that we are trying to call.
+ # In Ruby >= 1.9.1, we have to match the method run in this file.
+ def backtrace_match?(backtrace) #:nodoc:
method_name = /`#{Regexp.escape(name.split(':').last)}'/
+ backtrace =~ method_name || backtrace =~ FILE_REGEXP
+ end
- if e.message =~ /wrong number of arguments/ && e.backtrace.first.to_s =~ method_name
+ def parse_argument_error(instance, e, caller) #:nodoc:
+ if e.message =~ /wrong number of arguments/ && backtrace_match?(e.backtrace.first.to_s)
if instance.is_a?(Thor::Group)
raise e, "'#{name}' was called incorrectly. Are you sure it has arity equals to 0?"
else
View
3  lib/thor/util.rb
@@ -76,8 +76,9 @@ def self.namespaces_in_content(contents, file=__FILE__)
# Returns the thor classes declared inside the given class.
#
def self.thor_classes_in(klass)
+ stringfied_constants = klass.constants.map { |c| c.to_s }
Thor::Base.subclasses.select do |subclass|
- klass.constants.include?(subclass.name.gsub("#{klass.name}::", ''))
+ stringfied_constants.include?(subclass.name.gsub("#{klass.name}::", ''))
end
end
View
4 spec/runner_spec.rb
@@ -30,7 +30,7 @@
end
it "raises error if a class/task cannot be found" do
- mock(Thor::Runner).exit(1)
+ mock(Thor::Runner).exit(1){ }
content = capture(:stderr){ Thor::Runner.start(["help", "unknown"]) }
content.must =~ /could not find Thor class or task 'unknown'/
end
@@ -68,7 +68,7 @@
end
it "raises an error if class/task can't be found" do
- mock(Thor::Runner).exit(1)
+ mock(Thor::Runner).exit(1){ }
ARGV.replace ["unknown"]
capture(:stderr){ Thor::Runner.start }.must =~ /could not find Thor class or task 'unknown'/
end
View
2  spec/spec_helper.rb
@@ -48,5 +48,5 @@ def destination_root
File.join(File.dirname(__FILE__), 'sandbox')
end
- alias silence capture
+ alias :silence :capture
end
View
9 spec/thor_spec.rb
@@ -156,9 +156,9 @@ def shell
end
it "uses the maximum terminal size to show tasks" do
- mock(@shell).terminal_width { 80 }
- @content = capture(:stdout){ MyScript.help(shell) }
- @content.must =~ /aaa\.\.\.$/
+ stub(@shell).terminal_width { 80 }
+ content = capture(:stdout){ MyScript.help(shell) }
+ content.must =~ /aaa\.\.\.$/
end
it "provides description for tasks from classes in the same namespace" do
@@ -212,8 +212,7 @@ def shell
describe "instance method" do
it "calls the class method" do
- stub(MyScript).help(mock.instance_of(Thor::Base.shell), :task => nil)
- MyScript.start(["help"])
+ capture(:stdout){ MyScript.start(["help"]) }.must =~ /Tasks:/
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.