|
4 | 4 |
|
5 | 5 | RSpec.describe Sagittarius::Configuration do |
6 | 6 | describe '.config' do |
7 | | - let(:yaml_config) do |
8 | | - <<~CONFIG |
9 | | - rails: |
10 | | - web: |
11 | | - threads: 4 |
12 | | - grpc: |
13 | | - threads: 8 |
14 | | - CONFIG |
15 | | - end |
| 7 | + let(:default_config_file) { Rails.root.join('config/sagittarius.yml') } |
16 | 8 |
|
17 | 9 | before do |
18 | | - allow(File).to receive(:open).and_call_original |
19 | | - allow(File).to receive(:open) |
20 | | - .with(Rails.root.join('config/sagittarius.yml'), instance_of(String)) |
21 | | - .and_yield(yaml_config) |
| 10 | + allow(ENV).to receive(:fetch).and_call_original |
| 11 | + allow(ENV).to receive(:fetch).with('SAGITTARIUS_CONFIG_FILES', nil).and_return(nil) |
22 | 12 | described_class.clear_memoize(:config) |
23 | 13 | end |
24 | 14 |
|
25 | | - it 'loads the config' do |
| 15 | + it 'loads the default config file' do |
| 16 | + allow(YAML).to receive(:safe_load_file).with(default_config_file).and_return( |
| 17 | + 'rails' => { |
| 18 | + 'web' => { 'threads' => 4 }, |
| 19 | + 'grpc' => { 'threads' => 8 }, |
| 20 | + } |
| 21 | + ) |
| 22 | + |
26 | 23 | expect(described_class.config).to include( |
27 | 24 | rails: a_hash_including( |
28 | 25 | web: a_hash_including(threads: 4), |
29 | 26 | grpc: a_hash_including(threads: 8) |
30 | 27 | ) |
31 | 28 | ) |
32 | 29 | end |
| 30 | + |
| 31 | + it 'uses built-in defaults when the default config file does not exist' do |
| 32 | + allow(YAML).to receive(:safe_load_file).with(default_config_file).and_raise(Errno::ENOENT) |
| 33 | + |
| 34 | + expect(described_class.config).to eq(described_class.defaults) |
| 35 | + end |
| 36 | + |
| 37 | + context 'when SAGITTARIUS_CONFIG_FILES contains multiple paths' do |
| 38 | + before do |
| 39 | + configured_files = ' config/base.yml,config/environment.yml, config/runtime.yml ' |
| 40 | + allow(ENV).to receive(:fetch).with('SAGITTARIUS_CONFIG_FILES', nil) |
| 41 | + .and_return(configured_files) |
| 42 | + allow(YAML).to receive(:safe_load_file).with('config/base.yml').and_return( |
| 43 | + 'rails' => { |
| 44 | + 'web' => { 'threads' => 4, 'port' => 4000 }, |
| 45 | + 'grpc' => { 'threads' => 4 }, |
| 46 | + } |
| 47 | + ) |
| 48 | + allow(YAML).to receive(:safe_load_file).with('config/environment.yml').and_return( |
| 49 | + 'rails' => { |
| 50 | + 'web' => { 'threads' => 8 }, |
| 51 | + 'grpc' => { 'host' => 'environment:50051' }, |
| 52 | + } |
| 53 | + ) |
| 54 | + allow(YAML).to receive(:safe_load_file).with('config/runtime.yml').and_return( |
| 55 | + 'rails' => { |
| 56 | + 'web' => { 'threads' => 16 }, |
| 57 | + } |
| 58 | + ) |
| 59 | + end |
| 60 | + |
| 61 | + it 'deep merges files in order with the last file taking precedence' do |
| 62 | + expect(described_class.config).to include( |
| 63 | + rails: a_hash_including( |
| 64 | + web: a_hash_including(threads: 16, port: 4000), |
| 65 | + grpc: a_hash_including(threads: 4, host: 'environment:50051') |
| 66 | + ) |
| 67 | + ) |
| 68 | + end |
| 69 | + |
| 70 | + it 'loads each configured file in order' do |
| 71 | + described_class.config |
| 72 | + |
| 73 | + expect(YAML).to have_received(:safe_load_file).ordered.with('config/base.yml') |
| 74 | + expect(YAML).to have_received(:safe_load_file).ordered.with('config/environment.yml') |
| 75 | + expect(YAML).to have_received(:safe_load_file).ordered.with('config/runtime.yml') |
| 76 | + end |
| 77 | + |
| 78 | + it 'raises when a configured file does not exist' do |
| 79 | + allow(YAML).to receive(:safe_load_file).with('config/environment.yml').and_raise(Errno::ENOENT) |
| 80 | + |
| 81 | + expect { described_class.config }.to raise_error(Errno::ENOENT) |
| 82 | + end |
| 83 | + end |
33 | 84 | end |
34 | 85 |
|
35 | 86 | describe '.defaults' do |
|
0 commit comments