1
+ #!/usr/bin/python
2
+ # -*- coding: utf-8 -*-
3
+ #
4
+ # --- BEGIN_HEADER ---
5
+ #
6
+ # support - helper functions for unit testing
7
+ # Copyright (C) 2003-2024 The MiG Project by the Science HPC Center at UCPH
8
+ #
9
+ # This file is part of MiG.
10
+ #
11
+ # MiG is free software: you can redistribute it and/or modify
12
+ # it under the terms of the GNU General Public License as published by
13
+ # the Free Software Foundation; either version 2 of the License, or
14
+ # (at your option) any later version.
15
+ #
16
+ # MiG is distributed in the hope that it will be useful,
17
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
+ # GNU General Public License for more details.
20
+ #
21
+ # You should have received a copy of the GNU General Public License
22
+ # along with this program; if not, write to the Free Software
23
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24
+ #
25
+ # -- END_HEADER ---
26
+ #
27
+
28
+ """Supporting functions for the unit test framework"""
29
+
1
30
from collections import defaultdict
2
31
import errno
3
32
import logging
23
52
# provision an output directory up-front
24
53
try :
25
54
os .mkdir (TEST_OUTPUT_DIR )
26
- except EnvironmentError as e :
27
- if e .errno == errno .EEXIST :
55
+ except EnvironmentError as enverr :
56
+ if enverr .errno == errno .EEXIST :
28
57
pass # FileExistsError
29
58
30
- # basic global logging configuration for testing
31
- #
32
- # arrange a stream that ignores all logging messages
59
+ # Basic global logging configuration for testing
33
60
34
61
35
62
class BlackHole :
63
+ """Arrange a stream that ignores all logging messages"""
64
+
36
65
def write (self , message ):
37
66
pass
38
67
@@ -91,7 +120,7 @@ def warning(self, line):
91
120
self ._append_as ('warning' , line )
92
121
93
122
def write (self , message ):
94
- channel , namespace , specifics = message .split (':' , maxsplit = 2 )
123
+ channel , namespace , specifics = message .split (':' , 2 )
95
124
96
125
# ignore everything except warnings sent by th python runtime
97
126
if not (channel == 'WARNING' and namespace == 'py.warnings' ):
@@ -103,13 +132,16 @@ def write(self, message):
103
132
104
133
@staticmethod
105
134
def identify_unclosed_file (specifics ):
106
- filename , lineno , exc_name , message = specifics .split (':' , maxsplit = 3 )
135
+ filename , lineno , exc_name , message = specifics .split (':' , 3 )
136
+
107
137
exc_name = exc_name .lstrip ()
108
138
if exc_name != 'ResourceWarning' :
109
139
return
140
+
110
141
matched = FakeLogger .RE_UNCLOSEDFILE .match (message .lstrip ())
111
142
if matched is None :
112
143
return
144
+
113
145
relative_testfile = os .path .relpath (filename , start = MIG_BASE )
114
146
relative_outputfile = os .path .relpath (
115
147
matched .groups ('location' )[0 ], start = TEST_BASE )
@@ -162,7 +194,8 @@ def logger(self):
162
194
return self ._logger
163
195
164
196
def assertPathExists (self , relative_path ):
165
- assert (not os .path .isabs (relative_path )), "expected relative path within output folder"
197
+ assert not os .path .isabs (
198
+ relative_path ), "expected relative path within output folder"
166
199
absolute_path = os .path .join (TEST_OUTPUT_DIR , relative_path )
167
200
stat_result = os .stat (absolute_path )
168
201
if stat .S_ISDIR (stat_result .st_mode ):
@@ -172,12 +205,13 @@ def assertPathExists(self, relative_path):
172
205
173
206
174
207
def cleanpath (relative_path , test_case ):
175
- assert ( isinstance (test_case , MigTestCase ) )
208
+ assert isinstance (test_case , MigTestCase )
176
209
tmp_path = os .path .join (TEST_OUTPUT_DIR , relative_path )
177
210
test_case ._cleanup_paths .add (tmp_path )
178
211
212
+
179
213
def temppath (relative_path , test_case , skip_clean = False ):
180
- assert ( isinstance (test_case , MigTestCase ) )
214
+ assert isinstance (test_case , MigTestCase )
181
215
tmp_path = os .path .join (TEST_OUTPUT_DIR , relative_path )
182
216
if not skip_clean :
183
217
test_case ._cleanup_paths .add (tmp_path )
0 commit comments