This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 254
/
Copy pathutil.py
640 lines (533 loc) · 24.2 KB
/
util.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
#!/usr/bin/python2.5
# Copyright (C) 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Code shared between tests.
from __future__ import absolute_import
from __future__ import print_function
import os
import os.path
import re
import shutil
import subprocess
import sys
import tempfile
import traceback
import transitfeed
from transitfeed.compat import StringIO
import unittest
import zipfile
def check_call(cmd, expected_retcode=0, stdin_str="", **kwargs):
"""Convenience function that is in the docs for subprocess but not
installed on my system. Raises an Exception if the return code is not
expected_retcode. Returns a tuple of strings, (stdout, stderr)."""
try:
if 'stdout' in kwargs or 'stderr' in kwargs or 'stdin' in kwargs:
raise Exception("Don't pass stdout or stderr")
# If a custom 'env' is in kwargs this will be passed to subprocess.Popen and
# will prevent the subprocess from inheriting the parent's 'env'.
# On Windows 7 we have to make sure that our custom 'env' contains
# 'SystemRoot' as some code here is using os.urandom() which requires this
# system variable. See review at http://codereview.appspot.com/4240085/ and
# thread "is this a bug? no environment variables" at
# http://www.gossamer-threads.com/lists/python/dev/878941
if 'SystemRoot' in os.environ:
if 'env' in kwargs:
kwargs['env'].setdefault('SystemRoot', os.environ['SystemRoot'])
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stdin=subprocess.PIPE,
**kwargs)
(out, err) = p.communicate(stdin_str)
retcode = p.returncode
except Exception as e:
raise Exception("When running %s: %s" % (cmd, e))
if retcode < 0:
raise Exception(
"Child '%s' was terminated by signal %d. Output:\n%s\n%s\n" %
(cmd, -retcode, out, err))
elif retcode != expected_retcode:
raise Exception(
"Child '%s' returned %d. Output:\n%s\n%s\n" %
(cmd, retcode, out, err))
return (out, err)
def DataPath(path):
here = os.path.dirname(__file__)
return os.path.join(here, 'data', path)
def GetDataPathContents():
here = os.path.dirname(__file__)
return sorted(os.listdir(os.path.join(here, 'data')))
class TestCase(unittest.TestCase):
"""Base of every TestCase class in this project.
This adds some methods that perhaps should be in unittest.TestCase.
"""
# Note from Tom, Dec 9 2009: Be careful about adding setUp or tearDown
# because they will be run a few hundred times.
def assertMatchesRegex(self, regex, string):
"""Assert that regex is found in string."""
if not re.search(regex, string):
self.fail("string %r did not match regex %r" % (string, regex))
class RedirectStdOutTestCaseBase(TestCase):
"""Save stdout to the StringIO buffer self.this_stdout"""
def setUp(self):
self.saved_stdout = sys.stdout
self.this_stdout = StringIO()
sys.stdout = self.this_stdout
def tearDown(self):
sys.stdout = self.saved_stdout
self.this_stdout.close()
class GetPathTestCase(TestCase):
"""TestCase with method to get paths to files in the distribution."""
def setUp(self):
super(GetPathTestCase, self).setUp()
self._origcwd = os.getcwd()
def GetExamplePath(self, name):
"""Return the full path of a file in the examples directory"""
return self.GetPath('examples', name)
def GetTestDataPath(self, *path):
"""Return the full path of a file in the tests/data directory"""
return self.GetPath('tests', 'data', *path)
def GetPath(self, *path):
"""Return absolute path of path. path is relative main source directory."""
here = os.path.dirname(__file__) # Relative to _origcwd
return os.path.join(self._origcwd, here, '..', *path)
class TempDirTestCaseBase(GetPathTestCase):
"""Make a temporary directory the current directory before running the test
and remove it after the test.
"""
def setUp(self):
GetPathTestCase.setUp(self)
self.tempdirpath = tempfile.mkdtemp()
os.chdir(self.tempdirpath)
def tearDown(self):
os.chdir(self._origcwd)
shutil.rmtree(self.tempdirpath)
GetPathTestCase.tearDown(self)
def CheckCallWithPath(self, cmd, expected_retcode=0, stdin_str=""):
"""Run python script cmd[0] with args cmd[1:], making sure 'import
transitfeed' will use the module in this source tree. Raises an Exception
if the return code is not expected_retcode. Returns a tuple of strings,
(stdout, stderr)."""
tf_path = transitfeed.__file__
# Path of the directory containing transitfeed. When this is added to
# sys.path importing transitfeed should work independent of if
# transitfeed.__file__ is <parent>/transitfeed.py or
# <parent>/transitfeed/__init__.py
transitfeed_parent = tf_path[:tf_path.rfind("transitfeed")]
transitfeed_parent = transitfeed_parent.replace("\\", "/").rstrip("/")
script_path = cmd[0].replace("\\", "/")
script_args = cmd[1:]
# Propogate sys.path of this process to the subprocess. This is done
# because I assume that if this process has a customized sys.path it is
# meant to be used for all processes involved in the tests. The downside
# of this is that the subprocess is no longer a clean version of what you
# get when running "python" after installing transitfeed. Hopefully if this
# process uses a customized sys.path you know what you are doing.
env = {"PYTHONPATH": ":".join(sys.path)}
# Instead of directly running the script make sure that the transitfeed
# module in this source directory is at the front of sys.path. Then
# adjust sys.argv so it looks like the script was run directly. This lets
# OptionParser use the correct value for %proj.
cmd = [sys.executable, "-c",
"import sys; "
"sys.path.insert(0,'%s'); "
"sys.argv = ['%s'] + sys.argv[1:]; "
"exec(open('%s'))" %
(transitfeed_parent, script_path, script_path)] + script_args
return check_call(cmd, expected_retcode=expected_retcode, shell=False,
env=env, stdin_str=stdin_str)
def ConvertZipToDict(self, zip):
"""Converts a zip file into a dictionary.
Arguments:
zip: The zipfile whose contents are to be converted to a dictionary.
Returns:
A dictionary mapping filenames to file contents."""
zip_dict = {}
for archive_name in zip.namelist():
zip_dict[archive_name] = zip.read(archive_name)
zip.close()
return zip_dict
def ConvertDictToZip(self, dict):
"""Converts a dictionary to an in-memory zipfile.
Arguments:
dict: A dictionary mapping file names to file contents
Returns:
The new file's in-memory contents as a file-like object."""
zipfile_mem = StringIO()
zip = zipfile.ZipFile(zipfile_mem, 'a')
for arcname, contents in dict.items():
zip.writestr(arcname, contents)
zip.close()
return zipfile_mem
class TempFileTestCaseBase(TestCase):
"""
Subclass of TestCase which sets self.tempfilepath to a valid temporary zip
file name and removes the file if it exists when the test is done.
"""
def setUp(self):
(fd, self.tempfilepath) = tempfile.mkstemp(".zip")
# Open file handle causes an exception during remove in Windows
os.close(fd)
def tearDown(self):
if os.path.exists(self.tempfilepath):
os.remove(self.tempfilepath)
class MemoryZipTestCase(TestCase):
"""Base for TestCase classes which read from an in-memory zip file.
A test that loads data from this zip file exercises almost all the code used
when the feedvalidator runs, but does not touch disk. Unfortunately it is very
difficult to add new stops to the default stops.txt because a new stop will
break tests in StopHierarchyTestCase and StopsNearEachOther."""
_IGNORE_TYPES = ["ExpirationDate"]
def setUp(self):
self.accumulator = RecordingProblemAccumulator(self, self._IGNORE_TYPES)
self.problems = transitfeed.ProblemReporter(self.accumulator)
self.zip_contents = {}
self.SetArchiveContents(
"agency.txt",
"agency_id,agency_name,agency_url,agency_timezone\n"
"DTA,Demo Agency,http://google.com,America/Los_Angeles\n")
self.SetArchiveContents(
"calendar.txt",
"service_id,monday,tuesday,wednesday,thursday,friday,saturday,sunday,"
"start_date,end_date\n"
"FULLW,1,1,1,1,1,1,1,20070101,20101231\n"
"WE,0,0,0,0,0,1,1,20070101,20101231\n")
self.SetArchiveContents(
"calendar_dates.txt",
"service_id,date,exception_type\n"
"FULLW,20070101,1\n")
self.SetArchiveContents(
"routes.txt",
"route_id,agency_id,route_short_name,route_long_name,route_type\n"
"AB,DTA,,Airport Bullfrog,3\n")
self.SetArchiveContents(
"trips.txt",
"route_id,service_id,trip_id\n"
"AB,FULLW,AB1\n")
self.SetArchiveContents(
"stops.txt",
"stop_id,stop_name,stop_lat,stop_lon\n"
"BEATTY_AIRPORT,Airport,36.868446,-116.784582\n"
"BULLFROG,Bullfrog,36.88108,-116.81797\n"
"STAGECOACH,Stagecoach Hotel,36.915682,-116.751677\n")
self.SetArchiveContents(
"stop_times.txt",
"trip_id,arrival_time,departure_time,stop_id,stop_sequence\n"
"AB1,10:00:00,10:00:00,BEATTY_AIRPORT,1\n"
"AB1,10:20:00,10:20:00,BULLFROG,2\n"
"AB1,10:25:00,10:25:00,STAGECOACH,3\n")
def MakeLoaderAndLoad(self,
problems=None,
extra_validation=True,
gtfs_factory=None):
"""Returns a Schedule loaded with the contents of the file dict."""
if gtfs_factory is None:
gtfs_factory = transitfeed.GetGtfsFactory()
if problems is None:
problems = self.problems
self.CreateZip()
self.loader = transitfeed.Loader(
problems=problems,
extra_validation=extra_validation,
zip=self.zip,
gtfs_factory=gtfs_factory)
return self.loader.Load()
def AppendToArchiveContents(self, arcname, s):
"""Append string s to file arcname in the file dict.
All calls to this function, if any, should be made before calling
MakeLoaderAndLoad."""
current_contents = self.zip_contents[arcname]
self.zip_contents[arcname] = current_contents + s
def SetArchiveContents(self, arcname, contents):
"""Set the contents of file arcname in the file dict.
All calls to this function, if any, should be made before calling
MakeLoaderAndLoad."""
self.zip_contents[arcname] = contents
def GetArchiveContents(self, arcname):
"""Get the contents of file arcname in the file dict."""
return self.zip_contents[arcname]
def RemoveArchive(self, arcname):
"""Remove file arcname from the file dict.
All calls to this function, if any, should be made before calling
MakeLoaderAndLoad."""
del self.zip_contents[arcname]
def GetArchiveNames(self):
"""Get a list of all the archive names in the file dict."""
return self.zip_contents.keys()
def CreateZip(self):
"""Create an in-memory GTFS zipfile from the contents of the file dict."""
self.zipfile = StringIO()
self.zip = zipfile.ZipFile(self.zipfile, 'a')
for (arcname, contents) in self.zip_contents.items():
self.zip.writestr(arcname, contents)
def DumpZipFile(self, zf):
"""Print the contents of something zipfile can open, such as a StringIO."""
# Handy for debugging
z = zipfile.ZipFile(zf)
for n in z.namelist():
print("--\n%s\n%s" % (n, z.read(n)))
class LoadTestCase(TestCase):
def setUp(self):
self.accumulator = RecordingProblemAccumulator(self, ("ExpirationDate",))
self.problems = transitfeed.ProblemReporter(self.accumulator)
def Load(self, feed_name):
loader = transitfeed.Loader(
DataPath(feed_name), problems=self.problems, extra_validation=True)
loader.Load()
def ExpectInvalidValue(self, feed_name, column_name):
self.Load(feed_name)
self.accumulator.PopInvalidValue(column_name)
self.accumulator.AssertNoMoreExceptions()
def ExpectMissingFile(self, feed_name, file_name):
self.Load(feed_name)
e = self.accumulator.PopException("MissingFile")
self.assertEqual(file_name, e.file_name)
# Don't call AssertNoMoreExceptions() because a missing file causes
# many errors.
INVALID_VALUE = Exception()
class ValidationTestCase(TestCase):
def setUp(self):
self.accumulator = RecordingProblemAccumulator(
self, ("ExpirationDate", "NoServiceExceptions"))
self.problems = transitfeed.ProblemReporter(self.accumulator)
def tearDown(self):
self.accumulator.TearDownAssertNoMoreExceptions()
def ExpectNoProblems(self, object):
self.accumulator.AssertNoMoreExceptions()
object.Validate(self.problems)
self.accumulator.AssertNoMoreExceptions()
# TODO: think about Expect*Closure methods. With the
# RecordingProblemAccumulator it is now possible to replace
# self.ExpectMissingValueInClosure(lambda: o.method(...), foo)
# with
# o.method(...)
# self.ExpectMissingValueInClosure(foo)
# because problems don't raise an exception. This has the advantage of
# making it easy and clear to test the return value of o.method(...) and
# easier to test for a sequence of problems caused by one call.
# neun@ 2011-01-18: for the moment I don't remove the Expect*InClosure methods
# as they allow enforcing an AssertNoMoreExceptions() before validation.
# When removing them we do have to make sure that each "logical test block"
# before an Expect*InClosure usage really ends with AssertNoMoreExceptions.
# See http://codereview.appspot.com/4020041/
def ValidateAndExpectMissingValue(self, object, column_name):
self.accumulator.AssertNoMoreExceptions()
object.Validate(self.problems)
self.ExpectException('MissingValue', column_name)
def ExpectMissingValueInClosure(self, column_name, c):
self.accumulator.AssertNoMoreExceptions()
rv = c()
self.ExpectException('MissingValue', column_name)
def ValidateAndExpectInvalidValue(self, object, column_name,
value=INVALID_VALUE):
self.accumulator.AssertNoMoreExceptions()
object.Validate(self.problems)
self.ExpectException('InvalidValue', column_name, value)
def ExpectInvalidValueInClosure(self, column_name, value=INVALID_VALUE,
c=None):
self.accumulator.AssertNoMoreExceptions()
rv = c()
self.ExpectException('InvalidValue', column_name, value)
def ValidateAndExpectInvalidFloatValue(self, object, value):
self.accumulator.AssertNoMoreExceptions()
object.Validate(self.problems)
self.ExpectException('InvalidFloatValue', None, value)
def ValidateAndExpectOtherProblem(self, object):
self.accumulator.AssertNoMoreExceptions()
object.Validate(self.problems)
self.ExpectException('OtherProblem')
def ExpectOtherProblemInClosure(self, c):
self.accumulator.AssertNoMoreExceptions()
rv = c()
self.ExpectException('OtherProblem')
def ValidateAndExpectDateOutsideValidRange(self, object, column_name,
value=INVALID_VALUE):
self.accumulator.AssertNoMoreExceptions()
object.Validate(self.problems)
self.ExpectException('DateOutsideValidRange', column_name, value)
def ExpectException(self, type_name, column_name=None, value=INVALID_VALUE):
e = self.accumulator.PopException(type_name)
if column_name:
self.assertEqual(column_name, e.column_name)
if value != INVALID_VALUE:
self.assertEqual(value, e.value)
# these should not throw any exceptions
e.FormatProblem()
e.FormatContext()
self.accumulator.AssertNoMoreExceptions()
def SimpleSchedule(self):
"""Return a minimum schedule that will load without warnings."""
schedule = transitfeed.Schedule(problem_reporter=self.problems)
schedule.AddAgency("Fly Agency", "http://iflyagency.com",
"America/Los_Angeles")
service_period = transitfeed.ServicePeriod("WEEK")
service_period.SetWeekdayService(True)
service_period.SetStartDate("20091203")
service_period.SetEndDate("20111203")
service_period.SetDateHasService("20091203")
schedule.AddServicePeriodObject(service_period)
stop1 = schedule.AddStop(lng=1.00, lat=48.2, name="Stop 1", stop_id="stop1")
stop2 = schedule.AddStop(lng=1.01, lat=48.2, name="Stop 2", stop_id="stop2")
stop3 = schedule.AddStop(lng=1.03, lat=48.2, name="Stop 3", stop_id="stop3")
route = schedule.AddRoute("54C", "", "Bus", route_id="054C")
trip = route.AddTrip(schedule, "bus trip", trip_id="CITY1")
trip.AddStopTime(stop1, stop_time="12:00:00")
trip.AddStopTime(stop2, stop_time="12:00:45")
trip.AddStopTime(stop3, stop_time="12:02:30")
return schedule
#TODO(anog): Revisit this after we implement proper per-exception level change
class RecordingProblemAccumulator(transitfeed.ProblemAccumulatorInterface):
"""Save all problems for later inspection.
Args:
test_case: a unittest.TestCase object on which to report problems
ignore_types: sequence of string type names that will be ignored by the
ProblemAccumulator"""
def __init__(self, test_case, ignore_types=None):
self.exceptions = []
self._test_case = test_case
self._ignore_types = ignore_types or set()
self._sorted = False
def _Report(self, e):
# Ensure that these don't crash
e.FormatProblem()
e.FormatContext()
if e.__class__.__name__ in self._ignore_types:
return
# Keep the 7 nearest stack frames. This should be enough to identify
# the code path that created the exception while trimming off most of the
# large test framework's stack.
traceback_list = traceback.format_list(traceback.extract_stack()[-7:-1])
self.exceptions.append((e, ''.join(traceback_list)))
def PopException(self, type_name):
"""Return the first exception, which must be a type_name."""
if not self._sorted:
self._SortExceptionGroups()
self._sorted = True
e = self.exceptions.pop(0)
e_name = e[0].__class__.__name__
self._test_case.assertEqual(e_name, type_name,
"%s != %s\n%s" %
(e_name, type_name, self.FormatException(*e)))
return e[0]
def FormatException(self, exce, tb):
return ("%s\nwith gtfs file context %s\nand traceback\n%s" %
(exce.FormatProblem(), exce.FormatContext(), tb))
def TearDownAssertNoMoreExceptions(self):
"""Assert that there are no unexpected problems left after a test has run.
This function should be called on a test's tearDown. For more information
please see AssertNoMoreExceptions"""
assert len(self.exceptions) == 0, \
"see util.RecordingProblemAccumulator.AssertNoMoreExceptions"
def AssertNoMoreExceptions(self):
"""Check that no unexpected problems were reported.
Every test that uses a RecordingProblemReporter should end with a call to
this method. If setUp creates a RecordingProblemReporter it is good for
tearDown to double check that the exceptions list was emptied.
"""
exceptions_as_text = []
for e, tb in self.exceptions:
exceptions_as_text.append(self.FormatException(e, tb))
# If the assertFalse below fails the test will abort and tearDown is
# called. Some tearDown methods assert that self.exceptions is empty as
# protection against a test that doesn't end with AssertNoMoreExceptions
# and has exceptions remaining in the RecordingProblemReporter. It would
# be nice to trigger a normal test failure in tearDown but the idea was
# rejected (http://bugs.python.org/issue5531).
self.exceptions = []
self._test_case.assertFalse(exceptions_as_text,
"\n".join(exceptions_as_text))
def PopColumnSpecificException(self, type_name, column_name, file_name=None):
"""Pops and validates column-specific exceptions from the accumulator.
Asserts that the exception is of the given type, and originated in the
specified file and column.
Arguments:
type_name: the type of the exception as string, e.g. 'InvalidValue'
column_name: the name of the field (column) which caused the exception
file_name: optional, the name of the file containing the bad field
Returns:
the exception object
"""
e = self.PopException(type_name)
self._test_case.assertEquals(column_name, e.column_name)
if file_name:
self._test_case.assertEquals(file_name, e.file_name)
return e
def PopInvalidValue(self, column_name, file_name=None):
return self.PopColumnSpecificException("InvalidValue", column_name,
file_name)
def PopMissingValue(self, column_name, file_name=None):
return self.PopColumnSpecificException("MissingValue", column_name,
file_name)
def PopDateOutsideValidRange(self, column_name, file_name=None):
return self.PopColumnSpecificException("DateOutsideValidRange", column_name,
file_name)
def PopDuplicateColumn(self, file_name, header, count):
e = self.PopException("DuplicateColumn")
self._test_case.assertEquals(file_name, e.file_name)
self._test_case.assertEquals(header, e.header)
self._test_case.assertEquals(count, e.count)
return e
def _SortExceptionGroups(self):
"""Applies a consistent order to exceptions for repeatable testing.
Exceptions are only sorted when multiple exceptions of the same type appear
consecutively within the full exception list. For example, if the exception
list is ['B2', 'B1', 'A2', 'A1', 'A3', 'B3'], where A B and C are distinct
exception types, the resulting order is ['B1', 'B2', 'A1', 'A2', 'A3', 'B3']
Notice the order of exception types does not change, but grouped exceptions
of the same type are sorted within their group.
The ExceptionWithContext.GetOrderKey method id used for generating the sort
key for exceptions.
"""
sorted_exceptions = []
exception_group = []
current_exception_type = None
def ProcessExceptionGroup():
exception_group.sort(key=lambda x: x[0].GetOrderKey())
sorted_exceptions.extend(exception_group)
for e_tuple in self.exceptions:
e = e_tuple[0]
if e.__class__ != current_exception_type:
current_exception_type = e.__class__
ProcessExceptionGroup()
exception_group = []
exception_group.append(e_tuple)
ProcessExceptionGroup()
self.exceptions = sorted_exceptions
class TestFailureProblemAccumulator(transitfeed.ProblemAccumulatorInterface):
"""Causes a test failure immediately on any problem."""
def __init__(self, test_case, ignore_types=("ExpirationDate",)):
self.test_case = test_case
self._ignore_types = ignore_types or set()
def _Report(self, e):
# These should never crash
formatted_problem = e.FormatProblem()
formatted_context = e.FormatContext()
exception_class = e.__class__.__name__
if exception_class in self._ignore_types:
return
self.test_case.fail(
"%s: %s\n%s" % (exception_class, formatted_problem, formatted_context))
def GetTestFailureProblemReporter(test_case,
ignore_types=("ExpirationDate",)):
accumulator = TestFailureProblemAccumulator(test_case, ignore_types)
problems = transitfeed.ProblemReporter(accumulator)
return problems
class ExceptionProblemReporterNoExpiration(transitfeed.ProblemReporter):
"""Ignores feed expiration problems.
Use TestFailureProblemReporter in new code because it fails more cleanly, is
easier to extend and does more thorough checking.
"""
def __init__(self):
accumulator = transitfeed.ExceptionProblemAccumulator(raise_warnings=True)
transitfeed.ProblemReporter.__init__(self, accumulator)
def ExpirationDate(self, expiration, context=None):
pass # We don't want to give errors about our test data