Skip to content

Commit e8f825d

Browse files
committed
Fix error when loading empty config file
1 parent 032e17c commit e8f825d

2 files changed

Lines changed: 14 additions & 11 deletions

File tree

lib/sagittarius/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def self.config
1010
memoize(:config) do
1111
configured_files = ENV.fetch(CONFIG_FILES_ENV, nil)
1212
config_files(configured_files).reduce(defaults) do |config, config_file|
13-
file_config = YAML.safe_load_file(config_file).deep_symbolize_keys
13+
file_config = YAML.safe_load_file(config_file, fallback: {}).deep_symbolize_keys
1414
config.deep_merge(file_config)
1515
end
1616
rescue Errno::ENOENT

spec/lib/sagittarius/configuration_spec.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
end
1414

1515
it 'loads the default config file' do
16-
allow(YAML).to receive(:safe_load_file).with(default_config_file).and_return(
16+
allow(YAML).to receive(:safe_load_file).with(default_config_file, fallback: {}).and_return(
1717
'rails' => {
1818
'web' => { 'threads' => 4 },
1919
'grpc' => { 'threads' => 8 },
@@ -29,7 +29,7 @@
2929
end
3030

3131
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)
32+
allow(YAML).to receive(:safe_load_file).with(default_config_file, fallback: {}).and_raise(Errno::ENOENT)
3333

3434
expect(described_class.config).to eq(described_class.defaults)
3535
end
@@ -39,19 +39,19 @@
3939
configured_files = ' config/base.yml,config/environment.yml, config/runtime.yml '
4040
allow(ENV).to receive(:fetch).with('SAGITTARIUS_CONFIG_FILES', nil)
4141
.and_return(configured_files)
42-
allow(YAML).to receive(:safe_load_file).with('config/base.yml').and_return(
42+
allow(YAML).to receive(:safe_load_file).with('config/base.yml', fallback: {}).and_return(
4343
'rails' => {
4444
'web' => { 'threads' => 4, 'port' => 4000 },
4545
'grpc' => { 'threads' => 4 },
4646
}
4747
)
48-
allow(YAML).to receive(:safe_load_file).with('config/environment.yml').and_return(
48+
allow(YAML).to receive(:safe_load_file).with('config/environment.yml', fallback: {}).and_return(
4949
'rails' => {
5050
'web' => { 'threads' => 8 },
5151
'grpc' => { 'host' => 'environment:50051' },
5252
}
5353
)
54-
allow(YAML).to receive(:safe_load_file).with('config/runtime.yml').and_return(
54+
allow(YAML).to receive(:safe_load_file).with('config/runtime.yml', fallback: {}).and_return(
5555
'rails' => {
5656
'web' => { 'threads' => 16 },
5757
}
@@ -70,13 +70,13 @@
7070
it 'loads each configured file in order' do
7171
described_class.config
7272

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')
73+
expect(YAML).to have_received(:safe_load_file).ordered.with('config/base.yml', fallback: {})
74+
expect(YAML).to have_received(:safe_load_file).ordered.with('config/environment.yml', fallback: {})
75+
expect(YAML).to have_received(:safe_load_file).ordered.with('config/runtime.yml', fallback: {})
7676
end
7777

7878
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)
79+
allow(YAML).to receive(:safe_load_file).with('config/environment.yml', fallback: {}).and_raise(Errno::ENOENT)
8080

8181
expect { described_class.config }.to raise_error(Errno::ENOENT)
8282
end
@@ -85,7 +85,10 @@
8585

8686
describe '.defaults' do
8787
it 'matches the example config' do
88-
example_config = YAML.safe_load_file(Rails.root.join('config/sagittarius.example.yml')).deep_symbolize_keys
88+
example_config = YAML.safe_load_file(
89+
Rails.root.join('config/sagittarius.example.yml'),
90+
fallback: {}
91+
).deep_symbolize_keys
8992
expect(example_config).to eq(described_class.defaults)
9093
end
9194
end

0 commit comments

Comments
 (0)