Skip to content

Commit 43f8400

Browse files
author
Kevin Paulisse
committed
Add handling of multi-line strings
1 parent 9ffde84 commit 43f8400

File tree

2 files changed

+47
-6
lines changed
  • lib/octocatalog-diff/catalog-diff/display
  • spec/octocatalog-diff/tests/catalog-diff/display

2 files changed

+47
-6
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def self.diff_two_hashes_with_diffy(opts = {})
336336
strip_diff = opts.fetch(:strip_diff, false)
337337

338338
# Special case: addition only, no truncation
339-
return addition_only_no_truncation(depth, hash2, strip_diff) if hash1 == {} && limit.nil?
339+
return addition_only_no_truncation(depth, hash2) if hash1 == {} && limit.nil?
340340

341341
json_old = stringify_for_diffy(hash1)
342342
json_new = stringify_for_diffy(hash2)
@@ -360,22 +360,20 @@ def self.diff_two_hashes_with_diffy(opts = {})
360360
# Special case: addition only, no truncation
361361
# @param depth [Fixnum] Depth, for correct indentation
362362
# @param hash [Hash] Added object
363-
# @param strip_diff [Boolean] Strip leading +/-/" "
364363
# @return [Array<String>] Displayable result
365-
def self.addition_only_no_truncation(depth, hash, strip_diff)
366-
plus = strip_diff ? '' : '+ '
364+
def self.addition_only_no_truncation(depth, hash)
367365
result = []
368366

369367
# Single line strings
370368
hash.keys.sort.map do |key|
371369
next if hash[key] =~ /\n/
372-
result << left_pad(2 * depth + 2, [plus, key.inspect, ': ', hash[key].inspect].join('')).green
370+
result << left_pad(2 * depth + 4, [key.inspect, ': ', hash[key].inspect].join('')).green
373371
end
374372

375373
# Multi-line strings
376374
hash.keys.sort.map do |key|
377375
next if hash[key] !~ /\n/
378-
result << left_pad(2 * depth + 2, [plus, key.inspect, ': >>>'].join('')).green
376+
result << left_pad(2 * depth + 4, [key.inspect, ': >>>'].join('')).green
379377
result.concat hash[key].split(/\n/).map(&:green)
380378
result << '<<<'
381379
end

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,49 @@
279279
end
280280
end
281281

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
322+
end
323+
end
324+
282325
context 'without --no-truncate-details' do
283326
describe '#generate' do
284327
before(:all) do

0 commit comments

Comments
 (0)