mirror of https://github.com/yandex/gixy
Migrate parameterized tests to pytest
parent
80cc1edc9c
commit
b5124787fb
|
@ -1,3 +1,4 @@
|
||||||
|
import pytest
|
||||||
from gixy.core.regexp import Regexp
|
from gixy.core.regexp import Regexp
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
@ -12,247 +13,268 @@ CATEGORIES:
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
def test_positive_contains():
|
@pytest.mark.parametrize('regexp,char', (
|
||||||
cases = (
|
(r'[a-z]', 'a'),
|
||||||
(r'[a-z]', 'a'),
|
(r'[a-z]*', 'a'),
|
||||||
(r'[a-z]*', 'a'),
|
(r'[a-z]*?', 'a'),
|
||||||
(r'[a-z]*?', 'a'),
|
(r'[a-z]+?', 'a'),
|
||||||
(r'[a-z]+?', 'a'),
|
(r'[a-z]', 'z'),
|
||||||
(r'[a-z]', 'z'),
|
(r'(?:a|b)', 'b'),
|
||||||
(r'(?:a|b)', 'b'),
|
(r'(/|:|[a-z])', 'g'),
|
||||||
(r'(/|:|[a-z])', 'g'),
|
(r'[^a-z]', '/'),
|
||||||
(r'[^a-z]', '/'),
|
(r'[^a-z]', '\n'),
|
||||||
(r'[^a-z]', '\n'),
|
(r'[^0]', '9'),
|
||||||
(r'[^0]', '9'),
|
(r'[^0-2]', '3'),
|
||||||
(r'[^0-2]', '3'),
|
(r'[^0123a-z]', '9'),
|
||||||
(r'[^0123a-z]', '9'),
|
(r'\s', '\x20'),
|
||||||
(r'\s', '\x20'),
|
(r'[^\s]', 'a'),
|
||||||
(r'[^\s]', 'a'),
|
(r'\d', '1'),
|
||||||
(r'\d', '1'),
|
(r'[^\d]', 'b'),
|
||||||
(r'[^\d]', 'b'),
|
(r'\w', '_'),
|
||||||
(r'\w', '_'),
|
(r'[^\w]', '\n'),
|
||||||
(r'[^\w]', '\n'),
|
(r'\W', '\n'),
|
||||||
(r'\W', '\n'),
|
(r'[^\W]', 'a'),
|
||||||
(r'[^\W]', 'a'),
|
(r'.', 'a')
|
||||||
(r'.', 'a')
|
))
|
||||||
)
|
def test_positive_contains(regexp, char):
|
||||||
for case in cases:
|
reg = Regexp(regexp, case_sensitive=True)
|
||||||
regexp, char = case
|
assert reg.can_contain(char), '{reg!r} should contain {chr!r}'.format(reg=regexp, chr=char)
|
||||||
yield check_positive_contain, regexp, char
|
|
||||||
|
reg = Regexp(regexp, case_sensitive=False)
|
||||||
|
char = char.upper()
|
||||||
|
assert reg.can_contain(char), '{reg!r} (case insensitive) should contain {chr!r}'.format(reg=regexp, chr=char)
|
||||||
|
|
||||||
|
|
||||||
def test_negative_contains():
|
@pytest.mark.parametrize('regexp,char', (
|
||||||
cases = (
|
('[a-z]', '1'),
|
||||||
('[a-z]', '1'),
|
('[a-z]*', '2'),
|
||||||
('[a-z]*', '2'),
|
('[a-z]*?', '3'),
|
||||||
('[a-z]*?', '3'),
|
('[a-z]+?', '4'),
|
||||||
('[a-z]+?', '4'),
|
('[a-z]', '\n'),
|
||||||
('[a-z]', '\n'),
|
('(?:a|b)', 'c'),
|
||||||
('(?:a|b)', 'c'),
|
('(/|:|[a-z])', '\n'),
|
||||||
('(/|:|[a-z])', '\n'),
|
('[^a-z]', 'a'),
|
||||||
('[^a-z]', 'a'),
|
('[^0]', '0'),
|
||||||
('[^0]', '0'),
|
('[^0-2]', '0'),
|
||||||
('[^0-2]', '0'),
|
('[^0123a-z]', 'z'),
|
||||||
('[^0123a-z]', 'z'),
|
(r'\s', 'a'),
|
||||||
(r'\s', 'a'),
|
(r'[^\s]', '\n'),
|
||||||
(r'[^\s]', '\n'),
|
(r'\d', 'f'),
|
||||||
(r'\d', 'f'),
|
(r'[^\d]', '2'),
|
||||||
(r'[^\d]', '2'),
|
(r'\w', '\n'),
|
||||||
(r'\w', '\n'),
|
(r'[^\w]', '_'),
|
||||||
(r'[^\w]', '_'),
|
(r'\W', 'a'),
|
||||||
(r'\W', 'a'),
|
(r'[^\W]', '\n'),
|
||||||
(r'[^\W]', '\n'),
|
(r'.', '\n')
|
||||||
(r'.', '\n')
|
))
|
||||||
)
|
def test_negative_contains(regexp, char):
|
||||||
for case in cases:
|
reg = Regexp(regexp, case_sensitive=True)
|
||||||
regexp, char = case
|
assert not reg.can_contain(char), '{reg!r} should not contain {chr!r}'.format(reg=regexp, chr=char)
|
||||||
yield check_negative_contain, regexp, char
|
|
||||||
|
reg = Regexp(regexp, case_sensitive=False)
|
||||||
|
char = char.upper()
|
||||||
|
assert not reg.can_contain(char), '{reg!r} (case insensitive) should not contain {chr!r}'.format(reg=regexp, chr=char)
|
||||||
|
|
||||||
|
|
||||||
def test_groups_names():
|
@pytest.mark.parametrize('regexp,groups', (
|
||||||
cases = (
|
('foo', [0]),
|
||||||
('foo', [0]),
|
('(1)(2)(?:3)', [0, 1, 2]),
|
||||||
('(1)(2)(?:3)', [0, 1, 2]),
|
('(1)((2)|(?:3))', [0, 1, 2, 3]),
|
||||||
('(1)((2)|(?:3))', [0, 1, 2, 3]),
|
("(?'pcre_7'1as)(?P<outer>(?<inner>2)|(?:3))", [0, 1, 2, 3, 'pcre_7', 'outer', 'inner']),
|
||||||
("(?'pcre_7'1as)(?P<outer>(?<inner>2)|(?:3))", [0, 1, 2, 3, 'pcre_7', 'outer', 'inner']),
|
('/proxy/(?<proxy>.*)$', [0, 1, 'proxy'])
|
||||||
('/proxy/(?<proxy>.*)$', [0, 1, 'proxy'])
|
))
|
||||||
)
|
def test_groups_names(regexp, groups):
|
||||||
for case in cases:
|
reg = Regexp(regexp)
|
||||||
regexp, groups = case
|
assert set(reg.groups.keys()) == set(groups)
|
||||||
yield check_groups_names, regexp, groups
|
|
||||||
|
|
||||||
|
|
||||||
def test_to_string():
|
@pytest.mark.parametrize('regexp,string', (
|
||||||
cases = (
|
(r'foo', 'foo'),
|
||||||
(r'foo', 'foo'),
|
(r'(1)(2)(?:3)', '(1)(2)(?:3)'),
|
||||||
(r'(1)(2)(?:3)', '(1)(2)(?:3)'),
|
(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'\w|1|3-5|[a-z]', '(?:[\w]|1|3\\-5|[a-z])'),
|
(r'(1|(?:3)|([4-6]))', '((?:1|(?:3)|([4-6])))'),
|
||||||
(r'(1|(?:3)|([4-6]))', '((?:1|(?:3)|([4-6])))'),
|
(r'(1|(?:3)|(?P<aaa>[4-6]))', '((?:1|(?:3)|([4-6])))'),
|
||||||
(r'(1|(?:3)|(?P<aaa>[4-6]))', '((?:1|(?:3)|([4-6])))'),
|
(r'^sss', '^sss'),
|
||||||
(r'^sss', '^sss'),
|
(r'(^bb|11)$', '((?:^bb|11))$'),
|
||||||
(r'(^bb|11)$', '((?:^bb|11))$'),
|
(r'(http|https)', '(http(?:|s))'),
|
||||||
(r'(http|https)', '(http(?:|s))'),
|
(r'1*', '1*'),
|
||||||
(r'1*', '1*'),
|
(r'1*?', '1*?'),
|
||||||
(r'1*?', '1*?'),
|
(r'1+', '1+'),
|
||||||
(r'1+', '1+'),
|
))
|
||||||
)
|
def test_to_string(regexp, string):
|
||||||
for case in cases:
|
reg = Regexp(regexp)
|
||||||
regexp, string = case
|
assert str(reg) == string
|
||||||
yield check_to_string, regexp, string
|
|
||||||
|
|
||||||
|
|
||||||
def test_positive_startswith():
|
@pytest.mark.parametrize('regexp,char,strict', (
|
||||||
cases = (
|
(r'foo', 'q', False),
|
||||||
(r'foo', 'q', False),
|
(r'foo', 'f', True),
|
||||||
(r'foo', 'f', True),
|
(r'^foo', 'f', False),
|
||||||
(r'^foo', 'f', False),
|
(r'(^foo)', 'f', False),
|
||||||
(r'(^foo)', 'f', False),
|
(r'(^foo)', 'f', True),
|
||||||
(r'(^foo)', 'f', True),
|
(r'(^foo|g)', 'f', True),
|
||||||
(r'(^foo|g)', 'f', True),
|
(r'(^foo|g)', 'g', True),
|
||||||
(r'(^foo|g)', 'g', True),
|
(r'(^foo|g)', 'q', False),
|
||||||
(r'(^foo|g)', 'q', False),
|
(r'^[^/]+', '\n', True),
|
||||||
(r'^[^/]+', '\n', True),
|
(r'/[^/]+', '/', True),
|
||||||
(r'/[^/]+', '/', True),
|
(r'((a))', 'a', False),
|
||||||
(r'((a))', 'a', False),
|
(r'((a))', 'b', False),
|
||||||
(r'((a))', 'b', False),
|
(r'^[a-z]{0}0', '0', False),
|
||||||
(r'^[a-z]{0}0', '0', False),
|
(r'^[a-z]{1}0', 'a', False),
|
||||||
(r'^[a-z]{1}0', 'a', False),
|
))
|
||||||
)
|
def test_positive_startswith(regexp, char, strict):
|
||||||
for case in cases:
|
reg = Regexp(regexp, case_sensitive=True, strict=strict)
|
||||||
regexp, check, strict = case
|
assert reg.can_startswith(char), '{reg!r} can start\'s with {chr!r}'.format(reg=regexp, chr=char)
|
||||||
yield check_positive_startswith, regexp, check, strict
|
|
||||||
|
reg = Regexp(regexp, case_sensitive=False, strict=strict)
|
||||||
|
char = char.upper()
|
||||||
|
assert reg.can_startswith(char), '{reg!r} (case insensitive) can start\'s with {chr!r}'.format(reg=regexp, chr=char)
|
||||||
|
|
||||||
|
|
||||||
def test_negative_startswith():
|
@pytest.mark.parametrize('regexp,char,strict', (
|
||||||
cases = (
|
(r'foo', '\n', False),
|
||||||
(r'foo', '\n', False),
|
(r'foo', 'o', True),
|
||||||
(r'foo', 'o', True),
|
(r'^foo', 'o', False),
|
||||||
(r'^foo', 'o', False),
|
(r'(^foo)', 'q', False),
|
||||||
(r'(^foo)', 'q', False),
|
(r'(^foo)', 'q', True),
|
||||||
(r'(^foo)', 'q', True),
|
(r'(^foo|g)', 'q', True),
|
||||||
(r'(^foo|g)', 'q', True),
|
(r'(^foo|g)', 'o', True),
|
||||||
(r'(^foo|g)', 'o', True),
|
(r'(^foo|g)', '\n', False),
|
||||||
(r'(^foo|g)', '\n', False),
|
(r'^[^/]+', '/', True),
|
||||||
(r'^[^/]+', '/', True),
|
(r'/[^/]+', 'a', True),
|
||||||
(r'/[^/]+', 'a', True),
|
(r'((abc)|(ss))', 'b', True),
|
||||||
(r'((abc)|(ss))', 'b', True),
|
(r'^[a-z]{0}0', 'a', False),
|
||||||
(r'^[a-z]{0}0', 'a', False),
|
(r'^[a-z]{0}0', 'g', False),
|
||||||
(r'^[a-z]{0}0', 'g', False),
|
))
|
||||||
)
|
def test_negative_startswith(regexp, char, strict):
|
||||||
for case in cases:
|
reg = Regexp(regexp, case_sensitive=True, strict=strict)
|
||||||
regexp, check, strict = case
|
assert not reg.can_startswith(char), '{reg!r} can\'t start\'s with {chr!r}'.format(reg=regexp, chr=char)
|
||||||
yield check_negative_startswith, regexp, check, strict
|
|
||||||
|
reg = Regexp(regexp, case_sensitive=False, strict=strict)
|
||||||
|
char = char.upper()
|
||||||
|
assert not reg.can_startswith(char), '{reg!r} (case insensitive) can\'t start\'s with {chr!r}'.format(reg=regexp, chr=char)
|
||||||
|
|
||||||
|
|
||||||
def test_positive_must_contain():
|
@pytest.mark.parametrize('regexp,char', (
|
||||||
cases = (
|
(r'abc', 'a'),
|
||||||
(r'abc', 'a'),
|
(r'abc', 'b'),
|
||||||
(r'abc', 'b'),
|
(r'abc', 'c'),
|
||||||
(r'abc', 'c'),
|
(r'3+', '3'),
|
||||||
(r'3+', '3'),
|
(r'[0]', '0'),
|
||||||
(r'[0]', '0'),
|
(r'([0])', '0'),
|
||||||
(r'([0])', '0'),
|
(r'(?:[0])', '0'),
|
||||||
(r'(?:[0])', '0'),
|
(r'(?:[0])|0|((((0))))', '0'),
|
||||||
(r'(?:[0])|0|((((0))))', '0'),
|
))
|
||||||
)
|
def test_positive_must_contain(regexp, char):
|
||||||
for case in cases:
|
reg = Regexp(regexp, case_sensitive=True)
|
||||||
regexp, char = case
|
assert reg.must_contain(char), '{reg!r} must contain with {chr!r}'.format(reg=regexp, chr=char)
|
||||||
yield check_positive_must_contain, regexp, char
|
|
||||||
|
reg = Regexp(regexp, case_sensitive=False)
|
||||||
|
char = char.upper()
|
||||||
|
assert reg.must_contain(char), '{reg!r} (case insensitive) must contain with {chr!r}'.format(reg=regexp, chr=char)
|
||||||
|
|
||||||
|
|
||||||
def test_negative_must_contain():
|
@pytest.mark.parametrize('regexp,char', (
|
||||||
cases = (
|
(r'[a-z]', '1'),
|
||||||
(r'[a-z]', '1'),
|
(r'2{0}1', '2'),
|
||||||
(r'2{0}1', '2'),
|
(r'3?', '3'),
|
||||||
(r'3?', '3'),
|
(r'3*', '3'),
|
||||||
(r'3*', '3'),
|
(r'3*?', '3'),
|
||||||
(r'3*?', '3'),
|
(r'3+a', 'b'),
|
||||||
(r'3+a', 'b'),
|
(r'[a-z]', 'a'),
|
||||||
(r'[a-z]', 'a'),
|
(r'(?:a|b)', 'a'),
|
||||||
(r'(?:a|b)', 'a'),
|
(r'(?:a|b)', 'b'),
|
||||||
(r'(?:a|b)', 'b'),
|
(r'(/|:|[a-z])', '/'),
|
||||||
(r'(/|:|[a-z])', '/'),
|
(r'(/|:|[a-z])', 'z'),
|
||||||
(r'(/|:|[a-z])', 'z'),
|
(r'[^a-z]', '\n'),
|
||||||
(r'[^a-z]', '\n'),
|
(r'[^0]', '0'),
|
||||||
(r'[^0]', '0'),
|
(r'[^0-2]', '0'),
|
||||||
(r'[^0-2]', '0'),
|
(r'[^0123a-z]', 'z'),
|
||||||
(r'[^0123a-z]', 'z'),
|
(r'\s', '\x20'),
|
||||||
(r'\s', '\x20'),
|
(r'[^\s]', '\n'),
|
||||||
(r'[^\s]', '\n'),
|
(r'\d', '3'),
|
||||||
(r'\d', '3'),
|
(r'[^\d]', 'a'),
|
||||||
(r'[^\d]', 'a'),
|
(r'\w', 'a'),
|
||||||
(r'\w', 'a'),
|
(r'[^\w]', '\n'),
|
||||||
(r'[^\w]', '\n'),
|
(r'\W', '\n'),
|
||||||
(r'\W', '\n'),
|
(r'[^\W]', 'a'),
|
||||||
(r'[^\W]', 'a'),
|
(r'.', '\n')
|
||||||
(r'.', '\n')
|
))
|
||||||
)
|
def test_negative_must_contain(regexp, char):
|
||||||
for case in cases:
|
reg = Regexp(regexp, case_sensitive=True)
|
||||||
regexp, char = case
|
assert not reg.must_contain(char), '{reg!r} must NOT contain with {chr!r}'.format(reg=regexp, chr=char)
|
||||||
yield check_negative_must_contain, regexp, char
|
|
||||||
|
reg = Regexp(regexp, case_sensitive=False)
|
||||||
|
char = char.upper()
|
||||||
|
assert not reg.must_contain(char), '{reg!r} (case insensitive) must NOT contain with {chr!r}'.format(reg=regexp, chr=char)
|
||||||
|
|
||||||
|
|
||||||
def test_positive_must_startswith():
|
@pytest.mark.parametrize('regexp,char,strict', (
|
||||||
cases = (
|
(r'foo', 'f', True),
|
||||||
(r'foo', 'f', True),
|
(r'^foo', 'f', False),
|
||||||
(r'^foo', 'f', False),
|
(r'(^foo)', 'f', True),
|
||||||
(r'(^foo)', 'f', True),
|
(r'^((a))', 'a', False),
|
||||||
(r'^((a))', 'a', False),
|
(r'((a))', 'a', True),
|
||||||
(r'((a))', 'a', True),
|
(r'^[a-z]{0}0', '0', False),
|
||||||
(r'^[a-z]{0}0', '0', False),
|
(r'^a{1}0', 'a', False),
|
||||||
(r'^a{1}0', 'a', False),
|
))
|
||||||
)
|
def test_positive_must_startswith(regexp, char, strict):
|
||||||
for case in cases:
|
reg = Regexp(regexp, case_sensitive=True, strict=strict)
|
||||||
regexp, check, strict = case
|
assert reg.must_startswith(char), '{reg!r} MUST start\'s with {chr!r}'.format(reg=regexp, chr=char)
|
||||||
yield check_positive_must_startswith, regexp, check, strict
|
|
||||||
|
reg = Regexp(regexp, case_sensitive=False, strict=strict)
|
||||||
|
char = char.upper()
|
||||||
|
assert reg.must_startswith(char), '{reg!r} (case insensitive) MUST start\'s with {chr!r}'.format(reg=regexp, chr=char)
|
||||||
|
|
||||||
|
|
||||||
def test_negative_must_startswith():
|
@pytest.mark.parametrize('regexp,char,strict', (
|
||||||
cases = (
|
(r'foo', 'o', False),
|
||||||
(r'foo', 'o', False),
|
(r'^foo', 'o', False),
|
||||||
(r'^foo', 'o', False),
|
(r'(^foo)', 'o', False),
|
||||||
(r'(^foo)', 'o', False),
|
(r'[a-z]', '1', True),
|
||||||
(r'[a-z]', '1', True),
|
(r'[a-z]', 'a', True),
|
||||||
(r'[a-z]', 'a', True),
|
(r'/[^/]+', 'a', True),
|
||||||
(r'/[^/]+', 'a', True),
|
(r'3?', '3', True),
|
||||||
(r'3?', '3', True),
|
(r'3*', '3', True),
|
||||||
(r'3*', '3', True),
|
(r'3*?', '3', True),
|
||||||
(r'3*?', '3', True),
|
(r'3+a', 'b', True),
|
||||||
(r'3+a', 'b', True),
|
(r'^((a))', 'b', False),
|
||||||
(r'^((a))', 'b', False),
|
(r'((a))', 'a', False),
|
||||||
(r'((a))', 'a', False),
|
(r'^a{0}0', 'a', False),
|
||||||
(r'^a{0}0', 'a', False),
|
))
|
||||||
)
|
def test_negative_must_startswith(regexp, char, strict):
|
||||||
for case in cases:
|
reg = Regexp(regexp, case_sensitive=True, strict=strict)
|
||||||
regexp, check, strict = case
|
assert not reg.must_startswith(char), '{reg!r} MUST NOT start\'s with {chr!r}'.format(reg=regexp, chr=char)
|
||||||
yield check_negative_must_startswith, regexp, check, strict
|
|
||||||
|
reg = Regexp(regexp, case_sensitive=False, strict=strict)
|
||||||
|
char = char.upper()
|
||||||
|
assert not reg.must_startswith(char), '{reg!r} (case insensitive) MUST NOT start\'s with {chr!r}'.format(reg=regexp, chr=char)
|
||||||
|
|
||||||
|
|
||||||
def test_generate():
|
@pytest.mark.parametrize('regexp,values', (
|
||||||
cases = (
|
(r'foo', ['foo']),
|
||||||
(r'foo', ['foo']),
|
(r'^sss', ['^sss']),
|
||||||
(r'^sss', ['^sss']),
|
(r'(1)(2)(3)', ['123']),
|
||||||
(r'(1)(2)(3)', ['123']),
|
(r'(1)((2)|(?:3))', ['12', '13']),
|
||||||
(r'(1)((2)|(?:3))', ['12', '13']),
|
(r'(^1?2?|aa/)', ['^', '^1', '^2', '^12', 'aa/']),
|
||||||
(r'(^1?2?|aa/)', ['^', '^1', '^2', '^12', 'aa/']),
|
(r'^https?://yandex.ru', ['^http://yandex|ru', '^https://yandex|ru']),
|
||||||
(r'^https?://yandex.ru', ['^http://yandex|ru', '^https://yandex|ru']),
|
(r'(^bb|11)$', ['^bb$', '11$']),
|
||||||
(r'(^bb|11)$', ['^bb$', '11$']),
|
(r'(http|https)', ['http', 'https']),
|
||||||
(r'(http|https)', ['http', 'https']),
|
(r'1*', ['', '11111']),
|
||||||
(r'1*', ['', '11111']),
|
(r'1*?', ['', '11111']),
|
||||||
(r'1*?', ['', '11111']),
|
(r'1[0]?2', ['102', '12']),
|
||||||
(r'1[0]?2', ['102', '12']),
|
(r'1[0]2', ['102']),
|
||||||
(r'1[0]2', ['102']),
|
(r'1+', ['11111']),
|
||||||
(r'1+', ['11111']),
|
(r'[^/]?', ['', '|']),
|
||||||
(r'[^/]?', ['', '|']),
|
(r'^http://(foo|bar)|baz', ['^http://foo', '^http://bar', 'baz']),
|
||||||
(r'^http://(foo|bar)|baz', ['^http://foo', '^http://bar', 'baz']),
|
(r'[^\x00-\x7b|\x7e-\xff]', ['\x7d']),
|
||||||
(r'[^\x00-\x7b|\x7e-\xff]', ['\x7d']),
|
(r'(a|b|c)', ['a', 'b', 'c']),
|
||||||
(r'(a|b|c)', ['a', 'b', 'c']),
|
(r'[xyz]', ['x', 'y', 'z'])
|
||||||
(r'[xyz]', ['x', 'y', 'z'])
|
))
|
||||||
)
|
def test_generate(regexp, values):
|
||||||
for case in cases:
|
reg = Regexp(regexp)
|
||||||
regexp, values = case
|
assert sorted(reg.generate('|', anchored=True)) == sorted(values)
|
||||||
yield check_generate, regexp, values
|
|
||||||
|
|
||||||
|
|
||||||
def test_strict_generate():
|
def test_strict_generate():
|
||||||
|
@ -291,90 +313,3 @@ def test_group_can_contains():
|
||||||
assert reg.group(1).can_contain('\n'), 'Group 1 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='/')
|
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):
|
|
||||||
reg = Regexp(regexp, case_sensitive=True)
|
|
||||||
assert reg.can_contain(char), '{reg!r} should contain {chr!r}'.format(reg=regexp, chr=char)
|
|
||||||
|
|
||||||
reg = Regexp(regexp, case_sensitive=False)
|
|
||||||
char = char.upper()
|
|
||||||
assert reg.can_contain(char), '{reg!r} (case insensitive) should contain {chr!r}'.format(reg=regexp, chr=char)
|
|
||||||
|
|
||||||
|
|
||||||
def check_negative_contain(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)
|
|
||||||
|
|
||||||
reg = Regexp(regexp, case_sensitive=False)
|
|
||||||
char = char.upper()
|
|
||||||
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):
|
|
||||||
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)
|
|
||||||
|
|
||||||
reg = Regexp(regexp, case_sensitive=False, strict=strict)
|
|
||||||
char = char.upper()
|
|
||||||
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):
|
|
||||||
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)
|
|
||||||
|
|
||||||
reg = Regexp(regexp, case_sensitive=False, strict=strict)
|
|
||||||
char = char.upper()
|
|
||||||
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):
|
|
||||||
reg = Regexp(regexp, case_sensitive=True)
|
|
||||||
assert reg.must_contain(char), '{reg!r} must contain with {chr!r}'.format(reg=regexp, chr=char)
|
|
||||||
|
|
||||||
reg = Regexp(regexp, case_sensitive=False)
|
|
||||||
char = char.upper()
|
|
||||||
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):
|
|
||||||
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)
|
|
||||||
|
|
||||||
reg = Regexp(regexp, case_sensitive=False)
|
|
||||||
char = char.upper()
|
|
||||||
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):
|
|
||||||
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)
|
|
||||||
|
|
||||||
reg = Regexp(regexp, case_sensitive=False, strict=strict)
|
|
||||||
char = char.upper()
|
|
||||||
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):
|
|
||||||
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)
|
|
||||||
|
|
||||||
reg = Regexp(regexp, case_sensitive=False, strict=strict)
|
|
||||||
char = char.upper()
|
|
||||||
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):
|
|
||||||
reg = Regexp(regexp)
|
|
||||||
assert sorted(reg.generate('|', anchored=True)) == sorted(values)
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import pytest
|
||||||
from gixy.parser.nginx_parser import NginxParser
|
from gixy.parser.nginx_parser import NginxParser
|
||||||
from gixy.directives.directive import *
|
from gixy.directives.directive import *
|
||||||
from gixy.directives.block import *
|
from gixy.directives.block import *
|
||||||
|
@ -7,8 +8,8 @@ def _parse(config):
|
||||||
return NginxParser(cwd='', allow_includes=False).parse(config)
|
return NginxParser(cwd='', allow_includes=False).parse(config)
|
||||||
|
|
||||||
|
|
||||||
def test_directive():
|
@pytest.mark.parametrize('config,expected', zip(
|
||||||
configs = [
|
[
|
||||||
'access_log syslog:server=127.0.0.1,tag=nginx_sentry toolsformat;',
|
'access_log syslog:server=127.0.0.1,tag=nginx_sentry toolsformat;',
|
||||||
'user http;',
|
'user http;',
|
||||||
'internal;',
|
'internal;',
|
||||||
|
@ -16,34 +17,35 @@ def test_directive():
|
||||||
"set $foo 'bar';",
|
"set $foo 'bar';",
|
||||||
'proxy_pass http://unix:/run/sock.socket;',
|
'proxy_pass http://unix:/run/sock.socket;',
|
||||||
'rewrite ^/([a-zA-Z0-9]+)$ /$1/${arg_v}.pb break;'
|
'rewrite ^/([a-zA-Z0-9]+)$ /$1/${arg_v}.pb break;'
|
||||||
]
|
],
|
||||||
|
|
||||||
expected = [
|
[
|
||||||
[Directive],
|
[Directive],
|
||||||
[Directive],
|
[Directive],
|
||||||
[Directive],
|
[Directive],
|
||||||
[Directive, SetDirective],
|
[Directive, SetDirective],
|
||||||
|
[Directive, SetDirective],
|
||||||
[Directive],
|
[Directive],
|
||||||
[Directive, RewriteDirective]
|
[Directive, RewriteDirective]
|
||||||
]
|
]
|
||||||
|
))
|
||||||
for i, config in enumerate(configs):
|
def test_directive(config, expected):
|
||||||
return assert_config, config, expected[i]
|
assert_config(config, expected)
|
||||||
|
|
||||||
|
|
||||||
def test_blocks():
|
@pytest.mark.parametrize('config,expected', zip(
|
||||||
configs = [
|
[
|
||||||
'if (-f /some) {}',
|
'if (-f /some) {}',
|
||||||
'location / {}'
|
'location / {}'
|
||||||
]
|
],
|
||||||
|
|
||||||
expected = [
|
[
|
||||||
[Directive, Block, IfBlock],
|
[Directive, Block, IfBlock],
|
||||||
[Directive, Block, LocationBlock],
|
[Directive, Block, LocationBlock],
|
||||||
]
|
]
|
||||||
|
))
|
||||||
for i, config in enumerate(configs):
|
def test_blocks(config, expected):
|
||||||
yield assert_config, config, expected[i]
|
assert_config(config, expected)
|
||||||
|
|
||||||
|
|
||||||
def test_dump_simple():
|
def test_dump_simple():
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import os
|
import os
|
||||||
from os import path
|
from os import path
|
||||||
import json
|
import json
|
||||||
|
import pytest
|
||||||
|
|
||||||
from ..utils import *
|
from ..utils import *
|
||||||
from gixy.core.manager import Manager as Gixy
|
from gixy.core.manager import Manager as Gixy
|
||||||
|
@ -8,18 +9,13 @@ from gixy.core.plugins_manager import PluginsManager
|
||||||
from gixy.core.config import Config
|
from gixy.core.config import Config
|
||||||
|
|
||||||
|
|
||||||
def setup_module():
|
def generate_config_test_cases():
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def teardown_module():
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def test_from_config():
|
|
||||||
tested_plugins = set()
|
tested_plugins = set()
|
||||||
tested_fp_plugins = set()
|
tested_fp_plugins = set()
|
||||||
|
|
||||||
|
config_cases = []
|
||||||
|
config_fp_cases = []
|
||||||
|
|
||||||
conf_dir = path.join(path.dirname(__file__), 'simply')
|
conf_dir = path.join(path.dirname(__file__), 'simply')
|
||||||
for plugin in os.listdir(conf_dir):
|
for plugin in os.listdir(conf_dir):
|
||||||
if plugin in ('.', '..'):
|
if plugin in ('.', '..'):
|
||||||
|
@ -42,12 +38,10 @@ def test_from_config():
|
||||||
if not test_case.endswith('_fp.conf'):
|
if not test_case.endswith('_fp.conf'):
|
||||||
# Not False Positive test
|
# Not False Positive test
|
||||||
tested_plugins.add(plugin)
|
tested_plugins.add(plugin)
|
||||||
test_func = check_configuration
|
config_cases.append((plugin, config_path, config))
|
||||||
else:
|
else:
|
||||||
tested_fp_plugins.add(plugin)
|
tested_fp_plugins.add(plugin)
|
||||||
test_func = check_configuration_fp
|
config_fp_cases.append((plugin, config_path, config))
|
||||||
|
|
||||||
yield test_func, plugin, config_path, config
|
|
||||||
|
|
||||||
manager = PluginsManager()
|
manager = PluginsManager()
|
||||||
for plugin in manager.plugins:
|
for plugin in manager.plugins:
|
||||||
|
@ -55,6 +49,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_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)
|
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):
|
def parse_plugin_options(config_path):
|
||||||
with open(config_path, 'r') as f:
|
with open(config_path, 'r') as f:
|
||||||
|
@ -74,7 +73,8 @@ def yoda_provider(plugin, plugin_options=None):
|
||||||
return Gixy(config=config)
|
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)
|
plugin_options = parse_plugin_options(config_path)
|
||||||
with yoda_provider(plugin, plugin_options) as yoda:
|
with yoda_provider(plugin, plugin_options) as yoda:
|
||||||
yoda.audit(config_path, open(config_path, mode='r'))
|
yoda.audit(config_path, open(config_path, mode='r'))
|
||||||
|
@ -97,7 +97,8 @@ def check_configuration(plugin, config_path, test_config):
|
||||||
assert result['help_url'].startswith('https://'), 'help_url must starts with https://. It\'is URL!'
|
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:
|
with yoda_provider(plugin) as yoda:
|
||||||
yoda.audit(config_path, open(config_path, mode='r'))
|
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'
|
assert len([x for x in yoda.results]) == 0, 'False positive configuration must not trigger any plugins'
|
||||||
|
|
Loading…
Reference in New Issue