Convert assertions with nose2pytest

pull/146/head
Emily 2024-07-28 18:21:43 +01:00
parent 3bf5110983
commit df9655e00d
8 changed files with 279 additions and 304 deletions

View File

@ -6,7 +6,7 @@ from gixy.core.regexp import Regexp
def setup(): def setup():
assert_equals(len(CONTEXTS), 0) assert len(CONTEXTS) == 0
def tear_down(): def tear_down():
@ -17,39 +17,39 @@ def tear_down():
def test_push_pop_context(): def test_push_pop_context():
root_a = Root() root_a = Root()
push_context(root_a) push_context(root_a)
assert_equals(len(CONTEXTS), 1) assert len(CONTEXTS) == 1
root_b = Root() root_b = Root()
push_context(root_b) push_context(root_b)
assert_equals(len(CONTEXTS), 2) assert len(CONTEXTS) == 2
poped = pop_context() poped = pop_context()
assert_equals(len(CONTEXTS), 1) assert len(CONTEXTS) == 1
assert_equals(poped.block, root_b) assert poped.block == root_b
poped = pop_context() poped = pop_context()
assert_equals(len(CONTEXTS), 0) assert len(CONTEXTS) == 0
assert_equals(poped.block, root_a) assert poped.block == root_a
@with_setup(setup, tear_down) @with_setup(setup, tear_down)
def test_push_get_purge_context(): def test_push_get_purge_context():
root = Root() root = Root()
push_context(root) push_context(root)
assert_equals(len(CONTEXTS), 1) assert len(CONTEXTS) == 1
assert_equals(get_context().block, root) assert get_context().block == root
root = Root() root = Root()
push_context(root) push_context(root)
assert_equals(len(CONTEXTS), 2) assert len(CONTEXTS) == 2
assert_equals(get_context().block, root) assert get_context().block == root
purge_context() purge_context()
assert_equals(len(CONTEXTS), 0) assert len(CONTEXTS) == 0
@with_setup(setup, tear_down) @with_setup(setup, tear_down)
def test_add_variables(): def test_add_variables():
context = push_context(Root()) context = push_context(Root())
assert_equals(len(context.variables['index']), 0) assert len(context.variables['index']) == 0
assert_equals(len(context.variables['name']), 0) assert len(context.variables['name']) == 0
one_str_var = Variable('1') one_str_var = Variable('1')
context.add_var('1', one_str_var) context.add_var('1', one_str_var)
@ -58,77 +58,77 @@ def test_add_variables():
some_var = Variable('some') some_var = Variable('some')
context.add_var('some', some_var) context.add_var('some', some_var)
assert_equals(len(context.variables['index']), 1) assert len(context.variables['index']) == 1
assert_equals(context.variables['index'][1], one_int_var) assert context.variables['index'][1] == one_int_var
assert_equals(len(context.variables['name']), 1) assert len(context.variables['name']) == 1
assert_equals(context.variables['name']['some'], some_var) assert context.variables['name']['some'] == some_var
context.clear_index_vars() context.clear_index_vars()
assert_equals(len(context.variables['index']), 0) assert len(context.variables['index']) == 0
assert_equals(len(context.variables['name']), 1) assert len(context.variables['name']) == 1
assert_equals(context.variables['name']['some'], some_var) assert context.variables['name']['some'] == some_var
@with_setup(setup, tear_down) @with_setup(setup, tear_down)
def test_get_variables(): def test_get_variables():
context = push_context(Root()) context = push_context(Root())
assert_equals(len(context.variables['index']), 0) assert len(context.variables['index']) == 0
assert_equals(len(context.variables['name']), 0) assert len(context.variables['name']) == 0
one_var = Variable(1) one_var = Variable(1)
context.add_var(1, one_var) context.add_var(1, one_var)
some_var = Variable('some') some_var = Variable('some')
context.add_var('some', some_var) context.add_var('some', some_var)
assert_equals(context.get_var(1), one_var) assert context.get_var(1) == one_var
assert_equals(context.get_var('some'), some_var) assert context.get_var('some') == some_var
# Checks not existed variables, for now context may return None # Checks not existed variables, for now context may return None
assert_equals(context.get_var(0), None) assert context.get_var(0) == None
assert_equals(context.get_var('not_existed'), None) assert context.get_var('not_existed') == None
# Checks builtins variables # Checks builtins variables
assert_true(context.get_var('uri')) assert context.get_var('uri')
assert_true(context.get_var('document_uri')) assert context.get_var('document_uri')
assert_true(context.get_var('arg_asdsadasd')) assert context.get_var('arg_asdsadasd')
assert_true(context.get_var('args')) assert context.get_var('args')
@with_setup(setup, tear_down) @with_setup(setup, tear_down)
def test_context_depend_variables(): def test_context_depend_variables():
push_context(Root()) push_context(Root())
assert_equals(len(get_context().variables['index']), 0) assert len(get_context().variables['index']) == 0
assert_equals(len(get_context().variables['name']), 0) assert len(get_context().variables['name']) == 0
get_context().add_var(1, Variable(1, value='one')) get_context().add_var(1, Variable(1, value='one'))
get_context().add_var('some', Variable('some', value='some')) get_context().add_var('some', Variable('some', value='some'))
assert_equals(get_context().get_var(1).value, 'one') assert get_context().get_var(1).value == 'one'
assert_equals(get_context().get_var('some').value, 'some') assert get_context().get_var('some').value == 'some'
# Checks top context variables are still exists # Checks top context variables are still exists
push_context(Root()) push_context(Root())
assert_equals(get_context().get_var(1).value, 'one') assert get_context().get_var(1).value == 'one'
assert_equals(get_context().get_var('some').value, 'some') assert get_context().get_var('some').value == 'some'
# Checks variable overriding # Checks variable overriding
get_context().add_var('some', Variable('some', value='some_new')) get_context().add_var('some', Variable('some', value='some_new'))
get_context().add_var('foo', Variable('foo', value='foo')) get_context().add_var('foo', Variable('foo', value='foo'))
assert_not_equals(get_context().get_var('some').value, 'some') assert_not_equals(get_context().get_var('some').value, 'some')
assert_equals(get_context().get_var('some').value, 'some_new') assert get_context().get_var('some').value == 'some_new'
assert_equals(get_context().get_var('foo').value, 'foo') assert get_context().get_var('foo').value == 'foo'
assert_equals(get_context().get_var(1).value, 'one') assert get_context().get_var(1).value == 'one'
# Checks variables after restore previous context # Checks variables after restore previous context
pop_context() pop_context()
assert_not_equals(get_context().get_var('some').value, 'some_new') assert_not_equals(get_context().get_var('some').value, 'some_new')
assert_equals(get_context().get_var('some').value, 'some') assert get_context().get_var('some').value == 'some'
assert_equals(get_context().get_var('foo'), None) assert get_context().get_var('foo') == None
assert_equals(get_context().get_var(1).value, 'one') assert get_context().get_var(1).value == 'one'
@with_setup(setup, tear_down) @with_setup(setup, tear_down)
def test_push_failed_with_regexp_py35_gixy_10(): def test_push_failed_with_regexp_py35_gixy_10():
push_context(Root()) push_context(Root())
assert_equals(len(get_context().variables['index']), 0) assert len(get_context().variables['index']) == 0
assert_equals(len(get_context().variables['name']), 0) assert len(get_context().variables['name']) == 0
regexp = Regexp('^/some/(.*?)') regexp = Regexp('^/some/(.*?)')
for name, group in regexp.groups.items(): for name, group in regexp.groups.items():

