Skip to content

Commit 8477f1b

Browse files
author
Kevin Paulisse
committed
Implement --display-detail-add + --no-truncate-details
1 parent 1cdeffa commit 8477f1b

File tree

2 files changed

+64
-40
lines changed
  • lib/octocatalog-diff/catalog-diff/display
  • spec/octocatalog-diff/tests/catalog-diff/display

2 files changed

+64
-40
lines changed

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

Lines changed: 2 additions & 1 deletion
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
)

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

Lines changed: 62 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -241,50 +241,73 @@
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
265-
266-
it 'should display parameters when display_detail_add is true' do
267-
expect(@result[1]).to match(/^\s+parameters =>/)
268-
end
261+
]
262+
end
269263

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]}'"
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
274279
end
280+
end
275281

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
282+
context 'without --no-truncate-details' do
283+
describe '#generate' do
284+
before(:all) do
285+
@result = OctocatalogDiff::CatalogDiff::Display::Text.generate(@diff, display_detail_add: true, color: false)
286+
end
287+
288+
it 'should display parameters when display_detail_add is true' do
289+
expect(@result[1]).to match(/^\s+parameters =>/)
290+
end
291+
292+
it 'should truncate long strings' do
293+
expect(@result[2]).to match(/^\s+"content": /)
294+
# Desired line length is 84 because the '..."' adds 4 characters to the truncated length of 80
295+
expect(@result[2].length).to eq(84), "Wrong line length for: '#{@result[2]}'"
296+
end
297+
298+
it 'should sort keys in parameters hash' do
299+
index_content = @result.find_index { |x| x =~ /^\s+"content": / }
300+
expect(index_content).not_to be(nil), 'Results missing "content"'
301+
index_group = @result.find_index { |x| x =~ /^\s+"group": / }
302+
expect(index_group).not_to be(nil), 'Results missing "group"'
303+
index_mode = @result.find_index { |x| x =~ /^\s+"mode": / }
304+
expect(index_mode).not_to be(nil), 'Results missing "mode"'
305+
index_owner = @result.find_index { |x| x =~ /^\s+"owner": / }
306+
expect(index_owner).not_to be(nil), 'Results missing "owner"'
307+
expect(index_content).to be < index_group
308+
expect(index_group).to be < index_mode
309+
expect(index_mode).to be < index_owner
310+
end
288311
end
289312
end
290313
end

0 commit comments

Comments
 (0)