diff --git a/lib/thor/util.rb b/lib/thor/util.rb index 110bd9e6..de582366 100644 --- a/lib/thor/util.rb +++ b/lib/thor/util.rb @@ -130,9 +130,10 @@ def camel_case(str) # def find_class_and_command_by_namespace(namespace, fallback = true) if namespace.include?(":") # look for a namespaced command - pieces = namespace.split(":") - command = pieces.pop - klass = Thor::Util.find_by_namespace(pieces.join(":")) + *pieces, command = namespace.split(":") + namespace = pieces.join(":") + namespace = "default" if namespace.empty? + klass = Thor::Base.subclasses.detect { |klass| klass.namespace == namespace && klass.commands.keys.include?(command) } end unless klass # look for a Thor::Group with the right name klass = Thor::Util.find_by_namespace(namespace) diff --git a/spec/base_spec.rb b/spec/base_spec.rb index e5a9561c..2bac020d 100644 --- a/spec/base_spec.rb +++ b/spec/base_spec.rb @@ -195,7 +195,7 @@ def hello expect(Thor::Base.subclass_files[File.expand_path(thorfile)]).to eq([ MyScript, MyScript::AnotherScript, MyChildScript, Barn, PackageNameScript, Scripts::MyScript, Scripts::MyDefaults, - Scripts::ChildDefault, Scripts::Arities + Scripts::ChildDefault, Scripts::Arities, Apple, Pear ]) end diff --git a/spec/fixtures/script.thor b/spec/fixtures/script.thor index f4bca449..eac5d85e 100644 --- a/spec/fixtures/script.thor +++ b/spec/fixtures/script.thor @@ -249,3 +249,12 @@ module Scripts end end +class Apple < Thor + namespace :fruits + desc 'apple', 'apple'; def apple; end +end + +class Pear < Thor + namespace :fruits + desc 'pear', 'pear'; def pear; end +end diff --git a/spec/util_spec.rb b/spec/util_spec.rb index 9914706c..80665edc 100644 --- a/spec/util_spec.rb +++ b/spec/util_spec.rb @@ -109,6 +109,11 @@ def self.clear_user_home! it "falls back on the default namespace class if nothing else matches" do expect(Thor::Util.find_class_and_command_by_namespace("test")).to eq([Scripts::MyDefaults, "test"]) end + + it "returns correct Thor class and the command name when shared namespaces" do + expect(Thor::Util.find_class_and_command_by_namespace("fruits:apple")).to eq([Apple, "apple"]) + expect(Thor::Util.find_class_and_command_by_namespace("fruits:pear")).to eq([Pear, "pear"]) + end end describe "#thor_classes_in" do