diff --git a/tests/core/test_regexp.py b/tests/core/test_regexp.py index f7afcdf..f5f066d 100644 --- a/tests/core/test_regexp.py +++ b/tests/core/test_regexp.py @@ -1,3 +1,4 @@ +import pytest from gixy.core.regexp import Regexp ''' @@ -12,288 +13,30 @@ ''' -def test_positive_contains(): - cases = ( - (r'[a-z]', 'a'), - (r'[a-z]*', 'a'), - (r'[a-z]*?', 'a'), - (r'[a-z]+?', 'a'), - (r'[a-z]', 'z'), - (r'(?:a|b)', 'b'), - (r'(/|:|[a-z])', 'g'), - (r'[^a-z]', '/'), - (r'[^a-z]', '\n'), - (r'[^0]', '9'), - (r'[^0-2]', '3'), - (r'[^0123a-z]', '9'), - (r'\s', '\x20'), - (r'[^\s]', 'a'), - (r'\d', '1'), - (r'[^\d]', 'b'), - (r'\w', '_'), - (r'[^\w]', '\n'), - (r'\W', '\n'), - (r'[^\W]', 'a'), - (r'.', 'a') - ) - for case in cases: - regexp, char = case - yield check_positive_contain, regexp, char - - -def test_negative_contains(): - cases = ( - ('[a-z]', '1'), - ('[a-z]*', '2'), - ('[a-z]*?', '3'), - ('[a-z]+?', '4'), - ('[a-z]', '\n'), - ('(?:a|b)', 'c'), - ('(/|:|[a-z])', '\n'), - ('[^a-z]', 'a'), - ('[^0]', '0'), - ('[^0-2]', '0'), - ('[^0123a-z]', 'z'), - (r'\s', 'a'), - (r'[^\s]', '\n'), - (r'\d', 'f'), - (r'[^\d]', '2'), - (r'\w', '\n'), - (r'[^\w]', '_'), - (r'\W', 'a'), - (r'[^\W]', '\n'), - (r'.', '\n') - ) - for case in cases: - regexp, char = case - yield check_negative_contain, regexp, char - - -def test_groups_names(): - cases = ( - ('foo', [0]), - ('(1)(2)(?:3)', [0, 1, 2]), - ('(1)((2)|(?:3))', [0, 1, 2, 3]), - ("(?'pcre_7'1as)(?P(?2)|(?:3))", [0, 1, 2, 3, 'pcre_7', 'outer', 'inner']), - ('/proxy/(?.*)$', [0, 1, 'proxy']) - ) - for case in cases: - regexp, groups = case - yield check_groups_names, regexp, groups - - -def test_to_string(): - cases = ( - (r'foo', 'foo'), - (r'(1)(2)(?:3)', '(1)(2)(?:3)'), - (r'(1)((2)|(?:3))', '(1)((?:(2)|(?:3)))'), - (r'\w|1|3-5|[a-z]', '(?:[\w]|1|3\\-5|[a-z])'), - (r'(1|(?:3)|([4-6]))', '((?:1|(?:3)|([4-6])))'), - (r'(1|(?:3)|(?P[4-6]))', '((?:1|(?:3)|([4-6])))'), - (r'^sss', '^sss'), - (r'(^bb|11)$', '((?:^bb|11))$'), - (r'(http|https)', '(http(?:|s))'), - (r'1*', '1*'), - (r'1*?', '1*?'), - (r'1+', '1+'), - ) - for case in cases: - regexp, string = case - yield check_to_string, regexp, string - - -def test_positive_startswith(): - cases = ( - (r'foo', 'q', False), - (r'foo', 'f', True), - (r'^foo', 'f', False), - (r'(^foo)', 'f', False), - (r'(^foo)', 'f', True), - (r'(^foo|g)', 'f', True), - (r'(^foo|g)', 'g', True), - (r'(^foo|g)', 'q', False), - (r'^[^/]+', '\n', True), - (r'/[^/]+', '/', True), - (r'((a))', 'a', False), - (r'((a))', 'b', False), - (r'^[a-z]{0}0', '0', False), - (r'^[a-z]{1}0', 'a', False), - ) - for case in cases: - regexp, check, strict = case - yield check_positive_startswith, regexp, check, strict - - -def test_negative_startswith(): - cases = ( - (r'foo', '\n', False), - (r'foo', 'o', True), - (r'^foo', 'o', False), - (r'(^foo)', 'q', False), - (r'(^foo)', 'q', True), - (r'(^foo|g)', 'q', True), - (r'(^foo|g)', 'o', True), - (r'(^foo|g)', '\n', False), - (r'^[^/]+', '/', True), - (r'/[^/]+', 'a', True), - (r'((abc)|(ss))', 'b', True), - (r'^[a-z]{0}0', 'a', False), - (r'^[a-z]{0}0', 'g', False), - ) - for case in cases: - regexp, check, strict = case - yield check_negative_startswith, regexp, check, strict - - -def test_positive_must_contain(): - cases = ( - (r'abc', 'a'), - (r'abc', 'b'), - (r'abc', 'c'), - (r'3+', '3'), - (r'[0]', '0'), - (r'([0])', '0'), - (r'(?:[0])', '0'), - (r'(?:[0])|0|((((0))))', '0'), - ) - for case in cases: - regexp, char = case - yield check_positive_must_contain, regexp, char - - -def test_negative_must_contain(): - cases = ( - (r'[a-z]', '1'), - (r'2{0}1', '2'), - (r'3?', '3'), - (r'3*', '3'), - (r'3*?', '3'), - (r'3+a', 'b'), - (r'[a-z]', 'a'), - (r'(?:a|b)', 'a'), - (r'(?:a|b)', 'b'), - (r'(/|:|[a-z])', '/'), - (r'(/|:|[a-z])', 'z'), - (r'[^a-z]', '\n'), - (r'[^0]', '0'), - (r'[^0-2]', '0'), - (r'[^0123a-z]', 'z'), - (r'\s', '\x20'), - (r'[^\s]', '\n'), - (r'\d', '3'), - (r'[^\d]', 'a'), - (r'\w', 'a'), - (r'[^\w]', '\n'), - (r'\W', '\n'), - (r'[^\W]', 'a'), - (r'.', '\n') - ) - for case in cases: - regexp, char = case - yield check_negative_must_contain, regexp, char - - -def test_positive_must_startswith(): - cases = ( - (r'foo', 'f', True), - (r'^foo', 'f', False), - (r'(^foo)', 'f', True), - (r'^((a))', 'a', False), - (r'((a))', 'a', True), - (r'^[a-z]{0}0', '0', False), - (r'^a{1}0', 'a', False), - ) - for case in cases: - regexp, check, strict = case - yield check_positive_must_startswith, regexp, check, strict - - -def test_negative_must_startswith(): - cases = ( - (r'foo', 'o', False), - (r'^foo', 'o', False), - (r'(^foo)', 'o', False), - (r'[a-z]', '1', True), - (r'[a-z]', 'a', True), - (r'/[^/]+', 'a', True), - (r'3?', '3', True), - (r'3*', '3', True), - (r'3*?', '3', True), - (r'3+a', 'b', True), - (r'^((a))', 'b', False), - (r'((a))', 'a', False), - (r'^a{0}0', 'a', False), - ) - for case in cases: - regexp, check, strict = case - yield check_negative_must_startswith, regexp, check, strict - - -def test_generate(): - cases = ( - (r'foo', ['foo']), - (r'^sss', ['^sss']), - (r'(1)(2)(3)', ['123']), - (r'(1)((2)|(?:3))', ['12', '13']), - (r'(^1?2?|aa/)', ['^', '^1', '^2', '^12', 'aa/']), - (r'^https?://yandex.ru', ['^http://yandex|ru', '^https://yandex|ru']), - (r'(^bb|11)$', ['^bb$', '11$']), - (r'(http|https)', ['http', 'https']), - (r'1*', ['', '11111']), - (r'1*?', ['', '11111']), - (r'1[0]?2', ['102', '12']), - (r'1[0]2', ['102']), - (r'1+', ['11111']), - (r'[^/]?', ['', '|']), - (r'^http://(foo|bar)|baz', ['^http://foo', '^http://bar', 'baz']), - (r'[^\x00-\x7b|\x7e-\xff]', ['\x7d']), - (r'(a|b|c)', ['a', 'b', 'c']), - (r'[xyz]', ['x', 'y', 'z']) - ) - for case in cases: - regexp, values = case - yield check_generate, regexp, values - - -def test_strict_generate(): - reg = Regexp('^foo|bar', strict=True) - assert sorted(reg.generate('|', anchored=True)) == sorted(['^foo', '^bar']) - - -def test_gen_anchor(): - - reg = Regexp('^some$') - val = next(reg.generate('', anchored=False)) - assert val == 'some' - - reg = Regexp('^some$') - val = next(reg.generate('', anchored=True)) - assert val == '^some$' - - reg = Regexp('^some$', strict=True) - val = next(reg.generate('', anchored=False)) - assert val == 'some' - - reg = Regexp('^some$', strict=True) - val = next(reg.generate('', anchored=True)) - assert val == '^some$' - - -def test_group_can_contains(): - source = '/some/(?P[^/:.]+)/' - reg = Regexp(source) - assert reg.can_contain('\n'), 'Whole regex "{src}" can contains {sym!r}'.format(src=source, sym='\\n') - - assert reg.group(0).can_contain('\n'), 'Group 0 from regex "{src}" can contains {sym!r}'.format(src=source, sym='\\n') - - assert reg.group('action').can_contain('\n'), 'Group "action" from regex "{src}" can contains {sym!r}'.format(src=source, sym='\\n') - - assert reg.group(1).can_contain('\n'), 'Group 1 from regex "{src}" can contains {sym!r}'.format(src=source, sym='\\n') - - assert not reg.group('action').can_contain('/'), 'Group "action" from regex "{src}" CAN\'T (!) contain {sym!r}'.format(src=source, sym='/') - - -def check_positive_contain(regexp, char): +@pytest.mark.parametrize('regexp,char', ( + (r'[a-z]', 'a'), + (r'[a-z]*', 'a'), + (r'[a-z]*?', 'a'), + (r'[a-z]+?', 'a'), + (r'[a-z]', 'z'), + (r'(?:a|b)', 'b'), + (r'(/|:|[a-z])', 'g'), + (r'[^a-z]', '/'), + (r'[^a-z]', '\n'), + (r'[^0]', '9'), + (r'[^0-2]', '3'), + (r'[^0123a-z]', '9'), + (r'\s', '\x20'), + (r'[^\s]', 'a'), + (r'\d', '1'), + (r'[^\d]', 'b'), + (r'\w', '_'), + (r'[^\w]', '\n'), + (r'\W', '\n'), + (r'[^\W]', 'a'), + (r'.', 'a') +)) +def test_positive_contains(regexp, char): reg = Regexp(regexp, case_sensitive=True) assert reg.can_contain(char), '{reg!r} should contain {chr!r}'.format(reg=regexp, chr=char) @@ -302,7 +45,29 @@ def check_positive_contain(regexp, char): assert reg.can_contain(char), '{reg!r} (case insensitive) should contain {chr!r}'.format(reg=regexp, chr=char) -def check_negative_contain(regexp, char): +@pytest.mark.parametrize('regexp,char', ( + ('[a-z]', '1'), + ('[a-z]*', '2'), + ('[a-z]*?', '3'), + ('[a-z]+?', '4'), + ('[a-z]', '\n'), + ('(?:a|b)', 'c'), + ('(/|:|[a-z])', '\n'), + ('[^a-z]', 'a'), + ('[^0]', '0'), + ('[^0-2]', '0'), + ('[^0123a-z]', 'z'), + (r'\s', 'a'), + (r'[^\s]', '\n'), + (r'\d', 'f'), + (r'[^\d]', '2'), + (r'\w', '\n'), + (r'[^\w]', '_'), + (r'\W', 'a'), + (r'[^\W]', '\n'), + (r'.', '\n') +)) +def test_negative_contains(regexp, char): reg = Regexp(regexp, case_sensitive=True) assert not reg.can_contain(char), '{reg!r} should not contain {chr!r}'.format(reg=regexp, chr=char) @@ -311,7 +76,54 @@ def check_negative_contain(regexp, char): assert not reg.can_contain(char), '{reg!r} (case insensitive) should not contain {chr!r}'.format(reg=regexp, chr=char) -def check_positive_startswith(regexp, char, strict): +@pytest.mark.parametrize('regexp,groups', ( + ('foo', [0]), + ('(1)(2)(?:3)', [0, 1, 2]), + ('(1)((2)|(?:3))', [0, 1, 2, 3]), + ("(?'pcre_7'1as)(?P(?2)|(?:3))", [0, 1, 2, 3, 'pcre_7', 'outer', 'inner']), + ('/proxy/(?.*)$', [0, 1, 'proxy']) +)) +def test_groups_names(regexp, groups): + reg = Regexp(regexp) + assert set(reg.groups.keys()) == set(groups) + + +@pytest.mark.parametrize('regexp,string', ( + (r'foo', 'foo'), + (r'(1)(2)(?:3)', '(1)(2)(?:3)'), + (r'(1)((2)|(?:3))', '(1)((?:(2)|(?:3)))'), + (r'\w|1|3-5|[a-z]', '(?:[\w]|1|3\\-5|[a-z])'), + (r'(1|(?:3)|([4-6]))', '((?:1|(?:3)|([4-6])))'), + (r'(1|(?:3)|(?P[4-6]))', '((?:1|(?:3)|([4-6])))'), + (r'^sss', '^sss'), + (r'(^bb|11)$', '((?:^bb|11))$'), + (r'(http|https)', '(http(?:|s))'), + (r'1*', '1*'), + (r'1*?', '1*?'), + (r'1+', '1+'), +)) +def test_to_string(regexp, string): + reg = Regexp(regexp) + assert str(reg) == string + + +@pytest.mark.parametrize('regexp,char,strict', ( + (r'foo', 'q', False), + (r'foo', 'f', True), + (r'^foo', 'f', False), + (r'(^foo)', 'f', False), + (r'(^foo)', 'f', True), + (r'(^foo|g)', 'f', True), + (r'(^foo|g)', 'g', True), + (r'(^foo|g)', 'q', False), + (r'^[^/]+', '\n', True), + (r'/[^/]+', '/', True), + (r'((a))', 'a', False), + (r'((a))', 'b', False), + (r'^[a-z]{0}0', '0', False), + (r'^[a-z]{1}0', 'a', False), +)) +def test_positive_startswith(regexp, char, strict): reg = Regexp(regexp, case_sensitive=True, strict=strict) assert reg.can_startswith(char), '{reg!r} can start\'s with {chr!r}'.format(reg=regexp, chr=char) @@ -320,7 +132,22 @@ def check_positive_startswith(regexp, char, strict): assert reg.can_startswith(char), '{reg!r} (case insensitive) can start\'s with {chr!r}'.format(reg=regexp, chr=char) -def check_negative_startswith(regexp, char, strict): +@pytest.mark.parametrize('regexp,char,strict', ( + (r'foo', '\n', False), + (r'foo', 'o', True), + (r'^foo', 'o', False), + (r'(^foo)', 'q', False), + (r'(^foo)', 'q', True), + (r'(^foo|g)', 'q', True), + (r'(^foo|g)', 'o', True), + (r'(^foo|g)', '\n', False), + (r'^[^/]+', '/', True), + (r'/[^/]+', 'a', True), + (r'((abc)|(ss))', 'b', True), + (r'^[a-z]{0}0', 'a', False), + (r'^[a-z]{0}0', 'g', False), +)) +def test_negative_startswith(regexp, char, strict): reg = Regexp(regexp, case_sensitive=True, strict=strict) assert not reg.can_startswith(char), '{reg!r} can\'t start\'s with {chr!r}'.format(reg=regexp, chr=char) @@ -329,17 +156,17 @@ def check_negative_startswith(regexp, char, strict): assert not reg.can_startswith(char), '{reg!r} (case insensitive) can\'t start\'s with {chr!r}'.format(reg=regexp, chr=char) -def check_groups_names(regexp, groups): - reg = Regexp(regexp) - assert set(reg.groups.keys()) == set(groups) - - -def check_to_string(regexp, string): - reg = Regexp(regexp) - assert str(reg) == string - - -def check_positive_must_contain(regexp, char): +@pytest.mark.parametrize('regexp,char', ( + (r'abc', 'a'), + (r'abc', 'b'), + (r'abc', 'c'), + (r'3+', '3'), + (r'[0]', '0'), + (r'([0])', '0'), + (r'(?:[0])', '0'), + (r'(?:[0])|0|((((0))))', '0'), +)) +def test_positive_must_contain(regexp, char): reg = Regexp(regexp, case_sensitive=True) assert reg.must_contain(char), '{reg!r} must contain with {chr!r}'.format(reg=regexp, chr=char) @@ -348,7 +175,33 @@ def check_positive_must_contain(regexp, char): assert reg.must_contain(char), '{reg!r} (case insensitive) must contain with {chr!r}'.format(reg=regexp, chr=char) -def check_negative_must_contain(regexp, char): +@pytest.mark.parametrize('regexp,char', ( + (r'[a-z]', '1'), + (r'2{0}1', '2'), + (r'3?', '3'), + (r'3*', '3'), + (r'3*?', '3'), + (r'3+a', 'b'), + (r'[a-z]', 'a'), + (r'(?:a|b)', 'a'), + (r'(?:a|b)', 'b'), + (r'(/|:|[a-z])', '/'), + (r'(/|:|[a-z])', 'z'), + (r'[^a-z]', '\n'), + (r'[^0]', '0'), + (r'[^0-2]', '0'), + (r'[^0123a-z]', 'z'), + (r'\s', '\x20'), + (r'[^\s]', '\n'), + (r'\d', '3'), + (r'[^\d]', 'a'), + (r'\w', 'a'), + (r'[^\w]', '\n'), + (r'\W', '\n'), + (r'[^\W]', 'a'), + (r'.', '\n') +)) +def test_negative_must_contain(regexp, char): reg = Regexp(regexp, case_sensitive=True) assert not reg.must_contain(char), '{reg!r} must NOT contain with {chr!r}'.format(reg=regexp, chr=char) @@ -357,7 +210,16 @@ def check_negative_must_contain(regexp, char): assert not reg.must_contain(char), '{reg!r} (case insensitive) must NOT contain with {chr!r}'.format(reg=regexp, chr=char) -def check_positive_must_startswith(regexp, char, strict): +@pytest.mark.parametrize('regexp,char,strict', ( + (r'foo', 'f', True), + (r'^foo', 'f', False), + (r'(^foo)', 'f', True), + (r'^((a))', 'a', False), + (r'((a))', 'a', True), + (r'^[a-z]{0}0', '0', False), + (r'^a{1}0', 'a', False), +)) +def test_positive_must_startswith(regexp, char, strict): reg = Regexp(regexp, case_sensitive=True, strict=strict) assert reg.must_startswith(char), '{reg!r} MUST start\'s with {chr!r}'.format(reg=regexp, chr=char) @@ -366,7 +228,22 @@ def check_positive_must_startswith(regexp, char, strict): assert reg.must_startswith(char), '{reg!r} (case insensitive) MUST start\'s with {chr!r}'.format(reg=regexp, chr=char) -def check_negative_must_startswith(regexp, char, strict): +@pytest.mark.parametrize('regexp,char,strict', ( + (r'foo', 'o', False), + (r'^foo', 'o', False), + (r'(^foo)', 'o', False), + (r'[a-z]', '1', True), + (r'[a-z]', 'a', True), + (r'/[^/]+', 'a', True), + (r'3?', '3', True), + (r'3*', '3', True), + (r'3*?', '3', True), + (r'3+a', 'b', True), + (r'^((a))', 'b', False), + (r'((a))', 'a', False), + (r'^a{0}0', 'a', False), +)) +def test_negative_must_startswith(regexp, char, strict): reg = Regexp(regexp, case_sensitive=True, strict=strict) assert not reg.must_startswith(char), '{reg!r} MUST NOT start\'s with {chr!r}'.format(reg=regexp, chr=char) @@ -375,6 +252,64 @@ def check_negative_must_startswith(regexp, char, strict): assert not reg.must_startswith(char), '{reg!r} (case insensitive) MUST NOT start\'s with {chr!r}'.format(reg=regexp, chr=char) -def check_generate(regexp, values): +@pytest.mark.parametrize('regexp,values', ( + (r'foo', ['foo']), + (r'^sss', ['^sss']), + (r'(1)(2)(3)', ['123']), + (r'(1)((2)|(?:3))', ['12', '13']), + (r'(^1?2?|aa/)', ['^', '^1', '^2', '^12', 'aa/']), + (r'^https?://yandex.ru', ['^http://yandex|ru', '^https://yandex|ru']), + (r'(^bb|11)$', ['^bb$', '11$']), + (r'(http|https)', ['http', 'https']), + (r'1*', ['', '11111']), + (r'1*?', ['', '11111']), + (r'1[0]?2', ['102', '12']), + (r'1[0]2', ['102']), + (r'1+', ['11111']), + (r'[^/]?', ['', '|']), + (r'^http://(foo|bar)|baz', ['^http://foo', '^http://bar', 'baz']), + (r'[^\x00-\x7b|\x7e-\xff]', ['\x7d']), + (r'(a|b|c)', ['a', 'b', 'c']), + (r'[xyz]', ['x', 'y', 'z']) +)) +def test_generate(regexp, values): reg = Regexp(regexp) assert sorted(reg.generate('|', anchored=True)) == sorted(values) + + +def test_strict_generate(): + reg = Regexp('^foo|bar', strict=True) + assert sorted(reg.generate('|', anchored=True)) == sorted(['^foo', '^bar']) + + +def test_gen_anchor(): + + reg = Regexp('^some$') + val = next(reg.generate('', anchored=False)) + assert val == 'some' + + reg = Regexp('^some$') + val = next(reg.generate('', anchored=True)) + assert val == '^some$' + + reg = Regexp('^some$', strict=True) + val = next(reg.generate('', anchored=False)) + assert val == 'some' + + reg = Regexp('^some$', strict=True) + val = next(reg.generate('', anchored=True)) + assert val == '^some$' + + +def test_group_can_contains(): + source = '/some/(?P[^/:.]+)/' + reg = Regexp(source) + assert reg.can_contain('\n'), 'Whole regex "{src}" can contains {sym!r}'.format(src=source, sym='\\n') + + assert reg.group(0).can_contain('\n'), 'Group 0 from regex "{src}" can contains {sym!r}'.format(src=source, sym='\\n') + + assert reg.group('action').can_contain('\n'), 'Group "action" from regex "{src}" can contains {sym!r}'.format(src=source, sym='\\n') + + assert reg.group(1).can_contain('\n'), 'Group 1 from regex "{src}" can contains {sym!r}'.format(src=source, sym='\\n') + + assert not reg.group('action').can_contain('/'), 'Group "action" from regex "{src}" CAN\'T (!) contain {sym!r}'.format(src=source, sym='/') diff --git a/tests/parser/test_nginx_parser.py b/tests/parser/test_nginx_parser.py index 61bf3a9..2433703 100644 --- a/tests/parser/test_nginx_parser.py +++ b/tests/parser/test_nginx_parser.py @@ -1,3 +1,4 @@ +import pytest from gixy.parser.nginx_parser import NginxParser from gixy.directives.directive import * from gixy.directives.block import * @@ -7,8 +8,8 @@ def _parse(config): return NginxParser(cwd='', allow_includes=False).parse(config) -def test_directive(): - configs = [ +@pytest.mark.parametrize('config,expected', zip( + [ 'access_log syslog:server=127.0.0.1,tag=nginx_sentry toolsformat;', 'user http;', 'internal;', @@ -16,34 +17,35 @@ def test_directive(): "set $foo 'bar';", 'proxy_pass http://unix:/run/sock.socket;', 'rewrite ^/([a-zA-Z0-9]+)$ /$1/${arg_v}.pb break;' - ] + ], - expected = [ + [ [Directive], [Directive], [Directive], [Directive, SetDirective], + [Directive, SetDirective], [Directive], [Directive, RewriteDirective] ] - - for i, config in enumerate(configs): - return assert_config, config, expected[i] +)) +def test_directive(config, expected): + assert_config(config, expected) -def test_blocks(): - configs = [ +@pytest.mark.parametrize('config,expected', zip( + [ 'if (-f /some) {}', 'location / {}' - ] + ], - expected = [ + [ [Directive, Block, IfBlock], [Directive, Block, LocationBlock], ] - - for i, config in enumerate(configs): - yield assert_config, config, expected[i] +)) +def test_blocks(config, expected): + assert_config(config, expected) def test_dump_simple(): diff --git a/tests/plugins/test_simply.py b/tests/plugins/test_simply.py index 653e9fc..e54b60d 100644 --- a/tests/plugins/test_simply.py +++ b/tests/plugins/test_simply.py @@ -2,6 +2,7 @@ import os from os import path import json +import pytest from ..utils import * from gixy.core.manager import Manager as Gixy @@ -9,18 +10,13 @@ from gixy.core.config import Config -def setup_module(): - pass - - -def teardown_module(): - pass - - -def test_from_config(): +def generate_config_test_cases(): tested_plugins = set() tested_fp_plugins = set() + config_cases = [] + config_fp_cases = [] + conf_dir = path.join(path.dirname(__file__), 'simply') for plugin in os.listdir(conf_dir): if plugin in ('.', '..'): @@ -43,12 +39,10 @@ def test_from_config(): if not test_case.endswith('_fp.conf'): # Not False Positive test tested_plugins.add(plugin) - test_func = check_configuration + config_cases.append((plugin, config_path, config)) else: tested_fp_plugins.add(plugin) - test_func = check_configuration_fp - - yield test_func, plugin, config_path, config + config_fp_cases.append((plugin, config_path, config)) manager = PluginsManager() for plugin in manager.plugins: @@ -56,6 +50,11 @@ def test_from_config(): assert plugin in tested_plugins, 'Plugin {name!r} should have at least one simple test config'.format(name=plugin) assert plugin in tested_fp_plugins, 'Plugin {name!r} should have at least one simple test config with false positive'.format(name=plugin) + return config_cases, config_fp_cases + + +all_config_cases, all_config_fp_cases = generate_config_test_cases() + def parse_plugin_options(config_path): with open(config_path, 'r') as f: @@ -75,7 +74,8 @@ def yoda_provider(plugin, plugin_options=None): return Gixy(config=config) -def check_configuration(plugin, config_path, test_config): +@pytest.mark.parametrize('plugin,config_path,test_config', all_config_cases) +def test_configuration(plugin, config_path, test_config): plugin_options = parse_plugin_options(config_path) with yoda_provider(plugin, plugin_options) as yoda: yoda.audit(config_path, open(config_path, mode='r')) @@ -98,7 +98,8 @@ def check_configuration(plugin, config_path, test_config): assert result['help_url'].startswith('https://'), 'help_url must starts with https://. It\'is URL!' -def check_configuration_fp(plugin, config_path, test_config): +@pytest.mark.parametrize('plugin,config_path,test_config', all_config_fp_cases) +def test_configuration_fp(plugin, config_path, test_config): with yoda_provider(plugin) as yoda: yoda.audit(config_path, open(config_path, mode='r')) assert len([x for x in yoda.results]) == 0, 'False positive configuration must not trigger any plugins'