Skip to content

Commit b00015b

Browse files
committed
Update differ to support regular expression attributes
1 parent 89d6c99 commit b00015b

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,13 @@ def ignore_match?(rule_in, diff_type, hsh, old_val, new_val)
394394
return false unless rule[:title].casecmp(hsh[:title]).zero?
395395
end
396396

397+
# If rule[:attr] is a regular expression, handle that case here.
398+
if rule[:attr].is_a?(Regexp)
399+
return false unless hsh[:attr].is_a?(String)
400+
return false unless rule[:attr].match(hsh[:attr])
401+
return ignore_match_true(hsh, rule)
402+
end
403+
397404
# Special 'attributes': Ignore specific diff types (+ add, - remove, ~ and ! change)
398405
if rule[:attr] =~ /\A[\-\+~!]+\Z/
399406
return ignore_match_true(hsh, rule) if rule[:attr].include?(diff_type)

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,24 @@
12851285
expect(logger_str.string).not_to match(/Ignoring .+ matches {:type=>"\*", :title=>"dell", :attr=>"\*"}/)
12861286
end
12871287
end
1288+
1289+
context 'attrs regexp' do
1290+
it 'should filter on regular expression match' do
1291+
rule = { type: 'Apple', title: 'delicious', attr: Regexp.new("\\Aparameters\f(color|taste)\\z") }
1292+
logger, logger_str = OctocatalogDiff::Spec.setup_logger
1293+
testobj.instance_variable_set('@logger', logger)
1294+
expect(testobj.send(:"ignore_match?", rule, '+', resource, 'old_value', 'new_value')).to eq(true)
1295+
expect(logger_str.string).to match(/Ignoring .+ matches {:type=>"Apple", :title=>"delicious", :attr=>/)
1296+
end
1297+
1298+
it 'should not filter on regular expression non-match' do
1299+
rule = { type: 'Apple', title: 'delicious', attr: Regexp.new("\\Aparameters\f(odor|has_worms)\\z") }
1300+
logger, logger_str = OctocatalogDiff::Spec.setup_logger
1301+
testobj.instance_variable_set('@logger', logger)
1302+
expect(testobj.send(:"ignore_match?", rule, '+', resource, 'old_value', 'new_value')).to eq(false)
1303+
expect(logger_str.string).not_to match(/Ignoring .+ matches/)
1304+
end
1305+
end
12881306
end
12891307

12901308
describe '#ignore_tags' do

0 commit comments

Comments
 (0)