spec/figgy_spec.rb in figgy-1.2.0 vs spec/figgy_spec.rb in figgy-1.3.0
- old
+ new
@@ -47,12 +47,12 @@
it "loads in the order named" do
write_config 'values.yml', 'foo: 1'
write_config 'values.yaml', 'foo: 2'
- config = test_config do |config|
- config.define_handler('yml', 'yaml') { |body| YAML.load(body) }
+ config = test_config do |cfg|
+ cfg.define_handler('yml', 'yaml') { |body| YAML.load(body) }
end
expect(config.values.foo).to eq(2)
end
end
@@ -168,10 +168,31 @@
end
it "can merge with another hash" do
expect(config.values.with.merge(config.values.another)).to eq(config.values.altogether)
end
+
+ it 'can fetch a key with a default value' do
+ expect(config.values.without.fetch('two')).to eq(config.values.without.two)
+ expect(config.values.without.fetch(:two)).to eq(config.values.without.two)
+
+ expect { config.values.without.fetch('one') }.to raise_error(/key not found/)
+ expect { config.values.without.fetch(:one) }.to raise_error(/key not found/)
+
+ expect(config.values.without.fetch('one', 1)).to eq 1
+ expect(config.values.without.fetch(:one, 1)).to eq 1
+
+ expect(config.values.without.fetch('three') { 3 } ).to eq 3
+ expect(config.values.without.fetch(:three) { 3 } ).to eq 3
+ end
+
+ it 'can dig for nested values' do
+ skip if RUBY_VERSION < '2.3.0'
+
+ expect(config.values.dig(:without, 'two')).to eq config.values.without.two
+ expect(config.values.dig(:without, 'one')).to eq nil
+ end
end
end
context 'oddities' do
it "returns false for empty files (cf. YAML.load(''))" do
@@ -198,13 +219,13 @@
context "multiple roots" do
it "can be told to read from multiple directories" do
write_config 'root1/values', 'foo: 1'
write_config 'root2/values', 'bar: 2'
- config = test_config do |config|
- config.root = File.join(current_dir, 'root1')
- config.add_root File.join(current_dir, 'root2')
+ config = test_config do |cfg|
+ cfg.root = File.join(current_dir, 'root1')
+ cfg.add_root File.join(current_dir, 'root2')
end
expect(config.values.foo).to eq(1)
expect(config.values.bar).to eq(2)
end
@@ -213,14 +234,14 @@
write_config 'root1/values', 'foo: 1'
write_config 'root1/prod/values', 'foo: 2'
write_config 'root2/values', 'bar: 1'
write_config 'root2/prod/values', 'bar: 2'
- config = test_config do |config|
- config.root = File.join(current_dir, 'root1')
- config.add_root File.join(current_dir, 'root2')
- config.define_overlay :environment, 'prod'
+ config = test_config do |cfg|
+ cfg.root = File.join(current_dir, 'root1')
+ cfg.add_root File.join(current_dir, 'root2')
+ cfg.define_overlay :environment, 'prod'
end
expect(config.values.foo).to eq(2)
expect(config.values.bar).to eq(2)
end
@@ -228,14 +249,14 @@
it "reads from roots in *reverse* order of definition" do
write_config 'root1/values', 'foo: 1'
write_config 'root1/prod/values', 'foo: 2'
write_config 'root2/prod/values', 'foo: 3'
- config = test_config do |config|
- config.root = File.join(current_dir, 'root1')
- config.add_root File.join(current_dir, 'root2')
- config.define_overlay :environment, 'prod'
+ config = test_config do |cfg|
+ cfg.root = File.join(current_dir, 'root1')
+ cfg.add_root File.join(current_dir, 'root2')
+ cfg.define_overlay :environment, 'prod'
end
expect(config.values.foo).to eq(2)
end
end
@@ -246,31 +267,31 @@
expect(test_config.values).to eq({ "foo" => 1 })
end
it "interprets a nil overlay value as an indication to read from the config root" do
write_config 'values', "foo: 1"
- config = test_config do |config|
- config.define_overlay :default, nil
+ config = test_config do |cfg|
+ cfg.define_overlay :default, nil
end
expect(config.values).to eq({ "foo" => 1 })
end
it "allows the overlay's value to be the result of a block" do
write_config 'prod/values', "foo: 1"
- config = test_config do |config|
- config.define_overlay(:environment) { 'prod' }
+ config = test_config do |cfg|
+ cfg.define_overlay(:environment) { 'prod' }
end
expect(config.values).to eq({ "foo" => 1 })
end
it "overwrites values if the config file does not define a hash" do
write_config 'some_string', "foo bar baz"
write_config 'prod/some_string', "foo bar baz quux"
- config = test_config do |config|
- config.define_overlay :default, nil
- config.define_overlay :environment, 'prod'
+ config = test_config do |cfg|
+ cfg.define_overlay :default, nil
+ cfg.define_overlay :environment, 'prod'
end
expect(config.some_string).to eq("foo bar baz quux")
end
@@ -285,24 +306,24 @@
foo:
baz: 3
quux: hi!
YML
- config = test_config do |config|
- config.define_overlay :default, 'defaults'
- config.define_overlay :environment, 'prod'
+ config = test_config do |cfg|
+ cfg.define_overlay :default, 'defaults'
+ cfg.define_overlay :environment, 'prod'
end
expect(config.values).to eq({ "foo" => { "bar" => 1, "baz" => 3 }, "quux" => "hi!" })
end
it "can use both a nil overlay and an overlay with a value" do
write_config 'values', "foo: 1\nbar: 2"
write_config 'prod/values', "foo: 2"
- config = test_config do |config|
- config.define_overlay :default, nil
- config.define_overlay :environment, 'prod'
+ config = test_config do |cfg|
+ cfg.define_overlay :default, nil
+ cfg.define_overlay :environment, 'prod'
end
expect(config.values).to eq({ "foo" => 2, "bar" => 2 })
end
it "reads from overlays in order of definition" do
@@ -319,14 +340,14 @@
write_config 'local/values', <<-YML
baz: 3
YML
- config = test_config do |config|
- config.define_overlay :default, 'defaults'
- config.define_overlay :environment, 'prod'
- config.define_overlay :local, 'local'
+ config = test_config do |cfg|
+ cfg.define_overlay :default, 'defaults'
+ cfg.define_overlay :environment, 'prod'
+ cfg.define_overlay :local, 'local'
end
expect(config.values).to eq({ "foo" => 1, "bar" => 2, "baz" => 3 })
end
end
@@ -335,37 +356,37 @@
it "allows new overlays to be defined from the values of others" do
write_config 'keys', "foo: 1"
write_config 'prod/keys', "foo: 2"
write_config 'prod_US/keys', "foo: 3"
- config = test_config do |config|
- config.define_overlay :default, nil
- config.define_overlay :environment, 'prod'
- config.define_overlay :country, 'US'
- config.define_combined_overlay :environment, :country
+ config = test_config do |cfg|
+ cfg.define_overlay :default, nil
+ cfg.define_overlay :environment, 'prod'
+ cfg.define_overlay :country, 'US'
+ cfg.define_combined_overlay :environment, :country
end
expect(config.keys).to eq({ "foo" => 3 })
end
end
context "reloading" do
it "can reload on each access when config.always_reload = true" do
write_config 'values', 'foo: 1'
- config = test_config do |config|
- config.always_reload = true
+ config = test_config do |cfg|
+ cfg.always_reload = true
end
expect(config.values).to eq({ "foo" => 1 })
write_config 'values', 'foo: bar'
expect(config.values).to eq({ "foo" => "bar" })
end
it "does not reload when config.always_reload = false" do
write_config 'values', 'foo: 1'
- config = test_config do |config|
- config.always_reload = false
+ config = test_config do |cfg|
+ cfg.always_reload = false
end
expect(config.values).to eq({ "foo" => 1 })
write_config 'values', 'foo: bar'
expect(config.values).to eq({ "foo" => 1 })
@@ -376,14 +397,14 @@
it "can preload all available configs when config.preload = true" do
write_config 'values', 'foo: 1'
write_config 'prod/values', 'foo: 2'
write_config 'prod/prod_only', 'bar: baz'
- config = test_config do |config|
- config.define_overlay :default, nil
- config.define_overlay :environment, 'prod'
- config.preload = true
+ config = test_config do |cfg|
+ cfg.define_overlay :default, nil
+ cfg.define_overlay :environment, 'prod'
+ cfg.preload = true
end
write_config 'prod/values', 'foo: 3'
write_config 'prod_only', 'bar: quux'
@@ -395,26 +416,26 @@
write_config 'values.yaml', 'foo: 1'
write_config 'values.json', '{ "foo": 2 }'
write_config 'prod/lonely.yml', 'only: yml'
write_config 'local/json_values.json', '{ "json": true }'
- config = test_config do |config|
- config.define_overlay :default, nil
- config.define_overlay :environment, 'prod'
- config.define_overlay :local, 'local'
+ config = test_config do |cfg|
+ cfg.define_overlay :default, nil
+ cfg.define_overlay :environment, 'prod'
+ cfg.define_overlay :local, 'local'
end
finder = config.instance_variable_get(:@finder)
expect(finder.all_key_names).to eq(['values', 'lonely', 'json_values'])
end
it "still supports reloading when preloading is enabled" do
write_config 'values', 'foo: 1'
- config = test_config do |config|
- config.preload = true
- config.always_reload = true
+ config = test_config do |cfg|
+ cfg.preload = true
+ cfg.always_reload = true
end
expect(config.values['foo']).to eq(1)
write_config 'values', 'foo: 2'
@@ -428,12 +449,12 @@
expect(test_config.values.foo).not_to be_frozen
end
it "freezes the results when config.freeze = true" do
write_config 'values', "foo: '1'"
- config = test_config do |config|
- config.freeze = true
+ config = test_config do |cfg|
+ cfg.freeze = true
end
expect(config.values).to be_frozen
end
it "freezes all the way down" do
@@ -444,11 +465,11 @@
- some string
- another string
- and: an inner hash
YML
- config = test_config do |config|
- config.freeze = true
+ config = test_config do |cfg|
+ cfg.freeze = true
end
expect { config.values.outer.array[2]['and'] = 'foo' }.to raise_error(/can't modify frozen/)
assert_deeply_frozen(config.values)
end