Skip to content

Commit 089b699

Browse files
authored
Merge pull request #227 from cernops/issue207
Add --[no-]use-lcs
2 parents f5c1098 + 17bcef6 commit 089b699

File tree

7 files changed

+81
-3
lines changed

7 files changed

+81
-3
lines changed

doc/optionsref.md

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Usage: octocatalog-diff [command line options]
7474
--from-enc PATH Path to ENC script (for the from catalog only)
7575
--to-enc PATH Path to ENC script (for the to catalog only)
7676
--[no-]display-detail-add Display parameters and other details for added resources
77+
--[no-]use-lcs Use the LCS algorithm to determine differences in arrays
7778
--[no-]truncate-details Truncate details with --display-detail-add
7879
--no-header Do not print a header
7980
--default-header Print default header with output

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,11 @@ def hashdiff_initial(catalog1_in, catalog2_in)
535535
catalog2_resources = catalog2_in[:catalog]
536536

537537
@logger.debug "Entering hashdiff_initial; catalog sizes: #{catalog1_resources.size}, #{catalog2_resources.size}"
538+
use_lcs = @opts.fetch(:use_lcs, true)
539+
@logger.debug "HashDiff configuration: (use_lcs: #{use_lcs})"
538540
result = []
539541
hashdiff_add_remove = Set.new
540-
hashdiff_result = HashDiff.diff(catalog1_resources, catalog2_resources, delimiter: "\f")
542+
hashdiff_result = HashDiff.diff(catalog1_resources, catalog2_resources, delimiter: "\f", use_lcs: use_lcs)
541543
hashdiff_result.each do |obj|
542544
# Regular change
543545
if obj[0] == '~'

lib/octocatalog-diff/cli.rb

+2-1
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
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

+50
Original file line numberDiff line numberDiff line change
@@ -1611,5 +1611,55 @@
16111611
end
16121612
end
16131613
end
1614+
1615+
context 'use_lcs is taken into account' do
1616+
describe '#ignore' do
1617+
before(:all) do
1618+
r1 = [
1619+
{
1620+
'type' => 'Example1', 'title' => 'main', 'tags' => ['stage'], 'exported' => false,
1621+
'parameters' => {
1622+
'name' => 'main', 'toplevel' => 'toplevel attribute',
1623+
'nest' => {
1624+
'toplevel' => 'toplevel_nest attribute',
1625+
'nest' => { 'nest' => 'nested nested text' },
1626+
'nest2' => { 'chicken' => 'egg' },
1627+
'chicken' => 'egg'
1628+
}
1629+
}
1630+
}
1631+
]
1632+
@c1 = OctocatalogDiff::Spec.build_catalog(r1)
1633+
@c2 = OctocatalogDiff::Spec.build_catalog(r1)
1634+
end
1635+
1636+
it 'should honor the algo configuration passed in the options (false)' do
1637+
logger, logger_str = OctocatalogDiff::Spec.setup_logger
1638+
opts = { use_lcs: false, logger: logger }
1639+
testobj = OctocatalogDiff::CatalogDiff::Differ.new(opts, @c1, @c2)
1640+
testobj.diff
1641+
expect(logger_str.string).to match(/Entering hashdiff_initial; catalog sizes: 1, 1/)
1642+
expect(logger_str.string).to match(/HashDiff configuration: \(use_lcs: false\)/)
1643+
end
1644+
1645+
it 'should honor the algo configuration passed in the options (true)' do
1646+
logger, logger_str = OctocatalogDiff::Spec.setup_logger
1647+
opts = { use_lcs: true, logger: logger }
1648+
testobj = OctocatalogDiff::CatalogDiff::Differ.new(opts, @c1, @c2)
1649+
testobj.diff
1650+
expect(logger_str.string).to match(/Entering hashdiff_initial; catalog sizes: 1, 1/)
1651+
expect(logger_str.string).to match(/HashDiff configuration: \(use_lcs: true\)/)
1652+
end
1653+
1654+
it 'the default value is true' do
1655+
logger, logger_str = OctocatalogDiff::Spec.setup_logger
1656+
opts = { logger: logger }
1657+
testobj = OctocatalogDiff::CatalogDiff::Differ.new(opts, @c1, @c2)
1658+
testobj.diff
1659+
expect(logger_str.string).to match(/Entering hashdiff_initial; catalog sizes: 1, 1/)
1660+
expect(logger_str.string).to match(/HashDiff configuration: \(use_lcs: true\)/)
1661+
end
1662+
end
1663+
end
16141664
end
16151665
end
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

+2-1
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)