View File

@ -258,145 +258,124 @@ def test_generate():
def test_strict_generate(): def test_strict_generate():
reg = Regexp('^foo|bar', strict=True) reg = Regexp('^foo|bar', strict=True)
assert_equals(sorted(reg.generate('|', anchored=True)), sorted(['^foo', '^bar'])) assert sorted(reg.generate('|', anchored=True)) == sorted(['^foo', '^bar'])
def test_gen_anchor(): def test_gen_anchor():
reg = Regexp('^some$') reg = Regexp('^some$')
val = next(reg.generate('', anchored=False)) val = next(reg.generate('', anchored=False))
assert_equals(val, 'some') assert val == 'some'
reg = Regexp('^some$') reg = Regexp('^some$')
val = next(reg.generate('', anchored=True)) val = next(reg.generate('', anchored=True))
assert_equals(val, '^some$') assert val == '^some$'
reg = Regexp('^some$', strict=True) reg = Regexp('^some$', strict=True)
val = next(reg.generate('', anchored=False)) val = next(reg.generate('', anchored=False))
assert_equals(val, 'some') assert val == 'some'
reg = Regexp('^some$', strict=True) reg = Regexp('^some$', strict=True)
val = next(reg.generate('', anchored=True)) val = next(reg.generate('', anchored=True))
assert_equals(val, '^some$') assert val == '^some$'
def test_group_can_contains(): def test_group_can_contains():
source = '/some/(?P<action>[^/:.]+)/' source = '/some/(?P<action>[^/:.]+)/'
reg = Regexp(source) reg = Regexp(source)
assert_true(reg.can_contain('\n'), assert reg.can_contain('\n'), 'Whole regex "{src}" can contains {sym!r}'.format(src=source, sym='\\n')
'Whole regex "{src}" can contains {sym!r}'.format(src=source, sym='\\n'))
assert_true(reg.group(0).can_contain('\n'), assert reg.group(0).can_contain('\n'), 'Group 0 from regex "{src}" can contains {sym!r}'.format(src=source, sym='\\n')
'Group 0 from regex "{src}" can contains {sym!r}'.format(src=source, sym='\\n'))
assert_true(reg.group('action').can_contain('\n'), assert reg.group('action').can_contain('\n'), 'Group "action" from regex "{src}" can contains {sym!r}'.format(src=source, sym='\\n')
'Group "action" from regex "{src}" can contains {sym!r}'.format(src=source, sym='\\n'))
assert_true(reg.group(1).can_contain('\n'), assert reg.group(1).can_contain('\n'), 'Group 1 from regex "{src}" can contains {sym!r}'.format(src=source, sym='\\n')
'Group 1 from regex "{src}" can contains {sym!r}'.format(src=source, sym='\\n'))
assert_false(reg.group('action').can_contain('/'), assert not reg.group('action').can_contain('/'), 'Group "action" from regex "{src}" CAN\'T (!) contain {sym!r}'.format(src=source, sym='/')
'Group "action" from regex "{src}" CAN\'T (!) contain {sym!r}'.format(src=source, sym='/'))
def check_positive_contain(regexp, char): def check_positive_contain(regexp, char):
reg = Regexp(regexp, case_sensitive=True) reg = Regexp(regexp, case_sensitive=True)
assert_true(reg.can_contain(char), assert reg.can_contain(char), '{reg!r} should contain {chr!r}'.format(reg=regexp, chr=char)
'{reg!r} should contain {chr!r}'.format(reg=regexp, chr=char))
reg = Regexp(regexp, case_sensitive=False) reg = Regexp(regexp, case_sensitive=False)
char = char.upper() char = char.upper()
assert_true(reg.can_contain(char), assert reg.can_contain(char), '{reg!r} (case insensitive) should contain {chr!r}'.format(reg=regexp, chr=char)
'{reg!r} (case insensitive) should contain {chr!r}'.format(reg=regexp, chr=char))
def check_negative_contain(regexp, char): def check_negative_contain(regexp, char):
reg = Regexp(regexp, case_sensitive=True) reg = Regexp(regexp, case_sensitive=True)
assert_false(reg.can_contain(char), assert not reg.can_contain(char), '{reg!r} should not contain {chr!r}'.format(reg=regexp, chr=char)
'{reg!r} should not contain {chr!r}'.format(reg=regexp, chr=char))
reg = Regexp(regexp, case_sensitive=False) reg = Regexp(regexp, case_sensitive=False)
char = char.upper() char = char.upper()
assert_false(reg.can_contain(char), assert not reg.can_contain(char), '{reg!r} (case insensitive) should not contain {chr!r}'.format(reg=regexp, chr=char)
'{reg!r} (case insensitive) should not contain {chr!r}'.format(reg=regexp, chr=char))
def check_positive_startswith(regexp, char, strict): def check_positive_startswith(regexp, char, strict):
reg = Regexp(regexp, case_sensitive=True, strict=strict) reg = Regexp(regexp, case_sensitive=True, strict=strict)
assert_true(reg.can_startswith(char), assert reg.can_startswith(char), '{reg!r} can start\'s with {chr!r}'.format(reg=regexp, chr=char)
'{reg!r} can start\'s with {chr!r}'.format(reg=regexp, chr=char))
reg = Regexp(regexp, case_sensitive=False, strict=strict) reg = Regexp(regexp, case_sensitive=False, strict=strict)
char = char.upper() char = char.upper()
assert_true(reg.can_startswith(char), assert reg.can_startswith(char), '{reg!r} (case insensitive) can start\'s with {chr!r}'.format(reg=regexp, chr=char)
'{reg!r} (case insensitive) can start\'s with {chr!r}'.format(reg=regexp, chr=char))
def check_negative_startswith(regexp, char, strict): def check_negative_startswith(regexp, char, strict):
reg = Regexp(regexp, case_sensitive=True, strict=strict) reg = Regexp(regexp, case_sensitive=True, strict=strict)
assert_false(reg.can_startswith(char), assert not reg.can_startswith(char), '{reg!r} can\'t start\'s with {chr!r}'.format(reg=regexp, chr=char)
'{reg!r} can\'t start\'s with {chr!r}'.format(reg=regexp, chr=char))
reg = Regexp(regexp, case_sensitive=False, strict=strict) reg = Regexp(regexp, case_sensitive=False, strict=strict)
char = char.upper() char = char.upper()
assert_false(reg.can_startswith(char), assert not reg.can_startswith(char), '{reg!r} (case insensitive) can\'t start\'s with {chr!r}'.format(reg=regexp, chr=char)
'{reg!r} (case insensitive) can\'t start\'s with {chr!r}'.format(reg=regexp, chr=char))
def check_groups_names(regexp, groups): def check_groups_names(regexp, groups):
reg = Regexp(regexp) reg = Regexp(regexp)
assert_equals(set(reg.groups.keys()), set(groups)) assert set(reg.groups.keys()) == set(groups)
def check_to_string(regexp, string): def check_to_string(regexp, string):
reg = Regexp(regexp) reg = Regexp(regexp)
assert_equals(str(reg), string) assert str(reg) == string
def check_positive_must_contain(regexp, char): def check_positive_must_contain(regexp, char):
reg = Regexp(regexp, case_sensitive=True) reg = Regexp(regexp, case_sensitive=True)
assert_true(reg.must_contain(char), assert reg.must_contain(char), '{reg!r} must contain with {chr!r}'.format(reg=regexp, chr=char)
'{reg!r} must contain with {chr!r}'.format(reg=regexp, chr=char))
reg = Regexp(regexp, case_sensitive=False) reg = Regexp(regexp, case_sensitive=False)
char = char.upper() char = char.upper()
assert_true(reg.must_contain(char), assert reg.must_contain(char), '{reg!r} (case insensitive) must contain with {chr!r}'.format(reg=regexp, chr=char)
'{reg!r} (case insensitive) must contain with {chr!r}'.format(reg=regexp, chr=char))
def check_negative_must_contain(regexp, char): def check_negative_must_contain(regexp, char):
reg = Regexp(regexp, case_sensitive=True) reg = Regexp(regexp, case_sensitive=True)
assert_false(reg.must_contain(char), assert not reg.must_contain(char), '{reg!r} must NOT contain with {chr!r}'.format(reg=regexp, chr=char)
'{reg!r} must NOT contain with {chr!r}'.format(reg=regexp, chr=char))
reg = Regexp(regexp, case_sensitive=False) reg = Regexp(regexp, case_sensitive=False)
char = char.upper() char = char.upper()
assert_false(reg.must_contain(char), assert not reg.must_contain(char), '{reg!r} (case insensitive) must NOT contain with {chr!r}'.format(reg=regexp, chr=char)
'{reg!r} (case insensitive) must NOT contain with {chr!r}'.format(reg=regexp, chr=char))
def check_positive_must_startswith(regexp, char, strict): def check_positive_must_startswith(regexp, char, strict):
reg = Regexp(regexp, case_sensitive=True, strict=strict) reg = Regexp(regexp, case_sensitive=True, strict=strict)
assert_true(reg.must_startswith(char), assert reg.must_startswith(char), '{reg!r} MUST start\'s with {chr!r}'.format(reg=regexp, chr=char)
'{reg!r} MUST start\'s with {chr!r}'.format(reg=regexp, chr=char))
reg = Regexp(regexp, case_sensitive=False, strict=strict) reg = Regexp(regexp, case_sensitive=False, strict=strict)
char = char.upper() char = char.upper()
assert_true(reg.must_startswith(char), assert reg.must_startswith(char), '{reg!r} (case insensitive) MUST start\'s with {chr!r}'.format(reg=regexp, chr=char)
'{reg!r} (case insensitive) MUST start\'s with {chr!r}'.format(reg=regexp, chr=char))
def check_negative_must_startswith(regexp, char, strict): def check_negative_must_startswith(regexp, char, strict):
reg = Regexp(regexp, case_sensitive=True, strict=strict) reg = Regexp(regexp, case_sensitive=True, strict=strict)
assert_false(reg.must_startswith(char), assert not reg.must_startswith(char), '{reg!r} MUST NOT start\'s with {chr!r}'.format(reg=regexp, chr=char)
'{reg!r} MUST NOT start\'s with {chr!r}'.format(reg=regexp, chr=char))
reg = Regexp(regexp, case_sensitive=False, strict=strict) reg = Regexp(regexp, case_sensitive=False, strict=strict)
char = char.upper() char = char.upper()
assert_false(reg.must_startswith(char), assert not reg.must_startswith(char), '{reg!r} (case insensitive) MUST NOT start\'s with {chr!r}'.format(reg=regexp, chr=char)
'{reg!r} (case insensitive) MUST NOT start\'s with {chr!r}'.format(reg=regexp, chr=char))
def check_generate(regexp, values): def check_generate(regexp, values):
reg = Regexp(regexp) reg = Regexp(regexp)
assert_equals(sorted(reg.generate('|', anchored=True)), sorted(values)) assert sorted(reg.generate('|', anchored=True)) == sorted(values)

