Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.1
1.0.2
7 changes: 7 additions & 0 deletions doc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
</tr>
</thead><tbody>
<tr valign=top>
<td>1.0.2</td>
<td>2017-03-08</td>
<td>
<li><a href="https://github.com/github/octocatalog-diff/pull/91">#91</a>: `--no-truncate-details` option</li>
</td>
</tr>
<tr valign=top>
<td>1.0.1</td>
<td>2017-02-14</td>
<td>
Expand Down
16 changes: 16 additions & 0 deletions doc/optionsref.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Usage: octocatalog-diff [command line options]
--from-enc PATH Path to ENC script (for the from catalog only)
--to-enc PATH Path to ENC script (for the to catalog only)
--[no-]display-detail-add Display parameters and other details for added resources
--[no-]truncate-details Truncate details with --display-detail-add
--no-header Do not print a header
--default-header Print default header with output
--header STRING Specify header for output
Expand Down Expand Up @@ -1092,6 +1093,21 @@ These files must exist and be in Puppet catalog format. (<a href="../lib/octocat
</td>
</tr>

<tr>
<td valign=top>
<pre><code>--truncate-details
--no-truncate-details </code></pre>
</td>
<td valign=top>
Truncate details with --display-detail-add
</td>
<td valign=top>
When using `--display-detail-add` by default the details of any field will be truncated
at 80 characters. Specify `--no-truncate-details` to display the full output. This option
has no effect when `--display-detail-add` is not used. (<a href="../lib/octocatalog-diff/cli/options/truncate_details.rb">truncate_details.rb</a>)
</td>
</tr>

<tr>
<td valign=top>
<pre><code>--validate-references
Expand Down
1 change: 1 addition & 0 deletions lib/octocatalog-diff/catalog-diff/display.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def self.output(diff_in, options = {}, logger = nil)
opts[:compilation_from_dir] = options[:compilation_from_dir] || nil
opts[:compilation_to_dir] = options[:compilation_to_dir] || nil
opts[:display_detail_add] = options.fetch(:display_detail_add, false)
opts[:truncate_details] = options.fetch(:truncate_details, true)
opts[:display_datatype_changes] = options.fetch(:display_datatype_changes, false)

# Call appropriate display method
Expand Down
32 changes: 30 additions & 2 deletions lib/octocatalog-diff/catalog-diff/display/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,14 @@ def self.display_added_item(opts = {})
result = []
add_source_file_line_info(item: item, result: result, new_loc: new_loc, options: options, logger: logger)
if options[:display_detail_add] && diff.key?('parameters')
limit = options.fetch(:truncate_details, true) ? 80 : nil
result << "+ #{item} =>".green
result << ' parameters =>'.green
result.concat(
diff_two_hashes_with_diffy(
depth: 1,
hash2: Hash[diff['parameters'].sort], # Should work with somewhat older rubies too
limit: 80,
limit: limit,
strip_diff: true
).map(&:green)
)
Expand Down Expand Up @@ -326,14 +327,17 @@ def self.make_trailing_whitespace_visible(string_in)
# @param depth [Fixnum] Depth, for correct indentation
# @param limit [Fixnum] Maximum string length
# @param strip_diff [Boolean] Strip leading +/-/" "
# @return Array<String> Displayable result
# @return [Array<String>] Displayable result
def self.diff_two_hashes_with_diffy(opts = {})
depth = opts.fetch(:depth, 0)
hash1 = opts.fetch(:hash1, {})
hash2 = opts.fetch(:hash2, {})
limit = opts[:limit]
strip_diff = opts.fetch(:strip_diff, false)

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

json_old = stringify_for_diffy(hash1)
json_new = stringify_for_diffy(hash2)

Expand All @@ -353,6 +357,30 @@ def self.diff_two_hashes_with_diffy(opts = {})
end
end

# Special case: addition only, no truncation
# @param depth [Fixnum] Depth, for correct indentation
# @param hash [Hash] Added object
# @return [Array<String>] Displayable result
def self.addition_only_no_truncation(depth, hash)
result = []

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

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

result
end

# Limit length of a string
# @param str [String] String
# @param limit [Fixnum] Limit (0=unlimited)
Expand Down
16 changes: 16 additions & 0 deletions lib/octocatalog-diff/cli/options/truncate_details.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

# When using `--display-detail-add` by default the details of any field will be truncated
# at 80 characters. Specify `--no-truncate-details` to display the full output. This option
# has no effect when `--display-detail-add` is not used.
# @param parser [OptionParser object] The OptionParser argument
# @param options [Hash] Options hash being constructed; this is modified in this method.
OctocatalogDiff::Cli::Options::Option.newoption(:truncate_details) do
has_weight 251

def parse(parser, options)
parser.on('--[no-]truncate-details', 'Truncate details with --display-detail-add') do |x|
options[:truncate_details] = x
end
end
end
14 changes: 14 additions & 0 deletions spec/octocatalog-diff/integration/outputs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@
expect(result[:exitcode]).to eq(2), OctocatalogDiff::Integration.format_exception(result)
expect(result[:output]).to match(/\+ Package\[ruby1.8-dev\] =>/)
expect(result[:output]).to match(/"new-parameter": "new value"/)
expect(result[:output]).to match(/THIS FILE IS AUTOMATICALLY DISTRIBUTED BY PUPPET. ANY LOCAL CH\.\.\./)
end

it 'should display detail without truncation' do
argv = [
'--from-catalog', OctocatalogDiff::Spec.fixture_path('catalogs/catalog-empty.json'),
'--to-catalog', OctocatalogDiff::Spec.fixture_path('catalogs/catalog-2.json'),
'--display-detail-add', '--no-color', '--no-truncate-details'
]
result = OctocatalogDiff::Integration.integration(argv: argv)
expect(result[:exitcode]).to eq(2), OctocatalogDiff::Integration.format_exception(result)
expect(result[:output]).to match(/\+ Package\[ruby1.8-dev\] =>/)
expect(result[:output]).to match(/"new-parameter": "new value"/)
expect(result[:output]).to match(/THIS FILE IS AUTOMATICALLY DISTRIBUTED BY PUPPET. ANY LOCAL CHANGES/)
end

it 'should not display file source and line' do
Expand Down
140 changes: 103 additions & 37 deletions spec/octocatalog-diff/tests/catalog-diff/display/text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,50 +241,116 @@
end
end

context 'with display_detail_add on' do
describe '#generate' do
before(:all) do
diff = [
[
'+',
'File[/tmp/foo]',
{
'type' => 'File',
'title' => '/tmp/foo',
'parameters' => {
'mode' => '0644',
'content' => 'x' * 150,
'owner' => 'root',
'group' => 'wheel'
}
context 'with display_detail_add' do
before(:all) do
@diff = [
[
'+',
'File[/tmp/foo]',
{
'type' => 'File',
'title' => '/tmp/foo',
'parameters' => {
'mode' => '0644',
'content' => 'x' * 150,
'owner' => 'root',
'group' => 'wheel'
}
]
}
]
@result = OctocatalogDiff::CatalogDiff::Display::Text.generate(diff, display_detail_add: true, color: false)
end
]
end

it 'should display parameters when display_detail_add is true' do
expect(@result[1]).to match(/^\s+parameters =>/)
context 'with --no-truncate-details' do
describe '#generate' do
before(:all) do
@result = OctocatalogDiff::CatalogDiff::Display::Text.generate(
@diff,
display_detail_add: true,
color: false,
truncate_details: false
)
end

it 'should not truncate long strings' do
expect(@result[2]).to match(/^\s+"content": /)
expect(@result[2].length).to eq(169), "Wrong line length for: '#{@result[2]}': #{@result[2].length}"
end
end
end

it 'should truncate long strings' do
expect(@result[2]).to match(/^\s+"content": /)
# Desired line length is 84 because the '..."' adds 4 characters to the truncated length of 80
expect(@result[2].length).to eq(84), "Wrong line length for: '#{@result[2]}'"
context 'with --no-truncate-details and a multi-line string' do
describe '#generate' do
before(:all) do
diff = [
[
'+',
'File[/tmp/foo]',
{
'type' => 'File',
'title' => '/tmp/foo',
'parameters' => {
'mode' => '0644',
'content' => "foo\nbar\nbaz",
'owner' => 'root',
'group' => 'wheel'
}
}
]
]
@result = OctocatalogDiff::CatalogDiff::Display::Text.generate(
diff,
display_detail_add: true,
color: false,
truncate_details: false
)
end

it 'should sort keys without newlines before keys with newlines' do
expect(@result[2]).to match(/^\s+"group": /)
expect(@result[3]).to match(/^\s+"mode": /)
expect(@result[4]).to match(/^\s+"owner": /)
end

it 'should display lines on their own' do
expect(@result[5]).to match(/^\s+"content": >>>/)
expect(@result[6]).to eq('foo')
expect(@result[7]).to eq('bar')
expect(@result[8]).to eq('baz')
expect(@result[9]).to eq('<<<')
end
end
end

it 'should sort keys in parameters hash' do
index_content = @result.find_index { |x| x =~ /^\s+"content": / }
expect(index_content).not_to be(nil), 'Results missing "content"'
index_group = @result.find_index { |x| x =~ /^\s+"group": / }
expect(index_group).not_to be(nil), 'Results missing "group"'
index_mode = @result.find_index { |x| x =~ /^\s+"mode": / }
expect(index_mode).not_to be(nil), 'Results missing "mode"'
index_owner = @result.find_index { |x| x =~ /^\s+"owner": / }
expect(index_owner).not_to be(nil), 'Results missing "owner"'
expect(index_content).to be < index_group
expect(index_group).to be < index_mode
expect(index_mode).to be < index_owner
context 'without --no-truncate-details' do
describe '#generate' do
before(:all) do
@result = OctocatalogDiff::CatalogDiff::Display::Text.generate(@diff, display_detail_add: true, color: false)
end

it 'should display parameters when display_detail_add is true' do
expect(@result[1]).to match(/^\s+parameters =>/)
end

it 'should truncate long strings' do
expect(@result[2]).to match(/^\s+"content": /)
# Desired line length is 84 because the '..."' adds 4 characters to the truncated length of 80
expect(@result[2].length).to eq(84), "Wrong line length for: '#{@result[2]}'"
end

it 'should sort keys in parameters hash' do
index_content = @result.find_index { |x| x =~ /^\s+"content": / }
expect(index_content).not_to be(nil), 'Results missing "content"'
index_group = @result.find_index { |x| x =~ /^\s+"group": / }
expect(index_group).not_to be(nil), 'Results missing "group"'
index_mode = @result.find_index { |x| x =~ /^\s+"mode": / }
expect(index_mode).not_to be(nil), 'Results missing "mode"'
index_owner = @result.find_index { |x| x =~ /^\s+"owner": / }
expect(index_owner).not_to be(nil), 'Results missing "owner"'
expect(index_content).to be < index_group
expect(index_group).to be < index_mode
expect(index_mode).to be < index_owner
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require_relative '../options_helper'

describe OctocatalogDiff::Cli::Options do
describe '#opt_truncate_details' do
include_examples 'true/false option', 'truncate-details', :truncate_details
end
end