|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -require 'ostruct' |
4 | | - |
5 | 3 | require_relative '../../spec_helper' |
6 | 4 |
|
7 | 5 | require OctocatalogDiff::Spec.require_path('/api/v1/config') |
|
20 | 18 | end |
21 | 19 |
|
22 | 20 | describe '#config' do |
| 21 | + let(:filename) { '/fizz/buzz.rb' } |
| 22 | + |
| 23 | + context 'with missing configuration file' do |
| 24 | + let(:pattern) { Regexp.new("Unable to find configuration file in #{filename}") } |
| 25 | + |
| 26 | + context 'in test mode' do |
| 27 | + it 'should raise ConfigurationFileNotFoundError' do |
| 28 | + expect(File).to receive(:'file?').with(filename).and_return(false) |
| 29 | + expect do |
| 30 | + described_class.config(logger: @logger, filename: filename, test: true) |
| 31 | + end.to raise_error(OctocatalogDiff::API::V1::Config::ConfigurationFileNotFoundError, pattern) |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + context 'in normal mode' do |
| 36 | + it 'should return {}' do |
| 37 | + expect(File).to receive(:'file?').with(filename).and_return(false) |
| 38 | + result = described_class.config(logger: @logger, filename: filename) |
| 39 | + expect(result).to eq({}) |
| 40 | + expect(@logger_str.string).to match(pattern) |
| 41 | + end |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + context 'with found configuration file' do |
| 46 | + let(:pattern) { Regexp.new('Loaded 1 settings from /fizz/buzz.rb') } |
| 47 | + let(:debug_pattern) { Regexp.new(':foo => \(String\) "bar"') } |
| 48 | + |
| 49 | + context 'in test mode' do |
| 50 | + it 'should print settings to log and return settings' do |
| 51 | + expect(File).to receive(:'file?').with(filename).and_return(true) |
| 52 | + expect(described_class).to receive(:load_config_file).and_return(foo: 'bar') |
| 53 | + result = described_class.config(logger: @logger, filename: filename, test: true) |
| 54 | + expect(result).to eq(foo: 'bar') |
| 55 | + expect(@logger_str.string).to match(pattern) |
| 56 | + expect(@logger_str.string).to match(debug_pattern) |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + context 'in normal mode' do |
| 61 | + it 'should return settings' do |
| 62 | + expect(File).to receive(:'file?').with(filename).and_return(true) |
| 63 | + expect(described_class).to receive(:load_config_file).and_return(foo: 'bar') |
| 64 | + result = described_class.config(logger: @logger, filename: filename) |
| 65 | + expect(result).to eq(foo: 'bar') |
| 66 | + expect(@logger_str.string).to match(pattern) |
| 67 | + expect(@logger_str.string).not_to match(debug_pattern) |
| 68 | + end |
| 69 | + end |
| 70 | + end |
23 | 71 | end |
24 | 72 |
|
25 | 73 | describe '#debug_config_file' do |
|
0 commit comments