Skip to content

Commit 510b6b4

Browse files
authored
Merge pull request #91 from github/kpaulisse-no-truncate-details
Add option not to truncate details
2 parents 9a95923 + d21c96c commit 510b6b4

File tree

9 files changed

+197
-40
lines changed

9 files changed

+197
-40
lines changed

.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.1
1+
1.0.2

doc/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
</tr>
99
</thead><tbody>
1010
<tr valign=top>
11+
<td>1.0.2</td>
12+
<td>2017-03-08</td>
13+
<td>
14+
<li><a href="https://github.com/github/octocatalog-diff/pull/91">#91</a>: `--no-truncate-details` option</li>
15+
</td>
16+
</tr>
17+
<tr valign=top>
1118
<td>1.0.1</td>
1219
<td>2017-02-14</td>
1320
<td>

doc/optionsref.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Usage: octocatalog-diff [command line options]
5959
--from-enc PATH Path to ENC script (for the from catalog only)
6060
--to-enc PATH Path to ENC script (for the to catalog only)
6161
--[no-]display-detail-add Display parameters and other details for added resources
62+
--[no-]truncate-details Truncate details with --display-detail-add
6263
--no-header Do not print a header
6364
--default-header Print default header with output
6465
--header STRING Specify header for output
@@ -1092,6 +1093,21 @@ These files must exist and be in Puppet catalog format. (<a href="../lib/octocat
10921093
</td>
10931094
</tr>
10941095

1096+
<tr>
1097+
<td valign=top>
1098+
<pre><code>--truncate-details
1099+
--no-truncate-details </code></pre>
1100+
</td>
1101+
<td valign=top>
1102+
Truncate details with --display-detail-add
1103+
</td>
1104+
<td valign=top>
1105+
When using `--display-detail-add` by default the details of any field will be truncated
1106+
at 80 characters. Specify `--no-truncate-details` to display the full output. This option
1107+
has no effect when `--display-detail-add` is not used. (<a href="../lib/octocatalog-diff/cli/options/truncate_details.rb">truncate_details.rb</a>)
1108+
</td>
1109+
</tr>
1110+
10951111
<tr>
10961112
<td valign=top>
10971113
<pre><code>--validate-references

lib/octocatalog-diff/catalog-diff/display.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def self.output(diff_in, options = {}, logger = nil)
3737
opts[:compilation_from_dir] = options[:compilation_from_dir] || nil
3838
opts[:compilation_to_dir] = options[:compilation_to_dir] || nil
3939
opts[:display_detail_add] = options.fetch(:display_detail_add, false)
40+
opts[:truncate_details] = options.fetch(:truncate_details, true)
4041
opts[:display_datatype_changes] = options.fetch(:display_datatype_changes, false)
4142

4243
# Call appropriate display method

lib/octocatalog-diff/catalog-diff/display/text.rb

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,14 @@ def self.display_added_item(opts = {})
168168
result = []
169169
add_source_file_line_info(item: item, result: result, new_loc: new_loc, options: options, logger: logger)
170170
if options[:display_detail_add] && diff.key?('parameters')
171+
limit = options.fetch(:truncate_details, true) ? 80 : nil
171172
result << "+ #{item} =>".green
172173
result << ' parameters =>'.green
173174
result.concat(
174175
diff_two_hashes_with_diffy(
175176
depth: 1,
176177
hash2: Hash[diff['parameters'].sort], # Should work with somewhat older rubies too
177-
limit: 80,
178+
limit: limit,
178179
strip_diff: true
179180
).map(&:green)
180181
)
@@ -326,14 +327,17 @@ def self.make_trailing_whitespace_visible(string_in)
326327
# @param depth [Fixnum] Depth, for correct indentation
327328
# @param limit [Fixnum] Maximum string length
328329
# @param strip_diff [Boolean] Strip leading +/-/" "
329-
# @return Array<String> Displayable result
330+
# @return [Array<String>] Displayable result
330331
def self.diff_two_hashes_with_diffy(opts = {})
331332
depth = opts.fetch(:depth, 0)
332333
hash1 = opts.fetch(:hash1, {})
333334
hash2 = opts.fetch(:hash2, {})
334335
limit = opts[:limit]
335336
strip_diff = opts.fetch(:strip_diff, false)
336337

338+
# Special case: addition only, no truncation
339+
return addition_only_no_truncation(depth, hash2) if hash1 == {} && limit.nil?
340+
337341
json_old = stringify_for_diffy(hash1)
338342
json_new = stringify_for_diffy(hash2)
339343

@@ -353,6 +357,30 @@ def self.diff_two_hashes_with_diffy(opts = {})
353357
end
354358
end
355359

360+
# Special case: addition only, no truncation
361+
# @param depth [Fixnum] Depth, for correct indentation
362+
# @param hash [Hash] Added object
363+
# @return [Array<String>] Displayable result
364+
def self.addition_only_no_truncation(depth, hash)
365+
result = []
366+
367+
# Single line strings
368+
hash.keys.sort.map do |key|
369+
next if hash[key] =~ /\n/
370+
result << left_pad(2 * depth + 4, [key.inspect, ': ', hash[key].inspect].join('')).green
371+
end
372+
373+
# Multi-line strings
374+
hash.keys.sort.map do |key|
375+
next if hash[key] !~ /\n/
376+
result << left_pad(2 * depth + 4, [key.inspect, ': >>>'].join('')).green
377+
result.concat hash[key].split(/\n/).map(&:green)
378+
result << '<<<'.green
379+
end
380+
381+
result
382+
end
383+
356384
# Limit length of a string
357385
# @param str [String] String
358386
# @param limit [Fixnum] Limit (0=unlimited)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
# When using `--display-detail-add` by default the details of any field will be truncated
4+
# at 80 characters. Specify `--no-truncate-details` to display the full output. This option
5+
# has no effect when `--display-detail-add` is not used.
6+
# @param parser [OptionParser object] The OptionParser argument
7+
# @param options [Hash] Options hash being constructed; this is modified in this method.
8+
OctocatalogDiff::Cli::Options::Option.newoption(:truncate_details) do
9+
has_weight 251
10+
11+
def parse(parser, options)
12+
parser.on('--[no-]truncate-details', 'Truncate details with --display-detail-add') do |x|
13+
options[:truncate_details] = x
14+
end
15+
end
16+
end

spec/octocatalog-diff/integration/outputs_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,20 @@
160160
expect(result[:exitcode]).to eq(2), OctocatalogDiff::Integration.format_exception(result)
161161
expect(result[:output]).to match(/\+ Package\[ruby1.8-dev\] =>/)
162162
expect(result[:output]).to match(/"new-parameter": "new value"/)
163+
expect(result[:output]).to match(/THIS FILE IS AUTOMATICALLY DISTRIBUTED BY PUPPET. ANY LOCAL CH\.\.\./)
164+
end
165+
166+
it 'should display detail without truncation' do
167+
argv = [
168+
'--from-catalog', OctocatalogDiff::Spec.fixture_path('catalogs/catalog-empty.json'),
169+
'--to-catalog', OctocatalogDiff::Spec.fixture_path('catalogs/catalog-2.json'),
170+
'--display-detail-add', '--no-color', '--no-truncate-details'
171+
]
172+
result = OctocatalogDiff::Integration.integration(argv: argv)
173+
expect(result[:exitcode]).to eq(2), OctocatalogDiff::Integration.format_exception(result)
174+
expect(result[:output]).to match(/\+ Package\[ruby1.8-dev\] =>/)
175+
expect(result[:output]).to match(/"new-parameter": "new value"/)
176+
expect(result[:output]).to match(/THIS FILE IS AUTOMATICALLY DISTRIBUTED BY PUPPET. ANY LOCAL CHANGES/)
163177
end
164178

165179
it 'should not display file source and line' do

spec/octocatalog-diff/tests/catalog-diff/display/text_spec.rb

Lines changed: 103 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -241,50 +241,116 @@
241241
end
242242
end
243243

244-
context 'with display_detail_add on' do
245-
describe '#generate' do
246-
before(:all) do
247-
diff = [
248-
[
249-
'+',
250-
'File[/tmp/foo]',
251-
{
252-
'type' => 'File',
253-
'title' => '/tmp/foo',
254-
'parameters' => {
255-
'mode' => '0644',
256-
'content' => 'x' * 150,
257-
'owner' => 'root',
258-
'group' => 'wheel'
259-
}
244+
context 'with display_detail_add' do
245+
before(:all) do
246+
@diff = [
247+
[
248+
'+',
249+
'File[/tmp/foo]',
250+
{
251+
'type' => 'File',
252+
'title' => '/tmp/foo',
253+
'parameters' => {
254+
'mode' => '0644',
255+
'content' => 'x' * 150,
256+
'owner' => 'root',
257+
'group' => 'wheel'
260258
}
261-
]
259+
}
262260
]
263-
@result = OctocatalogDiff::CatalogDiff::Display::Text.generate(diff, display_detail_add: true, color: false)
264-
end
261+
]
262+
end
265263

266-
it 'should display parameters when display_detail_add is true' do
267-
expect(@result[1]).to match(/^\s+parameters =>/)
264+
context 'with --no-truncate-details' do
265+
describe '#generate' do
266+
before(:all) do
267+
@result = OctocatalogDiff::CatalogDiff::Display::Text.generate(
268+
@diff,
269+
display_detail_add: true,
270+
color: false,
271+
truncate_details: false
272+
)
273+
end
274+
275+
it 'should not truncate long strings' do
276+
expect(@result[2]).to match(/^\s+"content": /)
277+
expect(@result[2].length).to eq(169), "Wrong line length for: '#{@result[2]}': #{@result[2].length}"
278+
end
268279
end
280+
end
269281

270-
it 'should truncate long strings' do
271-
expect(@result[2]).to match(/^\s+"content": /)
272-
# Desired line length is 84 because the '..."' adds 4 characters to the truncated length of 80
273-
expect(@result[2].length).to eq(84), "Wrong line length for: '#{@result[2]}'"
282+
context 'with --no-truncate-details and a multi-line string' do
283+
describe '#generate' do
284+
before(:all) do
285+
diff = [
286+
[
287+
'+',
288+
'File[/tmp/foo]',
289+
{
290+
'type' => 'File',
291+
'title' => '/tmp/foo',
292+
'parameters' => {
293+
'mode' => '0644',
294+
'content' => "foo\nbar\nbaz",
295+
'owner' => 'root',
296+
'group' => 'wheel'
297+
}
298+
}
299+
]
300+
]
301+
@result = OctocatalogDiff::CatalogDiff::Display::Text.generate(
302+
diff,
303+
display_detail_add: true,
304+
color: false,
305+
truncate_details: false
306+
)
307+
end
308+
309+
it 'should sort keys without newlines before keys with newlines' do
310+
expect(@result[2]).to match(/^\s+"group": /)
311+
expect(@result[3]).to match(/^\s+"mode": /)
312+
expect(@result[4]).to match(/^\s+"owner": /)
313+
end
314+
315+
it 'should display lines on their own' do
316+
expect(@result[5]).to match(/^\s+"content": >>>/)
317+
expect(@result[6]).to eq('foo')
318+
expect(@result[7]).to eq('bar')
319+
expect(@result[8]).to eq('baz')
320+
expect(@result[9]).to eq('<<<')
321+
end
274322
end
323+
end
275324

276-
it 'should sort keys in parameters hash' do
277-
index_content = @result.find_index { |x| x =~ /^\s+"content": / }
278-
expect(index_content).not_to be(nil), 'Results missing "content"'
279-
index_group = @result.find_index { |x| x =~ /^\s+"group": / }
280-
expect(index_group).not_to be(nil), 'Results missing "group"'
281-
index_mode = @result.find_index { |x| x =~ /^\s+"mode": / }
282-
expect(index_mode).not_to be(nil), 'Results missing "mode"'
283-
index_owner = @result.find_index { |x| x =~ /^\s+"owner": / }
284-
expect(index_owner).not_to be(nil), 'Results missing "owner"'
285-
expect(index_content).to be < index_group
286-
expect(index_group).to be < index_mode
287-
expect(index_mode).to be < index_owner
325+
context 'without --no-truncate-details' do
326+
describe '#generate' do
327+
before(:all) do
328+
@result = OctocatalogDiff::CatalogDiff::Display::Text.generate(@diff, display_detail_add: true, color: false)
329+
end
330+
331+
it 'should display parameters when display_detail_add is true' do
332+
expect(@result[1]).to match(/^\s+parameters =>/)
333+
end
334+
335+
it 'should truncate long strings' do
336+
expect(@result[2]).to match(/^\s+"content": /)
337+
# Desired line length is 84 because the '..."' adds 4 characters to the truncated length of 80
338+
expect(@result[2].length).to eq(84), "Wrong line length for: '#{@result[2]}'"
339+
end
340+
341+
it 'should sort keys in parameters hash' do
342+
index_content = @result.find_index { |x| x =~ /^\s+"content": / }
343+
expect(index_content).not_to be(nil), 'Results missing "content"'
344+
index_group = @result.find_index { |x| x =~ /^\s+"group": / }
345+
expect(index_group).not_to be(nil), 'Results missing "group"'
346+
index_mode = @result.find_index { |x| x =~ /^\s+"mode": / }
347+
expect(index_mode).not_to be(nil), 'Results missing "mode"'
348+
index_owner = @result.find_index { |x| x =~ /^\s+"owner": / }
349+
expect(index_owner).not_to be(nil), 'Results missing "owner"'
350+
expect(index_content).to be < index_group
351+
expect(index_group).to be < index_mode
352+
expect(index_mode).to be < index_owner
353+
end
288354
end
289355
end
290356
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../options_helper'
4+
5+
describe OctocatalogDiff::Cli::Options do
6+
describe '#opt_truncate_details' do
7+
include_examples 'true/false option', 'truncate-details', :truncate_details
8+
end
9+
end

0 commit comments

Comments
 (0)