Skip to content

Commit aedee64

Browse files
author
Kevin Paulisse
committed
Add output format option for legacy_json
1 parent 36c9c20 commit aedee64

4 files changed

Lines changed: 51 additions & 7 deletions

File tree

doc/optionsref.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Usage: octocatalog-diff [command line options]
2727
--bootstrap-then-exit Bootstrap from-dir and/or to-dir and then exit
2828
--[no-]color Enable/disable colors in output
2929
-o, --output-file FILENAME Output results into FILENAME
30-
--output-format FORMAT Output format: text,json
30+
--output-format FORMAT Output format: text,json,legacy_json
3131
-d, --[no-]debug Print debugging messages to STDERR
3232
-q, --[no-]quiet Quiet (no status messages except errors)
3333
--ignore "Type1[Title1],Type2[Title2],..."
@@ -708,10 +708,12 @@ to ignore any changes for any defined type where this tag is set. (<a href="../l
708708
<pre><code>--output-format FORMAT</code></pre>
709709
</td>
710710
<td valign=top>
711-
Output format: text,json
711+
Output format: text,json,legacy_json
712712
</td>
713713
<td valign=top>
714-
Output format option (<a href="../lib/octocatalog-diff/cli/options/output_format.rb">output_format.rb</a>)
714+
Output format option. 'text' is human readable text, 'json' is an array of differences
715+
identified by human readable keys (the preferred octocatalog-diff 1.x format), and 'legacy_json' is an
716+
array of differences, where each difference is an array (the octocatalog-diff 0.x format). (<a href="../lib/octocatalog-diff/cli/options/output_format.rb">output_format.rb</a>)
715717
</td>
716718
</tr>
717719

lib/octocatalog-diff/cli/options/output_format.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# frozen_string_literal: true
22

3-
# Output format option
3+
# Output format option. 'text' is human readable text, 'json' is an array of differences
4+
# identified by human readable keys (the preferred octocatalog-diff 1.x format), and 'legacy_json' is an
5+
# array of differences, where each difference is an array (the octocatalog-diff 0.x format).
46
# @param parser [OptionParser object] The OptionParser argument
57
# @param options [Hash] Options hash being constructed; this is modified in this method.
68
OctocatalogDiff::Cli::Options::Option.newoption(:output_format) do
79
has_weight 100
810

911
def parse(parser, options)
10-
valid = %w(text json)
12+
valid = %w(text json legacy_json)
1113
parser.on('--output-format FORMAT', "Output format: #{valid.join(',')}") do |fmt|
1214
raise ArgumentError, "Invalid format. Must be one of: #{valid.join(',')}" unless valid.include?(fmt)
1315
options[:format] = fmt.to_sym

spec/octocatalog-diff/integration/outputs_spec.rb

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
expect(result[:output]).to match(pattern)
5252
end
5353

54-
it 'should write JSON output to a specified file' do
54+
it 'should write 1.x JSON output to a specified file' do
5555
output_file = File.join(@tmpdir, 'octo-output.json')
5656
argv = default_argv.concat ['-o', output_file, '--output-format', 'json']
5757
result = OctocatalogDiff::Integration.integration(argv: argv)
@@ -63,6 +63,46 @@
6363
expect(data).to be_a_kind_of(Hash)
6464
expect(data['header']).to eq(nil)
6565
expect(data['diff']).to be_a_kind_of(Array)
66+
67+
answer = {
68+
'diff_type' => '~',
69+
'type' => 'File',
70+
'title' => '/usr/bin/node-waf',
71+
'structure' => %w(parameters ensure),
72+
'old_value' => '/usr/share/nvm/0.8.11/bin/node-waf',
73+
'new_value' => 'link',
74+
'old_file' => '/environments/production/modules/nodejs/manifests/init.pp',
75+
'old_line' => 55,
76+
'new_file' => '/environments/production/modules/nodejs/manifests/init.pp',
77+
'new_line' => 55,
78+
'old_location' => { 'file' => '/environments/production/modules/nodejs/manifests/init.pp', 'line' => 55 },
79+
'new_location' => { 'file' => '/environments/production/modules/nodejs/manifests/init.pp', 'line' => 55 }
80+
}
81+
expect(data['diff']).to include(answer)
82+
end
83+
84+
it 'should write 0.x JSON output to a specified file' do
85+
output_file = File.join(@tmpdir, 'octo-output.json')
86+
argv = default_argv.concat ['-o', output_file, '--output-format', 'legacy_json']
87+
result = OctocatalogDiff::Integration.integration(argv: argv)
88+
expect(result[:exitcode]).to eq(2), OctocatalogDiff::Integration.format_exception(result)
89+
expect(result[:logs]).to match(/Wrote diff to #{Regexp.escape(output_file)}/)
90+
expect(File.file?(output_file)).to eq(true)
91+
content = File.read(output_file)
92+
data = JSON.parse(content)
93+
expect(data).to be_a_kind_of(Hash)
94+
expect(data['header']).to eq(nil)
95+
expect(data['diff']).to be_a_kind_of(Array)
96+
97+
answer = [
98+
'!',
99+
"File\f/usr/bin/npm\fparameters\ftarget",
100+
'/usr/share/nvm/0.8.11/bin/npm',
101+
nil,
102+
{ 'file' => '/environments/production/modules/nodejs/manifests/init.pp', 'line' => 46 },
103+
{ 'file' => '/environments/production/modules/nodejs/manifests/init.pp', 'line' => 46 }
104+
]
105+
expect(data['diff']).to include(answer)
66106
end
67107

68108
it 'should write JSON output to the screen' do

spec/octocatalog-diff/tests/cli/options/output_format_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
describe OctocatalogDiff::Cli::Options do
66
describe '#opt_output_format' do
7-
valid = %w(text json)
7+
valid = %w(text json legacy_json)
88
valid.each do |fmt|
99
it "should set output format to #{fmt}" do
1010
result = run_optparse(['--output-format', fmt])

0 commit comments

Comments
 (0)