Skip to content

Commit 6449f4d

Browse files
author
Kevin Paulisse
committed
Add additional test coverage
1 parent e42f430 commit 6449f4d

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# This is a configuration file for octocatalog-diff
2+
3+
module OctocatalogDiff
4+
class ConfigFooBar
5+
def self.config
6+
raise 'Fizz Buzz'
7+
end
8+
end
9+
end

spec/octocatalog-diff/tests/api/v1/config_spec.rb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,50 @@
127127
end
128128
end
129129

130+
context 'with a file that does not define OctocatalogDiff::Config' do
131+
let(:filename) { OctocatalogDiff::Spec.fixture_path('cli-configs/not-class.rb') }
132+
133+
it 'should raise ConfigurationFileContentError' do
134+
pattern = Regexp.new('must define OctocatalogDiff::Config!')
135+
expect do
136+
described_class.load_config_file(filename, @logger)
137+
end.to raise_error(OctocatalogDiff::API::V1::Config::ConfigurationFileContentError, pattern)
138+
end
139+
140+
it 'should log fatal message' do
141+
exception = nil
142+
begin
143+
described_class.load_config_file(filename, @logger)
144+
rescue OctocatalogDiff::API::V1::Config::ConfigurationFileContentError => exc
145+
exception = exc
146+
end
147+
expect(exception).to be_a_kind_of(OctocatalogDiff::API::V1::Config::ConfigurationFileContentError)
148+
expect(exception.message).to eq('Configuration must define OctocatalogDiff::Config!')
149+
expect(@logger_str.string).to match(/Configuration must define OctocatalogDiff::Config!/)
150+
end
151+
end
152+
130153
context 'with a file that does not define a hash' do
154+
let(:filename) { OctocatalogDiff::Spec.fixture_path('cli-configs/not-hash.rb') }
155+
156+
it 'should raise ConfigurationFileContentError' do
157+
pattern = Regexp.new('Configuration must be Hash not Array!')
158+
expect do
159+
described_class.load_config_file(filename, @logger)
160+
end.to raise_error(OctocatalogDiff::API::V1::Config::ConfigurationFileContentError, pattern)
161+
end
162+
163+
it 'should log fatal message' do
164+
exception = nil
165+
begin
166+
described_class.load_config_file(filename, @logger)
167+
rescue OctocatalogDiff::API::V1::Config::ConfigurationFileContentError => exc
168+
exception = exc
169+
end
170+
expect(exception).to be_a_kind_of(OctocatalogDiff::API::V1::Config::ConfigurationFileContentError)
171+
expect(exception.message).to eq('Configuration must be Hash not Array!')
172+
expect(@logger_str.string).to match(/Configuration must be Hash not Array!/)
173+
end
131174
end
132175

133176
context 'with a valid file' do
@@ -147,5 +190,28 @@
147190
end
148191

149192
describe '#first_file' do
193+
before(:each) do
194+
allow(File).to receive(:'file?') { |x| x =~ /^present/ }
195+
end
196+
197+
it 'should accept an empty array and return nil' do
198+
expect(described_class.first_file([])).to be_nil
199+
end
200+
201+
it 'should accept a single missing file and return nil' do
202+
expect(described_class.first_file(['missing1'])).to be_nil
203+
end
204+
205+
it 'should accept a multiple missing files and return nil' do
206+
expect(described_class.first_file(%w(missing1 missing2 missing3))).to be_nil
207+
end
208+
209+
it 'should accept nil in the array and still work' do
210+
expect(described_class.first_file(['missing1', nil, 'missing2', 'present1'])).to eq('present1')
211+
end
212+
213+
it 'should flatten arrays and still work' do
214+
expect(described_class.first_file(['missing1', [nil, 'missing2'], ['present1']])).to eq('present1')
215+
end
150216
end
151217
end

0 commit comments

Comments
 (0)