Skip to content

Commit 0e61be8

Browse files
committed
Add --to-save-catalog and --from-save-catalog
1 parent b706d69 commit 0e61be8

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-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
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

0 commit comments

Comments
 (0)