Skip to content

Commit 1e309be

Browse files
authored
Merge pull request #126 from github/kpaulisse-save-catalog
Add option to save catalogs when catalog-diff'ing
2 parents 23525be + 13b7c6e commit 1e309be

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
# Allow catalogs to be saved to a file before they are diff'd.
4+
# @param parser [OptionParser object] The OptionParser argument
5+
# @param options [Hash] Options hash being constructed; this is modified in this method.
6+
OctocatalogDiff::Cli::Options::Option.newoption(:save_catalog) do
7+
has_weight 155
8+
9+
def parse(parser, options)
10+
OctocatalogDiff::Cli::Options.option_globally_or_per_branch(
11+
parser: parser,
12+
options: options,
13+
cli_name: 'save-catalog',
14+
option_name: 'save_catalog',
15+
desc: 'Save intermediate catalogs into files',
16+
datatype: '',
17+
validator: lambda do |catalog_file|
18+
target_dir = File.dirname(catalog_file)
19+
unless File.directory?(target_dir)
20+
raise Errno::ENOENT, "Cannot save catalog to #{catalog_file} because parent directory does not exist"
21+
end
22+
if File.exist?(catalog_file) && !File.file?(catalog_file)
23+
raise ArgumentError, "Cannot overwrite #{catalog_file} which is not a file"
24+
end
25+
true
26+
end,
27+
post_process: lambda do |opts|
28+
if opts[:to_save_catalog] && opts[:to_save_catalog] == opts[:from_save_catalog]
29+
raise ArgumentError, 'Cannot use the same file for --to-save-catalog and --from-save-catalog'
30+
end
31+
end
32+
)
33+
end
34+
end

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]'
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../options_helper'
4+
5+
describe OctocatalogDiff::Cli::Options do
6+
describe '#opt_save_catalog' do
7+
it 'should raise error if catalog parent directory does not exist' do
8+
args = [
9+
'--to-save-catalog',
10+
OctocatalogDiff::Spec.fixture_path('non/exists/catalog.json')
11+
]
12+
expect { run_optparse(args) }.to raise_error(Errno::ENOENT, /parent directory does not exist/)
13+
end
14+
15+
it 'should raise error if catalog exists but is not a file' do
16+
args = [
17+
'--to-save-catalog',
18+
OctocatalogDiff::Spec.fixture_path('repos/default')
19+
]
20+
expect { run_optparse(args) }.to raise_error(ArgumentError, /Cannot overwrite/)
21+
end
22+
23+
it 'should not raise error if catalog file already exists' do
24+
args = [
25+
'--to-save-catalog',
26+
OctocatalogDiff::Spec.fixture_path('catalogs/catalog-1.json')
27+
]
28+
result = run_optparse(args)
29+
expect(result[:to_save_catalog]).to eq(OctocatalogDiff::Spec.fixture_path('catalogs/catalog-1.json'))
30+
end
31+
32+
it 'should not raise error if catalog file does not exist' do
33+
args = [
34+
'--to-save-catalog',
35+
OctocatalogDiff::Spec.fixture_path('catalogs/brand-new-catalog-1.json')
36+
]
37+
result = run_optparse(args)
38+
expect(result[:to_save_catalog]).to eq(OctocatalogDiff::Spec.fixture_path('catalogs/brand-new-catalog-1.json'))
39+
end
40+
41+
it 'should raise error if --to-save-catalog == --from-save-catalog' do
42+
args = [
43+
'--to-save-catalog',
44+
OctocatalogDiff::Spec.fixture_path('catalogs/brand-new-catalog-1.json'),
45+
'--from-save-catalog',
46+
OctocatalogDiff::Spec.fixture_path('catalogs/brand-new-catalog-1.json')
47+
]
48+
expect { run_optparse(args) }.to raise_error(ArgumentError, /Cannot use the same file for/)
49+
end
50+
51+
it 'should not raise error if --to-save-catalog and --from-save-catalog are both nil' do
52+
args = []
53+
result = run_optparse(args)
54+
expect(result[:to_save_catalog]).to be_nil
55+
expect(result[:from_save_catalog]).to be_nil
56+
end
57+
58+
it 'should set option correctly' do
59+
args = [
60+
'--to-save-catalog',
61+
OctocatalogDiff::Spec.fixture_path('catalogs/brand-new-catalog-1.json'),
62+
'--from-save-catalog',
63+
OctocatalogDiff::Spec.fixture_path('catalogs/brand-new-catalog-2.json')
64+
]
65+
result = run_optparse(args)
66+
expect(result[:to_save_catalog]).to eq(OctocatalogDiff::Spec.fixture_path('catalogs/brand-new-catalog-1.json'))
67+
expect(result[:from_save_catalog]).to eq(OctocatalogDiff::Spec.fixture_path('catalogs/brand-new-catalog-2.json'))
68+
end
69+
end
70+
end

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)