Skip to content

Commit a64adc3

Browse files
committed
Support catalog saving
1 parent 0e61be8 commit a64adc3

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lib/octocatalog-diff/util/catalogs.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ def add_parallel_result(result, parallel_catalog_obj, key_task_tuple)
197197
if catalog.valid?
198198
# The catalog was successfully compiled.
199199
result[key] = parallel_catalog_obj.output
200+
201+
if task.args[:save_catalog]
202+
File.open(task.args[:save_catalog], 'w') { |f| f.write(catalog.catalog_json) }
203+
@logger.debug "Saved catalog to #{task.args[:save_catalog]}"
204+
end
200205
else
201206
# The catalog failed, but a catalog object was returned so that better error reporting
202207
# can take place. In this error reporting, we will replace 'Error:' with '[Puppet Error]'

spec/octocatalog-diff/tests/util/catalogs_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,27 @@ def valid?
330330
expect(result[:from]).to eq('blank')
331331
end
332332

333+
context 'saving the catalog' do
334+
before(:each) { @tmpdir = Dir.mktmpdir }
335+
after(:each) { OctocatalogDiff::Spec.clean_up_tmpdir(@tmpdir) }
336+
337+
it 'should save the catalog when requested' do
338+
filename = File.join(@tmpdir, 'catalog.json')
339+
options = {
340+
to_catalog: OctocatalogDiff::Spec.fixture_path('catalogs/tiny-catalog.json'),
341+
from_catalog: OctocatalogDiff::Spec.fixture_path('catalogs/catalog-test-file.json'),
342+
from_save_catalog: filename
343+
}
344+
345+
logger, logger_str = OctocatalogDiff::Spec.setup_logger
346+
testobj = OctocatalogDiff::Util::Catalogs.new(options, logger)
347+
result = testobj.send(:build_catalog_parallelizer)
348+
349+
expect(logger_str.string).to match(Regexp.escape("Saved catalog to #{filename}"))
350+
expect(File.read(filename)).to eq(result[:from].catalog_json)
351+
end
352+
end
353+
333354
it 'should format error and raise error when a catalog compile fails' do
334355
error_lines = [
335356
'Debug: You do not care about this',

0 commit comments

Comments
 (0)