Skip to content

Commit

Permalink
Style check based on black
Browse files Browse the repository at this point in the history
Avocado has been using black to enforce a code style for a long time
now, with very good results.  Let's expand the same to Avocado-VT.
With a common code style across Avocado and Avocado-VT it'll be easier
to move libraries to the common autils repo.

Reference: avocado-framework/autils#3
Signed-off-by: Cleber Rosa <[email protected]>
  • Loading branch information
clebergnu committed Jan 15, 2024
1 parent fb0bc42 commit 64b53df
Show file tree
Hide file tree
Showing 355 changed files with 42,783 additions and 30,048 deletions.
21 changes: 3 additions & 18 deletions CODING_STYLE
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,9 @@ maintainable. (Obviously using C or whatever for writing tests is fine).

Base coding style

When writing python code, unless otherwise stated, stick to the python style
guide (http://www.python.org/dev/peps/pep-0008/). We now ship a script,
run_pep8.py, that can verify non compliances to the PEP8, with some of
the few exceptions we allow people to do. If you see that the script
finds problems with your code, we expect you to fix them before you
send pull requests. In order for the script to be useful you'll have to
use the tools provided by your distro to install the programs 'pep8' and
'autopep8'.


Indentation & whitespace

Format your code for an 80 character wide screen.

Indentation is now 4 spaces, as opposed to hard tabs (which it used to be).
This is the Python standard.

Don't leave trailing whitespace, or put whitespace on blank lines.
The coding style for all Python code is the one enforced by black (see
https://black.readthedocs.io/en/stable/the_black_code_style/). The
selftests/style.sh script can be used to verify the code style matches.


Variable names and UpPeR cAsE
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ requirements: pip
- $(PYTHON) -m pip install -r requirements.txt

check:
inspekt checkall --disable-lint W,R,C,E0203,E0601,E1002,E1101,E1102,E1103,E1120,F0401,I0011,E1003,W605,I1101 --disable-style W605,W606,E501,E265,W601,E402,E722,E741 --exclude avocado-libs,scripts/github --no-license-check
./selftests/style.sh
inspekt lint --disable W,R,C,E0203,E0601,E1002,E1101,E1102,E1103,E1120,F0401,I0011,E1003,W605,I1101 --exclude avocado-libs,scripts/github
pylint --errors-only --disable=all --enable=spelling --spelling-dict=en_US --spelling-private-dict-file=spell.ignore *

clean:
Expand Down
25 changes: 12 additions & 13 deletions avocado_vt/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@


class DiscoveryMixIn:

def _get_parser(self):
options_processor = VirtTestOptionsProcess(self.config)
return options_processor.get_parser()

def _save_parser_cartesian_config(self, parser):
path = get_opt(self.config, 'vt.save_config')
path = get_opt(self.config, "vt.save_config")
if path is None:
return
with open(path, 'w') as cartesian_config:
with open(path, "w") as cartesian_config:
cartesian_config.write("include %s\n" % parser.filename)
for statement in (parser.only_filters + parser.no_filters +
parser.assignments):
for statement in (
parser.only_filters + parser.no_filters + parser.assignments
):
cartesian_config.write("%s\n" % statement)

def convert_parameters(self, params):
Expand All @@ -46,19 +46,18 @@ def convert_parameters(self, params):
:return: dict with test name and vt parameters
"""
test_name = params.get("_short_name_map_file")["subtests.cfg"]
if (get_opt(self.config, 'vt.config')
and get_opt(self.config, 'vt.short_names_when_config')):
if get_opt(self.config, "vt.config") and get_opt(
self.config, "vt.short_names_when_config"
):
test_name = params.get("shortname")
elif get_opt(self.config, 'vt.type') == "spice":
elif get_opt(self.config, "vt.type") == "spice":
short_name_map_file = params.get("_short_name_map_file")
if "tests-variants.cfg" in short_name_map_file:
test_name = short_name_map_file.get("tests-variants.cfg",
test_name)
test_name = short_name_map_file.get("tests-variants.cfg", test_name)
# We want avocado to inject params coming from its multiplexer into
# the test params. This will allow users to access avocado params
# from inside virt tests. This feature would only work if the virt
# test in question is executed from inside avocado.
params['id'] = test_name
test_parameters = {'name': test_name,
'vt_params': params}
params["id"] = test_name
test_parameters = {"name": test_name, "vt_params": params}
return test_parameters
63 changes: 34 additions & 29 deletions avocado_vt/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

try:
from avocado.core import loader

AVOCADO_LOADER_AVAILABLE = True
except ImportError:
AVOCADO_LOADER_AVAILABLE = False
Expand All @@ -45,23 +46,24 @@ def guest_listing(config, guest_name_parser=None):
"""
List available guest operating systems and info about image availability
"""
if get_opt(config, 'vt.type') == 'lvsb':
if get_opt(config, "vt.type") == "lvsb":
raise ValueError("No guest types available for lvsb testing")
LOG.debug("Using %s for guest images\n",
os.path.join(data_dir.get_data_dir(), 'images'))
LOG.debug(
"Using %s for guest images\n", os.path.join(data_dir.get_data_dir(), "images")
)
LOG.info("Available guests in config:")
if guest_name_parser is None:
guest_name_parser = standalone_test.get_guest_name_parser(config)
for params in guest_name_parser.get_dicts():
base_dir = params.get("images_base_dir", data_dir.get_data_dir())
image_name = storage.get_image_filename(params, base_dir)
machine_type = get_opt(config, 'vt.common.machine_type')
name = params['name'].replace('.%s' % machine_type, '')
machine_type = get_opt(config, "vt.common.machine_type")
name = params["name"].replace(".%s" % machine_type, "")
if os.path.isfile(image_name):
out = name
else:
missing = "(missing %s)" % os.path.basename(image_name)
out = (name + " " + output.TERM_SUPPORT.warn_header_str(missing))
out = name + " " + output.TERM_SUPPORT.warn_header_str(missing)
LOG.debug(out)
LOG.debug("")

Expand All @@ -70,17 +72,17 @@ def arch_listing(config, guest_name_parser=None):
"""
List available machine/archs for given guest operating systems
"""
guest_os = get_opt(config, 'vt.guest_os')
guest_os = get_opt(config, "vt.guest_os")
if guest_os is not None:
extra = " for guest os \"%s\"" % guest_os
extra = ' for guest os "%s"' % guest_os
else:
extra = ""
LOG.info("Available arch profiles%s", extra)
if guest_name_parser is None:
guest_name_parser = standalone_test.get_guest_name_parser(config)
machine_type = get_opt(config, 'vt.common.machine_type')
machine_type = get_opt(config, "vt.common.machine_type")
for params in guest_name_parser.get_dicts():
LOG.debug(params['name'].replace('.%s' % machine_type, ''))
LOG.debug(params["name"].replace(".%s" % machine_type, ""))
LOG.debug("")


Expand All @@ -92,13 +94,14 @@ class NotAvocadoVTTest(object):


if AVOCADO_LOADER_AVAILABLE:

class VirtTestLoader(loader.TestLoader, DiscoveryMixIn):

"""
Avocado loader plugin to load avocado-vt tests
"""

name = 'vt'
name = "vt"

def __init__(self, config, extra_params):
"""
Expand All @@ -111,32 +114,32 @@ def __init__(self, config, extra_params):
# Avocado has renamed "args" to "config" in 84ae9a5d61, lets
# keep making the old name available for compatibility with
# new and old releases
if hasattr(self, 'config'):
self.args = self.config # pylint: disable=E0203
if hasattr(self, "config"):
self.args = self.config # pylint: disable=E0203
# And in case an older Avocado is used, the Loader class will
# contain an "args" attribute instead
else:
self.config = self.args # pylint: disable=E0203
self.config = self.args # pylint: disable=E0203
if vt_extra_params:
# We don't want to override the original config
self.config = copy.deepcopy(self.config)
extra = get_opt(self.config, 'vt.extra_params')
extra = get_opt(self.config, "vt.extra_params")
if extra is not None:
extra += vt_extra_params
else:
extra = vt_extra_params
set_opt(self.config, 'vt.extra_params', extra)
set_opt(self.config, "vt.extra_params", extra)

def get_extra_listing(self):
if get_opt(self.config, 'vt.list_guests'):
if get_opt(self.config, "vt.list_guests"):
config = copy.copy(self.config)
set_opt(config, 'vt.config', None)
set_opt(config, 'vt.guest_os', None)
set_opt(config, "vt.config", None)
set_opt(config, "vt.guest_os", None)
guest_listing(config)
if get_opt(self.config, 'vt.list_archs'):
if get_opt(self.config, "vt.list_archs"):
config = copy.copy(self.config)
set_opt(config, 'vt.common.machine_type', None)
set_opt(config, 'vt.common.arch', None)
set_opt(config, "vt.common.machine_type", None)
set_opt(config, "vt.common.arch", None)
arch_listing(config)

@staticmethod
Expand All @@ -147,7 +150,7 @@ def get_type_label_mapping():
:returns: a dictionary with the test class as key and description
as value.
"""
return {VirtTest: 'VT', NotAvocadoVTTest: "!VT"}
return {VirtTest: "VT", NotAvocadoVTTest: "!VT"}

@staticmethod
def get_decorator_mapping():
Expand All @@ -158,8 +161,10 @@ def get_decorator_mapping():
function as value.
"""
term_support = output.TermSupport()
return {VirtTest: term_support.healthy_str,
NotAvocadoVTTest: term_support.fail_header_str}
return {
VirtTest: term_support.healthy_str,
NotAvocadoVTTest: term_support.fail_header_str,
}

@staticmethod
def _report_bad_discovery(name, reason, which_tests):
Expand All @@ -184,15 +189,15 @@ def discover(self, url, which_tests=loader.DiscoverMode.DEFAULT):
# the other test plugins to handle the URL.
except cartesian_config.ParserError as details:
return self._report_bad_discovery(url, details, which_tests)
elif (which_tests is loader.DiscoverMode.DEFAULT and
not get_opt(self.config, 'vt.config')):
elif which_tests is loader.DiscoverMode.DEFAULT and not get_opt(
self.config, "vt.config"
):
# By default don't run anything unless vt.config provided
return []
# Create test_suite
test_suite = []
for params in (_ for _ in cartesian_parser.get_dicts()):
test_suite.append((VirtTest, self.convert_parameters(params)))
if which_tests is loader.DiscoverMode.ALL and not test_suite:
return self._report_bad_discovery(url, "No matching tests",
which_tests)
return self._report_bad_discovery(url, "No matching tests", which_tests)
return test_suite
Loading

0 comments on commit 64b53df

Please sign in to comment.