diff --git a/requirements.dev.txt b/requirements.dev.txt index ea9085e..4bbb3d5 100644 --- a/requirements.dev.txt +++ b/requirements.dev.txt @@ -1,5 +1,4 @@ -nose>=1.3.7 -mock>=2.0.0 +pytest>=7.0.0 coverage>=4.3 flake8>=3.2 -tox>=2.7.0 \ No newline at end of file +tox>=2.7.0 diff --git a/setup.py b/setup.py index 3323b7b..abb08c2 100644 --- a/setup.py +++ b/setup.py @@ -42,7 +42,6 @@ entry_points={ 'console_scripts': ['gixy=gixy.cli.main:main'], }, - test_suite='nose.collector', packages=find_packages(exclude=['tests', 'tests.*']), classifiers=[ 'Development Status :: 3 - Alpha', diff --git a/tests/asserts.py b/tests/asserts.py deleted file mode 100644 index 4a0a379..0000000 --- a/tests/asserts.py +++ /dev/null @@ -1,36 +0,0 @@ -from nose.tools import assert_true, assert_false - - -''' -Various nose.tools helpers that doesn't exists in Python 2.6 Unittest :( -Must be removed with drop Python 2.6 support -''' - - -def assert_is_instance(obj, cls, msg=None): - """Same as assert_true(isinstance(obj, cls)), with a nicer - default message.""" - if not msg: - msg = '{orig} is not an instance of {test}'.format(orig=type(obj), test=cls) - assert_true(isinstance(obj, cls), msg=msg) - - -def assert_is_none(obj, msg=None): - """Same as assert_true(obj is None), with a nicer default message.""" - if not msg: - msg = '{orig!r} is not None'.format(orig=obj) - assert_true(obj is None, msg=msg) - - -def assert_is_not_none(obj, msg=None): - """Same as assert_false(obj is None), with a nicer default message.""" - if not msg: - msg = '{orig!r} is None'.format(orig=obj) - assert_false(obj is None, msg=msg) - - -def assert_in(member, container, msg=None): - """Just like assert_true(a in b), but with a nicer default message.""" - if not msg: - msg = '{member!r} not found in {container!r}'.format(member=member, container=container) - assert_true(member in container, msg=msg) diff --git a/tests/core/test_context.py b/tests/core/test_context.py index a095c83..2238d0e 100644 --- a/tests/core/test_context.py +++ b/tests/core/test_context.py @@ -1,55 +1,51 @@ -from nose.tools import with_setup, assert_equals, assert_not_equals, assert_true from gixy.core.context import get_context, pop_context, push_context, purge_context, CONTEXTS, Context from gixy.directives.block import Root from gixy.core.variable import Variable from gixy.core.regexp import Regexp -def setup(): - assert_equals(len(CONTEXTS), 0) +def setup_function(): + assert len(CONTEXTS) == 0 -def tear_down(): +def teardown_function(): purge_context() -@with_setup(setup, tear_down) def test_push_pop_context(): root_a = Root() push_context(root_a) - assert_equals(len(CONTEXTS), 1) + assert len(CONTEXTS) == 1 root_b = Root() push_context(root_b) - assert_equals(len(CONTEXTS), 2) + assert len(CONTEXTS) == 2 poped = pop_context() - assert_equals(len(CONTEXTS), 1) - assert_equals(poped.block, root_b) + assert len(CONTEXTS) == 1 + assert poped.block == root_b poped = pop_context() - assert_equals(len(CONTEXTS), 0) - assert_equals(poped.block, root_a) + assert len(CONTEXTS) == 0 + assert poped.block == root_a -@with_setup(setup, tear_down) def test_push_get_purge_context(): root = Root() push_context(root) - assert_equals(len(CONTEXTS), 1) - assert_equals(get_context().block, root) + assert len(CONTEXTS) == 1 + assert get_context().block == root root = Root() push_context(root) - assert_equals(len(CONTEXTS), 2) - assert_equals(get_context().block, root) + assert len(CONTEXTS) == 2 + assert get_context().block == root purge_context() - assert_equals(len(CONTEXTS), 0) + assert len(CONTEXTS) == 0 -@with_setup(setup, tear_down) def test_add_variables(): context = push_context(Root()) - assert_equals(len(context.variables['index']), 0) - assert_equals(len(context.variables['name']), 0) + assert len(context.variables['index']) == 0 + assert len(context.variables['name']) == 0 one_str_var = Variable('1') context.add_var('1', one_str_var) @@ -58,77 +54,74 @@ def test_add_variables(): some_var = Variable('some') context.add_var('some', some_var) - assert_equals(len(context.variables['index']), 1) - assert_equals(context.variables['index'][1], one_int_var) - assert_equals(len(context.variables['name']), 1) - assert_equals(context.variables['name']['some'], some_var) + assert len(context.variables['index']) == 1 + assert context.variables['index'][1] == one_int_var + assert len(context.variables['name']) == 1 + assert context.variables['name']['some'] == some_var context.clear_index_vars() - assert_equals(len(context.variables['index']), 0) - assert_equals(len(context.variables['name']), 1) - assert_equals(context.variables['name']['some'], some_var) + assert len(context.variables['index']) == 0 + assert len(context.variables['name']) == 1 + assert context.variables['name']['some'] == some_var -@with_setup(setup, tear_down) def test_get_variables(): context = push_context(Root()) - assert_equals(len(context.variables['index']), 0) - assert_equals(len(context.variables['name']), 0) + assert len(context.variables['index']) == 0 + assert len(context.variables['name']) == 0 one_var = Variable(1) context.add_var(1, one_var) some_var = Variable('some') context.add_var('some', some_var) - assert_equals(context.get_var(1), one_var) - assert_equals(context.get_var('some'), some_var) + assert context.get_var(1) == one_var + assert context.get_var('some') == some_var # Checks not existed variables, for now context may return None - assert_equals(context.get_var(0), None) - assert_equals(context.get_var('not_existed'), None) + assert context.get_var(0) == None + assert context.get_var('not_existed') == None # Checks builtins variables - assert_true(context.get_var('uri')) - assert_true(context.get_var('document_uri')) - assert_true(context.get_var('arg_asdsadasd')) - assert_true(context.get_var('args')) + assert context.get_var('uri') + assert context.get_var('document_uri') + assert context.get_var('arg_asdsadasd') + assert context.get_var('args') -@with_setup(setup, tear_down) def test_context_depend_variables(): push_context(Root()) - assert_equals(len(get_context().variables['index']), 0) - assert_equals(len(get_context().variables['name']), 0) + assert len(get_context().variables['index']) == 0 + assert len(get_context().variables['name']) == 0 get_context().add_var(1, Variable(1, value='one')) get_context().add_var('some', Variable('some', value='some')) - assert_equals(get_context().get_var(1).value, 'one') - assert_equals(get_context().get_var('some').value, 'some') + assert get_context().get_var(1).value == 'one' + assert get_context().get_var('some').value == 'some' # Checks top context variables are still exists push_context(Root()) - assert_equals(get_context().get_var(1).value, 'one') - assert_equals(get_context().get_var('some').value, 'some') + assert get_context().get_var(1).value == 'one' + assert get_context().get_var('some').value == 'some' # Checks variable overriding get_context().add_var('some', Variable('some', value='some_new')) get_context().add_var('foo', Variable('foo', value='foo')) - assert_not_equals(get_context().get_var('some').value, 'some') - assert_equals(get_context().get_var('some').value, 'some_new') - assert_equals(get_context().get_var('foo').value, 'foo') - assert_equals(get_context().get_var(1).value, 'one') + assert get_context().get_var('some').value != 'some' + assert get_context().get_var('some').value == 'some_new' + assert get_context().get_var('foo').value == 'foo' + assert get_context().get_var(1).value == 'one' # Checks variables after restore previous context pop_context() - assert_not_equals(get_context().get_var('some').value, 'some_new') - assert_equals(get_context().get_var('some').value, 'some') - assert_equals(get_context().get_var('foo'), None) - assert_equals(get_context().get_var(1).value, 'one') + assert get_context().get_var('some').value != 'some_new' + assert get_context().get_var('some').value == 'some' + assert get_context().get_var('foo') == None + assert get_context().get_var(1).value == 'one' -@with_setup(setup, tear_down) def test_push_failed_with_regexp_py35_gixy_10(): push_context(Root()) - assert_equals(len(get_context().variables['index']), 0) - assert_equals(len(get_context().variables['name']), 0) + assert len(get_context().variables['index']) == 0 + assert len(get_context().variables['name']) == 0 regexp = Regexp('^/some/(.*?)') for name, group in regexp.groups.items(): diff --git a/tests/core/test_regexp.py b/tests/core/test_regexp.py index 348b386..f5f066d 100644 --- a/tests/core/test_regexp.py +++ b/tests/core/test_regexp.py @@ -1,4 +1,4 @@ -from nose.tools import assert_true, assert_false, assert_equals +import pytest from gixy.core.regexp import Regexp ''' @@ -13,390 +13,303 @@ ''' -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_equals(sorted(reg.generate('|', anchored=True)), sorted(['^foo', '^bar'])) - - -def test_gen_anchor(): - - reg = Regexp('^some$') - val = next(reg.generate('', anchored=False)) - assert_equals(val, 'some') - - reg = Regexp('^some$') - val = next(reg.generate('', anchored=True)) - assert_equals(val, '^some$') - - reg = Regexp('^some$', strict=True) - val = next(reg.generate('', anchored=False)) - assert_equals(val, 'some') - - reg = Regexp('^some$', strict=True) - val = next(reg.generate('', anchored=True)) - assert_equals(val, '^some$') - - -def test_group_can_contains(): - source = '/some/(?P[^/:.]+)/' - reg = Regexp(source) - assert_true(reg.can_contain('\n'), - 'Whole regex "{src}" can contains {sym!r}'.format(src=source, sym='\\n')) - - assert_true(reg.group(0).can_contain('\n'), - 'Group 0 from regex "{src}" can contains {sym!r}'.format(src=source, sym='\\n')) - - assert_true(reg.group('action').can_contain('\n'), - 'Group "action" from regex "{src}" can contains {sym!r}'.format(src=source, sym='\\n')) - - assert_true(reg.group(1).can_contain('\n'), - 'Group 1 from regex "{src}" can contains {sym!r}'.format(src=source, sym='\\n')) - - assert_false(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_true(reg.can_contain(char), - '{reg!r} should contain {chr!r}'.format(reg=regexp, chr=char)) + 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_true(reg.can_contain(char), - '{reg!r} (case insensitive) should contain {chr!r}'.format(reg=regexp, chr=char)) - - -def check_negative_contain(regexp, char): + assert reg.can_contain(char), '{reg!r} (case insensitive) should contain {chr!r}'.format(reg=regexp, chr=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_false(reg.can_contain(char), - '{reg!r} should not contain {chr!r}'.format(reg=regexp, chr=char)) + 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_false(reg.can_contain(char), - '{reg!r} (case insensitive) should not contain {chr!r}'.format(reg=regexp, chr=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_true(reg.can_startswith(char), - '{reg!r} can start\'s with {chr!r}'.format(reg=regexp, chr=char)) + 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_true(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): + assert reg.can_startswith(char), '{reg!r} (case insensitive) can start\'s with {chr!r}'.format(reg=regexp, chr=char) + + +@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_false(reg.can_startswith(char), - '{reg!r} can\'t start\'s with {chr!r}'.format(reg=regexp, chr=char)) + 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_false(reg.can_startswith(char), - '{reg!r} (case insensitive) can\'t start\'s with {chr!r}'.format(reg=regexp, chr=char)) + assert not reg.can_startswith(char), '{reg!r} (case insensitive) can\'t start\'s with {chr!r}'.format(reg=regexp, chr=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) + 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) + + +@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) -def check_groups_names(regexp, groups): - reg = Regexp(regexp) - assert_equals(set(reg.groups.keys()), set(groups)) + 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) + + +@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) + 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) + + +@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) -def check_to_string(regexp, string): + 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) + + +@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_equals(str(reg), string) + assert sorted(reg.generate('|', anchored=True)) == sorted(values) -def check_positive_must_contain(regexp, char): - reg = Regexp(regexp, case_sensitive=True) - assert_true(reg.must_contain(char), - '{reg!r} must contain with {chr!r}'.format(reg=regexp, chr=char)) +def test_strict_generate(): + reg = Regexp('^foo|bar', strict=True) + assert sorted(reg.generate('|', anchored=True)) == sorted(['^foo', '^bar']) - reg = Regexp(regexp, case_sensitive=False) - char = char.upper() - assert_true(reg.must_contain(char), - '{reg!r} (case insensitive) must contain with {chr!r}'.format(reg=regexp, chr=char)) +def test_gen_anchor(): -def check_negative_must_contain(regexp, char): - reg = Regexp(regexp, case_sensitive=True) - assert_false(reg.must_contain(char), - '{reg!r} must NOT contain with {chr!r}'.format(reg=regexp, chr=char)) + reg = Regexp('^some$') + val = next(reg.generate('', anchored=False)) + assert val == 'some' - reg = Regexp(regexp, case_sensitive=False) - char = char.upper() - assert_false(reg.must_contain(char), - '{reg!r} (case insensitive) must NOT contain with {chr!r}'.format(reg=regexp, chr=char)) + 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' -def check_positive_must_startswith(regexp, char, strict): - reg = Regexp(regexp, case_sensitive=True, strict=strict) - assert_true(reg.must_startswith(char), - '{reg!r} MUST start\'s with {chr!r}'.format(reg=regexp, chr=char)) + reg = Regexp('^some$', strict=True) + val = next(reg.generate('', anchored=True)) + assert val == '^some$' - reg = Regexp(regexp, case_sensitive=False, strict=strict) - char = char.upper() - assert_true(reg.must_startswith(char), - '{reg!r} (case insensitive) MUST start\'s with {chr!r}'.format(reg=regexp, chr=char)) +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') -def check_negative_must_startswith(regexp, char, strict): - reg = Regexp(regexp, case_sensitive=True, strict=strict) - assert_false(reg.must_startswith(char), - '{reg!r} MUST NOT start\'s with {chr!r}'.format(reg=regexp, chr=char)) + assert reg.group(0).can_contain('\n'), 'Group 0 from regex "{src}" can contains {sym!r}'.format(src=source, sym='\\n') - reg = Regexp(regexp, case_sensitive=False, strict=strict) - char = char.upper() - assert_false(reg.must_startswith(char), - '{reg!r} (case insensitive) MUST NOT start\'s with {chr!r}'.format(reg=regexp, chr=char)) + 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') -def check_generate(regexp, values): - reg = Regexp(regexp) - assert_equals(sorted(reg.generate('|', anchored=True)), sorted(values)) + 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/core/test_variable.py b/tests/core/test_variable.py index 25c813c..1c0d672 100644 --- a/tests/core/test_variable.py +++ b/tests/core/test_variable.py @@ -1,99 +1,93 @@ -from nose.tools import assert_true, assert_false, assert_equals, with_setup from gixy.core.context import get_context, push_context, purge_context from gixy.directives.block import Root from gixy.core.regexp import Regexp from gixy.core.variable import Variable -def setup(): +def setup_function(): push_context(Root()) -def tear_down(): +def teardown_function(): purge_context() -@with_setup(setup, tear_down) def test_literal(): var = Variable(name='simple', value='$uri', have_script=False) - assert_false(var.depends) - assert_false(var.regexp) - assert_equals(var.value, '$uri') + assert not var.depends + assert not var.regexp + assert var.value == '$uri' - assert_false(var.can_startswith('$')) - assert_false(var.can_contain('i')) - assert_true(var.must_contain('$')) - assert_true(var.must_contain('u')) - assert_false(var.must_contain('a')) - assert_true(var.must_startswith('$')) - assert_false(var.must_startswith('u')) + assert not var.can_startswith('$') + assert not var.can_contain('i') + assert var.must_contain('$') + assert var.must_contain('u') + assert not var.must_contain('a') + assert var.must_startswith('$') + assert not var.must_startswith('u') -@with_setup(setup, tear_down) def test_regexp(): var = Variable(name='simple', value=Regexp('^/.*')) - assert_false(var.depends) - assert_true(var.regexp) + assert not var.depends + assert var.regexp - assert_true(var.can_startswith('/')) - assert_false(var.can_startswith('a')) - assert_true(var.can_contain('a')) - assert_false(var.can_contain('\n')) - assert_true(var.must_contain('/')) - assert_false(var.must_contain('a')) - assert_true(var.must_startswith('/')) - assert_false(var.must_startswith('a')) + assert var.can_startswith('/') + assert not var.can_startswith('a') + assert var.can_contain('a') + assert not var.can_contain('\n') + assert var.must_contain('/') + assert not var.must_contain('a') + assert var.must_startswith('/') + assert not var.must_startswith('a') -@with_setup(setup, tear_down) def test_script(): get_context().add_var('foo', Variable(name='foo', value=Regexp('.*'))) var = Variable(name='simple', value='/$foo') - assert_true(var.depends) - assert_false(var.regexp) + assert var.depends + assert not var.regexp - assert_false(var.can_startswith('/')) - assert_false(var.can_startswith('a')) - assert_true(var.can_contain('/')) - assert_true(var.can_contain('a')) - assert_false(var.can_contain('\n')) - assert_true(var.must_contain('/')) - assert_false(var.must_contain('a')) - assert_true(var.must_startswith('/')) - assert_false(var.must_startswith('a')) + assert not var.can_startswith('/') + assert not var.can_startswith('a') + assert var.can_contain('/') + assert var.can_contain('a') + assert not var.can_contain('\n') + assert var.must_contain('/') + assert not var.must_contain('a') + assert var.must_startswith('/') + assert not var.must_startswith('a') -@with_setup(setup, tear_down) def test_regexp_boundary(): var = Variable(name='simple', value=Regexp('.*'), boundary=Regexp('/[a-z]', strict=True)) - assert_false(var.depends) - assert_true(var.regexp) - - assert_true(var.can_startswith('/')) - assert_false(var.can_startswith('a')) - assert_false(var.can_contain('/')) - assert_true(var.can_contain('a')) - assert_false(var.can_contain('0')) - assert_false(var.can_contain('\n')) - assert_true(var.must_contain('/')) - assert_false(var.must_contain('a')) - assert_true(var.must_startswith('/')) - assert_false(var.must_startswith('a')) - - -@with_setup(setup, tear_down) + assert not var.depends + assert var.regexp + + assert var.can_startswith('/') + assert not var.can_startswith('a') + assert not var.can_contain('/') + assert var.can_contain('a') + assert not var.can_contain('0') + assert not var.can_contain('\n') + assert var.must_contain('/') + assert not var.must_contain('a') + assert var.must_startswith('/') + assert not var.must_startswith('a') + + def test_script_boundary(): get_context().add_var('foo', Variable(name='foo', value=Regexp('.*'), boundary=Regexp('[a-z]', strict=True))) var = Variable(name='simple', value='/$foo', boundary=Regexp('[/a-z0-9]', strict=True)) - assert_true(var.depends) - assert_false(var.regexp) - - assert_false(var.can_startswith('/')) - assert_false(var.can_startswith('a')) - assert_false(var.can_contain('/')) - assert_true(var.can_contain('a')) - assert_false(var.can_contain('\n')) - assert_false(var.can_contain('0')) - assert_true(var.must_contain('/')) - assert_false(var.must_contain('a')) - assert_true(var.must_startswith('/')) - assert_false(var.must_startswith('a')) + assert var.depends + assert not var.regexp + + assert not var.can_startswith('/') + assert not var.can_startswith('a') + assert not var.can_contain('/') + assert var.can_contain('a') + assert not var.can_contain('\n') + assert not var.can_contain('0') + assert var.must_contain('/') + assert not var.must_contain('a') + assert var.must_startswith('/') + assert not var.must_startswith('a') diff --git a/tests/directives/test_block.py b/tests/directives/test_block.py index f8832f9..a9d0b85 100644 --- a/tests/directives/test_block.py +++ b/tests/directives/test_block.py @@ -1,5 +1,3 @@ -from nose.tools import assert_equals, assert_true, assert_false -from tests.asserts import assert_is_instance, assert_is_none, assert_is_not_none from gixy.parser.nginx_parser import NginxParser from gixy.directives.block import * @@ -15,10 +13,10 @@ def test_block(): config = 'some {some;}' directive = _get_parsed(config) - assert_is_instance(directive, Block) - assert_true(directive.is_block) - assert_true(directive.self_context) - assert_false(directive.provide_variables) + assert isinstance(directive, Block) + assert directive.is_block + assert directive.self_context + assert not directive.provide_variables def test_http(): @@ -31,10 +29,10 @@ def test_http(): ''' directive = _get_parsed(config) - assert_is_instance(directive, HttpBlock) - assert_true(directive.is_block) - assert_true(directive.self_context) - assert_false(directive.provide_variables) + assert isinstance(directive, HttpBlock) + assert directive.is_block + assert directive.self_context + assert not directive.provide_variables def test_server(): @@ -48,11 +46,11 @@ def test_server(): ''' directive = _get_parsed(config) - assert_is_instance(directive, ServerBlock) - assert_true(directive.is_block) - assert_true(directive.self_context) - assert_equals([d.args[0] for d in directive.get_names()], ['_', 'cool.io']) - assert_false(directive.provide_variables) + assert isinstance(directive, ServerBlock) + assert directive.is_block + assert directive.self_context + assert [d.args[0] for d in directive.get_names()] == ['_', 'cool.io'] + assert not directive.provide_variables def test_location(): @@ -62,13 +60,13 @@ def test_location(): ''' directive = _get_parsed(config) - assert_is_instance(directive, LocationBlock) - assert_true(directive.is_block) - assert_true(directive.self_context) - assert_true(directive.provide_variables) - assert_is_none(directive.modifier) - assert_equals(directive.path, '/') - assert_false(directive.is_internal) + assert isinstance(directive, LocationBlock) + assert directive.is_block + assert directive.self_context + assert directive.provide_variables + assert directive.modifier is None + assert directive.path == '/' + assert not directive.is_internal def test_location_internal(): @@ -79,8 +77,8 @@ def test_location_internal(): ''' directive = _get_parsed(config) - assert_is_instance(directive, LocationBlock) - assert_true(directive.is_internal) + assert isinstance(directive, LocationBlock) + assert directive.is_internal def test_location_modifier(): @@ -90,9 +88,9 @@ def test_location_modifier(): ''' directive = _get_parsed(config) - assert_is_instance(directive, LocationBlock) - assert_equals(directive.modifier, '=') - assert_equals(directive.path, '/') + assert isinstance(directive, LocationBlock) + assert directive.modifier == '=' + assert directive.path == '/' def test_if(): @@ -102,13 +100,13 @@ def test_if(): ''' directive = _get_parsed(config) - assert_is_instance(directive, IfBlock) - assert_true(directive.is_block) - assert_false(directive.self_context) - assert_false(directive.provide_variables) - assert_equals(directive.variable, '$some') - assert_is_none(directive.operand) - assert_is_none(directive.value) + assert isinstance(directive, IfBlock) + assert directive.is_block + assert not directive.self_context + assert not directive.provide_variables + assert directive.variable == '$some' + assert directive.operand is None + assert directive.value is None def test_if_modifier(): @@ -118,10 +116,10 @@ def test_if_modifier(): ''' directive = _get_parsed(config) - assert_is_instance(directive, IfBlock) - assert_equals(directive.operand, '-f') - assert_equals(directive.value, '/some') - assert_is_none(directive.variable) + assert isinstance(directive, IfBlock) + assert directive.operand == '-f' + assert directive.value == '/some' + assert directive.variable is None def test_if_variable(): @@ -131,10 +129,10 @@ def test_if_variable(): ''' directive = _get_parsed(config) - assert_is_instance(directive, IfBlock) - assert_equals(directive.variable, '$http_some') - assert_equals(directive.operand, '=') - assert_equals(directive.value, '/some') + assert isinstance(directive, IfBlock) + assert directive.variable == '$http_some' + assert directive.operand == '=' + assert directive.value == '/some' def test_block_some_flat(): @@ -151,8 +149,8 @@ def test_block_some_flat(): directive = _get_parsed(config) for d in ['default_type', 'sendfile', 'keepalive_timeout']: c = directive.some(d, flat=True) - assert_is_not_none(c) - assert_equals(c.name, d) + assert c is not None + assert c.name == d def test_block_some_not_flat(): @@ -168,7 +166,7 @@ def test_block_some_not_flat(): directive = _get_parsed(config) c = directive.some('keepalive_timeout', flat=False) - assert_is_none(c) + assert c is None def test_block_find_flat(): @@ -183,9 +181,9 @@ def test_block_find_flat(): directive = _get_parsed(config) finds = directive.find('directive', flat=True) - assert_equals(len(finds), 2) - assert_equals([x.name for x in finds], ['directive', 'directive']) - assert_equals([x.args[0] for x in finds], ['1', '2']) + assert len(finds) == 2 + assert [x.name for x in finds] == ['directive', 'directive'] + assert [x.args[0] for x in finds] == ['1', '2'] def test_block_find_not_flat(): @@ -200,9 +198,9 @@ def test_block_find_not_flat(): directive = _get_parsed(config) finds = directive.find('directive', flat=False) - assert_equals(len(finds), 1) - assert_equals([x.name for x in finds], ['directive']) - assert_equals([x.args[0] for x in finds], ['1']) + assert len(finds) == 1 + assert [x.name for x in finds] == ['directive'] + assert [x.args[0] for x in finds] == ['1'] def test_block_map(): @@ -214,11 +212,11 @@ def test_block_map(): ''' directive = _get_parsed(config) - assert_is_instance(directive, MapBlock) - assert_true(directive.is_block) - assert_false(directive.self_context) - assert_true(directive.provide_variables) - assert_equals(directive.variable, 'some_other_var') + assert isinstance(directive, MapBlock) + assert directive.is_block + assert not directive.self_context + assert directive.provide_variables + assert directive.variable == 'some_other_var' def test_block_geo_two_vars(): @@ -230,11 +228,11 @@ def test_block_geo_two_vars(): ''' directive = _get_parsed(config) - assert_is_instance(directive, GeoBlock) - assert_true(directive.is_block) - assert_false(directive.self_context) - assert_true(directive.provide_variables) - assert_equals(directive.variable, 'some_other_var') + assert isinstance(directive, GeoBlock) + assert directive.is_block + assert not directive.self_context + assert directive.provide_variables + assert directive.variable == 'some_other_var' def test_block_geo_one_var(): @@ -246,8 +244,8 @@ def test_block_geo_one_var(): ''' directive = _get_parsed(config) - assert_is_instance(directive, GeoBlock) - assert_true(directive.is_block) - assert_false(directive.self_context) - assert_true(directive.provide_variables) - assert_equals(directive.variable, 'some_var') + assert isinstance(directive, GeoBlock) + assert directive.is_block + assert not directive.self_context + assert directive.provide_variables + assert directive.variable == 'some_var' diff --git a/tests/directives/test_directive.py b/tests/directives/test_directive.py index 4f62a17..2ffddcc 100644 --- a/tests/directives/test_directive.py +++ b/tests/directives/test_directive.py @@ -1,5 +1,3 @@ -from nose.tools import assert_equals, assert_false, assert_true -from tests.asserts import assert_is_instance from gixy.parser.nginx_parser import NginxParser from gixy.directives.directive import * @@ -13,89 +11,89 @@ def test_directive(): config = 'some "foo" "bar";' directive = _get_parsed(config) - assert_is_instance(directive, Directive) - assert_equals(directive.name, 'some') - assert_equals(directive.args, ['foo', 'bar']) - assert_equals(str(directive), 'some foo bar;') + assert isinstance(directive, Directive) + assert directive.name == 'some' + assert directive.args == ['foo', 'bar'] + assert str(directive) == 'some foo bar;' def test_add_header(): config = 'add_header "X-Foo" "bar";' directive = _get_parsed(config) - assert_is_instance(directive, AddHeaderDirective) - assert_equals(directive.name, 'add_header') - assert_equals(directive.args, ['X-Foo', 'bar']) - assert_equals(directive.header, 'x-foo') - assert_equals(directive.value, 'bar') - assert_false(directive.always) - assert_equals(str(directive), 'add_header X-Foo bar;') + assert isinstance(directive, AddHeaderDirective) + assert directive.name == 'add_header' + assert directive.args == ['X-Foo', 'bar'] + assert directive.header == 'x-foo' + assert directive.value == 'bar' + assert not directive.always + assert str(directive) == 'add_header X-Foo bar;' def test_add_header_always(): config = 'add_header "X-Foo" "bar" always;' directive = _get_parsed(config) - assert_is_instance(directive, AddHeaderDirective) - assert_equals(directive.name, 'add_header') - assert_equals(directive.args, ['X-Foo', 'bar', 'always']) - assert_equals(directive.header, 'x-foo') - assert_equals(directive.value, 'bar') - assert_true(directive.always) - assert_equals(str(directive), 'add_header X-Foo bar always;') + assert isinstance(directive, AddHeaderDirective) + assert directive.name == 'add_header' + assert directive.args == ['X-Foo', 'bar', 'always'] + assert directive.header == 'x-foo' + assert directive.value == 'bar' + assert directive.always + assert str(directive) == 'add_header X-Foo bar always;' def test_set(): config = 'set $foo bar;' directive = _get_parsed(config) - assert_is_instance(directive, SetDirective) - assert_equals(directive.name, 'set') - assert_equals(directive.args, ['$foo', 'bar']) - assert_equals(directive.variable, 'foo') - assert_equals(directive.value, 'bar') - assert_equals(str(directive), 'set $foo bar;') - assert_true(directive.provide_variables) + assert isinstance(directive, SetDirective) + assert directive.name == 'set' + assert directive.args == ['$foo', 'bar'] + assert directive.variable == 'foo' + assert directive.value == 'bar' + assert str(directive) == 'set $foo bar;' + assert directive.provide_variables def test_rewrite(): config = 'rewrite ^ http://some;' directive = _get_parsed(config) - assert_is_instance(directive, RewriteDirective) - assert_equals(directive.name, 'rewrite') - assert_equals(directive.args, ['^', 'http://some']) - assert_equals(str(directive), 'rewrite ^ http://some;') - assert_true(directive.provide_variables) + assert isinstance(directive, RewriteDirective) + assert directive.name == 'rewrite' + assert directive.args == ['^', 'http://some'] + assert str(directive) == 'rewrite ^ http://some;' + assert directive.provide_variables - assert_equals(directive.pattern, '^') - assert_equals(directive.replace, 'http://some') - assert_equals(directive.flag, None) + assert directive.pattern == '^' + assert directive.replace == 'http://some' + assert directive.flag == None def test_rewrite_flags(): config = 'rewrite ^/(.*)$ http://some/$1 redirect;' directive = _get_parsed(config) - assert_is_instance(directive, RewriteDirective) - assert_equals(directive.name, 'rewrite') - assert_equals(directive.args, ['^/(.*)$', 'http://some/$1', 'redirect']) - assert_equals(str(directive), 'rewrite ^/(.*)$ http://some/$1 redirect;') - assert_true(directive.provide_variables) + assert isinstance(directive, RewriteDirective) + assert directive.name == 'rewrite' + assert directive.args == ['^/(.*)$', 'http://some/$1', 'redirect'] + assert str(directive) == 'rewrite ^/(.*)$ http://some/$1 redirect;' + assert directive.provide_variables - assert_equals(directive.pattern, '^/(.*)$') - assert_equals(directive.replace, 'http://some/$1') - assert_equals(directive.flag, 'redirect') + assert directive.pattern == '^/(.*)$' + assert directive.replace == 'http://some/$1' + assert directive.flag == 'redirect' def test_root(): config = 'root /var/www/html;' directive = _get_parsed(config) - assert_is_instance(directive, RootDirective) - assert_equals(directive.name, 'root') - assert_equals(directive.args, ['/var/www/html']) - assert_equals(str(directive), 'root /var/www/html;') - assert_true(directive.provide_variables) + assert isinstance(directive, RootDirective) + assert directive.name == 'root' + assert directive.args == ['/var/www/html'] + assert str(directive) == 'root /var/www/html;' + assert directive.provide_variables - assert_equals(directive.path, '/var/www/html') + assert directive.path == '/var/www/html' diff --git a/tests/parser/test_nginx_parser.py b/tests/parser/test_nginx_parser.py index 0f42e56..2433703 100644 --- a/tests/parser/test_nginx_parser.py +++ b/tests/parser/test_nginx_parser.py @@ -1,5 +1,4 @@ -from nose.tools import assert_equal -from tests.asserts import assert_is_instance +import pytest from gixy.parser.nginx_parser import NginxParser from gixy.directives.directive import * from gixy.directives.block import * @@ -9,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;', @@ -18,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(): @@ -65,38 +65,38 @@ def test_dump_simple(): ''' tree = _parse(config) - assert_is_instance(tree, Directive) - assert_is_instance(tree, Block) - assert_is_instance(tree, Root) + assert isinstance(tree, Directive) + assert isinstance(tree, Block) + assert isinstance(tree, Root) - assert_equal(len(tree.children), 1) + assert len(tree.children) == 1 http = tree.children[0] - assert_is_instance(http, Directive) - assert_is_instance(http, Block) - assert_is_instance(http, HttpBlock) + assert isinstance(http, Directive) + assert isinstance(http, Block) + assert isinstance(http, HttpBlock) - assert_equal(len(http.children), 1) + assert len(http.children) == 1 include_server = http.children[0] - assert_is_instance(include_server, Directive) - assert_is_instance(include_server, IncludeBlock) - assert_equal(include_server.file_path, '/etc/nginx/sites/default.conf') + assert isinstance(include_server, Directive) + assert isinstance(include_server, IncludeBlock) + assert include_server.file_path == '/etc/nginx/sites/default.conf' - assert_equal(len(include_server.children), 1) + assert len(include_server.children) == 1 server = include_server.children[0] - assert_is_instance(server, Directive) - assert_is_instance(server, Block) - assert_is_instance(server, ServerBlock) + assert isinstance(server, Directive) + assert isinstance(server, Block) + assert isinstance(server, ServerBlock) - assert_equal(len(server.children), 1) + assert len(server.children) == 1 include_listen = server.children[0] - assert_is_instance(include_listen, Directive) - assert_is_instance(include_listen, IncludeBlock) - assert_equal(include_listen.file_path, '/etc/nginx/conf.d/listen') + assert isinstance(include_listen, Directive) + assert isinstance(include_listen, IncludeBlock) + assert include_listen.file_path == '/etc/nginx/conf.d/listen' - assert_equal(len(include_listen.children), 1) + assert len(include_listen.children) == 1 listen = include_listen.children[0] - assert_is_instance(listen, Directive) - assert_equal(listen.args, ['80']) + assert isinstance(listen, Directive) + assert listen.args == ['80'] def test_encoding(): @@ -110,10 +110,10 @@ def test_encoding(): def assert_config(config, expected): tree = _parse(config) - assert_is_instance(tree, Directive) - assert_is_instance(tree, Block) - assert_is_instance(tree, Root) + assert isinstance(tree, Directive) + assert isinstance(tree, Block) + assert isinstance(tree, Root) child = tree.children[0] for ex in expected: - assert_is_instance(child, ex) + assert isinstance(child, ex) diff --git a/tests/parser/test_raw_parser.py b/tests/parser/test_raw_parser.py index 7fa4b01..0dca9b2 100644 --- a/tests/parser/test_raw_parser.py +++ b/tests/parser/test_raw_parser.py @@ -1,4 +1,3 @@ -from nose.tools import assert_equals from gixy.parser.raw_parser import * @@ -556,9 +555,9 @@ def test_national_comment_decoding(): ''' actual = RawParser().parse(config) - assert_equals(len(actual.asList()), 2) + assert len(actual.asList()) == 2 def assert_config(config, expected): actual = RawParser().parse(config) - assert_equals(actual.asList(), expected) + assert actual.asList() == expected diff --git a/tests/plugins/test_simply.py b/tests/plugins/test_simply.py index e2ee6db..e54b60d 100644 --- a/tests/plugins/test_simply.py +++ b/tests/plugins/test_simply.py @@ -1,10 +1,8 @@ -from nose.tools import assert_equals, assert_true - from gixy.formatters import BaseFormatter -from tests.asserts import assert_in import os from os import path import json +import pytest from ..utils import * from gixy.core.manager import Manager as Gixy @@ -12,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 ('.', '..'): @@ -46,20 +39,21 @@ 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: plugin = plugin.name - assert_true(plugin in tested_plugins, - 'Plugin {name!r} should have at least one simple test config'.format(name=plugin)) - assert_true(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_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): @@ -80,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')) @@ -88,24 +83,23 @@ def check_configuration(plugin, config_path, test_config): formatter.feed(config_path, yoda) _, results = formatter.reports.popitem() - assert_equals(len(results), 1, 'Should have one report') + assert len(results) == 1, 'Should have one report' result = results[0] if 'severity' in test_config: if not hasattr(test_config['severity'], '__iter__'): - assert_equals(result['severity'], test_config['severity']) + assert result['severity'] == test_config['severity'] else: - assert_in(result['severity'], test_config['severity']) - assert_equals(result['plugin'], plugin) - assert_true(result['summary']) - assert_true(result['description']) - assert_true(result['config']) - assert_true(result['help_url'].startswith('https://'), - 'help_url must starts with https://. It\'is URL!') + assert result['severity'] in test_config['severity'] + assert result['plugin'] == plugin + assert result['summary'] + assert result['description'] + assert result['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_equals(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' diff --git a/tox.ini b/tox.ini index c981f56..8576664 100644 --- a/tox.ini +++ b/tox.ini @@ -6,7 +6,7 @@ skip_missing_interpreters = True deps = -rrequirements.txt -rrequirements.dev.txt -commands = nosetests -v +commands = pytest -v [testenv:flake8] deps =