123
123
TestSuite = TestUtil.TestSuite
124
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)
126
213
class ExtendedTestResult(testtools.TextTestResult):
127
214
"""Accepts, reports and accumulates the results of running tests.
794
881
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)
797
903
class TestCase(testtools.TestCase):
798
904
"""Base class for bzr unit tests.
1231
1336
if haystack.find(needle) == -1:
1232
1337
self.fail("string %r not found in '''%s'''" % (needle, haystack))
1339
def assertNotContainsString(self, haystack, needle):
1340
if haystack.find(needle) != -1:
1341
self.fail("string %r found in '''%s'''" % (needle, haystack))
1234
1343
def assertSubset(self, sublist, superlist):
1235
1344
"""Assert that every entry in sublist is present in superlist."""
1236
1345
missing = set(sublist) - set(superlist)
1540
1649
def _cleanEnvironment(self):
1542
'BZR_HOME': None, # Don't inherit BZR_HOME to all the tests.
1543
'HOME': os.getcwd(),
1544
# bzr now uses the Win32 API and doesn't rely on APPDATA, but the
1545
# tests do check our impls match APPDATA
1546
'BZR_EDITOR': None, # test_msgeditor manipulates this variable
1550
'BZREMAIL': None, # may still be present in the environment
1551
'EMAIL': 'jrandom@example.com', # set EMAIL as bzr does not guess
1552
'BZR_PROGRESS_BAR': None,
1554
'BZR_PLUGIN_PATH': None,
1555
'BZR_DISABLE_PLUGINS': None,
1556
'BZR_PLUGINS_AT': None,
1557
'BZR_CONCURRENCY': None,
1558
# Make sure that any text ui tests are consistent regardless of
1559
# the environment the test case is run in; you may want tests that
1560
# test other combinations. 'dumb' is a reasonable guess for tests
1561
# going to a pipe or a StringIO.
1565
'BZR_COLUMNS': '80',
1567
'SSH_AUTH_SOCK': None,
1571
'https_proxy': None,
1572
'HTTPS_PROXY': None,
1577
# Nobody cares about ftp_proxy, FTP_PROXY AFAIK. So far at
1578
# least. If you do (care), please update this comment
1582
'BZR_REMOTE_PATH': None,
1583
# Generally speaking, we don't want apport reporting on crashes in
1584
# the test envirnoment unless we're specifically testing apport,
1585
# so that it doesn't leak into the real system environment. We
1586
# use an env var so it propagates to subprocesses.
1587
'APPORT_DISABLE': '1',
1589
for name, value in new_env.iteritems():
1650
for name, value in isolated_environ.iteritems():
1590
1651
self.overrideEnv(name, value)
1592
def _captureVar(self, name, newvalue):
1593
"""Set an environment variable, and reset it when finished."""
1594
self._old_env[name] = osutils.set_or_unset_env(name, newvalue)
1596
1653
def _restoreHooks(self):
1597
1654
for klass, (name, hooks) in self._preserved_hooks.items():
1598
1655
setattr(klass, name, hooks)
3839
# FIXME: Fixing bug #690563 revealed an isolation problem in the single
3840
# doctest for branchbuilder. Uncomment this when bug #321320 is fixed
3841
# to ensure the issue is addressed (note that to reproduce the bug in
3842
# the doctest below, one should comment the 'email' config var in
3843
# bazaar.conf (or anywhere else). This means an setup where *no* user
3844
# is being set at all in the environment.
3845
# 'bzrlib.branchbuilder',
3896
'bzrlib.branchbuilder',
3846
3897
'bzrlib.decorators',
3847
3898
'bzrlib.export',
3848
3899
'bzrlib.inventory',
3915
3966
# note that this really does mean "report only" -- doctest
3916
3967
# still runs the rest of the examples
3917
doc_suite = doctest.DocTestSuite(mod,
3918
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE)
3968
doc_suite = IsolatedDocTestSuite(
3969
mod, optionflags=doctest.REPORT_ONLY_FIRST_FAILURE)
3919
3970
except ValueError, e:
3920
3971
print '**failed to get doctest for: %s\n%s' % (mod, e)