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

Add invoke convenience method to Command #41

Merged
merged 6 commits into from Nov 12, 2014
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,10 @@

###### Enhancements

* Added convenience method to invoke commands more easily.
[Olivier Halligon](https://github.com/AliSoftware)
[#33](https://github.com/CocoaPods/CLAide/issues/40)

* Improved messages for exceptions generated by plugins.
[Fabio Pelosin](https://github.com/fabiopelosin)
[#28](https://github.com/CocoaPods/CLAide/pull/28)
Expand Down
18 changes: 18 additions & 0 deletions lib/claide/command.rb
Expand Up @@ -420,6 +420,24 @@ def initialize(argv)
@argv = argv
end

# Convenience method.
# Instantiate the command and run it with the provided arguments at once.
#
# @note This method validate! the command before running it, but contrary to
# CLAide::Command::run, it does not load plugins nor exit on failure.
# It is up to the caller to rescue any possible exception raised.
#
# @param [String..., Array<String>] args
# The arguments to initialize the command with
#
# @raise [Help] If validate! fails
#
def self.invoke(*args)
command = new(ARGV.new(args.flatten))
command.validate!
command.run
end

# @return [Bool] Whether the command was invoked by an abstract command by
# default.
#
Expand Down
31 changes: 31 additions & 0 deletions spec/command_spec.rb
Expand Up @@ -57,6 +57,37 @@ module CLAide
STDOUT.expects(:tty?).returns(false)
@subject.ansi_output.should.be.false
end

it 'invokes a command with the convenience method and args list' do
@subject::SpecFile.any_instance.expects(:validate!)
@subject::SpecFile.any_instance.expects(:run).once

@subject::SpecFile.invoke('arg1', 'arg2')

argv = @subject::SpecFile.latest_instance.instance_eval { @argv }
argv.should.be.an.instance_of? CLAide::ARGV
argv.arguments.should == %w(arg1 arg2)
end

it 'invokes a command with the convenience method and array args' do
@subject::SpecFile.any_instance.expects(:validate!)
@subject::SpecFile.any_instance.expects(:run).once

@subject::SpecFile.invoke %w(arg1 arg2)

argv = @subject::SpecFile.latest_instance.instance_eval { @argv }
argv.should.be.an.instance_of? CLAide::ARGV
argv.arguments.should == %w(arg1 arg2)
end

it 'raise when invoking a bad command with the convenience method' do
error = Fixture::Error.new('validate! did raise')
@subject::SpecFile.any_instance.stubs(:validate!).raises(error)

should.raise Fixture::Error do
@subject::SpecFile.invoke('arg1', 'arg2')
end.message.should.match /validate! did raise/
end
end

#-------------------------------------------------------------------------#
Expand Down
9 changes: 9 additions & 0 deletions spec/spec_helper/fixtures.rb
Expand Up @@ -9,6 +9,15 @@ class Command < CLAide::Command
self.command = 'bin'
self.ansi_output = false

class << self
attr_accessor :latest_instance
end

def initialize(*args)
self.class.latest_instance = self
super
end

class SpecFile < Command
self.abstract_command = true
self.description = 'Manage spec files.'
Expand Down