View File

@ -15,85 +15,85 @@ def tear_down():
@with_setup(setup, tear_down) @with_setup(setup, tear_down)
def test_literal(): def test_literal():
var = Variable(name='simple', value='$uri', have_script=False) var = Variable(name='simple', value='$uri', have_script=False)
assert_false(var.depends) assert not var.depends
assert_false(var.regexp) assert not var.regexp
assert_equals(var.value, '$uri') assert var.value == '$uri'
assert_false(var.can_startswith('$')) assert not var.can_startswith('$')
assert_false(var.can_contain('i')) assert not var.can_contain('i')
assert_true(var.must_contain('$')) assert var.must_contain('$')
assert_true(var.must_contain('u')) assert var.must_contain('u')
assert_false(var.must_contain('a')) assert not var.must_contain('a')
assert_true(var.must_startswith('$')) assert var.must_startswith('$')
assert_false(var.must_startswith('u')) assert not var.must_startswith('u')
@with_setup(setup, tear_down) @with_setup(setup, tear_down)
def test_regexp(): def test_regexp():
var = Variable(name='simple', value=Regexp('^/.*')) var = Variable(name='simple', value=Regexp('^/.*'))
assert_false(var.depends) assert not var.depends
assert_true(var.regexp) assert var.regexp
assert_true(var.can_startswith('/')) assert var.can_startswith('/')
assert_false(var.can_startswith('a')) assert not var.can_startswith('a')
assert_true(var.can_contain('a')) assert var.can_contain('a')
assert_false(var.can_contain('\n')) assert not var.can_contain('\n')
assert_true(var.must_contain('/')) assert var.must_contain('/')
assert_false(var.must_contain('a')) assert not var.must_contain('a')
assert_true(var.must_startswith('/')) assert var.must_startswith('/')
assert_false(var.must_startswith('a')) assert not var.must_startswith('a')
@with_setup(setup, tear_down) @with_setup(setup, tear_down)
def test_script(): def test_script():
get_context().add_var('foo', Variable(name='foo', value=Regexp('.*'))) get_context().add_var('foo', Variable(name='foo', value=Regexp('.*')))
var = Variable(name='simple', value='/$foo') var = Variable(name='simple', value='/$foo')
assert_true(var.depends) assert var.depends
assert_false(var.regexp) assert not var.regexp
assert_false(var.can_startswith('/')) assert not var.can_startswith('/')
assert_false(var.can_startswith('a')) assert not var.can_startswith('a')
assert_true(var.can_contain('/')) assert var.can_contain('/')
assert_true(var.can_contain('a')) assert var.can_contain('a')
assert_false(var.can_contain('\n')) assert not var.can_contain('\n')
assert_true(var.must_contain('/')) assert var.must_contain('/')
assert_false(var.must_contain('a')) assert not var.must_contain('a')
assert_true(var.must_startswith('/')) assert var.must_startswith('/')
assert_false(var.must_startswith('a')) assert not var.must_startswith('a')
@with_setup(setup, tear_down) @with_setup(setup, tear_down)
def test_regexp_boundary(): def test_regexp_boundary():
var = Variable(name='simple', value=Regexp('.*'), boundary=Regexp('/[a-z]', strict=True)) var = Variable(name='simple', value=Regexp('.*'), boundary=Regexp('/[a-z]', strict=True))
assert_false(var.depends) assert not var.depends
assert_true(var.regexp) assert var.regexp
assert_true(var.can_startswith('/')) assert var.can_startswith('/')
assert_false(var.can_startswith('a')) assert not var.can_startswith('a')
assert_false(var.can_contain('/')) assert not var.can_contain('/')
assert_true(var.can_contain('a')) assert var.can_contain('a')
assert_false(var.can_contain('0')) assert not var.can_contain('0')
assert_false(var.can_contain('\n')) assert not var.can_contain('\n')
assert_true(var.must_contain('/')) assert var.must_contain('/')
assert_false(var.must_contain('a')) assert not var.must_contain('a')
assert_true(var.must_startswith('/')) assert var.must_startswith('/')
assert_false(var.must_startswith('a')) assert not var.must_startswith('a')
@with_setup(setup, tear_down) @with_setup(setup, tear_down)
def test_script_boundary(): def test_script_boundary():
get_context().add_var('foo', Variable(name='foo', value=Regexp('.*'), boundary=Regexp('[a-z]', strict=True))) 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)) var = Variable(name='simple', value='/$foo', boundary=Regexp('[/a-z0-9]', strict=True))
assert_true(var.depends) assert var.depends
assert_false(var.regexp) assert not var.regexp
assert_false(var.can_startswith('/')) assert not var.can_startswith('/')
assert_false(var.can_startswith('a')) assert not var.can_startswith('a')
assert_false(var.can_contain('/')) assert not var.can_contain('/')
assert_true(var.can_contain('a')) assert var.can_contain('a')
assert_false(var.can_contain('\n')) assert not var.can_contain('\n')
assert_false(var.can_contain('0')) assert not var.can_contain('0')
assert_true(var.must_contain('/')) assert var.must_contain('/')
assert_false(var.must_contain('a')) assert not var.must_contain('a')
assert_true(var.must_startswith('/')) assert var.must_startswith('/')
assert_false(var.must_startswith('a')) assert not var.must_startswith('a')

