Permalink
Browse files

Merge pull request #81 from enriclluelles/feature/config_file

Feature/config file
  • Loading branch information...
2 parents 2133baf + 008dd29 commit 92f0e2358209de76be28e684b1d4e50d910de2c3 @josepjaume josepjaume committed Dec 6, 2011
Showing with 84 additions and 2 deletions.
  1. +10 −1 lib/spinach/cli.rb
  2. +24 −1 lib/spinach/config.rb
  3. +22 −0 test/spinach/cli_test.rb
  4. +28 −0 test/spinach/config_test.rb
View
@@ -61,9 +61,15 @@ def options
def parse_options
reporter_options = {}
reporter_options[:backtrace] = false
+ config = {}
begin
OptionParser.new do |opts|
+ opts.on('-c', '--config_path PATH',
+ 'Parse options from file (will get overriden by flags)') do |file|
+ Spinach.config[:config_path] = file
+ end
+
opts.on('-b', '--backtrace',
'Show backtrace of errors') do |show_backtrace|
reporter_options[:backtrace] = show_backtrace
@@ -81,9 +87,12 @@ def parse_options
opts.on('-f', '--features_path PATH',
'Path where your features will be searched for') do |path|
- Spinach.config[:features_path] = path
+ config[:features_path] = path
end
end.parse!(@args)
+
+ Spinach.config.parse_from_file
+ config.each{|k,v| Spinach.config[k] = v}
rescue OptionParser::ParseError => exception
puts exception.message.capitalize
exit 1
View
@@ -21,7 +21,7 @@ def self.config
#
class Config
attr_writer :features_path, :step_definitions_path, :default_reporter, :support_path,
- :failure_exceptions
+ :failure_exceptions, :config_path
# The "features path" holds the place where your features will be
# searched for. Defaults to 'features'
@@ -96,5 +96,28 @@ def []=(attribute, value)
def failure_exceptions
@failure_exceptions ||= []
end
+
+ # It allows you to set a config file to parse for all the other options to be set
+ #
+ # @return [String]
+ # The config file name
+ #
+ def config_path
+ @config_path ||= 'config/spinach.yml'
+ end
+
+ # Parse options from the config file
+ #
+ # @return [Boolean]
+ # If the config was parsed from the file
+ #
+ def parse_from_file
+ parsed_opts = YAML.load_file(config_path)
+ parsed_opts.delete_if{|k| k.to_s == 'config_path'}
+ parsed_opts.each_pair{|k,v| self[k] = v}
+ true
+ rescue Errno::ENOENT
+ false
+ end
end
end
View
@@ -53,6 +53,28 @@
end
end
+ describe 'config_path' do
+ %w{-c --config_path}.each do |opt|
+ it "sets the config path" do
+ config = Spinach::Config.new
+ Spinach.stubs(:config).returns(config)
+ cli = Spinach::Cli.new([opt, 'config_file'])
+ config.expects(:parse_from_file)
+ cli.options
+ config.config_path.must_equal 'config_file'
+ end
+
+ it "gets overriden by the other cli options" do
+ config = Spinach::Config.new
+ Spinach.stubs(:config).returns(config)
+ YAML.stubs(:load_file).returns({features_path: 'my_path'})
+ cli = Spinach::Cli.new(['-f', 'another_path', opt, 'config_file'])
+ cli.options
+ config.features_path.must_equal 'another_path'
+ end
+ end
+ end
+
describe 'undefined option' do
%w{-lorem --ipsum}.each do |opt|
it 'exits and outputs error message with #{opt}' do
@@ -53,4 +53,32 @@
subject[:failure_exceptions].must_include RuntimeError
end
end
+
+ describe '#config_path' do
+ it 'returns a default' do
+ subject[:config_path].must_equal 'config/spinach.yml'
+ end
+
+ it 'can be overwritten' do
+ subject[:config_path] = 'my_config_file.yml'
+ subject[:config_path].must_equal 'my_config_file.yml'
+ end
+ end
+
+ describe '#parse_from_file' do
+ before do
+ subject[:config_path] = 'a_path'
+ YAML.stubs(:load_file).returns({support_path: 'my_path', config_path: 'another_path'})
+ subject.parse_from_file
+ end
+
+ it 'sets the options' do
+ subject[:support_path].must_equal 'my_path'
+ end
+
+ it "doesn't set the config_path option" do
+ subject[:config_path].must_equal 'a_path'
+ end
+ end
+
end

0 comments on commit 92f0e23

Please sign in to comment.