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