Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use the correct class for shared namespaces #754

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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

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