diff --git a/muranopkgcheck/checkers/code_structure.py b/muranopkgcheck/checkers/code_structure.py index 6d0f8b1..eec5ced 100644 --- a/muranopkgcheck/checkers/code_structure.py +++ b/muranopkgcheck/checkers/code_structure.py @@ -37,16 +37,9 @@ CODE_STRUCTURE = { 'Try': { 'keywords': { 'Try': check_req('codeblock'), - 'Catch': check_req('codeblock'), + 'Catch': check_req('catchblock'), 'Else': check_req('codeblock', False), 'Finally': check_req('codeblock', False)}}, - 'As': { - 'keywords': { - 'As': check_req('string'), - 'With': check_req('string', False), - 'Do': check_req('codeblock'), - } - }, 'Parallel': { 'keywords': { 'Limit': check_req('codeblock', False), @@ -114,6 +107,7 @@ class CheckCodeStructure(object): def __init__(self): self._check_mappings = { 'codeblock': self.codeblock, + 'catchblock': self.catchblock, 'predicate': self.yaql, 'empty': self.empty, 'expression': self.yaql, @@ -139,6 +133,22 @@ class CheckCodeStructure(object): yield error.report.W202('"{0}" is not valid yaql expression' ''.format(value), value) + def catchblock(self, catchblock): + if isinstance(catchblock, list): + for block in catchblock: + yield self._single_catchblock(block) + else: + yield self._single_catchblock(catchblock) + + def _single_catchblock(self, catchblock): + do = catchblock.get('Do') + if not do: + yield error.report.E204('Catch is missing "Do" block', catchblock) + else: + yield self.codeblock(do) + yield self.string(catchblock.get('With', '')) + yield self.string(catchblock.get('As', '')) + def codeblock(self, codeblocks): if isinstance(codeblocks, list): for block in codeblocks: diff --git a/muranopkgcheck/tests/test_code_structure_checker.py b/muranopkgcheck/tests/test_code_structure_checker.py index 833c638..76b74fd 100644 --- a/muranopkgcheck/tests/test_code_structure_checker.py +++ b/muranopkgcheck/tests/test_code_structure_checker.py @@ -150,6 +150,14 @@ class CodeStructureTest(helpers.BaseValidatorTestClass): self.assertIn('Value of "213" should be a string', next(self.g).message) + def test_try_only_do(self): + MULTILINE_BODY = [ + {'Try': ['$port.deploy()'], + 'Catch': [{ + 'Do': ['$.string()']}]} + ] + self.g = self._checker.codeblock(MULTILINE_BODY) + def test_yaql_accept_bool(self): self.g = self._checker.yaql(True)