Permalink
Browse files

Fixed error handling in parse_load_paths. Updated specs

  • Loading branch information...
1 parent cccf7f9 commit 176c526c465968a33aad55524262a207df8f38de @rahmal committed May 3, 2012
View
@@ -1,3 +1,8 @@
+0.4.3
+=====
+* Fixed error handling in parse_load_paths
+* Updated specs
+
0.4.2
=====
* Fixed indifferent access in Config object.
View
@@ -1,10 +1,17 @@
#!/usr/bin/env rake
+ROOT_DIR = File.expand_path(File.dirname(__FILE__))
+CONF_DIR = File.join(ROOT_DIR, 'config')
+
+# Loads the rconfig library
+$LOAD_PATH << File.join(ROOT_DIR, 'lib')
+
+
require 'rubygems'
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
-require 'lib/rconfig'
+require 'rconfig'
Dir['tasks/**/*.rake'].sort.each { |lib| load lib }
View
@@ -65,7 +65,7 @@
require 'rconfig/core_ext/nil'
module RConfig
- VERSION = '0.4.2'
+ VERSION = '0.4.3'
autoload :Socket, 'socket'
autoload :YAML, 'yaml'
@@ -21,6 +21,7 @@ def set_load_paths(paths)
def add_load_path(path)
if path = parse_load_paths(path).first # only accept first one.
self.load_paths << path
+ self.load_paths.uniq!
return reload(true) # Load Paths have changed so force a reload
end
false
@@ -30,7 +31,6 @@ def add_load_path(path)
# If the paths are made up of a delimited string, then parse out the
# individual paths. Verify that each path is valid.
def parse_load_paths(paths)
- return [] unless paths
if paths.is_a? String
path_sep = (paths =~ /;/) ? ';' : ':'
paths = paths.split(/#{path_sep}+/)
@@ -39,7 +39,7 @@ def parse_load_paths(paths)
raise ArgumentError, "Must provide at least one load path: [#{paths.inspect}]" if paths.empty?
paths.each do |dir|
dir = CONFIG_ROOT if dir == 'CONFIG_ROOT'
- raise RConfig::InvalidConfigPathError, "This directory is invalid: [#{dir.inspect}]" unless Dir.exists?(dir)
+ raise InvalidLoadPathError, "This directory is invalid: [#{dir.inspect}]" unless Dir.exists?(dir)
end
paths
end
@@ -0,0 +1,3 @@
+admin:
+ name: <%= ENV['USER'] %>
+ home: <%= ENV['HOME'] %>
@@ -0,0 +1,18 @@
+[admin]
+name="Harry Potter"
+email="harry@hogwarts.com"
+
+[server]
+host=127.0.0.1
+port=8080
+
+[teams "alpha"]
+name="Alpha Squad"
+location="San Francisco, CA"
+num_members=12
+
+[teams "beta"]
+name="Beta Squad"
+location="New York, NY"
+num_members=10
+
View
@@ -0,0 +1,17 @@
+admin:
+ name: 'Harry Potter'
+ email: 'harry@hogwarts.com'
+
+server:
+ host: 127.0.0.1
+ port: 8080
+
+teams:
+ alpha:
+ name: 'Alpha Squad'
+ location: 'San Francisco, CA'
+ num_members: 12
+ beta:
+ beta: 'Beta Squad'
+ location: 'New York, NY'
+ num_members: 10
View
@@ -0,0 +1,22 @@
+<xml_config>
+ <admin>
+ <name>Harry Potter</name>
+ <email>harry@hogwarts.com</email>
+ </admin>
+ <server>
+ <host>127.0.0.1</host>
+ <port>8080</port>
+ </server>
+ <teams>
+ <alpha>
+ <name>Alpha Squad</name>
+ <location>San Francisco, CA</location>
+ <num_members>12</num_members>
+ </alpha>
+ <beta>
+ <name>Beta Squad</name>
+ <location>New York, NY</location>
+ <num_members>10</num_members>
+ </beta>
+ </teams>
+</xml_config>
View
No changes.
@@ -0,0 +1,37 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe RConfig do
+ before :each do
+ RConfig.load_paths = ['spec/config']
+ end
+
+ context 'load paths' do
+ it 'should have a valid load path' do
+ RConfig.load_paths.should_not be_blank
+ RConfig.load_paths.all?{|path| File.exists?(path) }.should be_true
+ end
+
+ it 'should allow multiple load paths' do
+ lambda { RConfig.add_load_path('spec/config2') }.should_not raise_error
+ RConfig.load_paths.size.should == 2
+ end
+
+ it 'should not allow duplicate load paths' do
+ RConfig.add_load_path('spec/config')
+ RConfig.load_paths.size.should == 1
+ end
+
+ it 'should not allow nil paths' do
+ lambda { RConfig.add_load_path(nil) }.should raise_error(ArgumentError)
+ end
+
+ it 'should not allow blank paths' do
+ lambda { RConfig.add_load_path('') }.should raise_error(ArgumentError)
+ end
+
+ it 'shoud not allow paths that do not exist' do
+ lambda { RConfig.add_load_path('/bad/path') }.should raise_error(RConfig::InvalidLoadPathError)
+ end
+ end
+
+end
@@ -0,0 +1,37 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe RConfig do
+ before :each do
+ RConfig.load_paths = ['spec/config']
+ end
+
+ context 'reload' do
+ it 'should reload when path is added' do
+ RConfig.should_receive(:reload).with(true).and_return(true)
+ RConfig.add_load_path('spec')
+ end
+
+ it 'should reload if reload enabled' do
+ RConfig.enable_reload = true
+ RConfig.reload.should be_true
+ end
+
+ it 'should not reload if reload disabled' do
+ RConfig.enable_reload = false
+ RConfig.reload.should be_false
+ end
+
+ it 'should reload if forced' do
+ RConfig.enable_reload = false
+ RConfig.reload(true).should be_true
+ end
+
+ it 'should flush cache on reload' do
+ RConfig.should_receive(:flush_cache)
+ RConfig.reload(true)
+ RConfig.cache.should == {}
+ end
+
+ end
+
+end
View
@@ -1,7 +1,65 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
-describe "RConfig" do
- it "fails" do
- fail "hey buddy, you should probably rename this file and start specing for real"
+describe RConfig do
+ before :each do
+ RConfig.load_paths = ['spec/config']
+ end
+
+ context 'reading config' do
+ it 'should read config for the specified file' do
+ RConfig.spec.should == CONFIG
+ end
+
+ it 'should read xml files' do
+ RConfig.xml_config.should == CONFIG2
+ end
+
+ it 'should read property files' do
+ RConfig.props_config.should == CONFIG2
+ end
+
+ it 'accept string keys' do
+ RConfig.spec['admin'].should == CONFIG['admin']
+ end
+
+ it 'should accept symbol keys' do
+ RConfig.spec[:admin].should == CONFIG['admin']
+ end
+
+ it 'should accept keys by method invocation' do
+ RConfig.spec.admin.should == CONFIG['admin']
+ end
+
+ it 'should return empty config for bad file names' do
+ RConfig.bad.should be_blank
+ end
+
+ it 'should return nil for bad properties' do
+ RConfig.spec.bad.should be_nil
+ end
+
+ it 'should parse erb contents' do
+ RConfig.erb_contents.admin.name.should == ENV['USER']
+ RConfig.erb_contents.admin.home.should == ENV['HOME']
+ end
+ end
+
+ context 'parsing files' do
+ it 'should raise error for bad file types' do
+ contents = %Q{{"admin"=>{"name"=>"/Users/rahmal", "home"=>"rahmal"}}}
+ lambda { RConfig.parse(contents, 'some_file', :bad) }.should raise_error(RConfig::ConfigError)
+ end
+
+ it 'should parse properties files with PropertiesFile parser' do
+ contents = %Q{{"admin"=>{"name"=>"/Users/rahmal", "home"=>"rahmal"}}}
+ RConfig::PropertiesFile.should_receive(:parse).with(contents)
+ RConfig.parse(contents, 'props_file', :conf)
+ end
+
+ it 'should parse xml files with activesupport hash' do
+ contents = "<admin><name>/Users/rahmal</name><home>rahmal</home></admin>"
+ Hash.should_receive(:from_xml).with(contents)
+ RConfig.parse(contents, 'xml_file', :xml)
+ end
end
end
View
@@ -1,16 +1,20 @@
ROOT_DIR = File.expand_path(File.dirname(__FILE__))
-CONF_DIR = File.join(ROOT_DIR, '..', 'test', 'config_files')
-
-STDOUT.puts "ROOT_DIR: #{ROOT_DIR}"
-STDOUT.puts "CONF_DIR: #{CONF_DIR}"
+CONF_DIR = File.join(ROOT_DIR, 'config')
# Loads the rconfig library
$LOAD_PATH << File.join(ROOT_DIR, '..', 'lib')
+require 'yaml'
+require 'active_support'
+require 'active_support/hash_with_indifferent_access'
+require 'active_support/core_ext/hash/conversions'
require 'rconfig'
-RConfig.initialize CONF_DIR, nil, true, true
-
-# Requires supporting files in ./support/ and its subdirectories.
-#Dir["#{ROOT_DIR}/support/**/*.rb"].each {|f| require f}
+RConfig.setup do |config|
+ config.load_paths = ['spec/config']
+ config.file_types = [:yml, :xml, :conf]
+ config.logger = RConfig::DisabledLogger.new
+end
+CONFIG = YAML.load_file(CONF_DIR + '/spec.yml')
+CONFIG2 = Hash.from_xml(File.read(CONF_DIR + '/xml_config.xml'))['xml_config']

0 comments on commit 176c526

Please sign in to comment.