Skip to content

Commit 2c754e9

Browse files
committed
Add --[no-]use-lcs
Add a CLI option (--[no-]use-lcs) so the usage of the LCS algo when comparing arrays can be switched on and off. The default is to keep it on as it's has been so far. Closes #207
1 parent 8d4b413 commit 2c754e9

File tree

7 files changed

+81
-3
lines changed

7 files changed

+81
-3
lines changed

doc/optionsref.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Usage: octocatalog-diff [command line options]
7272
--from-enc PATH Path to ENC script (for the from catalog only)
7373
--to-enc PATH Path to ENC script (for the to catalog only)
7474
--[no-]display-detail-add Display parameters and other details for added resources
75+
--[no-]use-lcs Use the LCS algorithm to determine differences in arrays
7576
--[no-]truncate-details Truncate details with --display-detail-add
7677
--no-header Do not print a header
7778
--default-header Print default header with output

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,11 @@ def hashdiff_initial(catalog1_in, catalog2_in)
531531
catalog2_resources = catalog2_in[:catalog]
532532

533533
@logger.debug "Entering hashdiff_initial; catalog sizes: #{catalog1_resources.size}, #{catalog2_resources.size}"
534+
use_lcs = @opts.fetch(:use_lcs, true)
535+
@logger.debug "HashDiff configuration: (use_lcs: #{use_lcs})"
534536
result = []
535537
hashdiff_add_remove = Set.new
536-
hashdiff_result = HashDiff.diff(catalog1_resources, catalog2_resources, delimiter: "\f")
538+
hashdiff_result = HashDiff.diff(catalog1_resources, catalog2_resources, delimiter: "\f", use_lcs: use_lcs)
537539
hashdiff_result.each do |obj|
538540
# Regular change
539541
if obj[0] == '~'

lib/octocatalog-diff/cli.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class Cli
4444
display_datatype_changes: true,
4545
parallel: true,
4646
suppress_absent_file_details: true,
47-
hiera_path: 'hieradata'
47+
hiera_path: 'hieradata',
48+
use_lcs: true
4849
}.freeze
4950

5051
# This method is the one to call externally. It is possible to specify alternate
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
# Configures using the Longest common subsequence (LCS) algorithm to determine differences in arrays
4+
# @param parser [OptionParser object] The OptionParser argument
5+
# @param options [Hash] Options hash being constructed; this is modified in this method.
6+
OctocatalogDiff::Cli::Options::Option.newoption(:use_lcs) do
7+
has_weight 250
8+
9+
def parse(parser, options)
10+
parser.on('--[no-]use-lcs', 'Use the LCS algorithm to determine differences in arrays') do |x|
11+
options[:use_lcs] = x
12+
end
13+
end
14+
end

spec/octocatalog-diff/tests/catalog-diff/differ_spec.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,5 +1544,55 @@
15441544
end
15451545
end
15461546
end
1547+
1548+
context 'use_lcs is taken into account' do
1549+
describe '#ignore' do
1550+
before(:all) do
1551+
r1 = [
1552+
{
1553+
'type' => 'Example1', 'title' => 'main', 'tags' => ['stage'], 'exported' => false,
1554+
'parameters' => {
1555+
'name' => 'main', 'toplevel' => 'toplevel attribute',
1556+
'nest' => {
1557+
'toplevel' => 'toplevel_nest attribute',
1558+
'nest' => { 'nest' => 'nested nested text' },
1559+
'nest2' => { 'chicken' => 'egg' },
1560+
'chicken' => 'egg'
1561+
}
1562+
}
1563+
}
1564+
]
1565+
@c1 = OctocatalogDiff::Spec.build_catalog(r1)
1566+
@c2 = OctocatalogDiff::Spec.build_catalog(r1)
1567+
end
1568+
1569+
it 'should honor the algo configuration passed in the options (false)' do
1570+
logger, logger_str = OctocatalogDiff::Spec.setup_logger
1571+
opts = { use_lcs: false, logger: logger }
1572+
testobj = OctocatalogDiff::CatalogDiff::Differ.new(opts, @c1, @c2)
1573+
testobj.diff
1574+
expect(logger_str.string).to match(/Entering hashdiff_initial; catalog sizes: 1, 1/)
1575+
expect(logger_str.string).to match(/HashDiff configuration: \(use_lcs: false\)/)
1576+
end
1577+
1578+
it 'should honor the algo configuration passed in the options (true)' do
1579+
logger, logger_str = OctocatalogDiff::Spec.setup_logger
1580+
opts = { use_lcs: true, logger: logger }
1581+
testobj = OctocatalogDiff::CatalogDiff::Differ.new(opts, @c1, @c2)
1582+
testobj.diff
1583+
expect(logger_str.string).to match(/Entering hashdiff_initial; catalog sizes: 1, 1/)
1584+
expect(logger_str.string).to match(/HashDiff configuration: \(use_lcs: true\)/)
1585+
end
1586+
1587+
it 'the default value is true' do
1588+
logger, logger_str = OctocatalogDiff::Spec.setup_logger
1589+
opts = { logger: logger }
1590+
testobj = OctocatalogDiff::CatalogDiff::Differ.new(opts, @c1, @c2)
1591+
testobj.diff
1592+
expect(logger_str.string).to match(/Entering hashdiff_initial; catalog sizes: 1, 1/)
1593+
expect(logger_str.string).to match(/HashDiff configuration: \(use_lcs: true\)/)
1594+
end
1595+
end
1596+
end
15471597
end
15481598
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_use_lcs' do
7+
include_examples 'true/false option', 'use-lcs', :use_lcs
8+
end
9+
end

spec/octocatalog-diff/tests/cli_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
display_datatype_changes: true,
3838
parallel: true,
3939
suppress_absent_file_details: true,
40-
hiera_path: 'hieradata'
40+
hiera_path: 'hieradata',
41+
use_lcs: true
4142
}
4243
logger, _logger_str = OctocatalogDiff::Spec.setup_logger
4344
expect(described_class).to receive(:catalog_only).with(logger, answer)

0 commit comments

Comments
 (0)