Skip to content

Commit 271fabd

Browse files
author
Kevin Paulisse
committed
Allow type to be a regexp
1 parent 95bc03d commit 271fabd

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,11 @@ def ignore_match?(rule_in, diff_type, hsh, old_val, new_val)
330330
rule = rule_in.dup
331331

332332
# Type matches?
333-
return false unless rule[:type] == '*' || rule[:type].casecmp(hsh[:type]).zero?
333+
if rule[:type].is_a?(Regexp)
334+
return false unless hsh[:type].match(rule[:type])
335+
elsif rule[:type].is_a?(String)
336+
return false unless rule[:type] == '*' || rule[:type].casecmp(hsh[:type]).zero?
337+
end
334338

335339
# Title matches? (Support regexp and string)
336340
if rule[:title].is_a?(Regexp)

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,83 @@
12101210
end
12111211
end
12121212

1213+
describe '#ignore_match?' do
1214+
let(:resource) { { type: 'Apple', title: 'delicious', attr: "parameters\fcolor" } }
1215+
let(:testobj) { described_class.allocate }
1216+
1217+
context 'type regex' do
1218+
it 'should filter matching resource' do
1219+
rule = { type: Regexp.new('A.+e\z'), title: '*', attr: '*' }
1220+
logger, logger_str = OctocatalogDiff::Spec.setup_logger
1221+
testobj.instance_variable_set('@logger', logger)
1222+
expect(testobj.send(:"ignore_match?", rule, '+', resource, 'old_value', 'new_value')).to eq(true)
1223+
expect(logger_str.string).to match(%r[Ignoring .+ matches {:type=>/A.+e\\z/, :title=>"\*", :attr=>"\*"}])
1224+
end
1225+
1226+
it 'should not filter non-matching resource' do
1227+
rule = { type: Regexp.new('A.+b\z'), title: '*', attr: '*' }
1228+
logger, logger_str = OctocatalogDiff::Spec.setup_logger
1229+
testobj.instance_variable_set('@logger', logger)
1230+
expect(testobj.send(:"ignore_match?", rule, '+', resource, 'old_value', 'new_value')).to eq(false)
1231+
expect(logger_str.string).not_to match(%r[Ignoring .+ matches {:type=>/A.+b\\z/, :title=>"\*", :attr=>"\*"}])
1232+
end
1233+
end
1234+
1235+
context 'type string' do
1236+
it 'should filter matching resource' do
1237+
rule = { type: 'Apple', title: '*', attr: '*' }
1238+
logger, logger_str = OctocatalogDiff::Spec.setup_logger
1239+
testobj.instance_variable_set('@logger', logger)
1240+
expect(testobj.send(:"ignore_match?", rule, '+', resource, 'old_value', 'new_value')).to eq(true)
1241+
expect(logger_str.string).to match(/Ignoring .+ matches {:type=>"Apple", :title=>"\*", :attr=>"\*"}/)
1242+
end
1243+
1244+
it 'should not filter non-matching resource' do
1245+
rule = { type: 'Banana', title: '*', attr: '*' }
1246+
logger, logger_str = OctocatalogDiff::Spec.setup_logger
1247+
testobj.instance_variable_set('@logger', logger)
1248+
expect(testobj.send(:"ignore_match?", rule, '+', resource, 'old_value', 'new_value')).to eq(false)
1249+
expect(logger_str.string).not_to match(/Ignoring .+ matches {:type=>"Banana", :title=>"\*", :attr=>"\*"}/)
1250+
end
1251+
end
1252+
1253+
context 'title regex' do
1254+
it 'should filter matching resource' do
1255+
rule = { type: '*', title: Regexp.new('del.+ous'), attr: '*' }
1256+
logger, logger_str = OctocatalogDiff::Spec.setup_logger
1257+
testobj.instance_variable_set('@logger', logger)
1258+
expect(testobj.send(:"ignore_match?", rule, '+', resource, 'old_value', 'new_value')).to eq(true)
1259+
expect(logger_str.string).to match(%r[Ignoring .+ matches {:type=>"\*", :title=>/del\.\+ous/, :attr=>"\*"}])
1260+
end
1261+
1262+
it 'should not filter non-matching resource' do
1263+
rule = { type: '*', title: Regexp.new('dell.+ous'), attr: '*' }
1264+
logger, logger_str = OctocatalogDiff::Spec.setup_logger
1265+
testobj.instance_variable_set('@logger', logger)
1266+
expect(testobj.send(:"ignore_match?", rule, '+', resource, 'old_value', 'new_value')).to eq(false)
1267+
expect(logger_str.string).not_to match(%r[Ignoring .+ matches {:type=>"\*", :title=>/dell\.\+ous/, :attr=>"\*"}])
1268+
end
1269+
end
1270+
1271+
context 'title string' do
1272+
it 'should filter matching resource' do
1273+
rule = { type: '*', title: 'delicious', attr: '*' }
1274+
logger, logger_str = OctocatalogDiff::Spec.setup_logger
1275+
testobj.instance_variable_set('@logger', logger)
1276+
expect(testobj.send(:"ignore_match?", rule, '+', resource, 'old_value', 'new_value')).to eq(true)
1277+
expect(logger_str.string).to match(/Ignoring .+ matches {:type=>"\*", :title=>"delicious", :attr=>"\*"}/)
1278+
end
1279+
1280+
it 'should not filter non-matching resource' do
1281+
rule = { type: '*', title: 'dell', attr: '*' }
1282+
logger, logger_str = OctocatalogDiff::Spec.setup_logger
1283+
testobj.instance_variable_set('@logger', logger)
1284+
expect(testobj.send(:"ignore_match?", rule, '+', resource, 'old_value', 'new_value')).to eq(false)
1285+
expect(logger_str.string).not_to match(/Ignoring .+ matches {:type=>"\*", :title=>"dell", :attr=>"\*"}/)
1286+
end
1287+
end
1288+
end
1289+
12131290
describe '#hashdiff_nested_changes' do
12141291
it 'should return array with proper results' do
12151292
hashdiff_add_remove = [

0 commit comments

Comments
 (0)