Skip to content

Commit 5c48f53

Browse files
author
Kevin Paulisse
committed
Allow convert_file_resources to work even in absence of environments/production
1 parent 884a6f6 commit 5c48f53

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ def self._convert_file_resources(resources, compilation_dir)
2929
# Calculate compilation directory. There is not explicit error checking here because
3030
# there is on-demand, explicit error checking for each file within the modification loop.
3131
return unless compilation_dir.is_a?(String) && compilation_dir != ''
32+
33+
# Making sure that compilation_dir/environments/production exists. Otherwise, try to find
34+
# compilation_dir/modules. If neither of those exist, this code can't run.
3235
env_dir = File.join(compilation_dir, 'environments', 'production')
36+
unless File.directory?(File.join(env_dir, 'modules'))
37+
return unless File.directory?(File.join(compilation_dir, 'modules'))
38+
env_dir = compilation_dir
39+
end
3340

3441
# Modify the resources
3542
resources.map! do |resource|

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,43 @@ def catalog_from_fixture(path)
2323
FileUtils.remove_entry_secure @tmpdir if File.directory?(@tmpdir)
2424
end
2525

26+
it 'should use compilation directory if environments/production is unavailable' do
27+
FileUtils.rm_f File.join(@tmpdir, 'environments', 'production')
28+
29+
# Set up test
30+
obj = catalog_from_fixture('catalogs/catalog-test-file-v4.json')
31+
obj.compilation_dir = @tmpdir
32+
resources_save = obj.resources.dup
33+
34+
# Perform test
35+
OctocatalogDiff::CatalogUtil::FileResources.convert_file_resources(obj)
36+
expect(obj.resources).to be_a_kind_of(Array), obj.resources.to_json
37+
expect(obj.resources.size).to eq(3), obj.resources.to_json
38+
expect(obj.resources[0]).to eq(resources_save[0]), obj.resources.to_json
39+
expect(obj.resources[1]).to eq(resources_save[1]), obj.resources.to_json
40+
expect(obj.resources[2]['parameters']['content']).to eq("foo\n"), obj.resources.to_json
41+
expect(obj.resources[2]['parameters'].key?('source')).to eq(false), obj.resources.to_json
42+
43+
# Make sure the symlink isn't there, i.e. that we actually tested this
44+
expect(File.exist?(File.join(@tmpdir, 'environments', 'production'))).to eq(false)
45+
end
46+
47+
it 'should not run at all if modules directory cannot be found' do
48+
module_dir = File.join(@tmpdir, 'modules')
49+
FileUtils.remove_entry_secure module_dir if File.directory?(module_dir)
50+
51+
# Set up test
52+
obj = catalog_from_fixture('catalogs/catalog-test-file-v4.json')
53+
obj.compilation_dir = @tmpdir
54+
55+
# Perform test
56+
OctocatalogDiff::CatalogUtil::FileResources.convert_file_resources(obj)
57+
expect(obj.resources).to be_a_kind_of(Array), obj.resources.to_json
58+
expect(obj.resources.size).to eq(3), obj.resources.to_json
59+
expect(obj.resources[2]['parameters']['source']).to eq('puppet:///modules/test/tmp/foo')
60+
expect(obj.resources[2]['parameters'].key?('content')).to eq(false), obj.resources.to_json
61+
end
62+
2663
it 'should modify array with convertible resources' do
2764
# Set up test
2865
obj = catalog_from_fixture('catalogs/catalog-test-file-v4.json')

0 commit comments

Comments
 (0)