Library for MQTT
mbed-mqtt-master/UNITTESTS/mbed_unittest.py@0:ba7e439238ab, 2022-06-20 (annotated)
- Committer:
- pavleradojkovic
- Date:
- Mon Jun 20 16:24:43 2022 +0000
- Revision:
- 0:ba7e439238ab
Inital commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pavleradojkovic | 0:ba7e439238ab | 1 | #!/usr/bin/env python |
pavleradojkovic | 0:ba7e439238ab | 2 | |
pavleradojkovic | 0:ba7e439238ab | 3 | """ |
pavleradojkovic | 0:ba7e439238ab | 4 | Copyright (c) 2018, Arm Limited |
pavleradojkovic | 0:ba7e439238ab | 5 | SPDX-License-Identifier: Apache-2.0 |
pavleradojkovic | 0:ba7e439238ab | 6 | |
pavleradojkovic | 0:ba7e439238ab | 7 | Licensed under the Apache License, Version 2.0 (the "License"); |
pavleradojkovic | 0:ba7e439238ab | 8 | you may not use this file except in compliance with the License. |
pavleradojkovic | 0:ba7e439238ab | 9 | You may obtain a copy of the License at |
pavleradojkovic | 0:ba7e439238ab | 10 | |
pavleradojkovic | 0:ba7e439238ab | 11 | http://www.apache.org/licenses/LICENSE-2.0 |
pavleradojkovic | 0:ba7e439238ab | 12 | |
pavleradojkovic | 0:ba7e439238ab | 13 | Unless required by applicable law or agreed to in writing, software |
pavleradojkovic | 0:ba7e439238ab | 14 | distributed under the License is distributed on an "AS IS" BASIS, |
pavleradojkovic | 0:ba7e439238ab | 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
pavleradojkovic | 0:ba7e439238ab | 16 | See the License for the specific language governing permissions and |
pavleradojkovic | 0:ba7e439238ab | 17 | limitations under the License. |
pavleradojkovic | 0:ba7e439238ab | 18 | |
pavleradojkovic | 0:ba7e439238ab | 19 | |
pavleradojkovic | 0:ba7e439238ab | 20 | UNIT TEST BUILD & RUN |
pavleradojkovic | 0:ba7e439238ab | 21 | """ |
pavleradojkovic | 0:ba7e439238ab | 22 | |
pavleradojkovic | 0:ba7e439238ab | 23 | from __future__ import print_function |
pavleradojkovic | 0:ba7e439238ab | 24 | import os |
pavleradojkovic | 0:ba7e439238ab | 25 | import logging |
pavleradojkovic | 0:ba7e439238ab | 26 | import re |
pavleradojkovic | 0:ba7e439238ab | 27 | |
pavleradojkovic | 0:ba7e439238ab | 28 | from unit_test.options import get_options_parser, \ |
pavleradojkovic | 0:ba7e439238ab | 29 | pretty_print_test_options |
pavleradojkovic | 0:ba7e439238ab | 30 | from unit_test.settings import DEFAULT_CMAKE_GENERATORS |
pavleradojkovic | 0:ba7e439238ab | 31 | from unit_test.test import UnitTestTool |
pavleradojkovic | 0:ba7e439238ab | 32 | from unit_test.new import UnitTestGeneratorTool |
pavleradojkovic | 0:ba7e439238ab | 33 | from unit_test.coverage import CoverageAPI |
pavleradojkovic | 0:ba7e439238ab | 34 | |
pavleradojkovic | 0:ba7e439238ab | 35 | def _mbed_unittest_test(options, cwd, pwd): |
pavleradojkovic | 0:ba7e439238ab | 36 | if options is None: |
pavleradojkovic | 0:ba7e439238ab | 37 | return |
pavleradojkovic | 0:ba7e439238ab | 38 | |
pavleradojkovic | 0:ba7e439238ab | 39 | if options.coverage: |
pavleradojkovic | 0:ba7e439238ab | 40 | options.debug_build = True |
pavleradojkovic | 0:ba7e439238ab | 41 | |
pavleradojkovic | 0:ba7e439238ab | 42 | if options.cmake_generator is None: |
pavleradojkovic | 0:ba7e439238ab | 43 | options.cmake_generator = DEFAULT_CMAKE_GENERATORS.get( |
pavleradojkovic | 0:ba7e439238ab | 44 | options.make_program, |
pavleradojkovic | 0:ba7e439238ab | 45 | "Unix Makefiles") |
pavleradojkovic | 0:ba7e439238ab | 46 | |
pavleradojkovic | 0:ba7e439238ab | 47 | # Do not clean directory if only run. |
pavleradojkovic | 0:ba7e439238ab | 48 | if options.clean and options.run_only and not options.compile_only: |
pavleradojkovic | 0:ba7e439238ab | 49 | options.clean = False |
pavleradojkovic | 0:ba7e439238ab | 50 | |
pavleradojkovic | 0:ba7e439238ab | 51 | # Build and run by default |
pavleradojkovic | 0:ba7e439238ab | 52 | if (not options.compile_only and |
pavleradojkovic | 0:ba7e439238ab | 53 | not options.run_only and |
pavleradojkovic | 0:ba7e439238ab | 54 | not options.clean): |
pavleradojkovic | 0:ba7e439238ab | 55 | options.compile_only = True |
pavleradojkovic | 0:ba7e439238ab | 56 | options.run_only = True |
pavleradojkovic | 0:ba7e439238ab | 57 | |
pavleradojkovic | 0:ba7e439238ab | 58 | # Set build path |
pavleradojkovic | 0:ba7e439238ab | 59 | if not os.path.isabs(options.build): |
pavleradojkovic | 0:ba7e439238ab | 60 | options.build = os.path.normpath( |
pavleradojkovic | 0:ba7e439238ab | 61 | os.path.join(cwd, options.build)) |
pavleradojkovic | 0:ba7e439238ab | 62 | |
pavleradojkovic | 0:ba7e439238ab | 63 | pretty_print_test_options(options) |
pavleradojkovic | 0:ba7e439238ab | 64 | |
pavleradojkovic | 0:ba7e439238ab | 65 | # Create test tool |
pavleradojkovic | 0:ba7e439238ab | 66 | tool = UnitTestTool(make_program=options.make_program) |
pavleradojkovic | 0:ba7e439238ab | 67 | |
pavleradojkovic | 0:ba7e439238ab | 68 | # Prepare build directory |
pavleradojkovic | 0:ba7e439238ab | 69 | tool.prepare_build_directory(path_to_src=pwd, |
pavleradojkovic | 0:ba7e439238ab | 70 | build_path=options.build, |
pavleradojkovic | 0:ba7e439238ab | 71 | clean=options.clean) |
pavleradojkovic | 0:ba7e439238ab | 72 | |
pavleradojkovic | 0:ba7e439238ab | 73 | if options.compile_only: |
pavleradojkovic | 0:ba7e439238ab | 74 | # Create makefiles |
pavleradojkovic | 0:ba7e439238ab | 75 | src_path = os.path.relpath(pwd, options.build) |
pavleradojkovic | 0:ba7e439238ab | 76 | tool.create_makefiles(path_to_src=src_path, |
pavleradojkovic | 0:ba7e439238ab | 77 | generator=options.cmake_generator, |
pavleradojkovic | 0:ba7e439238ab | 78 | coverage_output_type=options.coverage, |
pavleradojkovic | 0:ba7e439238ab | 79 | debug=options.debug_build) |
pavleradojkovic | 0:ba7e439238ab | 80 | |
pavleradojkovic | 0:ba7e439238ab | 81 | # Build tests |
pavleradojkovic | 0:ba7e439238ab | 82 | tool.build_tests() |
pavleradojkovic | 0:ba7e439238ab | 83 | |
pavleradojkovic | 0:ba7e439238ab | 84 | if options.run_only: |
pavleradojkovic | 0:ba7e439238ab | 85 | tool.run_tests(filter_regex=options.test_regex) |
pavleradojkovic | 0:ba7e439238ab | 86 | |
pavleradojkovic | 0:ba7e439238ab | 87 | # If code coverage generation: |
pavleradojkovic | 0:ba7e439238ab | 88 | if options.coverage: |
pavleradojkovic | 0:ba7e439238ab | 89 | cov_api = CoverageAPI( |
pavleradojkovic | 0:ba7e439238ab | 90 | mbed_os_root=os.path.normpath(os.path.join(pwd, "..")), |
pavleradojkovic | 0:ba7e439238ab | 91 | build_dir=options.build) |
pavleradojkovic | 0:ba7e439238ab | 92 | |
pavleradojkovic | 0:ba7e439238ab | 93 | # Generate reports |
pavleradojkovic | 0:ba7e439238ab | 94 | outputs = [options.coverage] |
pavleradojkovic | 0:ba7e439238ab | 95 | if options.coverage == "both": |
pavleradojkovic | 0:ba7e439238ab | 96 | outputs = ["html", "xml"] |
pavleradojkovic | 0:ba7e439238ab | 97 | |
pavleradojkovic | 0:ba7e439238ab | 98 | excludes = [pwd, options.build] |
pavleradojkovic | 0:ba7e439238ab | 99 | |
pavleradojkovic | 0:ba7e439238ab | 100 | if not options.include_headers: |
pavleradojkovic | 0:ba7e439238ab | 101 | excludes.append(re.compile(".*\\.h")) |
pavleradojkovic | 0:ba7e439238ab | 102 | |
pavleradojkovic | 0:ba7e439238ab | 103 | cov_api.generate_reports(outputs=outputs, |
pavleradojkovic | 0:ba7e439238ab | 104 | excludes=excludes, |
pavleradojkovic | 0:ba7e439238ab | 105 | filter_regex=options.test_regex, |
pavleradojkovic | 0:ba7e439238ab | 106 | build_path=options.build) |
pavleradojkovic | 0:ba7e439238ab | 107 | |
pavleradojkovic | 0:ba7e439238ab | 108 | def _mbed_unittest_new(options, pwd): |
pavleradojkovic | 0:ba7e439238ab | 109 | if options is None: |
pavleradojkovic | 0:ba7e439238ab | 110 | return |
pavleradojkovic | 0:ba7e439238ab | 111 | |
pavleradojkovic | 0:ba7e439238ab | 112 | generator = UnitTestGeneratorTool() |
pavleradojkovic | 0:ba7e439238ab | 113 | |
pavleradojkovic | 0:ba7e439238ab | 114 | mbed_os_root = os.path.normpath( |
pavleradojkovic | 0:ba7e439238ab | 115 | os.path.join(pwd, "..") |
pavleradojkovic | 0:ba7e439238ab | 116 | ) |
pavleradojkovic | 0:ba7e439238ab | 117 | |
pavleradojkovic | 0:ba7e439238ab | 118 | for filepath in options.new_files: |
pavleradojkovic | 0:ba7e439238ab | 119 | generator.create( |
pavleradojkovic | 0:ba7e439238ab | 120 | mbed_os_root=mbed_os_root, |
pavleradojkovic | 0:ba7e439238ab | 121 | filepath=filepath) |
pavleradojkovic | 0:ba7e439238ab | 122 | |
pavleradojkovic | 0:ba7e439238ab | 123 | def mbed_unittest(options=None): |
pavleradojkovic | 0:ba7e439238ab | 124 | """ |
pavleradojkovic | 0:ba7e439238ab | 125 | Create, build and run unit tests |
pavleradojkovic | 0:ba7e439238ab | 126 | """ |
pavleradojkovic | 0:ba7e439238ab | 127 | |
pavleradojkovic | 0:ba7e439238ab | 128 | if options is None: |
pavleradojkovic | 0:ba7e439238ab | 129 | return |
pavleradojkovic | 0:ba7e439238ab | 130 | |
pavleradojkovic | 0:ba7e439238ab | 131 | cwd = os.getcwd() |
pavleradojkovic | 0:ba7e439238ab | 132 | |
pavleradojkovic | 0:ba7e439238ab | 133 | # Change current working directory to script location |
pavleradojkovic | 0:ba7e439238ab | 134 | unittest_dir = os.path.dirname(os.path.realpath(__file__)) |
pavleradojkovic | 0:ba7e439238ab | 135 | os.chdir(unittest_dir) |
pavleradojkovic | 0:ba7e439238ab | 136 | |
pavleradojkovic | 0:ba7e439238ab | 137 | if options.new_files: |
pavleradojkovic | 0:ba7e439238ab | 138 | _mbed_unittest_new(options, unittest_dir) |
pavleradojkovic | 0:ba7e439238ab | 139 | else: |
pavleradojkovic | 0:ba7e439238ab | 140 | _mbed_unittest_test(options, cwd, unittest_dir) |
pavleradojkovic | 0:ba7e439238ab | 141 | |
pavleradojkovic | 0:ba7e439238ab | 142 | def mbed_unittest_main(): |
pavleradojkovic | 0:ba7e439238ab | 143 | """ |
pavleradojkovic | 0:ba7e439238ab | 144 | Create, build and run unit tests - main |
pavleradojkovic | 0:ba7e439238ab | 145 | """ |
pavleradojkovic | 0:ba7e439238ab | 146 | |
pavleradojkovic | 0:ba7e439238ab | 147 | # Get parser and parse command line arguments |
pavleradojkovic | 0:ba7e439238ab | 148 | parser = get_options_parser() |
pavleradojkovic | 0:ba7e439238ab | 149 | options = parser.parse_args() |
pavleradojkovic | 0:ba7e439238ab | 150 | |
pavleradojkovic | 0:ba7e439238ab | 151 | # Setup logger |
pavleradojkovic | 0:ba7e439238ab | 152 | if options.log_level: |
pavleradojkovic | 0:ba7e439238ab | 153 | logging.basicConfig(level=getattr(logging, options.log_level), |
pavleradojkovic | 0:ba7e439238ab | 154 | format="%(message)s") |
pavleradojkovic | 0:ba7e439238ab | 155 | else: |
pavleradojkovic | 0:ba7e439238ab | 156 | logging.basicConfig(level=logging.INFO, |
pavleradojkovic | 0:ba7e439238ab | 157 | format="%(message)s") |
pavleradojkovic | 0:ba7e439238ab | 158 | |
pavleradojkovic | 0:ba7e439238ab | 159 | mbed_unittest(options) |
pavleradojkovic | 0:ba7e439238ab | 160 | |
pavleradojkovic | 0:ba7e439238ab | 161 | if __name__ == "__main__": |
pavleradojkovic | 0:ba7e439238ab | 162 | mbed_unittest_main() |