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 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:'
44
46 if args:
47 msg = msg%args
48 print("[ROSUNIT]"+msg)
49
51 if args:
52 msg = msg%args
53 print('\033[1m[ROSUNIT]' + msg + '\033[0m')
54
56 if args:
57 msg = msg%args
58 print("[ROSUNIT]"+msg, file=sys.stderr)
59
60
61
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
73
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
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
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
105
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
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
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
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
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)
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