Skip to content

Commit

Permalink
Minor code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
vladgh committed Jun 14, 2014
1 parent 23283a9 commit b9091e5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
3 changes: 1 addition & 2 deletions lib/vscripts/command_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ def parse_command(args)
command_class = command.capitalize.to_sym
abort "Error: unknown subcommand '#{command}'\nTry --help." \
unless Commands.list.include?(command_class)
@command = command_class
@command_options = args
@command, @command_options = [command_class, args]
end
end # class CommandLine
end # module VScripts
11 changes: 4 additions & 7 deletions lib/vscripts/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,16 @@ class Config

# Loads class
# @param cfg_file [String] the path to the configuration file
def initialize(cfg_file = nil)
@cfg_file ||= cfg_file
end

# @return [Hash] the configuration options
def get
@get ||= GLOBAL_DEFAULTS.merge(options)
def initialize(config_file = nil)
@file = config_file
@get = GLOBAL_DEFAULTS.merge(options)
end

# Parses the configuration files in order
# @return [Hash] the first configuration hash found
def options
parse(@cfg_file) ||
parse(@file) ||
parse(DOT_FILE) ||
parse(SYSTEM_CONFIG_FILE) ||
{}
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/vscripts/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
describe '#new' do
context 'when config file not passed' do
it 'returns nil' do
expect(subject.instance_variable_get(:@cfg_file)).to be_nil
expect(subject.instance_variable_get(:@file)).to be_nil
end
end
context 'when config file passed' do
it 'returns the path' do
subject = VScripts::Config.new('/path/to/file')
expect(subject.instance_variable_get(:@cfg_file)).to eq('/path/to/file')
expect(subject.instance_variable_get(:@file)).to eq('/path/to/file')
end
end
end
Expand Down
21 changes: 14 additions & 7 deletions spec/unit/vscripts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,21 @@
expect($stdout.string).to match(/VScripts.*(c)/)
end
end
end

context 'when \'command\' and \'-h\'' do
it 'returns command help' do
stub_cli_with("#{cmd} -h")
expect{subject.run}.to raise_error(SystemExit)
expect($stdout.string).to match(/USAGE:/)
expect($stdout.string).to match(/OPTIONS:/)
end
describe '.cli' do
it 'returns subcommand and arguments' do
allow(VScripts::CommandLine).to receive(:new)
.and_return(VScripts::CommandLine.new("#{cmd} -h".split))
expect{subject.run}.to raise_error(SystemExit)
expect($stdout.string).to match(/USAGE:/)
expect($stdout.string).to match(/OPTIONS:/)
end
end

describe '.config' do
it 'returns a hash' do
expect(subject.config.get).to be_a Hash
end
end
end

0 comments on commit b9091e5

Please sign in to comment.