Skip to content

Commit

Permalink
Merge PR #754
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed May 11, 2023
2 parents a411056 + a601ca9 commit 9f41cfb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
7 changes: 4 additions & 3 deletions lib/thor/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion spec/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,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

Expand Down
9 changes: 9 additions & 0 deletions spec/fixtures/script.thor
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions spec/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9f41cfb

Please sign in to comment.