View File

@ -14,10 +14,10 @@ def test_block():
config = 'some {some;}' config = 'some {some;}'
directive = _get_parsed(config) directive = _get_parsed(config)
assert_is_instance(directive, Block) assert isinstance(directive, Block)
assert_true(directive.is_block) assert directive.is_block
assert_true(directive.self_context) assert directive.self_context
assert_false(directive.provide_variables) assert not directive.provide_variables
def test_http(): def test_http():
@ -30,10 +30,10 @@ http {
''' '''
directive = _get_parsed(config) directive = _get_parsed(config)
assert_is_instance(directive, HttpBlock) assert isinstance(directive, HttpBlock)
assert_true(directive.is_block) assert directive.is_block
assert_true(directive.self_context) assert directive.self_context
assert_false(directive.provide_variables) assert not directive.provide_variables
def test_server(): def test_server():
@ -47,11 +47,11 @@ server {
''' '''
directive = _get_parsed(config) directive = _get_parsed(config)
assert_is_instance(directive, ServerBlock) assert isinstance(directive, ServerBlock)
assert_true(directive.is_block) assert directive.is_block
assert_true(directive.self_context) assert directive.self_context
assert_equals([d.args[0] for d in directive.get_names()], ['_', 'cool.io']) assert [d.args[0] for d in directive.get_names()] == ['_', 'cool.io']
assert_false(directive.provide_variables) assert not directive.provide_variables
def test_location(): def test_location():
@ -61,13 +61,13 @@ location / {
''' '''
directive = _get_parsed(config) directive = _get_parsed(config)
assert_is_instance(directive, LocationBlock) assert isinstance(directive, LocationBlock)
assert_true(directive.is_block) assert directive.is_block
assert_true(directive.self_context) assert directive.self_context
assert_true(directive.provide_variables) assert directive.provide_variables
assert_is_none(directive.modifier) assert directive.modifier is None
assert_equals(directive.path, '/') assert directive.path == '/'
assert_false(directive.is_internal) assert not directive.is_internal
def test_location_internal(): def test_location_internal():
@ -78,8 +78,8 @@ location / {
''' '''
directive = _get_parsed(config) directive = _get_parsed(config)
assert_is_instance(directive, LocationBlock) assert isinstance(directive, LocationBlock)
assert_true(directive.is_internal) assert directive.is_internal
def test_location_modifier(): def test_location_modifier():
@ -89,9 +89,9 @@ location = / {
''' '''
directive = _get_parsed(config) directive = _get_parsed(config)
assert_is_instance(directive, LocationBlock) assert isinstance(directive, LocationBlock)
assert_equals(directive.modifier, '=') assert directive.modifier == '='
assert_equals(directive.path, '/') assert directive.path == '/'
def test_if(): def test_if():
@ -101,13 +101,13 @@ if ($some) {
''' '''
directive = _get_parsed(config) directive = _get_parsed(config)
assert_is_instance(directive, IfBlock) assert isinstance(directive, IfBlock)
assert_true(directive.is_block) assert directive.is_block
assert_false(directive.self_context) assert not directive.self_context
assert_false(directive.provide_variables) assert not directive.provide_variables
assert_equals(directive.variable, '$some') assert directive.variable == '$some'
assert_is_none(directive.operand) assert directive.operand is None
assert_is_none(directive.value) assert directive.value is None
def test_if_modifier(): def test_if_modifier():
@ -117,10 +117,10 @@ if (-f /some) {
''' '''
directive = _get_parsed(config) directive = _get_parsed(config)
assert_is_instance(directive, IfBlock) assert isinstance(directive, IfBlock)
assert_equals(directive.operand, '-f') assert directive.operand == '-f'
assert_equals(directive.value, '/some') assert directive.value == '/some'
assert_is_none(directive.variable) assert directive.variable is None
def test_if_variable(): def test_if_variable():
@ -130,10 +130,10 @@ if ($http_some = '/some') {
''' '''
directive = _get_parsed(config) directive = _get_parsed(config)
assert_is_instance(directive, IfBlock) assert isinstance(directive, IfBlock)
assert_equals(directive.variable, '$http_some') assert directive.variable == '$http_some'
assert_equals(directive.operand, '=') assert directive.operand == '='
assert_equals(directive.value, '/some') assert directive.value == '/some'
def test_block_some_flat(): def test_block_some_flat():
@ -150,8 +150,8 @@ def test_block_some_flat():
directive = _get_parsed(config) directive = _get_parsed(config)
for d in ['default_type', 'sendfile', 'keepalive_timeout']: for d in ['default_type', 'sendfile', 'keepalive_timeout']:
c = directive.some(d, flat=True) c = directive.some(d, flat=True)
assert_is_not_none(c) assert c is not None
assert_equals(c.name, d) assert c.name == d
def test_block_some_not_flat(): def test_block_some_not_flat():
@ -167,7 +167,7 @@ def test_block_some_not_flat():
directive = _get_parsed(config) directive = _get_parsed(config)
c = directive.some('keepalive_timeout', flat=False) c = directive.some('keepalive_timeout', flat=False)
assert_is_none(c) assert c is None
def test_block_find_flat(): def test_block_find_flat():
@ -182,9 +182,9 @@ def test_block_find_flat():
directive = _get_parsed(config) directive = _get_parsed(config)
finds = directive.find('directive', flat=True) finds = directive.find('directive', flat=True)
assert_equals(len(finds), 2) assert len(finds) == 2
assert_equals([x.name for x in finds], ['directive', 'directive']) assert [x.name for x in finds] == ['directive', 'directive']
assert_equals([x.args[0] for x in finds], ['1', '2']) assert [x.args[0] for x in finds] == ['1', '2']
def test_block_find_not_flat(): def test_block_find_not_flat():
@ -199,9 +199,9 @@ def test_block_find_not_flat():
directive = _get_parsed(config) directive = _get_parsed(config)
finds = directive.find('directive', flat=False) finds = directive.find('directive', flat=False)
assert_equals(len(finds), 1) assert len(finds) == 1
assert_equals([x.name for x in finds], ['directive']) assert [x.name for x in finds] == ['directive']
assert_equals([x.args[0] for x in finds], ['1']) assert [x.args[0] for x in finds] == ['1']
def test_block_map(): def test_block_map():
@ -213,11 +213,11 @@ map $some_var $some_other_var {
''' '''
directive = _get_parsed(config) directive = _get_parsed(config)
assert_is_instance(directive, MapBlock) assert isinstance(directive, MapBlock)
assert_true(directive.is_block) assert directive.is_block
assert_false(directive.self_context) assert not directive.self_context
assert_true(directive.provide_variables) assert directive.provide_variables
assert_equals(directive.variable, 'some_other_var') assert directive.variable == 'some_other_var'
def test_block_geo_two_vars(): def test_block_geo_two_vars():
@ -229,11 +229,11 @@ geo $some_var $some_other_var {
''' '''
directive = _get_parsed(config) directive = _get_parsed(config)
assert_is_instance(directive, GeoBlock) assert isinstance(directive, GeoBlock)
assert_true(directive.is_block) assert directive.is_block
assert_false(directive.self_context) assert not directive.self_context
assert_true(directive.provide_variables) assert directive.provide_variables
assert_equals(directive.variable, 'some_other_var') assert directive.variable == 'some_other_var'
def test_block_geo_one_var(): def test_block_geo_one_var():
@ -245,8 +245,8 @@ geo $some_var {
''' '''
directive = _get_parsed(config) directive = _get_parsed(config)
assert_is_instance(directive, GeoBlock) assert isinstance(directive, GeoBlock)
assert_true(directive.is_block) assert directive.is_block
assert_false(directive.self_context) assert not directive.self_context
assert_true(directive.provide_variables) assert directive.provide_variables
assert_equals(directive.variable, 'some_var') assert directive.variable == 'some_var'

View File

@ -12,89 +12,89 @@ def test_directive():
config = 'some "foo" "bar";' config = 'some "foo" "bar";'
directive = _get_parsed(config) directive = _get_parsed(config)
assert_is_instance(directive, Directive) assert isinstance(directive, Directive)
assert_equals(directive.name, 'some') assert directive.name == 'some'
assert_equals(directive.args, ['foo', 'bar']) assert directive.args == ['foo', 'bar']
assert_equals(str(directive), 'some foo bar;') assert str(directive) == 'some foo bar;'
def test_add_header(): def test_add_header():
config = 'add_header "X-Foo" "bar";' config = 'add_header "X-Foo" "bar";'
directive = _get_parsed(config) directive = _get_parsed(config)
assert_is_instance(directive, AddHeaderDirective) assert isinstance(directive, AddHeaderDirective)
assert_equals(directive.name, 'add_header') assert directive.name == 'add_header'
assert_equals(directive.args, ['X-Foo', 'bar']) assert directive.args == ['X-Foo', 'bar']
assert_equals(directive.header, 'x-foo') assert directive.header == 'x-foo'
assert_equals(directive.value, 'bar') assert directive.value == 'bar'
assert_false(directive.always) assert not directive.always
assert_equals(str(directive), 'add_header X-Foo bar;') assert str(directive) == 'add_header X-Foo bar;'
def test_add_header_always(): def test_add_header_always():
config = 'add_header "X-Foo" "bar" always;' config = 'add_header "X-Foo" "bar" always;'
directive = _get_parsed(config) directive = _get_parsed(config)
assert_is_instance(directive, AddHeaderDirective) assert isinstance(directive, AddHeaderDirective)
assert_equals(directive.name, 'add_header') assert directive.name == 'add_header'
assert_equals(directive.args, ['X-Foo', 'bar', 'always']) assert directive.args == ['X-Foo', 'bar', 'always']
assert_equals(directive.header, 'x-foo') assert directive.header == 'x-foo'
assert_equals(directive.value, 'bar') assert directive.value == 'bar'
assert_true(directive.always) assert directive.always
assert_equals(str(directive), 'add_header X-Foo bar always;') assert str(directive) == 'add_header X-Foo bar always;'
def test_set(): def test_set():
config = 'set $foo bar;' config = 'set $foo bar;'
directive = _get_parsed(config) directive = _get_parsed(config)
assert_is_instance(directive, SetDirective) assert isinstance(directive, SetDirective)
assert_equals(directive.name, 'set') assert directive.name == 'set'
assert_equals(directive.args, ['$foo', 'bar']) assert directive.args == ['$foo', 'bar']
assert_equals(directive.variable, 'foo') assert directive.variable == 'foo'
assert_equals(directive.value, 'bar') assert directive.value == 'bar'
assert_equals(str(directive), 'set $foo bar;') assert str(directive) == 'set $foo bar;'
assert_true(directive.provide_variables) assert directive.provide_variables
def test_rewrite(): def test_rewrite():
config = 'rewrite ^ http://some;' config = 'rewrite ^ http://some;'
directive = _get_parsed(config) directive = _get_parsed(config)
assert_is_instance(directive, RewriteDirective) assert isinstance(directive, RewriteDirective)
assert_equals(directive.name, 'rewrite') assert directive.name == 'rewrite'
assert_equals(directive.args, ['^', 'http://some']) assert directive.args == ['^', 'http://some']
assert_equals(str(directive), 'rewrite ^ http://some;') assert str(directive) == 'rewrite ^ http://some;'
assert_true(directive.provide_variables) assert directive.provide_variables
assert_equals(directive.pattern, '^') assert directive.pattern == '^'
assert_equals(directive.replace, 'http://some') assert directive.replace == 'http://some'
assert_equals(directive.flag, None) assert directive.flag == None
def test_rewrite_flags(): def test_rewrite_flags():
config = 'rewrite ^/(.*)$ http://some/$1 redirect;' config = 'rewrite ^/(.*)$ http://some/$1 redirect;'
directive = _get_parsed(config) directive = _get_parsed(config)
assert_is_instance(directive, RewriteDirective) assert isinstance(directive, RewriteDirective)
assert_equals(directive.name, 'rewrite') assert directive.name == 'rewrite'
assert_equals(directive.args, ['^/(.*)$', 'http://some/$1', 'redirect']) assert directive.args == ['^/(.*)$', 'http://some/$1', 'redirect']
assert_equals(str(directive), 'rewrite ^/(.*)$ http://some/$1 redirect;') assert str(directive) == 'rewrite ^/(.*)$ http://some/$1 redirect;'
assert_true(directive.provide_variables) assert directive.provide_variables
assert_equals(directive.pattern, '^/(.*)$') assert directive.pattern == '^/(.*)$'
assert_equals(directive.replace, 'http://some/$1') assert directive.replace == 'http://some/$1'
assert_equals(directive.flag, 'redirect') assert directive.flag == 'redirect'
def test_root(): def test_root():
config = 'root /var/www/html;' config = 'root /var/www/html;'
directive = _get_parsed(config) directive = _get_parsed(config)
assert_is_instance(directive, RootDirective) assert isinstance(directive, RootDirective)
assert_equals(directive.name, 'root') assert directive.name == 'root'
assert_equals(directive.args, ['/var/www/html']) assert directive.args == ['/var/www/html']
assert_equals(str(directive), 'root /var/www/html;') assert str(directive) == 'root /var/www/html;'
assert_true(directive.provide_variables) assert directive.provide_variables
assert_equals(directive.path, '/var/www/html') assert directive.path == '/var/www/html'

View File

@ -64,38 +64,38 @@ server {
''' '''
tree = _parse(config) tree = _parse(config)
assert_is_instance(tree, Directive) assert isinstance(tree, Directive)
assert_is_instance(tree, Block) assert isinstance(tree, Block)
assert_is_instance(tree, Root) assert isinstance(tree, Root)
assert_equal(len(tree.children), 1) assert len(tree.children) == 1
http = tree.children[0] http = tree.children[0]
assert_is_instance(http, Directive) assert isinstance(http, Directive)
assert_is_instance(http, Block) assert isinstance(http, Block)
assert_is_instance(http, HttpBlock) assert isinstance(http, HttpBlock)
assert_equal(len(http.children), 1) assert len(http.children) == 1
include_server = http.children[0] include_server = http.children[0]
assert_is_instance(include_server, Directive) assert isinstance(include_server, Directive)
assert_is_instance(include_server, IncludeBlock) assert isinstance(include_server, IncludeBlock)
assert_equal(include_server.file_path, '/etc/nginx/sites/default.conf') 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] server = include_server.children[0]
assert_is_instance(server, Directive) assert isinstance(server, Directive)
assert_is_instance(server, Block) assert isinstance(server, Block)
assert_is_instance(server, ServerBlock) assert isinstance(server, ServerBlock)
assert_equal(len(server.children), 1) assert len(server.children) == 1
include_listen = server.children[0] include_listen = server.children[0]
assert_is_instance(include_listen, Directive) assert isinstance(include_listen, Directive)
assert_is_instance(include_listen, IncludeBlock) assert isinstance(include_listen, IncludeBlock)
assert_equal(include_listen.file_path, '/etc/nginx/conf.d/listen') 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] listen = include_listen.children[0]
assert_is_instance(listen, Directive) assert isinstance(listen, Directive)
assert_equal(listen.args, ['80']) assert listen.args == ['80']
def test_encoding(): def test_encoding():
@ -109,10 +109,10 @@ def test_encoding():
def assert_config(config, expected): def assert_config(config, expected):
tree = _parse(config) tree = _parse(config)
assert_is_instance(tree, Directive) assert isinstance(tree, Directive)
assert_is_instance(tree, Block) assert isinstance(tree, Block)
assert_is_instance(tree, Root) assert isinstance(tree, Root)
child = tree.children[0] child = tree.children[0]
for ex in expected: for ex in expected:
assert_is_instance(child, ex) assert isinstance(child, ex)

View File

@ -556,9 +556,9 @@ add_header X-Test "Windows-1251";
''' '''
actual = RawParser().parse(config) actual = RawParser().parse(config)
assert_equals(len(actual.asList()), 2) assert len(actual.asList()) == 2
def assert_config(config, expected): def assert_config(config, expected):
actual = RawParser().parse(config) actual = RawParser().parse(config)
assert_equals(actual.asList(), expected) assert actual.asList() == expected

View File

@ -53,10 +53,8 @@ def test_from_config():
manager = PluginsManager() manager = PluginsManager()
for plugin in manager.plugins: for plugin in manager.plugins:
plugin = plugin.name plugin = plugin.name
assert_true(plugin in tested_plugins, assert plugin in tested_plugins, 'Plugin {name!r} should have at least one simple test config'.format(name=plugin)
'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_true(plugin in tested_fp_plugins,
'Plugin {name!r} should have at least one simple test config with false positive'.format(name=plugin))
def parse_plugin_options(config_path): def parse_plugin_options(config_path):
@ -85,24 +83,22 @@ def check_configuration(plugin, config_path, test_config):
formatter.feed(config_path, yoda) formatter.feed(config_path, yoda)
_, results = formatter.reports.popitem() _, results = formatter.reports.popitem()
assert_equals(len(results), 1, 'Should have one report') assert len(results) == 1, 'Should have one report'
result = results[0] result = results[0]
if 'severity' in test_config: if 'severity' in test_config:
if not hasattr(test_config['severity'], '__iter__'): if not hasattr(test_config['severity'], '__iter__'):
assert_equals(result['severity'], test_config['severity']) assert result['severity'] == test_config['severity']
else: else:
assert_in(result['severity'], test_config['severity']) assert result['severity'] in test_config['severity']
assert_equals(result['plugin'], plugin) assert result['plugin'] == plugin
assert_true(result['summary']) assert result['summary']
assert_true(result['description']) assert result['description']
assert_true(result['config']) assert result['config']
assert_true(result['help_url'].startswith('https://'), assert result['help_url'].startswith('https://'), 'help_url must starts with https://. It\'is URL!'
'help_url must starts with https://. It\'is URL!')
def check_configuration_fp(plugin, config_path, test_config): def check_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_equals(len([x for x in yoda.results]), 0, assert len([x for x in yoda.results]) == 0, 'False positive configuration must not trigger any plugins'
'False positive configuration must not trigger any plugins')