test: Format with black

Signed-off-by: Jonathan Woollett-Light <jcawl@amazon.co.uk>
This commit is contained in:
Jonathan Woollett-Light
2023-06-05 09:55:04 +01:00
committed by Jonathan Woollett-Light
parent 285971e8c7
commit 56bfdc97bb
8 changed files with 198 additions and 194 deletions
+34 -38
View File
@@ -51,15 +51,16 @@ def _read_test_config():
assert "exclude_path" in coverage_config
assert "crate_features" in coverage_config
assert ' ' not in coverage_config["crate_features"], \
"spaces are not allowed in crate_features value"
assert (
" " not in coverage_config["crate_features"]
), "spaces are not allowed in crate_features value"
return coverage_config
def _write_coverage_config(coverage_config):
"""Updates the coverage config file as per `coverage_config`"""
with open(COVERAGE_CONFIG_PATH, 'w') as outfile:
with open(COVERAGE_CONFIG_PATH, "w") as outfile:
json.dump(coverage_config, outfile)
@@ -78,48 +79,43 @@ def _get_current_coverage(coverage_config, no_cleanup, test_scope):
shutil.rmtree(kcov_output_dir, ignore_errors=True)
shutil.rmtree(kcov_build_dir, ignore_errors=True)
exclude_pattern = (
'${CARGO_HOME:-$HOME/.cargo/},'
'usr/lib/,'
'lib/'
)
exclude_pattern = "${CARGO_HOME:-$HOME/.cargo/}," "usr/lib/," "lib/"
exclude_region = "'mod tests {'"
additional_exclude_path = coverage_config["exclude_path"]
if additional_exclude_path:
exclude_pattern += ',' + additional_exclude_path
exclude_pattern += "," + additional_exclude_path
additional_kcov_param = ''
additional_kcov_param = ""
if test_scope == pytest.workspace:
additional_kcov_param += '--all '
additional_kcov_param += "--all "
crate_features = coverage_config["crate_features"]
if crate_features:
additional_kcov_param += '--features=' + crate_features
additional_kcov_param += "--features=" + crate_features
kcov_cmd = "CARGO_TARGET_DIR={} cargo kcov {} " \
"--output {} -- " \
"--exclude-region={} " \
"--exclude-pattern={} " \
"--verify".format(
kcov_build_dir,
additional_kcov_param,
kcov_output_dir,
exclude_region,
exclude_pattern
)
kcov_cmd = (
"CARGO_TARGET_DIR={} cargo kcov {} "
"--output {} -- "
"--exclude-region={} "
"--exclude-pattern={} "
"--verify".format(
kcov_build_dir,
additional_kcov_param,
kcov_output_dir,
exclude_region,
exclude_pattern,
)
)
# Pytest closes stdin by default, but some tests might need it to be open.
# In the future, should the need arise, we can feed custom data to stdin.
subprocess.run(kcov_cmd, shell=True, check=True, input=b'')
subprocess.run(kcov_cmd, shell=True, check=True, input=b"")
# Read the coverage reported by kcov.
coverage_file = os.path.join(kcov_output_dir, 'index.js')
coverage_file = os.path.join(kcov_output_dir, "index.js")
with open(coverage_file) as cov_output:
coverage = float(re.findall(
r'"covered":"(\d+\.\d)"',
cov_output.read()
)[0])
coverage = float(re.findall(r'"covered":"(\d+\.\d)"', cov_output.read())[0])
# Remove coverage related directories.
# If user provided `--no-cleanup` flag, `kcov_output_dir`
@@ -140,14 +136,12 @@ def test_coverage(profile, no_cleanup, test_scope):
if previous_coverage < current_coverage:
if profile == pytest.profile_ci:
# In the CI Profile we expect the coverage to be manually updated.
assert False, \
"Coverage is increased from {} to {}. " \
"Please update the coverage in coverage_config_{}.json."\
.format(
previous_coverage,
current_coverage,
platform.machine()
assert False, (
"Coverage is increased from {} to {}. "
"Please update the coverage in coverage_config_{}.json.".format(
previous_coverage, current_coverage, platform.machine()
)
)
elif profile == pytest.profile_devel:
coverage_config["coverage_score"] = current_coverage
_write_coverage_config(coverage_config)
@@ -157,5 +151,7 @@ def test_coverage(profile, no_cleanup, test_scope):
# `pytest_addoption`.
assert False, "Invalid test profile."
elif previous_coverage > current_coverage:
assert False, "Coverage drops by {:.2f}%. Please add unit tests for " \
"the uncovered lines.".format(diff)
assert False, (
"Coverage drops by {:.2f}%. Please add unit tests for "
"the uncovered lines.".format(diff)
)