1109
1106
BzrTestBase = TestCase
1109
class TestCaseWithMemoryTransport(TestCase):
1110
"""Common test class for tests that do not need disk resources.
1112
Tests that need disk resources should derive from TestCaseWithTransport.
1114
TestCaseWithMemoryTransport sets the TEST_ROOT variable for all bzr tests.
1116
For TestCaseWithMemoryTransport the test_home_dir is set to the name of
1117
a directory which does not exist. This serves to help ensure test isolation
1118
is preserved. test_dir is set to the TEST_ROOT, as is cwd, because they
1119
must exist. However, TestCaseWithMemoryTransport does not offer local
1120
file defaults for the transport in tests, nor does it obey the command line
1121
override, so tests that accidentally write to the common directory should
1129
def __init__(self, methodName='runTest'):
1130
# allow test parameterisation after test construction and before test
1131
# execution. Variables that the parameteriser sets need to be
1132
# ones that are not set by setUp, or setUp will trash them.
1133
super(TestCaseWithMemoryTransport, self).__init__(methodName)
1134
self.transport_server = default_transport
1135
self.transport_readonly_server = None
1137
def failUnlessExists(self, path):
1138
"""Fail unless path, which may be abs or relative, exists."""
1139
self.failUnless(osutils.lexists(path))
1141
def failIfExists(self, path):
1142
"""Fail if path, which may be abs or relative, exists."""
1143
self.failIf(osutils.lexists(path))
1145
def get_transport(self):
1146
"""Return a writeable transport for the test scratch space"""
1147
t = get_transport(self.get_url())
1148
self.assertFalse(t.is_readonly())
1151
def get_readonly_transport(self):
1152
"""Return a readonly transport for the test scratch space
1154
This can be used to test that operations which should only need
1155
readonly access in fact do not try to write.
1157
t = get_transport(self.get_readonly_url())
1158
self.assertTrue(t.is_readonly())
1161
def get_readonly_server(self):
1162
"""Get the server instance for the readonly transport
1164
This is useful for some tests with specific servers to do diagnostics.
1166
if self.__readonly_server is None:
1167
if self.transport_readonly_server is None:
1168
# readonly decorator requested
1169
# bring up the server
1171
self.__readonly_server = ReadonlyServer()
1172
self.__readonly_server.setUp(self.__server)
1174
self.__readonly_server = self.transport_readonly_server()
1175
self.__readonly_server.setUp()
1176
self.addCleanup(self.__readonly_server.tearDown)
1177
return self.__readonly_server
1179
def get_readonly_url(self, relpath=None):
1180
"""Get a URL for the readonly transport.
1182
This will either be backed by '.' or a decorator to the transport
1183
used by self.get_url()
1184
relpath provides for clients to get a path relative to the base url.
1185
These should only be downwards relative, not upwards.
1187
base = self.get_readonly_server().get_url()
1188
if relpath is not None:
1189
if not base.endswith('/'):
1191
base = base + relpath
1194
def get_server(self):
1195
"""Get the read/write server instance.
1197
This is useful for some tests with specific servers that need
1200
For TestCaseWithMemoryTransport this is always a MemoryServer, and there
1201
is no means to override it.
1203
if self.__server is None:
1204
self.__server = MemoryServer()
1205
self.__server.setUp()
1206
self.addCleanup(self.__server.tearDown)
1207
return self.__server
1209
def get_url(self, relpath=None):
1210
"""Get a URL (or maybe a path) for the readwrite transport.
1212
This will either be backed by '.' or to an equivalent non-file based
1214
relpath provides for clients to get a path relative to the base url.
1215
These should only be downwards relative, not upwards.
1217
base = self.get_server().get_url()
1218
if relpath is not None and relpath != '.':
1219
if not base.endswith('/'):
1221
# XXX: Really base should be a url; we did after all call
1222
# get_url()! But sometimes it's just a path (from
1223
# LocalAbspathServer), and it'd be wrong to append urlescaped data
1224
# to a non-escaped local path.
1225
if base.startswith('./') or base.startswith('/'):
1228
base += urlutils.escape(relpath)
1231
def _make_test_root(self):
1232
if TestCaseWithMemoryTransport.TEST_ROOT is not None:
1236
root = u'test%04d.tmp' % i
1240
if e.errno == errno.EEXIST:
1245
# successfully created
1246
TestCaseWithMemoryTransport.TEST_ROOT = osutils.abspath(root)
1248
# make a fake bzr directory there to prevent any tests propagating
1249
# up onto the source directory's real branch
1250
bzrdir.BzrDir.create_standalone_workingtree(
1251
TestCaseWithMemoryTransport.TEST_ROOT)
1253
def makeAndChdirToTestDir(self):
1254
"""Create a temporary directories for this one test.
1256
This must set self.test_home_dir and self.test_dir and chdir to
1259
For TestCaseWithMemoryTransport we chdir to the TEST_ROOT for this test.
1261
os.chdir(TestCaseWithMemoryTransport.TEST_ROOT)
1262
self.test_dir = TestCaseWithMemoryTransport.TEST_ROOT
1263
self.test_home_dir = self.test_dir + "/MemoryTransportMissingHomeDir"
1265
def make_branch(self, relpath, format=None):
1266
"""Create a branch on the transport at relpath."""
1267
repo = self.make_repository(relpath, format=format)
1268
return repo.bzrdir.create_branch()
1270
def make_bzrdir(self, relpath, format=None):
1272
# might be a relative or absolute path
1273
maybe_a_url = self.get_url(relpath)
1274
segments = maybe_a_url.rsplit('/', 1)
1275
t = get_transport(maybe_a_url)
1276
if len(segments) > 1 and segments[-1] not in ('', '.'):
1279
except errors.FileExists:
1282
format = bzrlib.bzrdir.BzrDirFormat.get_default_format()
1283
return format.initialize_on_transport(t)
1284
except errors.UninitializableFormat:
1285
raise TestSkipped("Format %s is not initializable." % format)
1287
def make_repository(self, relpath, shared=False, format=None):
1288
"""Create a repository on our default transport at relpath."""
1289
made_control = self.make_bzrdir(relpath, format=format)
1290
return made_control.create_repository(shared=shared)
1292
def make_branch_and_memory_tree(self, relpath):
1293
"""Create a branch on the default transport and a MemoryTree for it."""
1294
b = self.make_branch(relpath)
1295
return memorytree.MemoryTree.create_on_branch(b)
1297
def overrideEnvironmentForTesting(self):
1298
os.environ['HOME'] = self.test_home_dir
1299
os.environ['APPDATA'] = self.test_home_dir
1302
super(TestCaseWithMemoryTransport, self).setUp()
1303
self._make_test_root()
1304
_currentdir = os.getcwdu()
1305
def _leaveDirectory():
1306
os.chdir(_currentdir)
1307
self.addCleanup(_leaveDirectory)
1308
self.makeAndChdirToTestDir()
1309
self.overrideEnvironmentForTesting()
1310
self.__readonly_server = None
1311
self.__server = None
1112
class TestCaseInTempDir(TestCase):
1314
class TestCaseInTempDir(TestCaseWithMemoryTransport):
1113
1315
"""Derived class that runs a test within a temporary directory.
1115
1317
This is useful for tests that need to create a branch, etc.
1262
1430
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
1433
def get_server(self):
1306
"""Get the read/write server instance.
1434
"""See TestCaseWithMemoryTransport.
1308
1436
This is useful for some tests with specific servers that need
1314
1442
self.addCleanup(self.__server.tearDown)
1315
1443
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
1445
def make_branch_and_tree(self, relpath, format=None):
1388
1446
"""Create a branch on the transport and a tree locally.