takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers test.py Source File

test.py

00001 """
00002 Copyright (c) 2018, Arm Limited
00003 SPDX-License-Identifier: Apache-2.0
00004 
00005 Licensed under the Apache License, Version 2.0 (the "License");
00006 you may not use this file except in compliance with the License.
00007 You may obtain a copy of the License at
00008 
00009     http://www.apache.org/licenses/LICENSE-2.0
00010 
00011 Unless required by applicable law or agreed to in writing, software
00012 distributed under the License is distributed on an "AS IS" BASIS,
00013 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 See the License for the specific language governing permissions and
00015 limitations under the License.
00016 
00017 
00018 UNIT TEST BUILD & RUN
00019 """
00020 
00021 import os
00022 import logging
00023 import sys
00024 from shutil import copy
00025 
00026 from .utils import execute_program
00027 from .get_tools import get_make_tool, \
00028                        get_cmake_tool, \
00029                        get_cxx_tool, \
00030                        get_c_tool
00031 from .settings import DEFAULT_CMAKE_GENERATORS
00032 
00033 class UnitTestTool (object):
00034     """
00035     Unit test tool to:
00036         - prepare build directory
00037         - create makefiles
00038         - build unit tests
00039         - run unit tests
00040         - generate code coverage reports
00041     """
00042 
00043     def __init__ (self,
00044                  make_program=None):
00045         """
00046         Constructor
00047 
00048         Keyword arguments:
00049         make_program - Make tool to use
00050         """
00051 
00052         self.make_program  = make_program
00053 
00054         if self.make_program  is None:
00055             self.make_program  = get_make_tool()
00056 
00057     def create_makefiles (self,
00058                          path_to_src=None,
00059                          generator=None,
00060                          coverage_output_type=None,
00061                          debug=False):
00062         """
00063         Create Makefiles and prepare targets with CMake.
00064 
00065         Keyword arguments:
00066         path_to_src - Path to source directory
00067         generator - Type of Makefiles to generate
00068         coverage_output_type - Generate HTML, XML or both reports
00069         debug - Target debug or release build
00070         """
00071 
00072         if generator is None:
00073             generator = DEFAULT_CMAKE_GENERATORS.get(self.make_program ,
00074                                                      "Unix Makefiles")
00075 
00076         cmake = get_cmake_tool()
00077 
00078         if cmake is None:
00079             logging.error(
00080                 "No CMake found in Path. Install all the required tools.")
00081             sys.exit(1)
00082 
00083         args = [cmake,
00084                 "-G",
00085                 generator,
00086                 "-DCMAKE_MAKE_PROGRAM=%s" % self.make_program ,
00087                 "-DCMAKE_CXX_COMPILER=%s" % get_cxx_tool(),
00088                 "-DCMAKE_C_COMPILER=%s" % get_c_tool()]
00089 
00090         if debug:
00091             args.append("-DCMAKE_BUILD_TYPE=Debug")
00092 
00093         if coverage_output_type:
00094             args.append("-DCOVERAGE:STRING=%s" % coverage_output_type)
00095 
00096         if path_to_src is not None:
00097             args.append(path_to_src)
00098 
00099         execute_program(args,
00100                         "CMake failed to run successfully. See error message.")
00101 
00102     def build_tests (self):
00103         """
00104         Build unit tests and libraries to be tested.
00105         """
00106 
00107         args = [self.make_program ]
00108 
00109         if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
00110             args.append("VERBOSE=1")
00111 
00112         execute_program(args,
00113                         "Building unit tests failed.",
00114                         "Unit tests built successfully.")
00115 
00116     def run_tests (self, filter_regex=None):
00117         """
00118         Run unit tests.
00119 
00120         Keyword arguments:
00121         filter_regex - Regular expression to select which tests to run
00122         """
00123 
00124         args = [self.make_program , "test"]
00125 
00126         if filter_regex:
00127             args.append("ARGS=-R %s -V -D ExperimentalTest" % filter_regex)
00128         else:
00129             args.append("ARGS=-V -D ExperimentalTest")
00130 
00131         if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
00132             args.append("VERBOSE=1")
00133 
00134         execute_program(args, "Unit test run failed.")
00135 
00136     def prepare_build_directory (self,
00137                                 path_to_src=None,
00138                                 build_path=None,
00139                                 clean=False):
00140         """
00141         Create build directory if not exist and
00142         change current working directory to it
00143 
00144         Keyword arguments:
00145         path_to_src - Path to source directory
00146         build_path - Path to build directory
00147         clean - Clean build directory
00148         """
00149 
00150         if build_path is None:
00151             build_path = os.getcwd()
00152 
00153         # Clean CMake data if exists.
00154         if clean:
00155             self._clean_build (build_path)
00156 
00157         # Create build directory if not exist.
00158         if not os.path.exists(build_path):
00159             os.makedirs(build_path)
00160             filename = ".mbedignore"
00161             inputfile = os.path.join(path_to_src, filename)
00162             outputfile = os.path.join(build_path, filename)
00163             copy(inputfile, outputfile)
00164 
00165         # Change current working directory to build directory.
00166         os.chdir(build_path)
00167 
00168     def _clean_build(self, build_path=None):
00169         """
00170         Try clean build directory
00171 
00172         Keyword arguments:
00173         build_path - Path to build directory
00174         """
00175 
00176         logging.info("Cleaning build directory...")
00177 
00178         if os.path.exists(os.path.join(build_path, "CMakeCache.txt")):
00179             args = [self.make_program ,
00180                     "-C",
00181                     build_path,
00182                     "--no-print-directory",
00183                     "clean"]
00184 
00185             # Remove coverage files
00186             for root, _, files in os.walk(build_path):
00187                 for current_file in files:
00188                     if current_file.endswith((".gcno", ".gcda")):
00189                         os.remove(os.path.join(root, current_file))
00190 
00191             execute_program(args,
00192                             "Clean step failed.",
00193                             "Clean done.")
00194 
00195         else:
00196             logging.warning("%s does not exist or \
00197             does not contain previous build data.", build_path)