Beispiel #1
0
def main():
    """Git update hook.

    :param refname: Name of the ref, e.g., refs/heads/master
    :param oldrev: The old object name stored in the ref
    :param newrev: The new object name to be stored in the ref
    """
    refname, oldrev, newrev = sys.argv[1:]

    if ZERO_REV in (newrev, oldrev):
        # the branch is created or deleted,
        # we don't need to check them in these cases.
        sys.exit(0)

    with open(join(REPO_ROOT_DIR, 'aniketos.ini')) as fp:
        configparser = AniketosConfigParser()
        rules = configparser.readfp(fp)

    accepted = True
    for rule in rules.values():
        accepted = accepted and rule(refname, oldrev, newrev)
        # We still run all the checkers,
        # so the user will know which checks failed
        if accepted:
            sys.exit(0)
        else:
            sys.exit(1)
Beispiel #2
0
class TestConfigParser(object):
    def setup(self):
        self.cp = AniketosConfigParser()

    def test_read_config___no_rules(self):
        fp = StringIO("")
        assert len(self.cp.readfp(fp)) == 0

    def test_read_config___simple_rule(self):
        fp = StringIO("""[rule:foo]
refmatch=refs/heads/mybranch
checker=foochecker
[checker:foochecker]
type=pylint
staging_dir=/tmp
policy=foopolicy
[policy:foopolicy]
type=decremental
result_filepath=/tmp/staging
""")
        rules = self.cp.readfp(fp)
        assert len(rules) == 1
        assert isinstance(rules['foo'].get_checker('foochecker'), PylintChecker)

    def test_read_config___simple_rule_with_multiple_checkers(self):
        fp = StringIO("""[rule:foo]
refmatch=refs/heads/mybranch
checker=foochecker, barchecker
[checker:foochecker]
type=pylint
staging_dir=/tmp/foo
policy=foopolicy
[policy:foopolicy]
type=decremental
result_filepath=/tmp/foo/staging
[checker:barchecker]
type=pylint
staging_dir=/tmp/bar
policy=barpolicy
[policy:barpolicy]
type=decremental
result_filepath=/tmp/bar/staging
""")
        rules = self.cp.readfp(fp)

        assert len(rules) == 1

        assert isinstance(rules['foo'].get_checker('foochecker'), PylintChecker)
        assert rules['foo'].get_checker('foochecker').staging_dir, '/tmp/foo'
        assert isinstance(rules['foo'].get_checker('foochecker').policy, Decremental)

        assert isinstance(rules['foo'].get_checker('barchecker'), PylintChecker)
        assert rules['foo'].get_checker('barchecker').staging_dir, '/tmp/bar'
        assert isinstance(rules['foo'].get_checker('barchecker').policy, Decremental)


    def test_read_config___simple_rule_with_optional_policy(self):
        fp = StringIO("""[rule:foo]
refmatch=refs/heads/mybranch
checker=foochecker
[checker:foochecker]
type=norambling
max_title_width=3
""")
        rules = self.cp.readfp(fp)

        assert len(rules) == 1

        assert isinstance(rules['foo'].get_checker('foochecker'), NoRamblingChecker)