Skip to content

Loading…

Two fixes for undefined tasks when no thorfiles are in project #151

Merged
merged 2 commits into from

2 participants

@cldwalker

I came across these two bugs while in a rails project that had no thorfiles (i.e. no *.thor and Thorfile):

$ thor lst
/Users/bozo/.rvm/gems/ruby-1.9.2-p180@w/gems/thor-0.14.6/lib/thor/runner.rb:34:in `method_missing': undefined method `start' for nil:NilClass (NoMethodError)
    from /Users/bozo/.rvm/gems/ruby-1.9.2-p180@w/gems/thor-0.14.6/lib/thor/task.rb:22:in `run'
    from /Users/bozo/.rvm/gems/ruby-1.9.2-p180@w/gems/thor-0.14.6/lib/thor/task.rb:108:in `run'
    from /Users/bozo/.rvm/gems/ruby-1.9.2-p180@w/gems/thor-0.14.6/lib/thor/invocation.rb:118:in `invoke_task'
    from /Users/bozo/.rvm/gems/ruby-1.9.2-p180@w/gems/thor-0.14.6/lib/thor.rb:263:in `dispatch'
    from /Users/bozo/.rvm/gems/ruby-1.9.2-p180@w/gems/thor-0.14.6/lib/thor/base.rb:389:in `start'
    from /Users/bozo/.rvm/gems/ruby-1.9.2-p180@w/gems/thor-0.14.6/bin/thor:6:in `<top (required)>'
    from /Users/bozo/.rvm/gems/ruby-1.9.2-p180@w/bin/thor:19:in `load'
    from /Users/bozo/.rvm/gems/ruby-1.9.2-p180@w/bin/thor:19:in `<main>'

$ thor help instal
/Users/bozo/.rvm/gems/ruby-1.9.2-p180@w/gems/thor-0.14.6/lib/thor/runner.rb:20:in `help': undefined method `start' for nil:NilClass (NoMethodError)
    from /Users/bozo/.rvm/gems/ruby-1.9.2-p180@w/gems/thor-0.14.6/lib/thor/task.rb:22:in `run'
    from /Users/bozo/.rvm/gems/ruby-1.9.2-p180@w/gems/thor-0.14.6/lib/thor/invocation.rb:118:in `invoke_task'
    from /Users/bozo/.rvm/gems/ruby-1.9.2-p180@w/gems/thor-0.14.6/lib/thor.rb:263:in `dispatch'
    from /Users/bozo/.rvm/gems/ruby-1.9.2-p180@w/gems/thor-0.14.6/lib/thor/base.rb:389:in `start'
    from /Users/bozo/.rvm/gems/ruby-1.9.2-p180@w/gems/thor-0.14.6/bin/thor:6:in `<top (required)>'
    from /Users/bozo/.rvm/gems/ruby-1.9.2-p180@w/bin/thor:19:in `load'
    from /Users/bozo/.rvm/gems/ruby-1.9.2-p180@w/bin/thor:19:in `<main>'

I ended up handling the errors with Thor::Base.handle_no_task_error. Let me know if the fix needs adjusting

@eventualbuddha eventualbuddha merged commit 9fac82b into erikhuda:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
4 lib/thor/base.rb
@@ -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
2 lib/thor/runner.rb
@@ -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
27 spec/runner_spec.rb
@@ -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)
Something went wrong with that request. Please try again.