Skip to content

Commit

Permalink
Ensure test quality via linting
Browse files Browse the repository at this point in the history
- List standard modules first in imports.
- Use dict literals.
- Use with when opening files.
- Don't specify object as superclass.
  • Loading branch information
akosthekiss committed Nov 6, 2023
1 parent 39fcdf8 commit f6287c9
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 91 deletions.
1 change: 1 addition & 0 deletions .pylintrc
Expand Up @@ -19,6 +19,7 @@ disable=
redefined-builtin,
too-few-public-methods,
too-many-branches,
unspecified-encoding,
useless-option-value, # disables warning in recent pylint that does not check for no-self-use anymore

[REPORTS]
Expand Down
183 changes: 97 additions & 86 deletions tests/test_build_antlr.py
Expand Up @@ -5,12 +5,14 @@
# This file may not be copied, modified, or distributed except
# according to those terms.

import pytest
import sys

from distutils.errors import DistutilsModuleError
from os import makedirs
from os.path import abspath, dirname, isfile, join
from distutils.errors import DistutilsModuleError

import pytest

from setuptools.dist import Distribution

import antlerinator
Expand Down Expand Up @@ -38,19 +40,19 @@ def test_build_antlr_providers(tmpdir):
with tmpdir.as_cwd():
antlr_jar_path = antlerinator.download(version=tested_antlr_version, path=join(str(tmpdir), 'antlr.jar'))

