Skip to content

Commit

Permalink
Merge pull request #151 from cldwalker/undefined_task
Browse files Browse the repository at this point in the history
Two fixes for undefined tasks when no thorfiles are in project
  • Loading branch information
Brian Donovan committed Jul 7, 2011
2 parents 60864c6 + a62fb61 commit 9fac82b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/thor/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,8 @@ def public_task(*names)
end
end

def handle_no_task_error(task) #:nodoc:
if $thor_runner
def handle_no_task_error(task, has_namespace = $thor_runner) #:nodoc:
if has_namespace
raise UndefinedTaskError, "Could not find task #{task.inspect} in #{namespace.inspect} namespace."
else
raise UndefinedTaskError, "Could not find task #{task.inspect}."
Expand Down
2 changes: 2 additions & 0 deletions lib/thor/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def help(meth = nil)
if meth && !self.respond_to?(meth)
initialize_thorfiles(meth)
klass, task = Thor::Util.find_class_and_task_by_namespace(meth)
self.class.handle_no_task_error(task, false) if klass.nil?
klass.start(["-h", task].compact, :shell => self.shell)
else
super
Expand All @@ -30,6 +31,7 @@ def method_missing(meth, *args)
meth = meth.to_s
initialize_thorfiles(meth)
klass, task = Thor::Util.find_class_and_task_by_namespace(meth)
self.class.handle_no_task_error(task, false) if klass.nil?
args.unshift(task) if task
klass.start(args, :shell => self.shell)
end
Expand Down
27 changes: 27 additions & 0 deletions spec/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
require 'thor/runner'

describe Thor::Runner do
def when_no_thorfiles_exist
old_dir = Dir.pwd
Dir.chdir '..'
delete = Thor::Base.subclasses.select {|e| e.namespace == 'default' }
delete.each {|e| Thor::Base.subclasses.delete e }
yield
Thor::Base.subclasses.concat delete
Dir.chdir old_dir
end

describe "#help" do
it "shows information about Thor::Runner itself" do
capture(:stdout){ Thor::Runner.start(["help"]) }.should =~ /List the available thor tasks/
Expand Down Expand Up @@ -33,6 +43,14 @@
content = capture(:stderr){ Thor::Runner.start(["help", "unknown"]) }
content.strip.should == 'Could not find task "unknown" in "default" namespace.'
end

it "raises error if a class/task cannot be found for a setup without thorfiles" do
when_no_thorfiles_exist do
Thor::Runner.should_receive :exit
content = capture(:stderr){ Thor::Runner.start(["help", "unknown"]) }
content.strip.should == 'Could not find task "unknown".'
end
end
end

describe "#start" do
Expand Down Expand Up @@ -72,6 +90,15 @@
content.strip.should == 'Could not find task "unknown" in "default" namespace.'
end

it "raises an error if class/task can't be found in a setup without thorfiles" do
when_no_thorfiles_exist do
ARGV.replace ["unknown"]
Thor::Runner.should_receive :exit
content = capture(:stderr){ Thor::Runner.start }
content.strip.should == 'Could not find task "unknown".'
end
end

it "does not swallow NoMethodErrors that occur inside the called method" do
ARGV.replace ["my_script:call_unexistent_method"]
lambda { Thor::Runner.start }.should raise_error(NoMethodError)
Expand Down

0 comments on commit 9fac82b

Please sign in to comment.