def test_dont_create_evaluator_if_no_matcher(self): self.rule["file"] = [{"match": "test"}] del self.rule["line"] lef = LineEvalFactory(mode=LineEvalFactory.MODE_DIFF) evaluator = lef.create(self.rule) self.assertIsNone(evaluator)
def test_normal_del(self): lines = ["hello greeter", "hello world"] self.rule["diff"] = "del" lef = LineEvalFactory(mode=LineEvalFactory.MODE_SINGLE) evaluator = lef.create(self.rule) self.assertFalse(evaluator.matches({}, lines[0])) self.assertFalse(evaluator.matches({}, lines[1]))
def test_multisearch_criteria(self): lines = ["hello", "bello", "hello world"] self.rule["line"].append({"match": "bello"}) lef = LineEvalFactory(mode=LineEvalFactory.MODE_SINGLE) evaluator = lef.create(self.rule) self.assertTrue(evaluator.matches({}, lines[0])) self.assertTrue(evaluator.matches({}, lines[1])) self.assertFalse(evaluator.matches({}, lines[2]))
def test_normal_del(self): lines = [ "hello greeter", "hello world" ] self.rule["diff"] = "del" lef = LineEvalFactory(mode=LineEvalFactory.MODE_SINGLE) evaluator = lef.create(self.rule) self.assertFalse(evaluator.matches({}, lines[0])) self.assertFalse(evaluator.matches({}, lines[1]))
def test_diff_any(self): lines = [ " hello greeter", "+ hello greeter", "- hello greeter", " hello world" ] lef = LineEvalFactory(mode=LineEvalFactory.MODE_DIFF) evaluator = lef.create(self.rule) self.assertTrue(evaluator.matches({}, lines[0])) self.assertTrue(evaluator.matches({}, lines[1])) self.assertTrue(evaluator.matches({}, lines[2])) self.assertFalse(evaluator.matches({}, lines[3]))
def test_multisearch_criteria(self): lines = [ "hello", "bello", "hello world" ] self.rule["line"].append({"match": "bello"}) lef = LineEvalFactory(mode=LineEvalFactory.MODE_SINGLE) evaluator = lef.create(self.rule) self.assertTrue(evaluator.matches({}, lines[0])) self.assertTrue(evaluator.matches({}, lines[1])) self.assertFalse(evaluator.matches({}, lines[2]))
def test_diff_del(self): lines = [ " hello world", "- hello world", "- hello foobar", " have a nice day" ] self.rule["diff"] = "del" lef = LineEvalFactory(mode=LineEvalFactory.MODE_DIFF) evaluator = lef.create(self.rule) self.assertFalse(evaluator.matches({}, lines[0])) self.assertFalse(evaluator.matches({}, lines[1])) self.assertTrue(evaluator.matches({}, lines[2])) self.assertFalse(evaluator.matches({}, lines[3]))
def test_has_key(self): lef = LineEvalFactory(mode=LineEvalFactory.MODE_DIFF) evaluator = lef.create(self.rule) self.assertEquals("line", evaluator.key)
from core.evaluators import LineEvalFactory from core.ruleparser import load_rules, build_resolved_ruleset parser = argparse.ArgumentParser(description='Watch git repos for changes...') parser.add_argument('--rule-dir', default='rules/', help='Path to the rule directory') args = parser.parse_args() bare_rules = load_rules(args.rule_dir) resolved_rules = build_resolved_ruleset(bare_rules) testable_rules = {rn: rule for rn, rule in resolved_rules.iteritems() if 'tests' in rule} errors = [] line_eval_factory = LineEvalFactory(LineEvalFactory.MODE_SINGLE) for name, rule in testable_rules.iteritems(): try: evaluator = line_eval_factory.create(rule) tests = rule.get('tests', []) for test in tests: test_string = test.get('pass', test.get('fail', '')) expected = 'pass' in test actual = evaluator.matches({}, test_string) if expected == actual: sys.stdout.write('.') else: sys.stdout.write('F') errors.append(('F', 'Name: %s, Rules: %s' % (name, rule.get('line')), 'Actual: %s, Expected: %s, Test string: %s' % (actual, expected, test_string))) except: