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   
 39  import rospkg 
 40   
 41  from .xmlrunner import XMLTestRunner 
 42   
 43  XML_OUTPUT_FLAG = '--gtest_output=xml:'  # use gtest-compatible flag 
 44   
 45   
46 -def printlog(msg, *args):
47 if args: 48 msg = msg % args 49 print('[ROSUNIT]' + msg)
50 51
52 -def printlog_bold(msg, *args):
53 if args: 54 msg = msg % args 55 print('\033[1m[ROSUNIT]' + msg + '\033[0m')
56 57
58 -def printerrlog(msg, *args):
59 if args: 60 msg = msg % args 61 print('[ROSUNIT]' + msg, file=sys.stderr)
62 63 64 # this is a copy of the roslogging utility. it's been moved here as it is a common 65 # routine for programs using accessing ROS directories
66 -def makedirs_with_parent_perms(p):
67 """ 68 Create the directory using the permissions of the nearest 69 (existing) parent directory. This is useful for logging, where a 70 root process sometimes has to log in the user's space. 71 @param p: directory to create 72 @type p: str 73 """ 74 p = os.path.abspath(p) 75 parent = os.path.dirname(p) 76 # recurse upwards, checking to make sure we haven't reached the 77 # top 78 if not os.path.exists(p) and p and parent != p: 79 makedirs_with_parent_perms(parent) 80 s = os.stat(parent) 81 try: 82 os.mkdir(p) 83 except OSError as e: 84 if e.errno != errno.EEXIST: 85 raise 86 87 # if perms of new dir don't match, set anew 88 s2 = os.stat(p) 89 if s.st_uid != s2.st_uid or s.st_gid != s2.st_gid: 90 os.chown(p, s.st_uid, s.st_gid) 91 if s.st_mode != s2.st_mode: 92 os.chmod(p, s.st_mode)
93 94
95 -def xml_results_file(test_pkg, test_name, is_rostest=False, env=None):
96 """ 97 @param test_pkg: name of test's package 98 @type test_pkg: str 99 @param test_name str: name of test 100 @type test_name: str 101 @param is_rostest: True if the results file is for a rostest-generated unit instance 102 @type is_rostest: bool 103 @return: name of xml results file for specified test 104 @rtype: str 105 """ 106 test_dir = os.path.join(rospkg.get_test_results_dir(env=env), test_pkg) 107 if not os.path.exists(test_dir): 108 try: 109 makedirs_with_parent_perms(test_dir) 110 except OSError as error: 111 raise IOError('cannot create test results directory [%s]: %s' % (test_dir, str(error))) 112 113 # #576: strip out chars that would bork the filename 114 # this is fairly primitive, but for now just trying to catch some common cases 115 for c in ' "\'&$!`/\\': 116 if c in test_name: 117 test_name = test_name.replace(c, '_') 118 if is_rostest: 119 return os.path.join(test_dir, 'rostest-%s.xml' % test_name) 120 else: 121 return os.path.join(test_dir, 'rosunit-%s.xml' % test_name)
122 123
124 -def rostest_name_from_path(pkg_dir, test_file):
125 """ 126 Derive name of rostest based on file name/path. rostest follows a 127 certain convention defined above. 128 129 @return: name of test 130 @rtype: str 131 """ 132 test_file_abs = os.path.abspath(test_file) 133 if test_file_abs.startswith(pkg_dir): 134 # compute package-relative path 135 test_file = test_file_abs[len(pkg_dir):] 136 if test_file[0] == os.sep: 137 test_file = test_file[1:] 138 outname = test_file.replace(os.sep, '_') 139 if '.' in outname: 140 outname = outname[:outname.rfind('.')] 141 return outname
142 143
144 -def create_xml_runner(test_pkg, test_name, results_file=None, is_rostest=False):
145 """ 146 Create the unittest test runner with XML output 147 @param test_pkg: package name 148 @type test_pkg: str 149 @param test_name: test name 150 @type test_name: str 151 @param is_rostest: if True, use naming scheme for rostest itself instead of individual unit test naming 152 @type is_rostest: bool 153 """ 154 test_name = os.path.basename(test_name) 155 # determine output xml file name 156 if not results_file: 157 results_file = xml_results_file(test_pkg, test_name, is_rostest) 158 test_dir = os.path.abspath(os.path.dirname(results_file)) 159 if not os.path.exists(test_dir): 160 try: 161 makedirs_with_parent_perms(test_dir) # NOTE: this will pass up an error exception if it fails 162 except OSError as error: 163 raise IOError('cannot create test results directory [%s]: %s' % (test_dir, str(error))) 164 165 elif os.path.isfile(test_dir): 166 raise Exception('ERROR: cannot run test suite, file is preventing creation of test dir: %s' % test_dir) 167 168 print('[ROSUNIT] Outputting test results to ' + results_file) 169 outstream = open(results_file, 'w') 170 outstream.write('<?xml version="1.0" encoding="utf-8"?>\n') 171 return XMLTestRunner(stream=outstream)
172