![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/UNITTESTS/unit_test/coverage.py@3:f3764f852aa8, 2018-10-11 (annotated)
- Committer:
- kadonotakashi
- Date:
- Thu Oct 11 02:27:46 2018 +0000
- Revision:
- 3:f3764f852aa8
- Parent:
- 0:8fdf9a60065b
Nucreo 446 + SSD1331 test version;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | """ |
kadonotakashi | 0:8fdf9a60065b | 2 | Copyright (c) 2018, Arm Limited |
kadonotakashi | 0:8fdf9a60065b | 3 | SPDX-License-Identifier: Apache-2.0 |
kadonotakashi | 0:8fdf9a60065b | 4 | |
kadonotakashi | 0:8fdf9a60065b | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
kadonotakashi | 0:8fdf9a60065b | 6 | you may not use this file except in compliance with the License. |
kadonotakashi | 0:8fdf9a60065b | 7 | You may obtain a copy of the License at |
kadonotakashi | 0:8fdf9a60065b | 8 | |
kadonotakashi | 0:8fdf9a60065b | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
kadonotakashi | 0:8fdf9a60065b | 10 | |
kadonotakashi | 0:8fdf9a60065b | 11 | Unless required by applicable law or agreed to in writing, software |
kadonotakashi | 0:8fdf9a60065b | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
kadonotakashi | 0:8fdf9a60065b | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kadonotakashi | 0:8fdf9a60065b | 14 | See the License for the specific language governing permissions and |
kadonotakashi | 0:8fdf9a60065b | 15 | limitations under the License. |
kadonotakashi | 0:8fdf9a60065b | 16 | |
kadonotakashi | 0:8fdf9a60065b | 17 | |
kadonotakashi | 0:8fdf9a60065b | 18 | GENERATE TEST CODE COVERAGE |
kadonotakashi | 0:8fdf9a60065b | 19 | """ |
kadonotakashi | 0:8fdf9a60065b | 20 | |
kadonotakashi | 0:8fdf9a60065b | 21 | import os |
kadonotakashi | 0:8fdf9a60065b | 22 | import logging |
kadonotakashi | 0:8fdf9a60065b | 23 | import posixpath |
kadonotakashi | 0:8fdf9a60065b | 24 | import re |
kadonotakashi | 0:8fdf9a60065b | 25 | |
kadonotakashi | 0:8fdf9a60065b | 26 | from .utils import execute_program |
kadonotakashi | 0:8fdf9a60065b | 27 | from .get_tools import get_gcov_program, \ |
kadonotakashi | 0:8fdf9a60065b | 28 | get_gcovr_program |
kadonotakashi | 0:8fdf9a60065b | 29 | from .settings import COVERAGE_OUTPUT_TYPES |
kadonotakashi | 0:8fdf9a60065b | 30 | |
kadonotakashi | 0:8fdf9a60065b | 31 | |
kadonotakashi | 0:8fdf9a60065b | 32 | class CoverageAPI(object): |
kadonotakashi | 0:8fdf9a60065b | 33 | """ |
kadonotakashi | 0:8fdf9a60065b | 34 | Generate code coverage reports for unit tests. |
kadonotakashi | 0:8fdf9a60065b | 35 | """ |
kadonotakashi | 0:8fdf9a60065b | 36 | |
kadonotakashi | 0:8fdf9a60065b | 37 | def __init__(self, mbed_os_root=None, build_dir=None): |
kadonotakashi | 0:8fdf9a60065b | 38 | self.root = mbed_os_root |
kadonotakashi | 0:8fdf9a60065b | 39 | |
kadonotakashi | 0:8fdf9a60065b | 40 | if not self.root: |
kadonotakashi | 0:8fdf9a60065b | 41 | self.root = os.path.normpath(os.path.join( |
kadonotakashi | 0:8fdf9a60065b | 42 | os.path.dirname(os.path.realpath(__file__)), |
kadonotakashi | 0:8fdf9a60065b | 43 | "../..")) |
kadonotakashi | 0:8fdf9a60065b | 44 | |
kadonotakashi | 0:8fdf9a60065b | 45 | self.build_dir = build_dir |
kadonotakashi | 0:8fdf9a60065b | 46 | |
kadonotakashi | 0:8fdf9a60065b | 47 | if not self.build_dir: |
kadonotakashi | 0:8fdf9a60065b | 48 | logging.error("No build directory given for CoverageAPI.") |
kadonotakashi | 0:8fdf9a60065b | 49 | |
kadonotakashi | 0:8fdf9a60065b | 50 | def _gen_cmd(self, coverage_type, excludes, filter_regex): |
kadonotakashi | 0:8fdf9a60065b | 51 | # Generate coverage generation command: |
kadonotakashi | 0:8fdf9a60065b | 52 | args = [get_gcovr_program(), |
kadonotakashi | 0:8fdf9a60065b | 53 | "--gcov-executable", |
kadonotakashi | 0:8fdf9a60065b | 54 | get_gcov_program(), |
kadonotakashi | 0:8fdf9a60065b | 55 | "-r", |
kadonotakashi | 0:8fdf9a60065b | 56 | os.path.relpath(self.root, self.build_dir), |
kadonotakashi | 0:8fdf9a60065b | 57 | "."] |
kadonotakashi | 0:8fdf9a60065b | 58 | |
kadonotakashi | 0:8fdf9a60065b | 59 | if coverage_type == "html": |
kadonotakashi | 0:8fdf9a60065b | 60 | args.extend(["--html", |
kadonotakashi | 0:8fdf9a60065b | 61 | "--html-detail", |
kadonotakashi | 0:8fdf9a60065b | 62 | "-o", |
kadonotakashi | 0:8fdf9a60065b | 63 | "./coverage/index.html"]) |
kadonotakashi | 0:8fdf9a60065b | 64 | elif coverage_type == "xml": |
kadonotakashi | 0:8fdf9a60065b | 65 | args.extend(["-x", |
kadonotakashi | 0:8fdf9a60065b | 66 | "-o", |
kadonotakashi | 0:8fdf9a60065b | 67 | "./coverage.xml"]) |
kadonotakashi | 0:8fdf9a60065b | 68 | else: |
kadonotakashi | 0:8fdf9a60065b | 69 | logging.error("Invalid coverage output type: %s" % coverage_type) |
kadonotakashi | 0:8fdf9a60065b | 70 | |
kadonotakashi | 0:8fdf9a60065b | 71 | # Add exclude filters: |
kadonotakashi | 0:8fdf9a60065b | 72 | for path in excludes: |
kadonotakashi | 0:8fdf9a60065b | 73 | # Use posix separators if path is string |
kadonotakashi | 0:8fdf9a60065b | 74 | if isinstance(path, ("".__class__, u"".__class__)): |
kadonotakashi | 0:8fdf9a60065b | 75 | path = path.replace("\\", "/") |
kadonotakashi | 0:8fdf9a60065b | 76 | args.extend(["-e", path]) |
kadonotakashi | 0:8fdf9a60065b | 77 | # Use regular expressions as is |
kadonotakashi | 0:8fdf9a60065b | 78 | elif isinstance(path, type(re.compile(""))): |
kadonotakashi | 0:8fdf9a60065b | 79 | args.extend(["-e", path.pattern]) |
kadonotakashi | 0:8fdf9a60065b | 80 | |
kadonotakashi | 0:8fdf9a60065b | 81 | # Add include filters: |
kadonotakashi | 0:8fdf9a60065b | 82 | if filter_regex: |
kadonotakashi | 0:8fdf9a60065b | 83 | filters = filter_regex.split(",") |
kadonotakashi | 0:8fdf9a60065b | 84 | |
kadonotakashi | 0:8fdf9a60065b | 85 | for filt in filters: |
kadonotakashi | 0:8fdf9a60065b | 86 | regex = "(.+/)?%s" % filt.replace("-", "/") |
kadonotakashi | 0:8fdf9a60065b | 87 | args.extend(["-f", regex]) |
kadonotakashi | 0:8fdf9a60065b | 88 | |
kadonotakashi | 0:8fdf9a60065b | 89 | if logging.getLogger().getEffectiveLevel() == logging.DEBUG: |
kadonotakashi | 0:8fdf9a60065b | 90 | args.append("-v") |
kadonotakashi | 0:8fdf9a60065b | 91 | |
kadonotakashi | 0:8fdf9a60065b | 92 | return args |
kadonotakashi | 0:8fdf9a60065b | 93 | |
kadonotakashi | 0:8fdf9a60065b | 94 | def generate_reports(self, |
kadonotakashi | 0:8fdf9a60065b | 95 | outputs, |
kadonotakashi | 0:8fdf9a60065b | 96 | excludes=None, |
kadonotakashi | 0:8fdf9a60065b | 97 | filter_regex=None, |
kadonotakashi | 0:8fdf9a60065b | 98 | build_path=None): |
kadonotakashi | 0:8fdf9a60065b | 99 | """ |
kadonotakashi | 0:8fdf9a60065b | 100 | Run tests to generate coverage data, and generate coverage reports. |
kadonotakashi | 0:8fdf9a60065b | 101 | |
kadonotakashi | 0:8fdf9a60065b | 102 | Positional arguments: |
kadonotakashi | 0:8fdf9a60065b | 103 | outputs - list of types to generate |
kadonotakashi | 0:8fdf9a60065b | 104 | |
kadonotakashi | 0:8fdf9a60065b | 105 | Keyword arguments: |
kadonotakashi | 0:8fdf9a60065b | 106 | excludes - list of paths to exclude from the report |
kadonotakashi | 0:8fdf9a60065b | 107 | filter_regex - comma-separated string to use for test filtering |
kadonotakashi | 0:8fdf9a60065b | 108 | build_path - build path |
kadonotakashi | 0:8fdf9a60065b | 109 | """ |
kadonotakashi | 0:8fdf9a60065b | 110 | |
kadonotakashi | 0:8fdf9a60065b | 111 | # Check for the tool |
kadonotakashi | 0:8fdf9a60065b | 112 | if get_gcovr_program() is None: |
kadonotakashi | 0:8fdf9a60065b | 113 | logging.error( |
kadonotakashi | 0:8fdf9a60065b | 114 | "No gcovr tool found in path. " + |
kadonotakashi | 0:8fdf9a60065b | 115 | "Cannot generate coverage reports.") |
kadonotakashi | 0:8fdf9a60065b | 116 | return |
kadonotakashi | 0:8fdf9a60065b | 117 | |
kadonotakashi | 0:8fdf9a60065b | 118 | if build_path is None: |
kadonotakashi | 0:8fdf9a60065b | 119 | build_path = os.getcwd() |
kadonotakashi | 0:8fdf9a60065b | 120 | |
kadonotakashi | 0:8fdf9a60065b | 121 | if outputs is None: |
kadonotakashi | 0:8fdf9a60065b | 122 | outputs = [] |
kadonotakashi | 0:8fdf9a60065b | 123 | |
kadonotakashi | 0:8fdf9a60065b | 124 | if excludes is None: |
kadonotakashi | 0:8fdf9a60065b | 125 | excludes = [] |
kadonotakashi | 0:8fdf9a60065b | 126 | |
kadonotakashi | 0:8fdf9a60065b | 127 | for output in outputs: |
kadonotakashi | 0:8fdf9a60065b | 128 | # Skip if invalid/unsupported output type |
kadonotakashi | 0:8fdf9a60065b | 129 | if output not in COVERAGE_OUTPUT_TYPES: |
kadonotakashi | 0:8fdf9a60065b | 130 | logging.warning( |
kadonotakashi | 0:8fdf9a60065b | 131 | "Invalid output type. " + |
kadonotakashi | 0:8fdf9a60065b | 132 | "Skip coverage report for type: %s." % output.upper()) |
kadonotakashi | 0:8fdf9a60065b | 133 | continue |
kadonotakashi | 0:8fdf9a60065b | 134 | |
kadonotakashi | 0:8fdf9a60065b | 135 | if output == "html": |
kadonotakashi | 0:8fdf9a60065b | 136 | # Create a build directory if not exist |
kadonotakashi | 0:8fdf9a60065b | 137 | coverage_path = os.path.join(build_path, "coverage") |
kadonotakashi | 0:8fdf9a60065b | 138 | if not os.path.exists(coverage_path): |
kadonotakashi | 0:8fdf9a60065b | 139 | os.mkdir(coverage_path) |
kadonotakashi | 0:8fdf9a60065b | 140 | |
kadonotakashi | 0:8fdf9a60065b | 141 | # Generate the command |
kadonotakashi | 0:8fdf9a60065b | 142 | args = self._gen_cmd(output, excludes, filter_regex) |
kadonotakashi | 0:8fdf9a60065b | 143 | |
kadonotakashi | 0:8fdf9a60065b | 144 | # Run the coverage tool |
kadonotakashi | 0:8fdf9a60065b | 145 | execute_program( |
kadonotakashi | 0:8fdf9a60065b | 146 | args, |
kadonotakashi | 0:8fdf9a60065b | 147 | "%s code coverage report generation failed." % output.upper(), |
kadonotakashi | 0:8fdf9a60065b | 148 | "%s code coverage report created." % output.upper()) |