55
56
# nb: check this before importing anything else from within it
56
57
_testtools_version = getattr(testtools, '__version__', ())
57
if _testtools_version < (0, 9, 2):
58
raise ImportError("need at least testtools 0.9.2: %s is %r"
58
if _testtools_version < (0, 9, 5):
59
raise ImportError("need at least testtools 0.9.5: %s is %r"
59
60
% (testtools.__file__, _testtools_version))
60
61
from testtools import content
62
64
from bzrlib import (
68
commands as _mod_commands,
77
plugin as _mod_plugin,
84
transport as _mod_transport,
80
import bzrlib.commands
81
import bzrlib.timestamp
83
import bzrlib.inventory
84
import bzrlib.iterablefile
87
88
import bzrlib.lsprof
88
89
except ImportError:
89
90
# lsprof not available
91
from bzrlib.merge import merge_inner
94
from bzrlib.smart import client, request, server
96
from bzrlib import symbol_versioning
97
from bzrlib.symbol_versioning import (
92
from bzrlib.smart import client, request
105
93
from bzrlib.transport import (
110
import bzrlib.transport
111
from bzrlib.trace import mutter, note
112
97
from bzrlib.tests import (
116
from bzrlib.tests.http_server import HttpServer
117
from bzrlib.tests.TestUtil import (
121
from bzrlib.tests.treeshape import build_tree_contents
122
102
from bzrlib.ui import NullProgressView
123
103
from bzrlib.ui.text import TextUIFactory
124
import bzrlib.version_info_formats.format_custom
125
from bzrlib.workingtree import WorkingTree, WorkingTreeFormat2
127
105
# Mark this python module as being part of the implementation
128
106
# of unittest: this gives us better tracebacks where the last
140
118
SUBUNIT_SEEK_SET = 0
141
119
SUBUNIT_SEEK_CUR = 1
144
class ExtendedTestResult(unittest._TextTestResult):
121
# These are intentionally brought into this namespace. That way plugins, etc
122
# can just "from bzrlib.tests import TestCase, TestLoader, etc"
123
TestSuite = TestUtil.TestSuite
124
TestLoader = TestUtil.TestLoader
126
# Tests should run in a clean and clearly defined environment. The goal is to
127
# keep them isolated from the running environment as mush as possible. The test
128
# framework ensures the variables defined below are set (or deleted if the
129
# value is None) before a test is run and reset to their original value after
130
# the test is run. Generally if some code depends on an environment variable,
131
# the tests should start without this variable in the environment. There are a
132
# few exceptions but you shouldn't violate this rule lightly.
136
# bzr now uses the Win32 API and doesn't rely on APPDATA, but the
137
# tests do check our impls match APPDATA
138
'BZR_EDITOR': None, # test_msgeditor manipulates this variable
142
'BZREMAIL': None, # may still be present in the environment
143
'EMAIL': 'jrandom@example.com', # set EMAIL as bzr does not guess
144
'BZR_PROGRESS_BAR': None,
146
'BZR_PLUGIN_PATH': None,
147
'BZR_DISABLE_PLUGINS': None,
148
'BZR_PLUGINS_AT': None,
149
'BZR_CONCURRENCY': None,
150
# Make sure that any text ui tests are consistent regardless of
151
# the environment the test case is run in; you may want tests that
152
# test other combinations. 'dumb' is a reasonable guess for tests
153
# going to a pipe or a StringIO.
159
'SSH_AUTH_SOCK': None,
169
# Nobody cares about ftp_proxy, FTP_PROXY AFAIK. So far at
170
# least. If you do (care), please update this comment
174
'BZR_REMOTE_PATH': None,
175
# Generally speaking, we don't want apport reporting on crashes in
176
# the test envirnoment unless we're specifically testing apport,
177
# so that it doesn't leak into the real system environment. We
178
# use an env var so it propagates to subprocesses.
179
'APPORT_DISABLE': '1',
183
def override_os_environ(test, env=None):
184
"""Modify os.environ keeping a copy.
186
:param test: A test instance
188
:param env: A dict containing variable definitions to be installed
191
env = isolated_environ
192
test._original_os_environ = dict([(var, value)
193
for var, value in os.environ.iteritems()])
194
for var, value in env.iteritems():
195
osutils.set_or_unset_env(var, value)
196
if var not in test._original_os_environ:
197
# The var is new, add it with a value of None, so
198
# restore_os_environ will delete it
199
test._original_os_environ[var] = None
202
def restore_os_environ(test):
203
"""Restore os.environ to its original state.
205
:param test: A test instance previously passed to override_os_environ.
207
for var, value in test._original_os_environ.iteritems():
208
# Restore the original value (or delete it if the value has been set to
209
# None in override_os_environ).
210
osutils.set_or_unset_env(var, value)
213
class ExtendedTestResult(testtools.TextTestResult):
145
214
"""Accepts, reports and accumulates the results of running tests.
147
216
Compared to the unittest version this class adds support for
196
265
self._overall_start_time = time.time()
197
266
self._strict = strict
267
self._first_thread_leaker_id = None
268
self._tests_leaking_threads_count = 0
269
self._traceback_from_test = None
199
271
def stopTestRun(self):
200
272
run = self.testsRun
201
273
actionTaken = "Ran"
202
274
stopTime = time.time()
203
275
timeTaken = stopTime - self.startTime
205
self.stream.writeln(self.separator2)
206
self.stream.writeln("%s %d test%s in %.3fs" % (actionTaken,
276
# GZ 2010-07-19: Seems testtools has no printErrors method, and though
277
# the parent class method is similar have to duplicate
278
self._show_list('ERROR', self.errors)
279
self._show_list('FAIL', self.failures)
280
self.stream.write(self.sep2)
281
self.stream.write("%s %d test%s in %.3fs\n\n" % (actionTaken,
207
282
run, run != 1 and "s" or "", timeTaken))
208
self.stream.writeln()
209
283
if not self.wasSuccessful():
210
284
self.stream.write("FAILED (")
211
285
failed, errored = map(len, (self.failures, self.errors))
218
292
if failed or errored: self.stream.write(", ")
219
293
self.stream.write("known_failure_count=%d" %
220
294
self.known_failure_count)
221
self.stream.writeln(")")
295
self.stream.write(")\n")
223
297
if self.known_failure_count:
224
self.stream.writeln("OK (known_failures=%d)" %
298
self.stream.write("OK (known_failures=%d)\n" %
225
299
self.known_failure_count)
227
self.stream.writeln("OK")
301
self.stream.write("OK\n")
228
302
if self.skip_count > 0:
229
303
skipped = self.skip_count
230
self.stream.writeln('%d test%s skipped' %
304
self.stream.write('%d test%s skipped\n' %
231
305
(skipped, skipped != 1 and "s" or ""))
232
306
if self.unsupported:
233
307
for feature, count in sorted(self.unsupported.items()):
234
self.stream.writeln("Missing feature '%s' skipped %d tests." %
308
self.stream.write("Missing feature '%s' skipped %d tests.\n" %
235
309
(feature, count))
237
311
ok = self.wasStrictlySuccessful()
239
313
ok = self.wasSuccessful()
240
if TestCase._first_thread_leaker_id:
314
if self._first_thread_leaker_id:
241
315
self.stream.write(
242
316
'%s is leaking threads among %d leaking tests.\n' % (
243
TestCase._first_thread_leaker_id,
244
TestCase._leaking_threads_tests))
317
self._first_thread_leaker_id,
318
self._tests_leaking_threads_count))
245
319
# We don't report the main thread as an active one.
246
320
self.stream.write(
247
321
'%d non-main threads were left active in the end.\n'
248
% (TestCase._active_threads - 1))
322
% (len(self._active_threads) - 1))
250
324
def getDescription(self, test):
276
351
def _shortened_test_description(self, test):
278
what = re.sub(r'^bzrlib\.(tests|benchmarks)\.', '', what)
353
what = re.sub(r'^bzrlib\.tests\.', '', what)
356
# GZ 2010-10-04: Cloned tests may end up harmlessly calling this method
357
# multiple times in a row, because the handler is added for
358
# each test but the container list is shared between cases.
359
# See lp:498869 lp:625574 and lp:637725 for background.
360
def _record_traceback_from_test(self, exc_info):
361
"""Store the traceback from passed exc_info tuple till"""
362
self._traceback_from_test = exc_info[2]
281
364
def startTest(self, test):
282
unittest.TestResult.startTest(self, test)
365
super(ExtendedTestResult, self).startTest(test)
283
366
if self.count == 0:
284
367
self.startTests()
285
369
self.report_test_start(test)
286
370
test.number = self.count
287
371
self._recordTestStartTime()
372
# Make testtools cases give us the real traceback on failure
373
addOnException = getattr(test, "addOnException", None)
374
if addOnException is not None:
375
addOnException(self._record_traceback_from_test)
376
# Only check for thread leaks on bzrlib derived test cases
377
if isinstance(test, TestCase):
378
test.addCleanup(self._check_leaked_threads, test)
289
380
def startTests(self):
291
if getattr(sys, 'frozen', None) is None:
292
bzr_path = osutils.realpath(sys.argv[0])
294
bzr_path = sys.executable
296
'bzr selftest: %s\n' % (bzr_path,))
299
bzrlib.__path__[0],))
301
' bzr-%s python-%s %s\n' % (
302
bzrlib.version_string,
303
bzrlib._format_version_tuple(sys.version_info),
304
platform.platform(aliased=1),
306
self.stream.write('\n')
381
self.report_tests_starting()
382
self._active_threads = threading.enumerate()
384
def stopTest(self, test):
385
self._traceback_from_test = None
387
def _check_leaked_threads(self, test):
388
"""See if any threads have leaked since last call
390
A sample of live threads is stored in the _active_threads attribute,
391
when this method runs it compares the current live threads and any not
392
in the previous sample are treated as having leaked.
394
now_active_threads = set(threading.enumerate())
395
threads_leaked = now_active_threads.difference(self._active_threads)
397
self._report_thread_leak(test, threads_leaked, now_active_threads)
398
self._tests_leaking_threads_count += 1
399
if self._first_thread_leaker_id is None:
400
self._first_thread_leaker_id = test.id()
401
self._active_threads = now_active_threads
308
403
def _recordTestStartTime(self):
309
404
"""Record that a test has started."""
310
self._start_time = time.time()
312
def _cleanupLogFile(self, test):
313
# We can only do this if we have one of our TestCases, not if
315
setKeepLogfile = getattr(test, 'setKeepLogfile', None)
316
if setKeepLogfile is not None:
405
self._start_datetime = self._now()
319
407
def addError(self, test, err):
320
408
"""Tell result that test finished with an error.
322
410
Called from the TestCase run() method when the test
323
411
fails with an unexpected error.
326
unittest.TestResult.addError(self, test, err)
413
self._post_mortem(self._traceback_from_test)
414
super(ExtendedTestResult, self).addError(test, err)
327
415
self.error_count += 1
328
416
self.report_error(test, err)
329
417
if self.stop_early:
331
self._cleanupLogFile(test)
333
420
def addFailure(self, test, err):
334
421
"""Tell result that test failed.
336
423
Called from the TestCase run() method when the test
337
424
fails because e.g. an assert() method failed.
340
unittest.TestResult.addFailure(self, test, err)
426
self._post_mortem(self._traceback_from_test)
427
super(ExtendedTestResult, self).addFailure(test, err)
341
428
self.failure_count += 1
342
429
self.report_failure(test, err)
343
430
if self.stop_early:
345
self._cleanupLogFile(test)
347
433
def addSuccess(self, test, details=None):
348
434
"""Tell result that test completed successfully.
356
442
self._formatTime(benchmark_time),
358
444
self.report_success(test)
359
self._cleanupLogFile(test)
360
unittest.TestResult.addSuccess(self, test)
445
super(ExtendedTestResult, self).addSuccess(test)
361
446
test._log_contents = ''
363
448
def addExpectedFailure(self, test, err):
364
449
self.known_failure_count += 1
365
450
self.report_known_failure(test, err)
452
def addUnexpectedSuccess(self, test, details=None):
453
"""Tell result the test unexpectedly passed, counting as a failure
455
When the minimum version of testtools required becomes 0.9.8 this
456
can be updated to use the new handling there.
458
super(ExtendedTestResult, self).addFailure(test, details=details)
459
self.failure_count += 1
460
self.report_unexpected_success(test,
461
"".join(details["reason"].iter_text()))
367
465
def addNotSupported(self, test, feature):
368
466
"""The test will not be run because of a missing feature.
401
500
raise errors.BzrError("Unknown whence %r" % whence)
403
def report_cleaning_up(self):
502
def report_tests_starting(self):
503
"""Display information before the test run begins"""
504
if getattr(sys, 'frozen', None) is None:
505
bzr_path = osutils.realpath(sys.argv[0])
507
bzr_path = sys.executable
509
'bzr selftest: %s\n' % (bzr_path,))
512
bzrlib.__path__[0],))
514
' bzr-%s python-%s %s\n' % (
515
bzrlib.version_string,
516
bzrlib._format_version_tuple(sys.version_info),
517
platform.platform(aliased=1),
519
self.stream.write('\n')
521
def report_test_start(self, test):
522
"""Display information on the test just about to be run"""
524
def _report_thread_leak(self, test, leaked_threads, active_threads):
525
"""Display information on a test that leaked one or more threads"""
526
# GZ 2010-09-09: A leak summary reported separately from the general
527
# thread debugging would be nice. Tests under subunit
528
# need something not using stream, perhaps adding a
529
# testtools details object would be fitting.
530
if 'threads' in selftest_debug_flags:
531
self.stream.write('%s is leaking, active is now %d\n' %
532
(test.id(), len(active_threads)))
406
534
def startTestRun(self):
407
535
self.startTime = time.time()
444
572
self.pb.finished()
445
573
super(TextTestResult, self).stopTestRun()
447
def startTestRun(self):
448
super(TextTestResult, self).startTestRun()
575
def report_tests_starting(self):
576
super(TextTestResult, self).report_tests_starting()
449
577
self.pb.update('[test 0/%d] Starting' % (self.num_tests))
451
def printErrors(self):
452
# clear the pb to make room for the error listing
454
super(TextTestResult, self).printErrors()
456
579
def _progress_prefix_text(self):
457
580
# the longer this text, the less space we have to show the test
551
676
return '%s%s' % (indent, err[1])
553
678
def report_error(self, test, err):
554
self.stream.writeln('ERROR %s\n%s'
679
self.stream.write('ERROR %s\n%s\n'
555
680
% (self._testTimeString(test),
556
681
self._error_summary(err)))
558
683
def report_failure(self, test, err):
559
self.stream.writeln(' FAIL %s\n%s'
684
self.stream.write(' FAIL %s\n%s\n'
560
685
% (self._testTimeString(test),
561
686
self._error_summary(err)))
563
688
def report_known_failure(self, test, err):
564
self.stream.writeln('XFAIL %s\n%s'
689
self.stream.write('XFAIL %s\n%s\n'
565
690
% (self._testTimeString(test),
566
691
self._error_summary(err)))
693
def report_unexpected_success(self, test, reason):
694
self.stream.write(' FAIL %s\n%s: %s\n'
695
% (self._testTimeString(test),
696
"Unexpected success. Should have failed",
568
699
def report_success(self, test):
569
self.stream.writeln(' OK %s' % self._testTimeString(test))
700
self.stream.write(' OK %s\n' % self._testTimeString(test))
570
701
for bench_called, stats in getattr(test, '_benchcalls', []):
571
self.stream.writeln('LSProf output for %s(%s, %s)' % bench_called)
702
self.stream.write('LSProf output for %s(%s, %s)\n' % bench_called)
572
703
stats.pprint(file=self.stream)
573
704
# flush the stream so that we get smooth output. This verbose mode is
574
705
# used to show the output in PQM.
575
706
self.stream.flush()
577
708
def report_skip(self, test, reason):
578
self.stream.writeln(' SKIP %s\n%s'
709
self.stream.write(' SKIP %s\n%s\n'
579
710
% (self._testTimeString(test), reason))
581
712
def report_not_applicable(self, test, reason):
582
self.stream.writeln(' N/A %s\n %s'
713
self.stream.write(' N/A %s\n %s\n'
583
714
% (self._testTimeString(test), reason))
585
716
def report_unsupported(self, test, feature):
586
717
"""test cannot be run because feature is missing."""
587
self.stream.writeln("NODEP %s\n The feature '%s' is not available."
718
self.stream.write("NODEP %s\n The feature '%s' is not available.\n"
588
719
%(self._testTimeString(test), feature))
617
748
encode = codec[0]
619
750
encode = codec.encode
620
stream = osutils.UnicodeOrBytesToBytesWriter(encode, stream)
751
# GZ 2010-09-08: Really we don't want to be writing arbitrary bytes,
752
# so should swap to the plain codecs.StreamWriter
753
stream = osutils.UnicodeOrBytesToBytesWriter(encode, stream,
621
755
stream.encoding = new_encoding
622
self.stream = unittest._WritelnDecorator(stream)
623
757
self.descriptions = descriptions
624
758
self.verbosity = verbosity
625
759
self._bench_history = bench_history
773
907
return NullProgressView()
910
def isolated_doctest_setUp(test):
911
override_os_environ(test)
914
def isolated_doctest_tearDown(test):
915
restore_os_environ(test)
918
def IsolatedDocTestSuite(*args, **kwargs):
919
"""Overrides doctest.DocTestSuite to handle isolation.
921
The method is really a factory and users are expected to use it as such.
924
kwargs['setUp'] = isolated_doctest_setUp
925
kwargs['tearDown'] = isolated_doctest_tearDown
926
return doctest.DocTestSuite(*args, **kwargs)
776
929
class TestCase(testtools.TestCase):
777
930
"""Base class for bzr unit tests.
789
942
routine, and to build and check bzr trees.
791
944
In addition to the usual method of overriding tearDown(), this class also
792
allows subclasses to register functions into the _cleanups list, which is
945
allows subclasses to register cleanup functions via addCleanup, which are
793
946
run in order as the object is torn down. It's less likely this will be
794
947
accidentally overlooked.
797
_active_threads = None
798
_leaking_threads_tests = 0
799
_first_thread_leaker_id = None
800
_log_file_name = None
801
951
# record lsprof data when performing benchmark calls.
802
952
_gather_lsprof_in_benchmarks = False
804
954
def __init__(self, methodName='testMethod'):
805
955
super(TestCase, self).__init__(methodName)
807
956
self._directory_isolation = True
808
957
self.exception_handlers.insert(0,
809
958
(UnavailableFeature, self._do_unsupported_or_skip))
827
976
self._track_transports()
828
977
self._track_locks()
829
978
self._clear_debug_flags()
830
TestCase._active_threads = threading.activeCount()
831
self.addCleanup(self._check_leaked_threads)
979
# Isolate global verbosity level, to make sure it's reproducible
980
# between tests. We should get rid of this altogether: bug 656694. --
982
self.overrideAttr(bzrlib.trace, '_verbosity_level', 0)
983
# Isolate config option expansion until its default value for bzrlib is
984
# settled on or a the FIXME associated with _get_expand_default_value
985
# is addressed -- vila 20110219
986
self.overrideAttr(config, '_expand_default_value', None)
834
989
# debug a frame up.
836
991
pdb.Pdb().set_trace(sys._getframe().f_back)
838
def _check_leaked_threads(self):
839
active = threading.activeCount()
840
leaked_threads = active - TestCase._active_threads
841
TestCase._active_threads = active
842
# If some tests make the number of threads *decrease*, we'll consider
843
# that they are just observing old threads dieing, not agressively kill
844
# random threads. So we don't report these tests as leaking. The risk
845
# is that we have false positives that way (the test see 2 threads
846
# going away but leak one) but it seems less likely than the actual
847
# false positives (the test see threads going away and does not leak).
848
if leaked_threads > 0:
849
TestCase._leaking_threads_tests += 1
850
if TestCase._first_thread_leaker_id is None:
851
TestCase._first_thread_leaker_id = self.id()
993
def discardDetail(self, name):
994
"""Extend the addDetail, getDetails api so we can remove a detail.
996
eg. bzr always adds the 'log' detail at startup, but we don't want to
997
include it for skipped, xfail, etc tests.
999
It is safe to call this for a detail that doesn't exist, in case this
1000
gets called multiple times.
1002
# We cheat. details is stored in __details which means we shouldn't
1003
# touch it. but getDetails() returns the dict directly, so we can
1005
details = self.getDetails()
853
1009
def _clear_debug_flags(self):
854
1010
"""Prevent externally set debug flags affecting tests.
866
1022
def _clear_hooks(self):
867
1023
# prevent hooks affecting tests
1024
known_hooks = hooks.known_hooks
868
1025
self._preserved_hooks = {}
869
for key, factory in hooks.known_hooks.items():
870
parent, name = hooks.known_hooks_key_to_parent_and_attribute(key)
871
current_hooks = hooks.known_hooks_key_to_object(key)
1026
for key, (parent, name) in known_hooks.iter_parent_objects():
1027
current_hooks = getattr(parent, name)
872
1028
self._preserved_hooks[parent] = (name, current_hooks)
1029
self._preserved_lazy_hooks = hooks._lazy_hooks
1030
hooks._lazy_hooks = {}
873
1031
self.addCleanup(self._restoreHooks)
874
for key, factory in hooks.known_hooks.items():
875
parent, name = hooks.known_hooks_key_to_parent_and_attribute(key)
1032
for key, (parent, name) in known_hooks.iter_parent_objects():
1033
factory = known_hooks.get(key)
876
1034
setattr(parent, name, factory())
877
1035
# this hook should always be installed
878
1036
request._install_hook()
1135
1293
'st_mtime did not match')
1136
1294
self.assertEqual(expected.st_ctime, actual.st_ctime,
1137
1295
'st_ctime did not match')
1138
if sys.platform != 'win32':
1296
if sys.platform == 'win32':
1139
1297
# On Win32 both 'dev' and 'ino' cannot be trusted. In python2.4 it
1140
1298
# is 'dev' that varies, in python 2.5 (6?) it is st_ino that is
1141
# odd. Regardless we shouldn't actually try to assert anything
1142
# about their values
1299
# odd. We just force it to always be 0 to avoid any problems.
1300
self.assertEqual(0, expected.st_dev)
1301
self.assertEqual(0, actual.st_dev)
1302
self.assertEqual(0, expected.st_ino)
1303
self.assertEqual(0, actual.st_ino)
1143
1305
self.assertEqual(expected.st_dev, actual.st_dev,
1144
1306
'st_dev did not match')
1145
1307
self.assertEqual(expected.st_ino, actual.st_ino,
1211
1372
if haystack.find(needle) == -1:
1212
1373
self.fail("string %r not found in '''%s'''" % (needle, haystack))
1375
def assertNotContainsString(self, haystack, needle):
1376
if haystack.find(needle) != -1:
1377
self.fail("string %r found in '''%s'''" % (needle, haystack))
1214
1379
def assertSubset(self, sublist, superlist):
1215
1380
"""Assert that every entry in sublist is present in superlist."""
1216
1381
missing = set(sublist) - set(superlist)
1322
1487
self.assertEqual(expected_docstring, obj.__doc__)
1489
@symbol_versioning.deprecated_method(symbol_versioning.deprecated_in((2, 4)))
1324
1490
def failUnlessExists(self, path):
1491
return self.assertPathExists(path)
1493
def assertPathExists(self, path):
1325
1494
"""Fail unless path or paths, which may be abs or relative, exist."""
1326
1495
if not isinstance(path, basestring):
1328
self.failUnlessExists(p)
1497
self.assertPathExists(p)
1330
self.failUnless(osutils.lexists(path),path+" does not exist")
1499
self.assertTrue(osutils.lexists(path),
1500
path + " does not exist")
1502
@symbol_versioning.deprecated_method(symbol_versioning.deprecated_in((2, 4)))
1332
1503
def failIfExists(self, path):
1504
return self.assertPathDoesNotExist(path)
1506
def assertPathDoesNotExist(self, path):
1333
1507
"""Fail if path or paths, which may be abs or relative, exist."""
1334
1508
if not isinstance(path, basestring):
1336
self.failIfExists(p)
1510
self.assertPathDoesNotExist(p)
1338
self.failIf(osutils.lexists(path),path+" exists")
1512
self.assertFalse(osutils.lexists(path),
1340
1515
def _capture_deprecation_warnings(self, a_callable, *args, **kwargs):
1341
1516
"""A helper for callDeprecated and applyDeprecated.
1487
1660
debug.debug_flags.discard('strict_locks')
1489
def addCleanup(self, callable, *args, **kwargs):
1490
"""Arrange to run a callable when this case is torn down.
1492
Callables are run in the reverse of the order they are registered,
1493
ie last-in first-out.
1495
self._cleanups.append((callable, args, kwargs))
1497
1662
def overrideAttr(self, obj, attr_name, new=_unitialized_attr):
1498
1663
"""Overrides an object attribute restoring it after the test.
1513
1678
setattr(obj, attr_name, new)
1681
def overrideEnv(self, name, new):
1682
"""Set an environment variable, and reset it after the test.
1684
:param name: The environment variable name.
1686
:param new: The value to set the variable to. If None, the
1687
variable is deleted from the environment.
1689
:returns: The actual variable value.
1691
value = osutils.set_or_unset_env(name, new)
1692
self.addCleanup(osutils.set_or_unset_env, name, value)
1516
1695
def _cleanEnvironment(self):
1518
'BZR_HOME': None, # Don't inherit BZR_HOME to all the tests.
1519
'HOME': os.getcwd(),
1520
# bzr now uses the Win32 API and doesn't rely on APPDATA, but the
1521
# tests do check our impls match APPDATA
1522
'BZR_EDITOR': None, # test_msgeditor manipulates this variable
1526
'BZREMAIL': None, # may still be present in the environment
1528
'BZR_PROGRESS_BAR': None,
1530
'BZR_PLUGIN_PATH': None,
1531
'BZR_DISABLE_PLUGINS': None,
1532
'BZR_PLUGINS_AT': None,
1533
'BZR_CONCURRENCY': None,
1534
# Make sure that any text ui tests are consistent regardless of
1535
# the environment the test case is run in; you may want tests that
1536
# test other combinations. 'dumb' is a reasonable guess for tests
1537
# going to a pipe or a StringIO.
1541
'BZR_COLUMNS': '80',
1543
'SSH_AUTH_SOCK': None,
1547
'https_proxy': None,
1548
'HTTPS_PROXY': None,
1553
# Nobody cares about ftp_proxy, FTP_PROXY AFAIK. So far at
1554
# least. If you do (care), please update this comment
1558
'BZR_REMOTE_PATH': None,
1559
# Generally speaking, we don't want apport reporting on crashes in
1560
# the test envirnoment unless we're specifically testing apport,
1561
# so that it doesn't leak into the real system environment. We
1562
# use an env var so it propagates to subprocesses.
1563
'APPORT_DISABLE': '1',
1566
self.addCleanup(self._restoreEnvironment)
1567
for name, value in new_env.iteritems():
1568
self._captureVar(name, value)
1570
def _captureVar(self, name, newvalue):
1571
"""Set an environment variable, and reset it when finished."""
1572
self._old_env[name] = osutils.set_or_unset_env(name, newvalue)
1574
def _restoreEnvironment(self):
1575
for name, value in self._old_env.iteritems():
1576
osutils.set_or_unset_env(name, value)
1696
for name, value in isolated_environ.iteritems():
1697
self.overrideEnv(name, value)
1578
1699
def _restoreHooks(self):
1579
1700
for klass, (name, hooks) in self._preserved_hooks.items():
1580
1701
setattr(klass, name, hooks)
1702
hooks._lazy_hooks = self._preserved_lazy_hooks
1582
1704
def knownFailure(self, reason):
1583
1705
"""This test has failed for some known reason."""
1584
1706
raise KnownFailure(reason)
1708
def _suppress_log(self):
1709
"""Remove the log info from details."""
1710
self.discardDetail('log')
1586
1712
def _do_skip(self, result, reason):
1713
self._suppress_log()
1587
1714
addSkip = getattr(result, 'addSkip', None)
1588
1715
if not callable(addSkip):
1589
1716
result.addSuccess(result)
1612
1741
self._do_skip(result, reason)
1744
def _report_skip(self, result, err):
1745
"""Override the default _report_skip.
1747
We want to strip the 'log' detail. If we waint until _do_skip, it has
1748
already been formatted into the 'reason' string, and we can't pull it
1751
self._suppress_log()
1752
super(TestCase, self)._report_skip(self, result, err)
1755
def _report_expected_failure(self, result, err):
1758
See _report_skip for motivation.
1760
self._suppress_log()
1761
super(TestCase, self)._report_expected_failure(self, result, err)
1615
1764
def _do_unsupported_or_skip(self, result, e):
1616
1765
reason = e.args[0]
1766
self._suppress_log()
1617
1767
addNotSupported = getattr(result, 'addNotSupported', None)
1618
1768
if addNotSupported is not None:
1619
1769
result.addNotSupported(self, reason)
1666
1816
unicodestr = self._log_contents.decode('utf8', 'replace')
1667
1817
self._log_contents = unicodestr.encode('utf8')
1668
1818
return self._log_contents
1670
if bzrlib.trace._trace_file:
1671
# flush the log file, to get all content
1672
bzrlib.trace._trace_file.flush()
1673
if self._log_file_name is not None:
1674
logfile = open(self._log_file_name)
1676
log_contents = logfile.read()
1819
if self._log_file is not None:
1820
log_contents = self._log_file.getvalue()
1680
1822
log_contents.decode('utf8')
1681
1823
except UnicodeDecodeError:
1682
1824
unicodestr = log_contents.decode('utf8', 'replace')
1683
1825
log_contents = unicodestr.encode('utf8')
1684
1826
if not keep_log_file:
1686
max_close_attempts = 100
1687
first_close_error = None
1688
while close_attempts < max_close_attempts:
1691
self._log_file.close()
1692
except IOError, ioe:
1693
if ioe.errno is None:
1694
# No errno implies 'close() called during
1695
# concurrent operation on the same file object', so
1696
# retry. Probably a thread is trying to write to
1698
if first_close_error is None:
1699
first_close_error = ioe
1704
if close_attempts > 1:
1706
'Unable to close log file on first attempt, '
1707
'will retry: %s\n' % (first_close_error,))
1708
if close_attempts == max_close_attempts:
1710
'Unable to close log file after %d attempts.\n'
1711
% (max_close_attempts,))
1712
1827
self._log_file = None
1713
1828
# Permit multiple calls to get_log until we clean it up in
1714
1829
# finishLogFile
1715
1830
self._log_contents = log_contents
1717
os.remove(self._log_file_name)
1719
if sys.platform == 'win32' and e.errno == errno.EACCES:
1720
sys.stderr.write(('Unable to delete log file '
1721
' %r\n' % self._log_file_name))
1724
self._log_file_name = None
1725
1831
return log_contents
1727
return "No log file content and no log file name."
1833
return "No log file content."
1729
1835
def get_log(self):
1730
1836
"""Get a unicode string containing the log from bzrlib.trace.
1945
2052
variables. A value of None will unset the env variable.
1946
2053
The values must be strings. The change will only occur in the
1947
2054
child, so you don't need to fix the environment after running.
1948
:param skip_if_plan_to_signal: raise TestSkipped when true and os.kill
2055
:param skip_if_plan_to_signal: raise TestSkipped when true and system
2056
doesn't support signalling subprocesses.
1950
2057
:param allow_plugins: If False (default) pass --no-plugins to bzr.
1952
2059
:returns: Popen object for the started process.
1954
2061
if skip_if_plan_to_signal:
1955
if not getattr(os, 'kill', None):
1956
raise TestSkipped("os.kill not available.")
2062
if os.name != "posix":
2063
raise TestSkipped("Sending signals not supported")
1958
2065
if env_changes is None:
1959
2066
env_changes = {}
2036
2145
if retcode is not None and retcode != process.returncode:
2037
2146
if process_args is None:
2038
2147
process_args = "(unknown args)"
2039
mutter('Output of bzr %s:\n%s', process_args, out)
2040
mutter('Error for bzr %s:\n%s', process_args, err)
2148
trace.mutter('Output of bzr %s:\n%s', process_args, out)
2149
trace.mutter('Error for bzr %s:\n%s', process_args, err)
2041
2150
self.fail('Command bzr %s failed with retcode %s != %s'
2042
2151
% (process_args, retcode, process.returncode))
2043
2152
return [out, err]
2045
def check_inventory_shape(self, inv, shape):
2046
"""Compare an inventory to a list of expected names.
2154
def check_tree_shape(self, tree, shape):
2155
"""Compare a tree to a list of expected names.
2048
2157
Fail if they are not precisely equal.
2051
2160
shape = list(shape) # copy
2052
for path, ie in inv.entries():
2161
for path, ie in tree.iter_entries_by_dir():
2053
2162
name = path.replace('\\', '/')
2054
2163
if ie.kind == 'directory':
2055
2164
name = name + '/'
2166
pass # ignore root entry
2057
2168
shape.remove(name)
2059
2170
extras.append(name)
2408
2519
made_control = self.make_bzrdir(relpath, format=format)
2409
2520
return made_control.create_repository(shared=shared)
2411
def make_smart_server(self, path):
2522
def make_smart_server(self, path, backing_server=None):
2523
if backing_server is None:
2524
backing_server = self.get_server()
2412
2525
smart_server = test_server.SmartTCPServer_for_testing()
2413
self.start_server(smart_server, self.get_server())
2414
remote_transport = get_transport(smart_server.get_url()).clone(path)
2526
self.start_server(smart_server, backing_server)
2527
remote_transport = _mod_transport.get_transport(smart_server.get_url()
2415
2529
return remote_transport
2417
2531
def make_branch_and_memory_tree(self, relpath, format=None):
2427
2541
test_home_dir = self.test_home_dir
2428
2542
if isinstance(test_home_dir, unicode):
2429
2543
test_home_dir = test_home_dir.encode(sys.getfilesystemencoding())
2430
os.environ['HOME'] = test_home_dir
2431
os.environ['BZR_HOME'] = test_home_dir
2544
self.overrideEnv('HOME', test_home_dir)
2545
self.overrideEnv('BZR_HOME', test_home_dir)
2433
2547
def setUp(self):
2434
2548
super(TestCaseWithMemoryTransport, self).setUp()
2549
# Ensure that ConnectedTransport doesn't leak sockets
2550
def get_transport_with_cleanup(*args, **kwargs):
2551
t = orig_get_transport(*args, **kwargs)
2552
if isinstance(t, _mod_transport.ConnectedTransport):
2553
self.addCleanup(t.disconnect)
2556
orig_get_transport = self.overrideAttr(_mod_transport, 'get_transport',
2557
get_transport_with_cleanup)
2435
2558
self._make_test_root()
2436
2559
self.addCleanup(os.chdir, os.getcwdu())
2437
2560
self.makeAndChdirToTestDir()
3191
3320
def partition_tests(suite, count):
3192
3321
"""Partition suite into count lists of tests."""
3194
tests = list(iter_suite_tests(suite))
3195
tests_per_process = int(math.ceil(float(len(tests)) / count))
3196
for block in range(count):
3197
low_test = block * tests_per_process
3198
high_test = low_test + tests_per_process
3199
process_tests = tests[low_test:high_test]
3200
result.append(process_tests)
3322
# This just assigns tests in a round-robin fashion. On one hand this
3323
# splits up blocks of related tests that might run faster if they shared
3324
# resources, but on the other it avoids assigning blocks of slow tests to
3325
# just one partition. So the slowest partition shouldn't be much slower
3327
partitions = [list() for i in range(count)]
3328
tests = iter_suite_tests(suite)
3329
for partition, test in itertools.izip(itertools.cycle(partitions), tests):
3330
partition.append(test)
3204
3334
def workaround_zealous_crypto_random():
3311
3441
if '--no-plugins' in sys.argv:
3312
3442
argv.append('--no-plugins')
3313
# stderr=STDOUT would be ideal, but until we prevent noise on
3314
# stderr it can interrupt the subunit protocol.
3315
process = Popen(argv, stdin=PIPE, stdout=PIPE, stderr=PIPE,
3443
# stderr=subprocess.STDOUT would be ideal, but until we prevent
3444
# noise on stderr it can interrupt the subunit protocol.
3445
process = subprocess.Popen(argv, stdin=subprocess.PIPE,
3446
stdout=subprocess.PIPE,
3447
stderr=subprocess.PIPE,
3317
3449
test = TestInSubprocess(process, test_list_file_name)
3318
3450
result.append(test)
3325
class ForwardingResult(unittest.TestResult):
3327
def __init__(self, target):
3328
unittest.TestResult.__init__(self)
3329
self.result = target
3331
def startTest(self, test):
3332
self.result.startTest(test)
3334
def stopTest(self, test):
3335
self.result.stopTest(test)
3337
def startTestRun(self):
3338
self.result.startTestRun()
3340
def stopTestRun(self):
3341
self.result.stopTestRun()
3343
def addSkip(self, test, reason):
3344
self.result.addSkip(test, reason)
3346
def addSuccess(self, test):
3347
self.result.addSuccess(test)
3349
def addError(self, test, err):
3350
self.result.addError(test, err)
3352
def addFailure(self, test, err):
3353
self.result.addFailure(test, err)
3354
ForwardingResult = testtools.ExtendedToOriginalDecorator
3357
class ProfileResult(ForwardingResult):
3457
class ProfileResult(testtools.ExtendedToOriginalDecorator):
3358
3458
"""Generate profiling data for all activity between start and success.
3360
3460
The profile data is appended to the test's _benchcalls attribute and can
3633
3739
'bzrlib.tests.blackbox',
3634
3740
'bzrlib.tests.commands',
3741
'bzrlib.tests.doc_generate',
3635
3742
'bzrlib.tests.per_branch',
3636
3743
'bzrlib.tests.per_bzrdir',
3637
'bzrlib.tests.per_bzrdir_colo',
3744
'bzrlib.tests.per_controldir',
3745
'bzrlib.tests.per_controldir_colo',
3638
3746
'bzrlib.tests.per_foreign_vcs',
3639
3747
'bzrlib.tests.per_interrepository',
3640
3748
'bzrlib.tests.per_intertree',
3648
3756
'bzrlib.tests.per_repository',
3649
3757
'bzrlib.tests.per_repository_chk',
3650
3758
'bzrlib.tests.per_repository_reference',
3759
'bzrlib.tests.per_repository_vf',
3651
3760
'bzrlib.tests.per_uifactory',
3652
3761
'bzrlib.tests.per_versionedfile',
3653
3762
'bzrlib.tests.per_workingtree',
3654
3763
'bzrlib.tests.test__annotator',
3655
3764
'bzrlib.tests.test__bencode',
3765
'bzrlib.tests.test__btree_serializer',
3656
3766
'bzrlib.tests.test__chk_map',
3657
3767
'bzrlib.tests.test__dirstate_helpers',
3658
3768
'bzrlib.tests.test__groupcompress',
3686
3796
'bzrlib.tests.test_commit_merge',
3687
3797
'bzrlib.tests.test_config',
3688
3798
'bzrlib.tests.test_conflicts',
3799
'bzrlib.tests.test_controldir',
3689
3800
'bzrlib.tests.test_counted_lock',
3690
3801
'bzrlib.tests.test_crash',
3691
3802
'bzrlib.tests.test_decorators',
3692
3803
'bzrlib.tests.test_delta',
3693
3804
'bzrlib.tests.test_debug',
3694
'bzrlib.tests.test_deprecated_graph',
3695
3805
'bzrlib.tests.test_diff',
3696
3806
'bzrlib.tests.test_directory_service',
3697
3807
'bzrlib.tests.test_dirstate',
3699
3809
'bzrlib.tests.test_eol_filters',
3700
3810
'bzrlib.tests.test_errors',
3701
3811
'bzrlib.tests.test_export',
3812
'bzrlib.tests.test_export_pot',
3702
3813
'bzrlib.tests.test_extract',
3703
3814
'bzrlib.tests.test_fetch',
3815
'bzrlib.tests.test_fixtures',
3704
3816
'bzrlib.tests.test_fifo_cache',
3705
3817
'bzrlib.tests.test_filters',
3706
3818
'bzrlib.tests.test_ftp_transport',
3789
3905
'bzrlib.tests.test_switch',
3790
3906
'bzrlib.tests.test_symbol_versioning',
3791
3907
'bzrlib.tests.test_tag',
3908
'bzrlib.tests.test_test_server',
3792
3909
'bzrlib.tests.test_testament',
3793
3910
'bzrlib.tests.test_textfile',
3794
3911
'bzrlib.tests.test_textmerge',
3912
'bzrlib.tests.test_cethread',
3795
3913
'bzrlib.tests.test_timestamp',
3796
3914
'bzrlib.tests.test_trace',
3797
3915
'bzrlib.tests.test_transactions',
3807
3926
'bzrlib.tests.test_upgrade',
3808
3927
'bzrlib.tests.test_upgrade_stacked',
3809
3928
'bzrlib.tests.test_urlutils',
3929
'bzrlib.tests.test_utextwrap',
3810
3930
'bzrlib.tests.test_version',
3811
3931
'bzrlib.tests.test_version_info',
3932
'bzrlib.tests.test_versionedfile',
3812
3933
'bzrlib.tests.test_weave',
3813
3934
'bzrlib.tests.test_whitebox',
3814
3935
'bzrlib.tests.test_win32utils',
3938
4061
# Some tests mentioned in the list are not in the test suite. The
3939
4062
# list may be out of date, report to the tester.
3940
4063
for id in not_found:
3941
bzrlib.trace.warning('"%s" not found in the test suite', id)
4064
trace.warning('"%s" not found in the test suite', id)
3942
4065
for id in duplicates:
3943
bzrlib.trace.warning('"%s" is used as an id by several tests', id)
4066
trace.warning('"%s" is used as an id by several tests', id)
3948
def multiply_scenarios(scenarios_left, scenarios_right):
4071
def multiply_scenarios(*scenarios):
4072
"""Multiply two or more iterables of scenarios.
4074
It is safe to pass scenario generators or iterators.
4076
:returns: A list of compound scenarios: the cross-product of all
4077
scenarios, with the names concatenated and the parameters
4080
return reduce(_multiply_two_scenarios, map(list, scenarios))
4083
def _multiply_two_scenarios(scenarios_left, scenarios_right):
3949
4084
"""Multiply two sets of scenarios.
3951
4086
:returns: the cartesian product of the two sets of scenarios, that is
4035
4170
:param new_id: The id to assign to it.
4036
4171
:return: The new test.
4038
new_test = copy(test)
4173
new_test = copy.copy(test)
4039
4174
new_test.id = lambda: new_id
4175
# XXX: Workaround <https://bugs.launchpad.net/testtools/+bug/637725>, which
4176
# causes cloned tests to share the 'details' dict. This makes it hard to
4177
# read the test output for parameterized tests, because tracebacks will be
4178
# associated with irrelevant tests.
4180
details = new_test._TestCase__details
4181
except AttributeError:
4182
# must be a different version of testtools than expected. Do nothing.
4185
# Reset the '__details' dict.
4186
new_test._TestCase__details = {}
4040
4187
return new_test
4102
4249
if test_id != None:
4103
4250
ui.ui_factory.clear_term()
4104
4251
sys.stderr.write('\nWhile running: %s\n' % (test_id,))
4252
# Ugly, but the last thing we want here is fail, so bear with it.
4253
printable_e = str(e).decode(osutils.get_user_encoding(), 'replace'
4254
).encode('ascii', 'replace')
4105
4255
sys.stderr.write('Unable to remove testing dir %s\n%s'
4106
% (os.path.basename(dirname), e))
4256
% (os.path.basename(dirname), printable_e))
4109
4259
class Feature(object):
4446
4599
case_sensitive_filesystem_feature = _CaseSensitiveFilesystemFeature()
4449
# Kept for compatibility, use bzrlib.tests.features.subunit instead
4450
SubUnitFeature = _CompatabilityThunkFeature(
4451
deprecated_in((2,1,0)),
4452
'bzrlib.tests.features', 'SubUnitFeature', 'subunit')
4453
4602
# Only define SubUnitBzrRunner if subunit is available.
4455
4604
from subunit import TestProtocolClient
4456
4605
from subunit.test_results import AutoTimingTestResultDecorator
4606
class SubUnitBzrProtocolClient(TestProtocolClient):
4608
def addSuccess(self, test, details=None):
4609
# The subunit client always includes the details in the subunit
4610
# stream, but we don't want to include it in ours.
4611
if details is not None and 'log' in details:
4613
return super(SubUnitBzrProtocolClient, self).addSuccess(
4457
4616
class SubUnitBzrRunner(TextTestRunner):
4458
4617
def run(self, test):
4459
4618
result = AutoTimingTestResultDecorator(
4460
TestProtocolClient(self.stream))
4619
SubUnitBzrProtocolClient(self.stream))
4461
4620
test.run(result)
4463
4622
except ImportError: