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 import logging
39
40 import rospkg
41
42 from .xmlrunner import XMLTestRunner
43
44 XML_OUTPUT_FLAG = '--gtest_output=xml:'
45
47 if args:
48 msg = msg%args
49 print("[ROSUNIT]"+msg)
50
52 if args:
53 msg = msg%args
54 print('\033[1m[ROSUNIT]' + msg + '\033[0m')
55
57 if args:
58 msg = msg%args
59 print("[ROSUNIT]"+msg, file=sys.stderr)
60
61
62
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
74
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
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
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
110
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
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
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
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
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)
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