dist = Distribution(dict(
name='pkg',
script_name='setup.py',
script_args=['build_antlr'],
options=dict(
build_antlr=dict(
commands=f'''
dist = Distribution({
'name': 'pkg',
'script_name': 'setup.py',
'script_args': ['build_antlr'],
'options': {
'build_antlr': {
'commands': f'''
antlerinator:{tested_antlr_version} {join(resources_dir, "Hello.g4")} -Dlanguage=Python3 -o {tmpdir} -Xexact-output-dir
file:{antlr_jar_path} {join(resources_dir, "Bello.g4")} -Dlanguage=Python3 -o {tmpdir} -Xexact-output-dir
''',
),
),
))
},
},
})

dist.parse_command_line()
dist.run_commands()
Expand All @@ -66,17 +68,17 @@ def test_build_antlr_java(tmpdir):
Test whether ``build_antlr`` can deal with a custom java VM.
"""
with tmpdir.as_cwd():
dist = Distribution(dict(
name='pkg',
script_name='setup.py',
script_args=['build_antlr'],
options=dict(
build_antlr=dict(
commands='file:antlr.jar Dummy.g4',
java=f'{join(resources_dir, "mock_java")}{script_ext}',
),
),
))
dist = Distribution({
'name': 'pkg',
'script_name': 'setup.py',
'script_args': ['build_antlr'],
'options': {
'build_antlr': {
'commands': 'file:antlr.jar Dummy.g4',
'java': f'{join(resources_dir, "mock_java")}{script_ext}',
},
},
})

dist.parse_command_line()
dist.run_commands()
Expand All @@ -92,19 +94,20 @@ def test_build(tmpdir):
command is invoked (which is also invoked during ``install``).
"""
with tmpdir.as_cwd():
dist = Distribution(dict(
name='pkg',
packages=['pkg'],
script_name='setup.py',
script_args=['build', f'--build-lib={join("build", "lib")}'], # NOTE: --build-lib is necessary to ensure that purelib build directory is used
options=dict(
build_antlr=dict(
commands=f'antlerinator:{tested_antlr_version} {join(resources_dir, "Hello.g4")} -Dlanguage=Python3 -o {join(str(tmpdir), "pkg")} -Xexact-output-dir',
),
),
))
dist = Distribution({
'name': 'pkg',
'packages': ['pkg'],
'script_name': 'setup.py',
'script_args': ['build', f'--build-lib={join("build", "lib")}'], # NOTE: --build-lib is necessary to ensure that purelib build directory is used
'options': {
'build_antlr': {
'commands': f'antlerinator:{tested_antlr_version} {join(resources_dir, "Hello.g4")} -Dlanguage=Python3 -o {join(str(tmpdir), "pkg")} -Xexact-output-dir',
},
},
})
makedirs('pkg')
open(join('pkg', '__init__.py'), 'w').close()
with open(join('pkg', '__init__.py'), 'w'):
pass

dist.parse_command_line()
dist.run_commands()
Expand All @@ -119,19 +122,20 @@ def test_develop(tmpdir):
development mode).
"""
with tmpdir.as_cwd():
dist = Distribution(dict(
name='pkg',
packages=['pkg'],
script_name='setup.py',
script_args=['develop'],
options=dict(
build_antlr=dict(
commands=f'antlerinator:{tested_antlr_version} {join(resources_dir, "Hello.g4")} -Dlanguage=Python3 -o {join(str(tmpdir), "pkg")} -Xexact-output-dir',
),
),
))
dist = Distribution({
'name': 'pkg',
'packages': ['pkg'],
'script_name': 'setup.py',
'script_args': ['develop'],
'options': {
'build_antlr': {
'commands': f'antlerinator:{tested_antlr_version} {join(resources_dir, "Hello.g4")} -Dlanguage=Python3 -o {join(str(tmpdir), "pkg")} -Xexact-output-dir',
},
},
})
makedirs('pkg')
open(join('pkg', '__init__.py'), 'w').close()
with open(join('pkg', '__init__.py'), 'w'):
pass

dist.parse_command_line()
dist.run_commands()
Expand All @@ -147,19 +151,20 @@ def test_editable_wheel(tmpdir):
editable wheels).
"""
with tmpdir.as_cwd():
dist = Distribution(dict(
name='pkg',
packages=['pkg'],
script_name='setup.py',
script_args=['editable_wheel'],
options=dict(
build_antlr=dict(
commands=f'antlerinator:{tested_antlr_version} {join(resources_dir, "Hello.g4")} -Dlanguage=Python3 -o {join(str(tmpdir), "pkg")} -Xexact-output-dir',
),
),
))
dist = Distribution({
'name': 'pkg',
'packages': ['pkg'],
'script_name': 'setup.py',
'script_args': ['editable_wheel'],
'options': {
'build_antlr': {
'commands': f'antlerinator:{tested_antlr_version} {join(resources_dir, "Hello.g4")} -Dlanguage=Python3 -o {join(str(tmpdir), "pkg")} -Xexact-output-dir',
},
},
})
makedirs('pkg')
open(join('pkg', '__init__.py'), 'w').close()
with open(join('pkg', '__init__.py'), 'w'):
pass

dist.parse_command_line()
dist.run_commands()
Expand All @@ -173,21 +178,24 @@ def test_clean(tmpdir):
Test whether cleanup removes generated files.
"""
with tmpdir.as_cwd():
dist = Distribution(dict(
name='pkg',
packages=['pkg'],
script_name='setup.py',
script_args=['clean'],
options=dict(
build_antlr=dict(
output=join('pkg', 'Dummy*.py'),
),
),
))
dist = Distribution({
'name': 'pkg',
'packages': ['pkg'],
'script_name': 'setup.py',
'script_args': ['clean'],
'options': {
'build_antlr': {
'output': join('pkg', 'Dummy*.py'),
},
},
})
makedirs('pkg')
open(join('pkg', '__init__.py'), 'w').close()
open(join('pkg', 'DummyLexer.py'), 'w').close()
open(join('pkg', 'DummyParser.py'), 'w').close()
with open(join('pkg', '__init__.py'), 'w'):
pass
with open(join('pkg', 'DummyLexer.py'), 'w'):
pass
with open(join('pkg', 'DummyParser.py'), 'w'):
pass

dist.parse_command_line()
dist.run_commands()
Expand All @@ -203,20 +211,23 @@ def test_sdist(tmpdir):
MANIFEST.in.
"""
with tmpdir.as_cwd():
dist = Distribution(dict(
name='pkg',
packages=['pkg'],
script_name='setup.py',
options=dict(
build_antlr=dict(
output=join('pkg', 'Dummy*.py'),
),
),
))
dist = Distribution({
'name': 'pkg',
'packages': ['pkg'],
'script_name': 'setup.py',
'options': {
'build_antlr': {
'output': join('pkg', 'Dummy*.py'),
},
},
})
makedirs('pkg')
open(join('pkg', '__init__.py'), 'w').close()
open(join('pkg', 'DummyLexer.py'), 'w').close()
open(join('pkg', 'DummyParser.py'), 'w').close()
with open(join('pkg', '__init__.py'), 'w'):
pass
with open(join('pkg', 'DummyLexer.py'), 'w'):
pass
with open(join('pkg', 'DummyParser.py'), 'w'):
pass
with open('MANIFEST.in', 'w') as f:
f.write('exclude pkg/Dummy*.py')

Expand Down
7 changes: 4 additions & 3 deletions tests/test_download.py
@@ -1,15 +1,16 @@
# Copyright (c) 2017-2022 Renata Hodovan, Akos Kiss.
# Copyright (c) 2017-2023 Renata Hodovan, Akos Kiss.
#
# Licensed under the BSD 3-Clause License
# <LICENSE.rst or https://opensource.org/licenses/BSD-3-Clause>.
# This file may not be copied, modified, or distributed except
# according to those terms.

import os
import pytest
import subprocess
import sys

import pytest

import antlerinator


Expand Down Expand Up @@ -43,7 +44,7 @@ def run_download(args, exp_ok):
True,
False
])
class TestDownload(object):
class TestDownload:

def test_cli(self, antlr_version, default_path, tmpdir):
args = [f'--antlr-version={antlr_version}']
Expand Down
5 changes: 3 additions & 2 deletions tox.ini
Expand Up @@ -16,9 +16,10 @@ usedevelop = true
deps =
pycodestyle
pylint
pytest
commands =
pylint src/antlerinator
pycodestyle src/antlerinator --ignore=E501
pylint src/antlerinator tests
pycodestyle src/antlerinator tests --ignore=E501

[testenv:docs]
deps =
Expand Down

0 comments on commit f6287c9

Please sign in to comment.