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