Skip to content
This repository has been archived by the owner on Apr 6, 2021. It is now read-only.

Commit

Permalink
Changed my mind, keeping Commander after figuring out how to fix specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
nathankleyn committed Feb 7, 2015
1 parent 00f0a90 commit 814081a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
19 changes: 18 additions & 1 deletion lib/shanty/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def setup_tasks
def setup_task(name, task)
command(name) do |c|
c.description = I18n.t(task[:desc], default: task[:desc])
c.syntax = task[:syntax]
c.syntax = syntax_for_command(name, task)
add_options_to_command(task, c)
add_action_to_command(name, task, c)
end
Expand All @@ -64,6 +64,7 @@ def add_action_to_command(name, task, command)
command.action do |_, options|
task = tasks[name]
options.default(Hash[defaults_for_options(task)])
enforce_required_options(task, options)
execute_task(name, task, options)
end
end
Expand All @@ -81,6 +82,14 @@ def defaults_for_options(task)
end
end

def syntax_for_command(name, task)
option_syntax = task[:options].map do |option_name, option|
syntax_for_option(option_name, option)
end

[name, *option_syntax].join(' ')
end

def syntax_for_option(name, option)
required = option[:required]
case option[:type]
Expand All @@ -99,5 +108,13 @@ def default_for_type(option)
option[:default]
end
end

def enforce_required_options(task, options)
missing = task[:options].keep_if do |option_name, option|
option[:required] && options.send(option_name).nil?
end.keys.join(', ')

abort("missing required params: #{missing}. Use --help for more information.") unless missing.empty?
end
end
end
14 changes: 10 additions & 4 deletions spec/lib/shanty/cli_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'commander'
require 'spec_helper'
require 'shanty/cli'
require 'shanty/env'
Expand Down Expand Up @@ -96,17 +97,22 @@ module Shanty
subject.run
end

xit('fails to run with no command specified') do
it('fails to run with no command specified') do
expect(Commander::Runner.instance).to receive(:abort).with('invalid command. Use --help for more information')
subject.run
end

xit('fails to run an invalid command') do
it('fails to run an invalid command') do
expect(Commander::Runner.instance).to receive(:abort).with('invalid command. Use --help for more information')

ARGV.concat(%w(xulu))

subject.run
end

xit('fails to run a command when run without required options') do
it('fails to run a command when run without required options') do
expect(subject).to receive(:abort).with('missing required params: catweasel. Use --help for more information.')

ARGV.concat(%w(foo))

subject.run
Expand All @@ -118,7 +124,7 @@ module Shanty
subject.run
end

xit('executes a command correctly when run without non-required options') do
it('executes a command correctly when run without non-required options') do
ARGV.concat(%w(foo --catweasel=noiamacatweasel))

subject.run
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/shanty/project_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Shanty
it('raises an exception if the given path is not a valid directory') do
allow(File).to receive(:directory?).and_return(false)

expect { subject }.to raise_exception('Path to project must be a directory.')
expect { subject }.to raise_error('Path to project must be a directory.')
end
end

Expand Down

0 comments on commit 814081a

Please sign in to comment.