Permalink
Browse files

Merge pull request #151 from cldwalker/undefined_task

Two fixes for undefined tasks when no thorfiles are in project
  • Loading branch information...
2 parents 60864c6 + a62fb61 commit 9fac82b38b71d8245f9db5375a922cf7a3e99779 Brian Donovan committed Jul 7, 2011
Showing with 31 additions and 2 deletions.
  1. +2 −2 lib/thor/base.rb
  2. +2 −0 lib/thor/runner.rb
  3. +27 −0 spec/runner_spec.rb
View
@@ -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}."
View
@@ -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
@@ -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
View
@@ -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/
@@ -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
@@ -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)

0 comments on commit 9fac82b

Please sign in to comment.