745
760
def log(self, *args):
749
"""Return as a string the log for this test"""
750
if self._log_file_name:
751
return open(self._log_file_name).read()
763
def _get_log(self, keep_log_file=False):
764
"""Return as a string the log for this test. If the file is still
765
on disk and keep_log_file=False, delete the log file and store the
766
content in self._log_contents."""
767
# flush the log file, to get all content
769
bzrlib.trace._trace_file.flush()
770
if self._log_contents:
753
771
return self._log_contents
754
# TODO: Delete the log after it's been read in
772
if self._log_file_name is not None:
773
logfile = open(self._log_file_name)
775
log_contents = logfile.read()
778
if not keep_log_file:
779
self._log_contents = log_contents
780
os.remove(self._log_file_name)
783
return "DELETED log file to reduce memory footprint"
756
785
def capture(self, cmd, retcode=0):
757
786
"""Shortcut that splits cmd into words, runs, and returns stdout"""
758
787
return self.run_bzr_captured(cmd.split(), retcode=retcode)[0]
760
def run_bzr_captured(self, argv, retcode=0, encoding=None, stdin=None):
789
def run_bzr_captured(self, argv, retcode=0, encoding=None, stdin=None,
761
791
"""Invoke bzr and return (stdout, stderr).
763
793
Useful for code that wants to check the contents of the
1054
1125
BzrTestBase = TestCase
1128
class TestCaseWithMemoryTransport(TestCase):
1129
"""Common test class for tests that do not need disk resources.
1131
Tests that need disk resources should derive from TestCaseWithTransport.
1133
TestCaseWithMemoryTransport sets the TEST_ROOT variable for all bzr tests.
1135
For TestCaseWithMemoryTransport the test_home_dir is set to the name of
1136
a directory which does not exist. This serves to help ensure test isolation
1137
is preserved. test_dir is set to the TEST_ROOT, as is cwd, because they
1138
must exist. However, TestCaseWithMemoryTransport does not offer local
1139
file defaults for the transport in tests, nor does it obey the command line
1140
override, so tests that accidentally write to the common directory should
1148
def __init__(self, methodName='runTest'):
1149
# allow test parameterisation after test construction and before test
1150
# execution. Variables that the parameteriser sets need to be
1151
# ones that are not set by setUp, or setUp will trash them.
1152
super(TestCaseWithMemoryTransport, self).__init__(methodName)
1153
self.transport_server = default_transport
1154
self.transport_readonly_server = None
1156
def failUnlessExists(self, path):
1157
"""Fail unless path, which may be abs or relative, exists."""
1158
self.failUnless(osutils.lexists(path))
1160
def failIfExists(self, path):
1161
"""Fail if path, which may be abs or relative, exists."""
1162
self.failIf(osutils.lexists(path))
1164
def get_transport(self):
1165
"""Return a writeable transport for the test scratch space"""
1166
t = get_transport(self.get_url())
1167
self.assertFalse(t.is_readonly())
1170
def get_readonly_transport(self):
1171
"""Return a readonly transport for the test scratch space
1173
This can be used to test that operations which should only need
1174
readonly access in fact do not try to write.
1176
t = get_transport(self.get_readonly_url())
1177
self.assertTrue(t.is_readonly())
1180
def get_readonly_server(self):
1181
"""Get the server instance for the readonly transport
1183
This is useful for some tests with specific servers to do diagnostics.
1185
if self.__readonly_server is None:
1186
if self.transport_readonly_server is None:
1187
# readonly decorator requested
1188
# bring up the server
1190
self.__readonly_server = ReadonlyServer()
1191
self.__readonly_server.setUp(self.__server)
1193
self.__readonly_server = self.transport_readonly_server()
1194
self.__readonly_server.setUp()
1195
self.addCleanup(self.__readonly_server.tearDown)
1196
return self.__readonly_server
1198
def get_readonly_url(self, relpath=None):
1199
"""Get a URL for the readonly transport.
1201
This will either be backed by '.' or a decorator to the transport
1202
used by self.get_url()
1203
relpath provides for clients to get a path relative to the base url.
1204
These should only be downwards relative, not upwards.
1206
base = self.get_readonly_server().get_url()
1207
if relpath is not None:
1208
if not base.endswith('/'):
1210
base = base + relpath
1213
def get_server(self):
1214
"""Get the read/write server instance.
1216
This is useful for some tests with specific servers that need
1219
For TestCaseWithMemoryTransport this is always a MemoryServer, and there
1220
is no means to override it.
1222
if self.__server is None:
1223
self.__server = MemoryServer()
1224
self.__server.setUp()
1225
self.addCleanup(self.__server.tearDown)
1226
return self.__server
1228
def get_url(self, relpath=None):
1229
"""Get a URL (or maybe a path) for the readwrite transport.
1231
This will either be backed by '.' or to an equivalent non-file based
1233
relpath provides for clients to get a path relative to the base url.
1234
These should only be downwards relative, not upwards.
1236
base = self.get_server().get_url()
1237
if relpath is not None and relpath != '.':
1238
if not base.endswith('/'):
1240
# XXX: Really base should be a url; we did after all call
1241
# get_url()! But sometimes it's just a path (from
1242
# LocalAbspathServer), and it'd be wrong to append urlescaped data
1243
# to a non-escaped local path.
1244
if base.startswith('./') or base.startswith('/'):
1247
base += urlutils.escape(relpath)
1250
def _make_test_root(self):
1251
if TestCaseWithMemoryTransport.TEST_ROOT is not None:
1255
root = u'test%04d.tmp' % i
1259
if e.errno == errno.EEXIST:
1264
# successfully created
1265
TestCaseWithMemoryTransport.TEST_ROOT = osutils.abspath(root)
1267
# make a fake bzr directory there to prevent any tests propagating
1268
# up onto the source directory's real branch
1269
bzrdir.BzrDir.create_standalone_workingtree(
1270
TestCaseWithMemoryTransport.TEST_ROOT)
1272
def makeAndChdirToTestDir(self):
1273
"""Create a temporary directories for this one test.
1275
This must set self.test_home_dir and self.test_dir and chdir to
1278
For TestCaseWithMemoryTransport we chdir to the TEST_ROOT for this test.
1280
os.chdir(TestCaseWithMemoryTransport.TEST_ROOT)
1281
self.test_dir = TestCaseWithMemoryTransport.TEST_ROOT
1282
self.test_home_dir = self.test_dir + "/MemoryTransportMissingHomeDir"
1284
def make_branch(self, relpath, format=None):
1285
"""Create a branch on the transport at relpath."""
1286
repo = self.make_repository(relpath, format=format)
1287
return repo.bzrdir.create_branch()
1289
def make_bzrdir(self, relpath, format=None):
1291
# might be a relative or absolute path
1292
maybe_a_url = self.get_url(relpath)
1293
segments = maybe_a_url.rsplit('/', 1)
1294
t = get_transport(maybe_a_url)
1295
if len(segments) > 1 and segments[-1] not in ('', '.'):
1298
except errors.FileExists:
1301
format = bzrlib.bzrdir.BzrDirFormat.get_default_format()
1302
return format.initialize_on_transport(t)
1303
except errors.UninitializableFormat:
1304
raise TestSkipped("Format %s is not initializable." % format)
1306
def make_repository(self, relpath, shared=False, format=None):
1307
"""Create a repository on our default transport at relpath."""
1308
made_control = self.make_bzrdir(relpath, format=format)
1309
return made_control.create_repository(shared=shared)
1311
def make_branch_and_memory_tree(self, relpath, format=None):
1312
"""Create a branch on the default transport and a MemoryTree for it."""
1313
b = self.make_branch(relpath, format=format)
1314
return memorytree.MemoryTree.create_on_branch(b)
1316
def overrideEnvironmentForTesting(self):
1317
os.environ['HOME'] = self.test_home_dir
1318
os.environ['APPDATA'] = self.test_home_dir
1321
super(TestCaseWithMemoryTransport, self).setUp()
1322
self._make_test_root()
1323
_currentdir = os.getcwdu()
1324
def _leaveDirectory():
1325
os.chdir(_currentdir)
1326
self.addCleanup(_leaveDirectory)
1327
self.makeAndChdirToTestDir()
1328
self.overrideEnvironmentForTesting()
1329
self.__readonly_server = None
1330
self.__server = None
1057
class TestCaseInTempDir(TestCase):
1333
class TestCaseInTempDir(TestCaseWithMemoryTransport):
1058
1334
"""Derived class that runs a test within a temporary directory.
1060
1336
This is useful for tests that need to create a branch, etc.
1207
1449
readwrite one must both define get_url() as resolving to os.getcwd().
1210
def __init__(self, methodName='testMethod'):
1211
super(TestCaseWithTransport, self).__init__(methodName)
1212
self.__readonly_server = None
1213
self.__server = None
1214
self.transport_server = default_transport
1215
self.transport_readonly_server = None
1217
def get_readonly_url(self, relpath=None):
1218
"""Get a URL for the readonly transport.
1220
This will either be backed by '.' or a decorator to the transport
1221
used by self.get_url()
1222
relpath provides for clients to get a path relative to the base url.
1223
These should only be downwards relative, not upwards.
1225
base = self.get_readonly_server().get_url()
1226
if relpath is not None:
1227
if not base.endswith('/'):
1229
base = base + relpath
1232
def get_readonly_server(self):
1233
"""Get the server instance for the readonly transport
1235
This is useful for some tests with specific servers to do diagnostics.
1237
if self.__readonly_server is None:
1238
if self.transport_readonly_server is None:
1239
# readonly decorator requested
1240
# bring up the server
1242
self.__readonly_server = ReadonlyServer()
1243
self.__readonly_server.setUp(self.__server)
1245
self.__readonly_server = self.transport_readonly_server()
1246
self.__readonly_server.setUp()
1247
self.addCleanup(self.__readonly_server.tearDown)
1248
return self.__readonly_server
1250
1452
def get_server(self):
1251
"""Get the read/write server instance.
1453
"""See TestCaseWithMemoryTransport.
1253
1455
This is useful for some tests with specific servers that need
1259
1461
self.addCleanup(self.__server.tearDown)
1260
1462
return self.__server
1262
def get_url(self, relpath=None):
1263
"""Get a URL (or maybe a path) for the readwrite transport.
1265
This will either be backed by '.' or to an equivalent non-file based
1267
relpath provides for clients to get a path relative to the base url.
1268
These should only be downwards relative, not upwards.
1270
base = self.get_server().get_url()
1271
if relpath is not None and relpath != '.':
1272
if not base.endswith('/'):
1274
# XXX: Really base should be a url; we did after all call
1275
# get_url()! But sometimes it's just a path (from
1276
# LocalAbspathServer), and it'd be wrong to append urlescaped data
1277
# to a non-escaped local path.
1278
if base.startswith('./') or base.startswith('/'):
1281
base += urlutils.escape(relpath)
1284
def get_transport(self):
1285
"""Return a writeable transport for the test scratch space"""
1286
t = get_transport(self.get_url())
1287
self.assertFalse(t.is_readonly())
1290
def get_readonly_transport(self):
1291
"""Return a readonly transport for the test scratch space
1293
This can be used to test that operations which should only need
1294
readonly access in fact do not try to write.
1296
t = get_transport(self.get_readonly_url())
1297
self.assertTrue(t.is_readonly())
1300
def make_branch(self, relpath, format=None):
1301
"""Create a branch on the transport at relpath."""
1302
repo = self.make_repository(relpath, format=format)
1303
return repo.bzrdir.create_branch()
1305
def make_bzrdir(self, relpath, format=None):
1307
# might be a relative or absolute path
1308
maybe_a_url = self.get_url(relpath)
1309
segments = maybe_a_url.rsplit('/', 1)
1310
t = get_transport(maybe_a_url)
1311
if len(segments) > 1 and segments[-1] not in ('', '.'):
1314
except errors.FileExists:
1317
format = bzrlib.bzrdir.BzrDirFormat.get_default_format()
1318
return format.initialize_on_transport(t)
1319
except errors.UninitializableFormat:
1320
raise TestSkipped("Format %s is not initializable." % format)
1322
def make_repository(self, relpath, shared=False, format=None):
1323
"""Create a repository on our default transport at relpath."""
1324
made_control = self.make_bzrdir(relpath, format=format)
1325
return made_control.create_repository(shared=shared)
1327
def make_branch_and_memory_tree(self, relpath):
1328
"""Create a branch on the default transport and a MemoryTree for it."""
1329
b = self.make_branch(relpath)
1330
return memorytree.MemoryTree.create_on_branch(b)
1332
1464
def make_branch_and_tree(self, relpath, format=None):
1333
1465
"""Create a branch on the transport and a tree locally.