File: config_file_spec.rb

package info (click to toggle)
ruby-sinatra 4.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,944 kB
  • sloc: ruby: 17,702; sh: 25; makefile: 8
file content (106 lines) | stat: -rw-r--r-- 3,353 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
require 'spec_helper'

RSpec.describe Sinatra::ConfigFile do
  def config_file(*args, &block)
    mock_app do
      register Sinatra::ConfigFile
      set :root, File.expand_path('config_file', __dir__)
      instance_eval(&block) if block
      config_file(*args)
    end
  end

  it 'should set options from a simple config_file' do
    config_file 'key_value.yml'
    expect(settings.foo).to eq('bar')
    expect(settings.something).to eq(42)
  end

  it 'should create indifferent hashes' do
    config_file 'key_value.yml'
    expect(settings.nested['a']).to eq(1)
    expect(settings.nested[:a]).to eq(1)
  end

  it 'should render options in ERB tags when using .yml files' do
    config_file 'key_value.yml'
    expect(settings.bar).to eq "bar"
    expect(settings.something).to eq 42
    expect(settings.nested['a']).to eq 1
    expect(settings.nested[:a]).to eq 1
    expect(settings.nested['b']).to eq 2
    expect(settings.nested[:b]).to eq 2
  end

  it 'should render options in ERB tags when using .yml.erb files' do
    config_file 'key_value.yml.erb'
    expect(settings.foo).to eq("bar")
    expect(settings.something).to eq(42)
    expect(settings.nested['a']).to eq(1)
    expect(settings.nested[:a]).to eq(1)
    expect(settings.nested['b']).to eq(2)
    expect(settings.nested[:b]).to eq(2)
  end

  it 'should render options in ERB tags when using .yaml files' do
    config_file 'key_value.yaml'
    expect(settings.foo).to eq("bar")
    expect(settings.something).to eq(42)
    expect(settings.nested['a']).to eq(1)
    expect(settings.nested[:a]).to eq(1)
    expect(settings.nested['b']).to eq(2)
    expect(settings.nested[:b]).to eq(2)
  end

  it 'should raise error if config file extension is not .yml, .yaml or .erb' do
    expect{ config_file 'config.txt' }.to raise_error(Sinatra::ConfigFile::UnsupportedConfigType)
  end

  it 'should recognize env specific settings per file' do
    config_file 'with_envs.yml'
    expect(settings.foo).to eq('test')
  end

  it 'should recognize env specific settings per setting' do
    config_file 'with_nested_envs.yml'
    expect(settings.database[:adapter]).to eq('sqlite')
  end

  it 'should not set present values to nil if the current env is missing' do
    # first let's check the test is actually working properly
    config_file('missing_env.yml') { set :foo => 42, :environment => :production }
    expect(settings.foo).to eq(10)
    # now test it
    config_file('missing_env.yml') { set :foo => 42, :environment => :test }
    expect(settings.foo).to eq(42)
  end

  it 'should prioritize settings in latter files' do
    # first let's check the test is actually working properly
    config_file 'key_value.yml'
    expect(settings.foo).to eq('bar')
    # now test it
    config_file 'key_value_override.yml'
    expect(settings.foo).to eq('foo')
  end

  context 'when file contains superfluous environments' do
    before { config_file 'with_env_defaults.yml' }

    it 'loads settings for the current environment anyway' do
      expect { settings.foo }.not_to raise_error
    end
  end

  context 'when file contains defaults' do
    before { config_file 'with_env_defaults.yml' }

    it 'uses the overridden value' do
      expect(settings.foo).to eq('test')
    end

    it 'uses the default value' do
      expect(settings.bar).to eq('baz')
    end
  end
end