1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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:'
44
45
47 if args:
48 msg = msg % args
49 print('[ROSUNIT]' + msg)
50
51
53 if args:
54 msg = msg % args
55 print('\033[1m[ROSUNIT]' + msg + '\033[0m')
56
57
59 if args:
60 msg = msg % args
61 print('[ROSUNIT]' + msg, file=sys.stderr)
62
63
64
65
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
77
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
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
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
114
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
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
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
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
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)
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