56
56
# nb: check this before importing anything else from within it
57
57
_testtools_version = getattr(testtools, '__version__', ())
58
if _testtools_version < (0, 9, 5):
59
raise ImportError("need at least testtools 0.9.5: %s is %r"
58
if _testtools_version < (0, 9, 2):
59
raise ImportError("need at least testtools 0.9.2: %s is %r"
60
60
% (testtools.__file__, _testtools_version))
61
61
from testtools import content
64
63
from bzrlib import (
68
commands as _mod_commands,
77
plugin as _mod_plugin,
84
77
transport as _mod_transport,
81
import bzrlib.commands
82
import bzrlib.timestamp
84
import bzrlib.inventory
85
import bzrlib.iterablefile
88
88
import bzrlib.lsprof
89
89
except ImportError:
90
90
# lsprof not available
92
from bzrlib.smart import client, request
92
from bzrlib.merge import merge_inner
95
from bzrlib.smart import client, request, server
97
from bzrlib import symbol_versioning
98
from bzrlib.symbol_versioning import (
93
106
from bzrlib.transport import (
110
from bzrlib.trace import mutter, note
97
111
from bzrlib.tests import (
123
139
TestSuite = TestUtil.TestSuite
124
140
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
142
class ExtendedTestResult(testtools.TextTestResult):
214
143
"""Accepts, reports and accumulates the results of running tests.
353
281
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]
364
284
def startTest(self, test):
365
285
super(ExtendedTestResult, self).startTest(test)
366
286
if self.count == 0:
369
289
self.report_test_start(test)
370
290
test.number = self.count
371
291
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)
292
# Only check for thread leaks if the test case supports cleanups
293
addCleanup = getattr(test, "addCleanup", None)
294
if addCleanup is not None:
295
addCleanup(self._check_leaked_threads, test)
380
297
def startTests(self):
381
298
self.report_tests_starting()
382
299
self._active_threads = threading.enumerate()
384
def stopTest(self, test):
385
self._traceback_from_test = None
387
301
def _check_leaked_threads(self, test):
388
302
"""See if any threads have leaked since last call
471
385
self.not_applicable_count += 1
472
386
self.report_not_applicable(test, reason)
474
def _post_mortem(self, tb=None):
388
def _post_mortem(self):
475
389
"""Start a PDB post mortem session."""
476
390
if os.environ.get('BZR_TEST_PDB', None):
391
import pdb;pdb.post_mortem()
480
393
def progress(self, offset, whence):
481
394
"""The test is adjusting the count of tests to run."""
881
791
return NullProgressView()
884
def isolated_doctest_setUp(test):
885
override_os_environ(test)
888
def isolated_doctest_tearDown(test):
889
restore_os_environ(test)
892
def IsolatedDocTestSuite(*args, **kwargs):
893
"""Overrides doctest.DocTestSuite to handle isolation.
895
The method is really a factory and users are expected to use it as such.
898
kwargs['setUp'] = isolated_doctest_setUp
899
kwargs['tearDown'] = isolated_doctest_tearDown
900
return doctest.DocTestSuite(*args, **kwargs)
903
794
class TestCase(testtools.TestCase):
904
795
"""Base class for bzr unit tests.
950
841
self._track_transports()
951
842
self._track_locks()
952
843
self._clear_debug_flags()
953
# Isolate global verbosity level, to make sure it's reproducible
954
# between tests. We should get rid of this altogether: bug 656694. --
956
self.overrideAttr(bzrlib.trace, '_verbosity_level', 0)
957
# Isolate config option expansion until its default value for bzrlib is
958
# settled on or a the FIXME associated with _get_expand_default_value
959
# is addressed -- vila 20110219
960
self.overrideAttr(config, '_expand_default_value', None)
963
846
# debug a frame up.
996
879
def _clear_hooks(self):
997
880
# prevent hooks affecting tests
998
known_hooks = hooks.known_hooks
999
881
self._preserved_hooks = {}
1000
for key, (parent, name) in known_hooks.iter_parent_objects():
1001
current_hooks = getattr(parent, name)
882
for key, factory in hooks.known_hooks.items():
883
parent, name = hooks.known_hooks_key_to_parent_and_attribute(key)
884
current_hooks = hooks.known_hooks_key_to_object(key)
1002
885
self._preserved_hooks[parent] = (name, current_hooks)
1003
self._preserved_lazy_hooks = hooks._lazy_hooks
1004
hooks._lazy_hooks = {}
1005
886
self.addCleanup(self._restoreHooks)
1006
for key, (parent, name) in known_hooks.iter_parent_objects():
1007
factory = known_hooks.get(key)
887
for key, factory in hooks.known_hooks.items():
888
parent, name = hooks.known_hooks_key_to_parent_and_attribute(key)
1008
889
setattr(parent, name, factory())
1009
890
# this hook should always be installed
1010
891
request._install_hook()
1267
1148
'st_mtime did not match')
1268
1149
self.assertEqual(expected.st_ctime, actual.st_ctime,
1269
1150
'st_ctime did not match')
1270
if sys.platform == 'win32':
1151
if sys.platform != 'win32':
1271
1152
# On Win32 both 'dev' and 'ino' cannot be trusted. In python2.4 it
1272
1153
# is 'dev' that varies, in python 2.5 (6?) it is st_ino that is
1273
# odd. We just force it to always be 0 to avoid any problems.
1274
self.assertEqual(0, expected.st_dev)
1275
self.assertEqual(0, actual.st_dev)
1276
self.assertEqual(0, expected.st_ino)
1277
self.assertEqual(0, actual.st_ino)
1154
# odd. Regardless we shouldn't actually try to assert anything
1155
# about their values
1279
1156
self.assertEqual(expected.st_dev, actual.st_dev,
1280
1157
'st_dev did not match')
1281
1158
self.assertEqual(expected.st_ino, actual.st_ino,
1346
1224
if haystack.find(needle) == -1:
1347
1225
self.fail("string %r not found in '''%s'''" % (needle, haystack))
1349
def assertNotContainsString(self, haystack, needle):
1350
if haystack.find(needle) != -1:
1351
self.fail("string %r found in '''%s'''" % (needle, haystack))
1353
1227
def assertSubset(self, sublist, superlist):
1354
1228
"""Assert that every entry in sublist is present in superlist."""
1355
1229
missing = set(sublist) - set(superlist)
1461
1335
self.assertEqual(expected_docstring, obj.__doc__)
1463
@symbol_versioning.deprecated_method(symbol_versioning.deprecated_in((2, 4)))
1464
1337
def failUnlessExists(self, path):
1465
return self.assertPathExists(path)
1467
def assertPathExists(self, path):
1468
1338
"""Fail unless path or paths, which may be abs or relative, exist."""
1469
1339
if not isinstance(path, basestring):
1471
self.assertPathExists(p)
1341
self.failUnlessExists(p)
1473
self.assertTrue(osutils.lexists(path),
1474
path + " does not exist")
1343
self.failUnless(osutils.lexists(path),path+" does not exist")
1476
@symbol_versioning.deprecated_method(symbol_versioning.deprecated_in((2, 4)))
1477
1345
def failIfExists(self, path):
1478
return self.assertPathDoesNotExist(path)
1480
def assertPathDoesNotExist(self, path):
1481
1346
"""Fail if path or paths, which may be abs or relative, exist."""
1482
1347
if not isinstance(path, basestring):
1484
self.assertPathDoesNotExist(p)
1349
self.failIfExists(p)
1486
self.assertFalse(osutils.lexists(path),
1351
self.failIf(osutils.lexists(path),path+" exists")
1489
1353
def _capture_deprecation_warnings(self, a_callable, *args, **kwargs):
1490
1354
"""A helper for callDeprecated and applyDeprecated.
1614
1478
Close the file and delete it, unless setKeepLogfile was called.
1616
if trace._trace_file:
1480
if bzrlib.trace._trace_file:
1617
1481
# flush the log file, to get all content
1618
trace._trace_file.flush()
1619
trace.pop_log_file(self._log_memento)
1482
bzrlib.trace._trace_file.flush()
1483
bzrlib.trace.pop_log_file(self._log_memento)
1620
1484
# Cache the log result and delete the file on disk
1621
1485
self._get_log(False)
1652
1516
setattr(obj, attr_name, new)
1655
def overrideEnv(self, name, new):
1656
"""Set an environment variable, and reset it after the test.
1658
:param name: The environment variable name.
1660
:param new: The value to set the variable to. If None, the
1661
variable is deleted from the environment.
1663
:returns: The actual variable value.
1665
value = osutils.set_or_unset_env(name, new)
1666
self.addCleanup(osutils.set_or_unset_env, name, value)
1669
1519
def _cleanEnvironment(self):
1670
for name, value in isolated_environ.iteritems():
1671
self.overrideEnv(name, value)
1521
'BZR_HOME': None, # Don't inherit BZR_HOME to all the tests.
1522
'HOME': os.getcwd(),
1523
# bzr now uses the Win32 API and doesn't rely on APPDATA, but the
1524
# tests do check our impls match APPDATA
1525
'BZR_EDITOR': None, # test_msgeditor manipulates this variable
1529
'BZREMAIL': None, # may still be present in the environment
1530
'EMAIL': 'jrandom@example.com', # set EMAIL as bzr does not guess
1531
'BZR_PROGRESS_BAR': None,
1533
'BZR_PLUGIN_PATH': None,
1534
'BZR_DISABLE_PLUGINS': None,
1535
'BZR_PLUGINS_AT': None,
1536
'BZR_CONCURRENCY': None,
1537
# Make sure that any text ui tests are consistent regardless of
1538
# the environment the test case is run in; you may want tests that
1539
# test other combinations. 'dumb' is a reasonable guess for tests
1540
# going to a pipe or a StringIO.
1544
'BZR_COLUMNS': '80',
1546
'SSH_AUTH_SOCK': None,
1550
'https_proxy': None,
1551
'HTTPS_PROXY': None,
1556
# Nobody cares about ftp_proxy, FTP_PROXY AFAIK. So far at
1557
# least. If you do (care), please update this comment
1561
'BZR_REMOTE_PATH': None,
1562
# Generally speaking, we don't want apport reporting on crashes in
1563
# the test envirnoment unless we're specifically testing apport,
1564
# so that it doesn't leak into the real system environment. We
1565
# use an env var so it propagates to subprocesses.
1566
'APPORT_DISABLE': '1',
1569
self.addCleanup(self._restoreEnvironment)
1570
for name, value in new_env.iteritems():
1571
self._captureVar(name, value)
1573
def _captureVar(self, name, newvalue):
1574
"""Set an environment variable, and reset it when finished."""
1575
self._old_env[name] = osutils.set_or_unset_env(name, newvalue)
1577
def _restoreEnvironment(self):
1578
for name, value in self._old_env.iteritems():
1579
osutils.set_or_unset_env(name, value)
1673
1581
def _restoreHooks(self):
1674
1582
for klass, (name, hooks) in self._preserved_hooks.items():
1675
1583
setattr(klass, name, hooks)
1676
hooks._lazy_hooks = self._preserved_lazy_hooks
1678
1585
def knownFailure(self, reason):
1679
1586
"""This test has failed for some known reason."""
2119
2025
if retcode is not None and retcode != process.returncode:
2120
2026
if process_args is None:
2121
2027
process_args = "(unknown args)"
2122
trace.mutter('Output of bzr %s:\n%s', process_args, out)
2123
trace.mutter('Error for bzr %s:\n%s', process_args, err)
2028
mutter('Output of bzr %s:\n%s', process_args, out)
2029
mutter('Error for bzr %s:\n%s', process_args, err)
2124
2030
self.fail('Command bzr %s failed with retcode %s != %s'
2125
2031
% (process_args, retcode, process.returncode))
2126
2032
return [out, err]
2513
2419
test_home_dir = self.test_home_dir
2514
2420
if isinstance(test_home_dir, unicode):
2515
2421
test_home_dir = test_home_dir.encode(sys.getfilesystemencoding())
2516
self.overrideEnv('HOME', test_home_dir)
2517
self.overrideEnv('BZR_HOME', test_home_dir)
2422
os.environ['HOME'] = test_home_dir
2423
os.environ['BZR_HOME'] = test_home_dir
2519
2425
def setUp(self):
2520
2426
super(TestCaseWithMemoryTransport, self).setUp()
3429
class ProfileResult(testtools.ExtendedToOriginalDecorator):
3335
class ForwardingResult(unittest.TestResult):
3337
def __init__(self, target):
3338
unittest.TestResult.__init__(self)
3339
self.result = target
3341
def startTest(self, test):
3342
self.result.startTest(test)
3344
def stopTest(self, test):
3345
self.result.stopTest(test)
3347
def startTestRun(self):
3348
self.result.startTestRun()
3350
def stopTestRun(self):
3351
self.result.stopTestRun()
3353
def addSkip(self, test, reason):
3354
self.result.addSkip(test, reason)
3356
def addSuccess(self, test):
3357
self.result.addSuccess(test)
3359
def addError(self, test, err):
3360
self.result.addError(test, err)
3362
def addFailure(self, test, err):
3363
self.result.addFailure(test, err)
3364
ForwardingResult = testtools.ExtendedToOriginalDecorator
3367
class ProfileResult(ForwardingResult):
3430
3368
"""Generate profiling data for all activity between start and success.
3432
3370
The profile data is appended to the test's _benchcalls attribute and can
3454
3392
test._benchcalls = []
3455
3393
calls = test._benchcalls
3456
3394
calls.append(((test.id(), "", ""), stats))
3457
testtools.ExtendedToOriginalDecorator.addSuccess(self, test)
3395
ForwardingResult.addSuccess(self, test)
3459
3397
def stopTest(self, test):
3460
testtools.ExtendedToOriginalDecorator.stopTest(self, test)
3398
ForwardingResult.stopTest(self, test)
3461
3399
self.profiler = None
3728
3665
'bzrlib.tests.per_repository',
3729
3666
'bzrlib.tests.per_repository_chk',
3730
3667
'bzrlib.tests.per_repository_reference',
3731
'bzrlib.tests.per_repository_vf',
3732
3668
'bzrlib.tests.per_uifactory',
3733
3669
'bzrlib.tests.per_versionedfile',
3734
3670
'bzrlib.tests.per_workingtree',
3768
3704
'bzrlib.tests.test_commit_merge',
3769
3705
'bzrlib.tests.test_config',
3770
3706
'bzrlib.tests.test_conflicts',
3771
'bzrlib.tests.test_controldir',
3772
3707
'bzrlib.tests.test_counted_lock',
3773
3708
'bzrlib.tests.test_crash',
3774
3709
'bzrlib.tests.test_decorators',
3825
3760
'bzrlib.tests.test_merge3',
3826
3761
'bzrlib.tests.test_merge_core',
3827
3762
'bzrlib.tests.test_merge_directive',
3828
'bzrlib.tests.test_mergetools',
3829
3763
'bzrlib.tests.test_missing',
3830
3764
'bzrlib.tests.test_msgeditor',
3831
3765
'bzrlib.tests.test_multiparent',
3840
3774
'bzrlib.tests.test_permissions',
3841
3775
'bzrlib.tests.test_plugins',
3842
3776
'bzrlib.tests.test_progress',
3843
'bzrlib.tests.test_pyutils',
3844
3777
'bzrlib.tests.test_read_bundle',
3845
3778
'bzrlib.tests.test_reconcile',
3846
3779
'bzrlib.tests.test_reconfigure',
3881
3813
'bzrlib.tests.test_testament',
3882
3814
'bzrlib.tests.test_textfile',
3883
3815
'bzrlib.tests.test_textmerge',
3884
'bzrlib.tests.test_cethread',
3885
3816
'bzrlib.tests.test_timestamp',
3886
3817
'bzrlib.tests.test_trace',
3887
3818
'bzrlib.tests.test_transactions',
4032
3962
# Some tests mentioned in the list are not in the test suite. The
4033
3963
# list may be out of date, report to the tester.
4034
3964
for id in not_found:
4035
trace.warning('"%s" not found in the test suite', id)
3965
bzrlib.trace.warning('"%s" not found in the test suite', id)
4036
3966
for id in duplicates:
4037
trace.warning('"%s" is used as an id by several tests', id)
3967
bzrlib.trace.warning('"%s" is used as an id by several tests', id)
4042
def multiply_scenarios(*scenarios):
4043
"""Multiply two or more iterables of scenarios.
4045
It is safe to pass scenario generators or iterators.
4047
:returns: A list of compound scenarios: the cross-product of all
4048
scenarios, with the names concatenated and the parameters
4051
return reduce(_multiply_two_scenarios, map(list, scenarios))
4054
def _multiply_two_scenarios(scenarios_left, scenarios_right):
3972
def multiply_scenarios(scenarios_left, scenarios_right):
4055
3973
"""Multiply two sets of scenarios.
4057
3975
:returns: the cartesian product of the two sets of scenarios, that is
4340
4258
symbol_versioning.warn(depr_msg + use_msg, DeprecationWarning)
4341
4259
# Import the new feature and use it as a replacement for the
4342
4260
# deprecated one.
4343
self._feature = pyutils.get_named_object(
4344
self._replacement_module, self._replacement_name)
4261
mod = __import__(self._replacement_module, {}, {},
4262
[self._replacement_name])
4263
self._feature = getattr(mod, self._replacement_name)
4346
4265
def _probe(self):