From 626f84736b61a31454c4e21bdb85ffaef0b5cda7 Mon Sep 17 00:00:00 2001 From: Yu Ming Zhu Date: Jul 07 2021 13:39:28 +0000 Subject: [policy] FindTest for list item matching --- diff --git a/koji/policy.py b/koji/policy.py index 34cf308..d972c95 100644 --- a/koji/policy.py +++ b/koji/policy.py @@ -141,11 +141,11 @@ class MatchTest(BaseSimpleTest): return False -class AdvancedMatchTest(MatchTest): +class AdvancedMatchTest(BaseSimpleTest): """Matches a field in the data against glob patterns The field could be the same as match test, - or a dot-spiited string which indicates key in a (nested) dict + or a dot-splited string which indicates key in a (nested) dict True if any of the expressions match, else False This test can be used as-is, or it can be subclassed to @@ -181,6 +181,32 @@ class AdvancedMatchTest(MatchTest): return False +class FindTest(BaseSimpleTest): + """Matches any item of a list/tuple/set value in the data against glob patterns + + True if any of the expressions matches any item in the list/tuple/set, else False. + If the field doesn't exist or isn't a list/tuple/set, the test returns False + + Syntax: + find field pattern1 [pattern2 ...] + + """ + name = 'find' + field = None + + def run(self, data): + args = self.str.split()[1:] + self.field = args[0] + args = args[1:] + tgt = data.get(self.field) + if tgt and isinstance(tgt, (list, tuple, set)): + for pattern in args: + for i in tgt: + if fnmatch.fnmatch(i, pattern): + return True + return False + + class TargetTest(MatchTest): """Matches target in the data against glob patterns diff --git a/tests/test_lib/test_policy.py b/tests/test_lib/test_policy.py index be2630b..eb7934b 100644 --- a/tests/test_lib/test_policy.py +++ b/tests/test_lib/test_policy.py @@ -83,6 +83,13 @@ class TestBasicTests(unittest.TestCase): } }})) + def test_find_test(self): + obj = koji.policy.FindTest('not_important foo *bar*') + self.assertTrue(obj.run({'foo': ['barrrr', 'any']})) + self.assertFalse(obj.run({'foo': ['nah....']})) + self.assertFalse(obj.run({'foo': 'nah...'})) + self.assertFalse(obj.run({'bar': ['any']})) + def test_target_test(self): obj = koji.policy.TargetTest('target valid') self.assertTrue(obj.run({'target': 'valid'})) @@ -142,6 +149,7 @@ class TestDiscovery(unittest.TestCase): 'has': koji.policy.HasTest, 'match': koji.policy.MatchTest, 'adv_match': koji.policy.AdvancedMatchTest, + 'find': koji.policy.FindTest, 'none': koji.policy.NoneTest, 'target': koji.policy.TargetTest, 'true': koji.policy.TrueTest,