Package rosunit :: Module core
[frames] | no frames]

Source Code for Module rosunit.core

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2008, Willow Garage, Inc. 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  # 
 10  #  * Redistributions of source code must retain the above copyright 
 11  #    notice, this list of conditions and the following disclaimer. 
 12  #  * Redistributions in binary form must reproduce the above 
 13  #    copyright notice, this list of conditions and the following 
 14  #    disclaimer in the documentation and/or other materials provided 
 15  #    with the distribution. 
 16  #  * Neither the name of Willow Garage, Inc. nor the names of its 
 17  #    contributors may be used to endorse or promote products derived 
 18  #    from this software without specific prior written permission. 
 19  # 
 20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 23  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 24  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 25  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 26  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 27  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 28  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 29  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 30  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  # POSSIBILITY OF SUCH DAMAGE. 
 32   
 33  from __future__ import print_function 
 34   
 35  import errno 
 36  import os 
 37  import sys 
 38  import logging 
 39   
 40  import rospkg 
 41   
 42  from .xmlrunner import XMLTestRunner 
 43   
 44  XML_OUTPUT_FLAG = '--gtest_output=xml:' #use gtest-compatible flag 
 45   
46 -def printlog(msg, *args):
47 if args: 48 msg = msg%args 49 print("[ROSUNIT]"+msg)
50
51 -def printlog_bold(msg, *args):
52 if args: 53 msg = msg%args 54 print('\033[1m[ROSUNIT]' + msg + '\033[0m')
55
56 -def printerrlog(msg, *args):
57 if args: 58 msg = msg%args 59 print("[ROSUNIT]"+msg, file=sys.stderr)
60 61 # this is a copy of the roslogging utility. it's been moved here as it is a common 62 # routine for programs using accessing ROS directories
63 -def makedirs_with_parent_perms(p):
64 """ 65 Create the directory using the permissions of the nearest 66 (existing) parent directory. This is useful for logging, where a 67 root process sometimes has to log in the user's space. 68 @param p: directory to create 69 @type p: str 70 """ 71 p = os.path.abspath(p) 72 parent = os.path.dirname(p) 73 # recurse upwards, checking to make sure we haven't reached the 74 # top 75 if not os.path.exists(p) and p and parent != p: 76 makedirs_with_parent_perms(parent) 77 s = os.stat(parent) 78 try: 79 os.mkdir(p) 80 except OSError as e: 81 if e.errno != errno.EEXIST: 82 raise 83 84 # if perms of new dir don't match, set anew 85 s2 = os.stat(p) 86 if s.st_uid != s2.st_uid or s.st_gid != s2.st_gid: 87 os.chown(p, s.st_uid, s.st_gid) 88 if s.st_mode != s2.st_mode: 89 os.chmod(p, s.st_mode)
90
91 -def xml_results_file(test_pkg, test_name, is_rostest=False, env=None):
92 """ 93 @param test_pkg: name of test's package 94 @type test_pkg: str 95 @param test_name str: name of test 96 @type test_name: str 97 @param is_rostest: True if the results file is for a rostest-generated unit instance 98 @type is_rostest: bool 99 @return: name of xml results file for specified test 100 @rtype: str 101 """ 102 test_dir = os.path.join(rospkg.get_test_results_dir(env=env), test_pkg) 103 if not os.path.exists(test_dir): 104 try: 105 makedirs_with_parent_perms(test_dir) 106 except OSError as error: 107 raise IOError("cannot create test results directory [%s]: %s"%(test_dir, str(error))) 108 109 # #576: strip out chars that would bork the filename 110 # this is fairly primitive, but for now just trying to catch some common cases 111 for c in ' "\'&$!`/\\': 112 if c in test_name: 113 test_name = test_name.replace(c, '_') 114 if is_rostest: 115 return os.path.join(test_dir, 'rostest-%s.xml'%test_name) 116 else: 117 return os.path.join(test_dir, 'rosunit-%s.xml'%test_name)
118
119 -def rostest_name_from_path(pkg_dir, test_file):
120 """ 121 Derive name of rostest based on file name/path. rostest follows a 122 certain convention defined above. 123 124 @return: name of test 125 @rtype: str 126 """ 127 test_file_abs = os.path.abspath(test_file) 128 if test_file_abs.startswith(pkg_dir): 129 # compute package-relative path 130 test_file = test_file_abs[len(pkg_dir):] 131 if test_file[0] == os.sep: 132 test_file = test_file[1:] 133 outname = test_file.replace(os.sep, '_') 134 if '.' in outname: 135 outname = outname[:outname.rfind('.')] 136 return outname
137
138 -def create_xml_runner(test_pkg, test_name, results_file=None, is_rostest=False):
139 """ 140 Create the unittest test runner with XML output 141 @param test_pkg: package name 142 @type test_pkg: str 143 @param test_name: test name 144 @type test_name: str 145 @param is_rostest: if True, use naming scheme for rostest itself instead of individual unit test naming 146 @type is_rostest: bool 147 """ 148 test_name = os.path.basename(test_name) 149 # determine output xml file name 150 if not results_file: 151 results_file = xml_results_file(test_pkg, test_name, is_rostest) 152 test_dir = os.path.abspath(os.path.dirname(results_file)) 153 if not os.path.exists(test_dir): 154 try: 155 makedirs_with_parent_perms(test_dir) #NOTE: this will pass up an error exception if it fails 156 except OSError as error: 157 raise IOError("cannot create test results directory [%s]: %s"%(test_dir, str(error))) 158 159 elif os.path.isfile(test_dir): 160 raise Exception("ERROR: cannot run test suite, file is preventing creation of test dir: %s"%test_dir) 161 162 print("[ROSUNIT] Outputting test results to " + results_file) 163 outstream = open(results_file, 'w') 164 outstream.write('<?xml version="1.0" encoding="utf-8"?>\n') 165 return XMLTestRunner(stream=outstream)
166