-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtestrules.py
executable file
·51 lines (44 loc) · 1.56 KB
/
testrules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import traceback
import argparse
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:
errors.append(('E', name, traceback.format_exc()))
sys.stdout.write('E')
print
if errors:
print
for error in errors:
print error[0], error[1]
print error[2], '\n\n'
print 'FAIL'
exit(1)
else:
print 'OK'
exit(0)