forked from rust-vmm/linux-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_coverage.py
110 lines (94 loc) · 3.8 KB
/
test_coverage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
"""Test the coverage and update the threshold when coverage is increased."""
import os, re, shutil, subprocess
import pytest
def _get_current_coverage():
"""Helper function that returns the coverage computed with kcov."""
kcov_ouput_dir = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"kcov_output"
)
# By default the build output for kcov and unit tests are both in the debug
# directory. This causes some linker errors that I haven't investigated.
# Error: error: linking with `cc` failed: exit code: 1
# An easy fix is to have separate build directories for kcov & unit tests.
kcov_build_dir = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"kcov_build"
)
# Remove kcov output and build directory to be sure we are always working
# on a clean environment.
shutil.rmtree(kcov_ouput_dir, ignore_errors=True)
shutil.rmtree(kcov_build_dir, ignore_errors=True)
exclude_pattern = (
'${CARGO_HOME:-$HOME/.cargo/},'
'usr/lib/,'
'lib/,'
'bootparam.rs,'
'elf.rs'
)
exclude_region = "'mod tests {'"
kcov_cmd = "CARGO_TARGET_DIR={} cargo kcov --all --features=elf,bzimage " \
"--output {} -- " \
"--exclude-region={} " \
"--exclude-pattern={} " \
"--verify".format(
kcov_build_dir,
kcov_ouput_dir,
exclude_region,
exclude_pattern
)
subprocess.run(kcov_cmd, shell=True, check=True)
# Read the coverage reported by kcov.
coverage_file = os.path.join(kcov_ouput_dir, 'index.js')
with open(coverage_file) as cov_output:
coverage = float(re.findall(
r'"covered":"(\d+\.\d)"',
cov_output.read()
)[0])
# Remove coverage related directories.
shutil.rmtree(kcov_ouput_dir, ignore_errors=True)
shutil.rmtree(kcov_build_dir, ignore_errors=True)
return coverage
def _get_previous_coverage():
"""Helper function that returns the last reported coverage."""
coverage_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'coverage'
)
# The first and only line of the file contains the coverage.
with open(coverage_path) as f:
coverage = f.readline()
return float(coverage.strip())
def _update_coverage(cov_value):
"""Updates the coverage in the coverage file."""
coverage_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'coverage'
)
with open(coverage_path, "w") as f:
f.write(str(cov_value))
def test_coverage(profile):
current_coverage = _get_current_coverage()
previous_coverage = _get_previous_coverage()
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 " \
"tests/coverage.".format(
previous_coverage,
current_coverage
)
elif profile == pytest.profile_devel:
_update_coverage(current_coverage)
else:
# This should never happen because pytest should only accept
# the valid test profiles specified with `choices` in
# `pytest_addoption`.
assert False, "Invalid test profile."
elif previous_coverage > current_coverage:
diff = float(previous_coverage - current_coverage)
assert False, "Coverage drops by {:.2f}%. Please add unit tests for" \
"the uncovered lines.".format(diff)