Skip to content

Commit

Permalink
Fix bug if default task is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
rleber committed Jul 2, 2011
1 parent 1af071c commit 14cd8b5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/thor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,13 @@ def dispatch(meth, given_args, given_opts, config) #:nodoc:
elsif @default_task && @default_task_allows_args
meth = @default_task
task = all_tasks[normalize_task_name(meth)]
given_args = original_args # Restore original arguments (meth has been popped above)
args, opts = Thor::Options.split(given_args)
if task
given_args = original_args # Restore original arguments (meth has been popped above)
args, opts = Thor::Options.split(given_args)
else # Default task not found
args, opts = given_args, nil
task = Thor::DynamicTask.new(meth)
end
else
args, opts = given_args, nil
task = Thor::DynamicTask.new(meth)
Expand Down
6 changes: 5 additions & 1 deletion lib/thor/task.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'pp'

class Thor
class Task < Struct.new(:name, :description, :long_description, :usage, :options)
FILE_REGEXP = /^#{Regexp.escape(File.dirname(__FILE__))}/
Expand Down Expand Up @@ -106,7 +108,9 @@ def initialize(name, options=nil)
end

def run(instance, args=[])
if (instance.methods & [name.to_s, name.to_sym]).empty?
if name == '' # Missing task -- probably misnamed default task
instance.class.handle_no_task_error(instance.class.default_task)
elsif (instance.methods & [name.to_s, name.to_sym]).empty?
super
else
instance.class.handle_no_task_error(name)
Expand Down
18 changes: 18 additions & 0 deletions spec/fixtures/script.thor
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,23 @@ class WithArgsOnDefaultTask < Thor
def example_default_task(*args)
["default task", options, args]
end

class WithBadDefaultTask < Thor
default_task :bad_default_task

banner "MyScript does really cool stuff"

map "-T" => :animal, ["-f", "--foo"] => :foo

desc "zoo", "zoo around"
def zoo
"zoo"
end

desc "example_default_task", "example!"
method_options :with => :string
def example_default_task(*args)
["default task", options, args]
end
end

6 changes: 6 additions & 0 deletions spec/thor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@
end
end

describe "#bad_default_task" do
it "calls method missing if no command is specified" do
BadDefaultTask.start([]).should == [nil, []]
end
end

describe "#map" do
it "calls the alias of a method if one is provided" do
MyScript.start(["-T", "fish"]).should == ["fish"]
Expand Down

0 comments on commit 14cd8b5

Please sign in to comment.