Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adambenali committed Feb 28, 2020
1 parent af670b1 commit b18a596
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 23 deletions.
11 changes: 11 additions & 0 deletions tests/plugins/tokenize_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,14 @@ def factory(code: str):
lines = io.StringIO(dedent(code))
return list(tokenize.generate_tokens(lambda: next(lines)))
return factory


@pytest.fixture(scope='session')
def parse_file_tokens():
"""Parses tokens from a file."""
def factory(filename: str):
with open(filename, 'r', encoding='utf-8') as test_file:
file_content = test_file.read()
lines = io.StringIO(dedent(file_content))
return list(tokenize.generate_tokens(lambda: next(lines)))
return factory
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
)
from wemake_python_styleguide.visitors.tokenize.comments import ShebangVisitor

SHEBANG_RESOURCES_FOLDER = 'test_valid_shebang_resources'
SHEBANG_RESOURCES_FOLDER = 'test_executable_mismatch_resources'
TESTS_FOLDER = Path(__file__).absolute().parent.parent.parent.parent
RESOURCES_FULL_PATH = TESTS_FOLDER / 'fixtures' / SHEBANG_RESOURCES_FOLDER

Expand All @@ -23,22 +23,19 @@
])
def test_exe_negative(
assert_errors,
parse_tokens,
parse_file_tokens,
default_options,
error_code,
):
"""Testing cases when no errors should be reported."""
filename = RESOURCES_FULL_PATH / ''.join([error_code, '_neg.py'])
with open(filename, 'r', encoding='utf-8') as test_file:
file_content = test_file.read()
file_tokens = parse_tokens(file_content)
visitor = ShebangVisitor(
default_options,
filename=filename,
file_tokens=file_tokens,
)
visitor.run()
assert_errors(visitor, [])
file_tokens = parse_file_tokens(filename)
visitor = ShebangVisitor(
default_options,
filename=filename,
file_tokens=file_tokens,
)
assert_errors(visitor, [])


@pytest.mark.parametrize('error_code', [
Expand All @@ -50,19 +47,17 @@ def test_exe_negative(
])
def test_exe_positive(
assert_errors,
parse_tokens,
parse_file_tokens,
default_options,
error_code,
):
"""Testing cases when errors should be reported."""
filename = RESOURCES_FULL_PATH / ''.join([error_code, '_pos.py'])
with open(filename, 'r', encoding='utf-8') as test_file:
file_content = test_file.read()
file_tokens = parse_tokens(file_content)
visitor = ShebangVisitor(
default_options,
filename=filename,
file_tokens=file_tokens,
)
visitor.run()
assert_errors(visitor, [ExecutableMismatchViolation])
file_tokens = parse_file_tokens(filename)
visitor = ShebangVisitor(
default_options,
filename=filename,
file_tokens=file_tokens,
)
visitor.run()
assert_errors(visitor, [ExecutableMismatchViolation])

0 comments on commit b18a596

Please sign in to comment.