Skip to content

Commit 0c9f52a

Browse files
committed
Support array of sources for file text population
1 parent 4069cb3 commit 0c9f52a

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

lib/octocatalog-diff/catalog-util/fileresources.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,22 @@ def self.convert_file_resources(obj, environment = 'production')
2727
# module path that is specified within the environment.conf file (assuming the default 'modules'
2828
# directory doesn't exist or the module isn't found in there). If the file can't be found then
2929
# this returns nil which may trigger an error.
30-
# @param src [String] A file reference: puppet:///modules/xxx/yyy
30+
# @param src_in [String|Array] A file reference: puppet:///modules/xxx/yyy
3131
# @param modulepaths [Array] Cached module path
3232
# @return [String] File system path to referenced file
33-
def self.file_path(src, modulepaths)
34-
unless src =~ %r{^puppet:///modules/([^/]+)/(.+)}
35-
raise ArgumentError, "Bad parameter source #{src}"
33+
def self.file_path(src_in, modulepaths)
34+
valid_sources = [src_in].flatten.select { |line| line =~ %r{\Apuppet:///modules/([^/]+)/(.+)} }
35+
unless valid_sources.any?
36+
raise ArgumentError, "Bad parameter source #{src_in}"
3637
end
3738

38-
path = File.join(Regexp.last_match(1), 'files', Regexp.last_match(2))
39-
modulepaths.each do |mp|
40-
file = File.join(mp, path)
41-
return file if File.exist?(file)
39+
valid_sources.each do |src|
40+
src =~ %r{\Apuppet:///modules/([^/]+)/(.+)}
41+
path = File.join(Regexp.last_match(1), 'files', Regexp.last_match(2))
42+
modulepaths.each do |mp|
43+
file = File.join(mp, path)
44+
return file if File.exist?(file)
45+
end
4246
end
4347

4448
nil

spec/octocatalog-diff/tests/catalog-util/fileresources_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,59 @@ def catalog_from_fixture(path)
2222
end
2323

2424
it 'should return path if file is found' do
25+
allow(File).to receive(:exist?).and_call_original
2526
allow(File).to receive(:exist?).with('/a/foo/files/bar').and_return(true)
2627
result = OctocatalogDiff::CatalogUtil::FileResources.file_path('puppet:///modules/foo/bar', ['/a'])
2728
expect(result).to eq('/a/foo/files/bar')
2829
end
2930

3031
it 'should return nil if file is not found' do
32+
allow(File).to receive(:exist?).and_call_original
3133
allow(File).to receive(:exist?).with('/a/foo/files/bar').and_return(false)
3234
result = OctocatalogDiff::CatalogUtil::FileResources.file_path('puppet:///modules/foo/bar', ['/a'])
3335
expect(result).to eq(nil)
3436
end
37+
38+
it 'should return the first entry from an array if found' do
39+
allow(File).to receive(:exist?).and_call_original
40+
allow(File).to receive(:exist?).with('/a/foo/files/bar').and_return(true)
41+
expect(File).not_to receive(:exist?).with('/a/foo/files/baz')
42+
tries = ['puppet:///modules/foo/bar', 'puppet:///modules/foo/baz']
43+
result = OctocatalogDiff::CatalogUtil::FileResources.file_path(tries, ['/a'])
44+
expect(result).to eq('/a/foo/files/bar')
45+
end
46+
47+
it 'should return the first actually found entry from an array' do
48+
allow(File).to receive(:exist?).and_call_original
49+
allow(File).to receive(:exist?).with('/a/foo/files/bar').and_return(false)
50+
allow(File).to receive(:exist?).with('/a/foo/files/baz').and_return(true)
51+
tries = ['sdfasdf', 'puppet:///modules/foo/bar', 'puppet:///modules/foo/baz']
52+
result = OctocatalogDiff::CatalogUtil::FileResources.file_path(tries, ['/a'])
53+
expect(result).to eq('/a/foo/files/baz')
54+
end
55+
56+
it 'should return nil if no entries from an array are found' do
57+
allow(File).to receive(:exist?).and_call_original
58+
allow(File).to receive(:exist?).with('/a/foo/files/bar').and_return(false)
59+
allow(File).to receive(:exist?).with('/a/foo/files/baz').and_return(false)
60+
tries = ['puppet:///modules/foo/bar', 'puppet:///modules/foo/baz']
61+
result = OctocatalogDiff::CatalogUtil::FileResources.file_path(tries, ['/a'])
62+
expect(result).to be_nil
63+
end
64+
65+
it 'should raise an error if the only entry is malformed' do
66+
tries = 'sddfsdfsdf'
67+
expect do
68+
OctocatalogDiff::CatalogUtil::FileResources.file_path(tries, ['/a'])
69+
end.to raise_error(ArgumentError, /Bad parameter source/)
70+
end
71+
72+
it 'should raise an error if the all entries are malformed' do
73+
tries = %w[sddfsdfsdf asdfasfdasdf]
74+
expect do
75+
OctocatalogDiff::CatalogUtil::FileResources.file_path(tries, ['/a'])
76+
end.to raise_error(ArgumentError, /Bad parameter source/)
77+
end
3578
end
3679

3780
describe '#module_path' do

0 commit comments

Comments
 (0)