Skip to content

Commit

Permalink
Reformat code with black
Browse files Browse the repository at this point in the history
This commit simply runs "black" on all our Python files except
those in testsuites/tests/.

Note that I ran into one small difficulty when reformatting setup.py,
where black was reformatting a couple of lines in the code in a way
that it triggered the following E231 flake8 violations:

    setup.py:23:69: E231 missing whitespace after ','
    setup.py:25:74: E231 missing whitespace after ','

This is a known issue with black...

    psf/black#1288

... which is fortunately easily worked around by simply removing
the offending comma (black does not add it back).

Change-Id: I492eb1ca5fa371d063e691f7fe06c47daae60d4c
  • Loading branch information
brobecke committed Apr 20, 2021
1 parent 1eb13e8 commit a42c2a5
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 81 deletions.
15 changes: 6 additions & 9 deletions asclib/checkers/typific/__init__.py
Expand Up @@ -30,11 +30,9 @@ def __init__(self, filename, config):
# The purpose of this change is to make sure child class
# insn't missing an entry for one of the checkers.
for rulific_checker in ALL_RULIFIC_CHECKERS:
assert (
rulific_checker.RULE_CONFIG_NAME in self.rulific_decision_map
), "%s checker missing config about %s rule" % (
self.file_type,
rulific_checker.RULE_CONFIG_NAME,
assert rulific_checker.RULE_CONFIG_NAME in self.rulific_decision_map, (
"%s checker missing config about %s rule"
% (self.file_type, rulific_checker.RULE_CONFIG_NAME,)
)

# Build the list of checkers we actually want to run
Expand Down Expand Up @@ -206,10 +204,9 @@ def dump_checks(self, cvs_check_file_type, print_header=False, print_footer=Fals
for checker in ALL_RULIFIC_CHECKERS
if checker not in [c for _, c in RULES_LIST]
]
assert (
not missed_checkers
), "The following rules are missing from the dump above: " + ", ".join(
missed_checkers
assert not missed_checkers, (
"The following rules are missing from the dump above: "
+ ", ".join(missed_checkers)
)

######################################################################
Expand Down
1 change: 0 additions & 1 deletion asclib/checkers/typific/python.py
Expand Up @@ -40,7 +40,6 @@
# For more details, see:
# https://github.com/PyCQA/pycodestyle/issues/373
"E203",

# flake8: E402: module level import not at top of file
#
# Rationale: See Q721-011: We sometimes need to import sys,
Expand Down
11 changes: 2 additions & 9 deletions setup.py
Expand Up @@ -20,14 +20,7 @@
long_description_content_type="text/markdown",
packages=find_packages(where="."),
package_dir={"": "."},
package_data={
"asclib.checkers.typific": ["*.xml"],
"etc": ["*"],
},
package_data={"asclib.checkers.typific": ["*.xml"], "etc": ["*"]},
install_requires=install_requires,
entry_points={
"console_scripts": [
"style_checker = asclib.main:main"
],
},
entry_points={"console_scripts": ["style_checker = asclib.main:main"]},
)
2 changes: 1 addition & 1 deletion style_checker
Expand Up @@ -3,5 +3,5 @@
from asclib.main import main


if __name__ == '__main__':
if __name__ == "__main__":
main()
56 changes: 26 additions & 30 deletions testsuite/conftest.py
Expand Up @@ -30,18 +30,17 @@ def env_setup(request):
# file or directory it might be creating, by verifying at the end
# of the test that this temporary directory is empty.
style_checker_tmp_dir = tempfile.mkdtemp("", "style_checker-")
style_checker_tmp_dir = os.path.join(tmp_dir, 'tmp')
style_checker_tmp_dir = os.path.join(tmp_dir, "tmp")
os.mkdir(style_checker_tmp_dir)
for var_name in ('TMPDIR', 'TEMP', 'TMP'):
for var_name in ("TMPDIR", "TEMP", "TMP"):
os.environ[var_name] = style_checker_tmp_dir

def env_teardown():
os.environ.clear()
os.environ.update(saved_environ)

style_checker_tmp_dir_contents = os.listdir(style_checker_tmp_dir)
assert not style_checker_tmp_dir_contents, \
style_checker_tmp_dir_contents
assert not style_checker_tmp_dir_contents, style_checker_tmp_dir_contents

os.chdir(saved_cwd)
shutil.rmtree(tmp_dir)
Expand All @@ -63,6 +62,7 @@ class StyleCheckerFixture:
to be performed as if this was the current year (an int).
See the set_year method for additional information.
"""

def __init__(self, src_prefix_dir, testcase_src_dir, testcase_work_dir):
"""Initialize self.
Expand All @@ -74,8 +74,7 @@ def __init__(self, src_prefix_dir, testcase_src_dir, testcase_work_dir):
self.src_prefix_dir = src_prefix_dir
self.src_dir = testcase_src_dir
self.work_dir = testcase_work_dir
self.style_checker_exe = os.path.join(self.src_prefix_dir,
'style_checker')
self.style_checker_exe = os.path.join(self.src_prefix_dir, "style_checker")
self.forced_year = None

# Set the testcase up, by copying everything from the testcase's
Expand Down Expand Up @@ -139,18 +138,17 @@ def run_style_checker(self, *args, **kwargs):
"""
use_sys_executable = False
# The named parameters to use when calling subprocess.run
run_kwargs = {'cwd': self.work_dir,
'timeout': 60}
run_kwargs = {"cwd": self.work_dir, "timeout": 60}

for arg_name, arg_val in kwargs.items():
if arg_name in ('input', ):
if arg_name in ("input",):
run_kwargs[arg_name] = arg_val
elif arg_name == 'use_sys_executable' and arg_val:
elif arg_name == "use_sys_executable" and arg_val:
use_sys_executable = True
else:
raise ValueError(
'Invalid argument in call to run_style_checker: {}'
.format(arg_name))
"Invalid argument in call to run_style_checker: {}".format(arg_name)
)

cmd = []

Expand All @@ -159,7 +157,7 @@ def run_style_checker(self, *args, **kwargs):
cmd.append(self.style_checker_exe)

if self.forced_year is not None:
cmd.append('--forced-year=%d' % self.forced_year)
cmd.append("--forced-year=%d" % self.forced_year)

cmd.extend(list(args))

Expand All @@ -169,9 +167,7 @@ def enable_unit_test(self):
"""Set the environment up to allow us to perform unit tests."""
sys.path.insert(0, self.src_prefix_dir)

def make_minimal_copy_of_python_install(
self, target_dir, exclude_modules=None
):
def make_minimal_copy_of_python_install(self, target_dir, exclude_modules=None):
"""Create a minimal copy of Python in target_dir.
The goal of this method is to provide a minimal install that
Expand Down Expand Up @@ -209,20 +205,15 @@ def make_minimal_copy_of_python_install(
ignore = None
else:
ignore = []
site_pkg_dir = os.path.join(
src_root_dir, "lib", "python*", "site-packages"
)
site_pkg_dir = os.path.join(src_root_dir, "lib", "python*", "site-packages")
for module_name in exclude_modules:
ignore.extend(
os.path.relpath(p, src_root_dir)
for p in ls(os.path.join(site_pkg_dir, f"{module_name}*"))
)

sync_tree(
source=src_root_dir,
target=target_dir,
ignore=ignore,
file_list=file_list,
source=src_root_dir, target=target_dir, ignore=ignore, file_list=file_list,
)

def assertEqual(self, lhs, rhs, msg_if_fails):
Expand Down Expand Up @@ -302,21 +293,24 @@ def assertRunOutputEmpty(self, r):
PARAMETERS
r: A Run object.
"""
self.assertRunOutputEqual(r, '')
self.assertRunOutputEqual(r, "")


@pytest.fixture(scope="function")
def style_checker(pytestconfig, request):
"""Return a StyleCheckerFixture."""
testcase_script_filename = request.fspath.strpath
testcase_src_dir = os.path.dirname(testcase_script_filename)
return StyleCheckerFixture(src_prefix_dir=pytestconfig.rootdir.strpath,
testcase_src_dir=testcase_src_dir,
testcase_work_dir=os.getcwd())
return StyleCheckerFixture(
src_prefix_dir=pytestconfig.rootdir.strpath,
testcase_src_dir=testcase_src_dir,
testcase_work_dir=os.getcwd(),
)


class Run(e3.os.process.Run):
"""An e3.os.process.Run subclass with a few extra bells and whistles..."""

@property
def cmd_out(self):
"""Same as self.out, except that the output is sanitized.
Expand All @@ -334,6 +328,8 @@ def image(self):
REMARKS
This assumes that this command has run to completion.
"""
return '%% %s -> %s\n%s' % (self.command_line_image(),
self.status,
self.cmd_out)
return "%% %s -> %s\n%s" % (
self.command_line_image(),
self.status,
self.cmd_out,
)
82 changes: 51 additions & 31 deletions testsuite/run-validation-tests
Expand Up @@ -9,12 +9,11 @@ import shutil
import sys

# The minimum version of Python we need to run the validation testing.
MINIMUM_PYTHON_VERSION = '3.8'
MINIMUM_PYTHON_VERSION = "3.8"

# The root directory of this repository. By construction, it is
# the parent directory of the directory containing this script.
REPOSITORY_ROOT_DIR = \
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
REPOSITORY_ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


def run(cmds, **kwargs):
Expand Down Expand Up @@ -67,14 +66,15 @@ def check_dependencies(args):
required_packages.append("flake8")

# First, check that the Python being used is recent enough.
python_version = StrictVersion(
"{v.major}.{v.minor}".format(v=sys.version_info))
python_version = StrictVersion("{v.major}.{v.minor}".format(v=sys.version_info))
if python_version < MINIMUM_PYTHON_VERSION:
print("ERROR: Your version of Python is too old: "
"({v.major}.{v.minor}.{v.micro}-{v.releaselevel})"
.format(v=sys.version_info))
print(" Minimum version required: {}"
.format(MINIMUM_PYTHON_VERSION))
print(
"ERROR: Your version of Python is too old: "
"({v.major}.{v.minor}.{v.micro}-{v.releaselevel})".format(
v=sys.version_info
)
)
print(" Minimum version required: {}".format(MINIMUM_PYTHON_VERSION))
print("Aborting.")
sys.exit(1)

Expand Down Expand Up @@ -133,8 +133,7 @@ def run_testsuite(args):
# activated.
testsuite_cmd.extend(["-n", args.jobs])
if args.include_coverage:
testsuite_cmd.extend(["--cov", REPOSITORY_ROOT_DIR,
"--cov-report=html"])
testsuite_cmd.extend(["--cov", REPOSITORY_ROOT_DIR, "--cov-report=html"])
if args.testsuite_filter is not None:
testsuite_cmd.extend(["-k", args.testsuite_filter])

Expand All @@ -154,25 +153,46 @@ def run_style_conformance_checks(args):
def main():
"""Implement the main subprogram for this script."""
parser = ArgumentParser(description="Run the style_checker testsuite")
parser.add_argument("--jobs", "-j", metavar="N", default="auto",
help="Run the testsuite with N jobs in parallel."
" If not provided, the system determines the number"
" of jobs based on the number of CPU cores available.")
parser.add_argument("--no-testsuite", dest="run_testsuite",
default=True, action="store_false",
help="Skip running the testsuite (useful when"
" only trying to perform coding style conformance"
" checks")
parser.add_argument("--no-coverage", dest="include_coverage",
default=True, action="store_false",
help="Run the testsuite with coverage analysis")
parser.add_argument("--no-style-checking", dest="verify_style_conformance",
default=True, action="store_false",
help="Skip the coding style conformance checks")
parser.add_argument("testsuite_filter", metavar="EXPRESSION", nargs="?",
help="Ask pytest to restring the testing to the tests"
" matching the given substring expression (passed"
" to pytest -via -k)")
parser.add_argument(
"--jobs",
"-j",
metavar="N",
default="auto",
help="Run the testsuite with N jobs in parallel."
" If not provided, the system determines the number"
" of jobs based on the number of CPU cores available.",
)
parser.add_argument(
"--no-testsuite",
dest="run_testsuite",
default=True,
action="store_false",
help="Skip running the testsuite (useful when"
" only trying to perform coding style conformance"
" checks",
)
parser.add_argument(
"--no-coverage",
dest="include_coverage",
default=True,
action="store_false",
help="Run the testsuite with coverage analysis",
)
parser.add_argument(
"--no-style-checking",
dest="verify_style_conformance",
default=True,
action="store_false",
help="Skip the coding style conformance checks",
)
parser.add_argument(
"testsuite_filter",
metavar="EXPRESSION",
nargs="?",
help="Ask pytest to restring the testing to the tests"
" matching the given substring expression (passed"
" to pytest -via -k)",
)
args = parser.parse_args()

check_dependencies(args)
Expand Down

0 comments on commit a42c2a5

Please sign in to comment.