Skip to content

Commit 9f41cfb

Browse files
committed
Merge PR #754
2 parents a411056 + a601ca9 commit 9f41cfb

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

lib/thor/util.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,10 @@ def camel_case(str)
130130
#
131131
def find_class_and_command_by_namespace(namespace, fallback = true)
132132
if namespace.include?(":") # look for a namespaced command
133-
pieces = namespace.split(":")
134-
command = pieces.pop
135-
klass = Thor::Util.find_by_namespace(pieces.join(":"))
133+
*pieces, command = namespace.split(":")
134+
namespace = pieces.join(":")
135+
namespace = "default" if namespace.empty?
136+
klass = Thor::Base.subclasses.detect { |klass| klass.namespace == namespace && klass.commands.keys.include?(command) }
136137
end
137138
unless klass # look for a Thor::Group with the right name
138139
klass = Thor::Util.find_by_namespace(namespace)

spec/base_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def hello
196196
expect(Thor::Base.subclass_files[File.expand_path(thorfile)]).to eq([
197197
MyScript, MyScript::AnotherScript, MyChildScript, Barn,
198198
PackageNameScript, Scripts::MyScript, Scripts::MyDefaults,
199-
Scripts::ChildDefault, Scripts::Arities
199+
Scripts::ChildDefault, Scripts::Arities, Apple, Pear
200200
])
201201
end
202202

spec/fixtures/script.thor

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,12 @@ module Scripts
249249
end
250250
end
251251

252+
class Apple < Thor
253+
namespace :fruits
254+
desc 'apple', 'apple'; def apple; end
255+
end
256+
257+
class Pear < Thor
258+
namespace :fruits
259+
desc 'pear', 'pear'; def pear; end
260+
end

spec/util_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ def self.clear_user_home!
109109
it "falls back on the default namespace class if nothing else matches" do
110110
expect(Thor::Util.find_class_and_command_by_namespace("test")).to eq([Scripts::MyDefaults, "test"])
111111
end
112+
113+
it "returns correct Thor class and the command name when shared namespaces" do
114+
expect(Thor::Util.find_class_and_command_by_namespace("fruits:apple")).to eq([Apple, "apple"])
115+
expect(Thor::Util.find_class_and_command_by_namespace("fruits:pear")).to eq([Pear, "pear"])
116+
end
112117
end
113118

114119
describe "#thor_classes_in" do

0 commit comments

Comments
 (0)