1109
1110
BzrTestBase = TestCase
1113
class TestCaseWithMemoryTransport(TestCase):
1114
"""Common test class for tests that do not need disk resources.
1116
Tests that need disk resources should derive from TestCaseWithTransport.
1118
TestCaseWithMemoryTransport sets the TEST_ROOT variable for all bzr tests.
1120
For TestCaseWithMemoryTransport the test_home_dir is set to the name of
1121
a directory which does not exist. This serves to help ensure test isolation
1122
is preserved. test_dir is set to the TEST_ROOT, as is cwd, because they
1123
must exist. However, TestCaseWithMemoryTransport does not offer local
1124
file defaults for the transport in tests, nor does it obey the command line
1125
override, so tests that accidentally write to the common directory should
1133
def __init__(self, methodName='runTest'):
1134
# allow test parameterisation after test construction and before test
1135
# execution. Variables that the parameteriser sets need to be
1136
# ones that are not set by setUp, or setUp will trash them.
1137
super(TestCaseWithMemoryTransport, self).__init__(methodName)
1138
self.transport_server = default_transport
1139
self.transport_readonly_server = None
1141
def failUnlessExists(self, path):
1142
"""Fail unless path, which may be abs or relative, exists."""
1143
self.failUnless(osutils.lexists(path))
1145
def failIfExists(self, path):
1146
"""Fail if path, which may be abs or relative, exists."""
1147
self.failIf(osutils.lexists(path))
1149
def get_transport(self):
1150
"""Return a writeable transport for the test scratch space"""
1151
t = get_transport(self.get_url())
1152
self.assertFalse(t.is_readonly())
1155
def get_readonly_transport(self):
1156
"""Return a readonly transport for the test scratch space
1158
This can be used to test that operations which should only need
1159
readonly access in fact do not try to write.
1161
t = get_transport(self.get_readonly_url())
1162
self.assertTrue(t.is_readonly())
1165
def get_readonly_server(self):
1166
"""Get the server instance for the readonly transport
1168
This is useful for some tests with specific servers to do diagnostics.
1170
if self.__readonly_server is None:
1171
if self.transport_readonly_server is None:
1172
# readonly decorator requested
1173
# bring up the server
1175
self.__readonly_server = ReadonlyServer()
1176
self.__readonly_server.setUp(self.__server)
1178
self.__readonly_server = self.transport_readonly_server()
1179
self.__readonly_server.setUp()
1180
self.addCleanup(self.__readonly_server.tearDown)
1181
return self.__readonly_server
1183
def get_readonly_url(self, relpath=None):
1184
"""Get a URL for the readonly transport.
1186
This will either be backed by '.' or a decorator to the transport
1187
used by self.get_url()
1188
relpath provides for clients to get a path relative to the base url.
1189
These should only be downwards relative, not upwards.
1191
base = self.get_readonly_server().get_url()
1192
if relpath is not None:
1193
if not base.endswith('/'):
1195
base = base + relpath
1198
def get_server(self):
1199
"""Get the read/write server instance.
1201
This is useful for some tests with specific servers that need
1204
For TestCaseWithMemoryTransport this is always a MemoryServer, and there
1205
is no means to override it.
1207
if self.__server is None:
1208
self.__server = MemoryServer()
1209
self.__server.setUp()
1210
self.addCleanup(self.__server.tearDown)
1211
return self.__server
1213
def get_url(self, relpath=None):
1214
"""Get a URL (or maybe a path) for the readwrite transport.
1216
This will either be backed by '.' or to an equivalent non-file based
1218
relpath provides for clients to get a path relative to the base url.
1219
These should only be downwards relative, not upwards.
1221
base = self.get_server().get_url()
1222
if relpath is not None and relpath != '.':
1223
if not base.endswith('/'):
1225
# XXX: Really base should be a url; we did after all call
1226
# get_url()! But sometimes it's just a path (from
1227
# LocalAbspathServer), and it'd be wrong to append urlescaped data
1228
# to a non-escaped local path.
1229
if base.startswith('./') or base.startswith('/'):
1232
base += urlutils.escape(relpath)
1235
def _make_test_root(self):
1236
if TestCaseWithMemoryTransport.TEST_ROOT is not None:
1240
root = u'test%04d.tmp' % i
1244
if e.errno == errno.EEXIST:
1249
# successfully created
1250
TestCaseWithMemoryTransport.TEST_ROOT = osutils.abspath(root)
1252
# make a fake bzr directory there to prevent any tests propagating
1253
# up onto the source directory's real branch
1254
bzrdir.BzrDir.create_standalone_workingtree(
1255
TestCaseWithMemoryTransport.TEST_ROOT)
1257
def makeAndChdirToTestDir(self):
1258
"""Create a temporary directories for this one test.
1260
This must set self.test_home_dir and self.test_dir and chdir to
1263
For TestCaseWithMemoryTransport we chdir to the TEST_ROOT for this test.
1265
os.chdir(TestCaseWithMemoryTransport.TEST_ROOT)
1266
self.test_dir = TestCaseWithMemoryTransport.TEST_ROOT
1267
self.test_home_dir = self.test_dir + "/MemoryTransportMissingHomeDir"
1269
def make_branch(self, relpath, format=None):
1270
"""Create a branch on the transport at relpath."""
1271
repo = self.make_repository(relpath, format=format)
1272
return repo.bzrdir.create_branch()
1274
def make_bzrdir(self, relpath, format=None):
1276
# might be a relative or absolute path
1277
maybe_a_url = self.get_url(relpath)
1278
segments = maybe_a_url.rsplit('/', 1)
1279
t = get_transport(maybe_a_url)
1280
if len(segments) > 1 and segments[-1] not in ('', '.'):
1283
except errors.FileExists:
1286
format = bzrlib.bzrdir.BzrDirFormat.get_default_format()
1287
return format.initialize_on_transport(t)
1288
except errors.UninitializableFormat:
1289
raise TestSkipped("Format %s is not initializable." % format)
1291
def make_repository(self, relpath, shared=False, format=None):
1292
"""Create a repository on our default transport at relpath."""
1293
made_control = self.make_bzrdir(relpath, format=format)
1294
return made_control.create_repository(shared=shared)
1296
def make_branch_and_memory_tree(self, relpath):
1297
"""Create a branch on the default transport and a MemoryTree for it."""
1298
b = self.make_branch(relpath)
1299
return memorytree.MemoryTree.create_on_branch(b)
1301
def overrideEnvironmentForTesting(self):
1302
os.environ['HOME'] = self.test_home_dir
1303
os.environ['APPDATA'] = self.test_home_dir
1306
super(TestCaseWithMemoryTransport, self).setUp()
1307
self._make_test_root()
1308
_currentdir = os.getcwdu()
1309
def _leaveDirectory():
1310
os.chdir(_currentdir)
1311
self.addCleanup(_leaveDirectory)
1312
self.makeAndChdirToTestDir()
1313
self.overrideEnvironmentForTesting()
1314
self.__readonly_server = None
1315
self.__server = None
1112
class TestCaseInTempDir(TestCase):
1318
class TestCaseInTempDir(TestCaseWithMemoryTransport):
1113
1319
"""Derived class that runs a test within a temporary directory.
1115
1321
This is useful for tests that need to create a branch, etc.
1262
1434
readwrite one must both define get_url() as resolving to os.getcwd().
1265
def __init__(self, methodName='testMethod'):
1266
super(TestCaseWithTransport, self).__init__(methodName)
1267
self.__readonly_server = None
1268
self.__server = None
1269
self.transport_server = default_transport
1270
self.transport_readonly_server = None
1272
def get_readonly_url(self, relpath=None):
1273
"""Get a URL for the readonly transport.
1275
This will either be backed by '.' or a decorator to the transport
1276
used by self.get_url()
1277
relpath provides for clients to get a path relative to the base url.
1278
These should only be downwards relative, not upwards.
1280
base = self.get_readonly_server().get_url()
1281
if relpath is not None:
1282
if not base.endswith('/'):
1284
base = base + relpath
1287
def get_readonly_server(self):
1288
"""Get the server instance for the readonly transport
1290
This is useful for some tests with specific servers to do diagnostics.
1292
if self.__readonly_server is None:
1293
if self.transport_readonly_server is None:
1294
# readonly decorator requested
1295
# bring up the server
1297
self.__readonly_server = ReadonlyServer()
1298
self.__readonly_server.setUp(self.__server)
1300
self.__readonly_server = self.transport_readonly_server()
1301
self.__readonly_server.setUp()
1302
self.addCleanup(self.__readonly_server.tearDown)
1303
return self.__readonly_server
1305
1437
def get_server(self):
1306
"""Get the read/write server instance.
1438
"""See TestCaseWithMemoryTransport.
1308
1440
This is useful for some tests with specific servers that need
1314
1446
self.addCleanup(self.__server.tearDown)
1315
1447
return self.__server
1317
def get_url(self, relpath=None):
1318
"""Get a URL (or maybe a path) for the readwrite transport.
1320
This will either be backed by '.' or to an equivalent non-file based
1322
relpath provides for clients to get a path relative to the base url.
1323
These should only be downwards relative, not upwards.
1325
base = self.get_server().get_url()
1326
if relpath is not None and relpath != '.':
1327
if not base.endswith('/'):
1329
# XXX: Really base should be a url; we did after all call
1330
# get_url()! But sometimes it's just a path (from
1331
# LocalAbspathServer), and it'd be wrong to append urlescaped data
1332
# to a non-escaped local path.
1333
if base.startswith('./') or base.startswith('/'):
1336
base += urlutils.escape(relpath)
1339
def get_transport(self):
1340
"""Return a writeable transport for the test scratch space"""
1341
t = get_transport(self.get_url())
1342
self.assertFalse(t.is_readonly())
1345
def get_readonly_transport(self):
1346
"""Return a readonly transport for the test scratch space
1348
This can be used to test that operations which should only need
1349
readonly access in fact do not try to write.
1351
t = get_transport(self.get_readonly_url())
1352
self.assertTrue(t.is_readonly())
1355
def make_branch(self, relpath, format=None):
1356
"""Create a branch on the transport at relpath."""
1357
repo = self.make_repository(relpath, format=format)
1358
return repo.bzrdir.create_branch()
1360
def make_bzrdir(self, relpath, format=None):
1362
# might be a relative or absolute path
1363
maybe_a_url = self.get_url(relpath)
1364
segments = maybe_a_url.rsplit('/', 1)
1365
t = get_transport(maybe_a_url)
1366
if len(segments) > 1 and segments[-1] not in ('', '.'):
1369
except errors.FileExists:
1372
format = bzrlib.bzrdir.BzrDirFormat.get_default_format()
1373
return format.initialize_on_transport(t)
1374
except errors.UninitializableFormat:
1375
raise TestSkipped("Format %s is not initializable." % format)
1377
def make_repository(self, relpath, shared=False, format=None):
1378
"""Create a repository on our default transport at relpath."""
1379
made_control = self.make_bzrdir(relpath, format=format)
1380
return made_control.create_repository(shared=shared)
1382
def make_branch_and_memory_tree(self, relpath):
1383
"""Create a branch on the default transport and a MemoryTree for it."""
1384
b = self.make_branch(relpath)
1385
return memorytree.MemoryTree.create_on_branch(b)
1387
1449
def make_branch_and_tree(self, relpath, format=None):
1388
1450
"""Create a branch on the transport and a tree locally.