1108
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
1111
class TestCaseInTempDir(TestCase):
1314
class TestCaseInTempDir(TestCaseWithMemoryTransport):
1112
1315
"""Derived class that runs a test within a temporary directory.
1114
1317
This is useful for tests that need to create a branch, etc.
1261
1430
readwrite one must both define get_url() as resolving to os.getcwd().
1264
def __init__(self, methodName='testMethod'):
1265
super(TestCaseWithTransport, self).__init__(methodName)
1266
self.__readonly_server = None
1267
self.__server = None
1268
self.transport_server = default_transport
1269
self.transport_readonly_server = None
1271
def get_readonly_url(self, relpath=None):
1272
"""Get a URL for the readonly transport.
1274
This will either be backed by '.' or a decorator to the transport
1275
used by self.get_url()
1276
relpath provides for clients to get a path relative to the base url.
1277
These should only be downwards relative, not upwards.
1279
base = self.get_readonly_server().get_url()
1280
if relpath is not None:
1281
if not base.endswith('/'):
1283
base = base + relpath
1286
def get_readonly_server(self):
1287
"""Get the server instance for the readonly transport
1289
This is useful for some tests with specific servers to do diagnostics.
1291
if self.__readonly_server is None:
1292
if self.transport_readonly_server is None:
1293
# readonly decorator requested
1294
# bring up the server
1296
self.__readonly_server = ReadonlyServer()
1297
self.__readonly_server.setUp(self.__server)
1299
self.__readonly_server = self.transport_readonly_server()
1300
self.__readonly_server.setUp()
1301
self.addCleanup(self.__readonly_server.tearDown)
1302
return self.__readonly_server
1304
1433
def get_server(self):
1305
"""Get the read/write server instance.
1434
"""See TestCaseWithMemoryTransport.
1307
1436
This is useful for some tests with specific servers that need
1313
1442
self.addCleanup(self.__server.tearDown)
1314
1443
return self.__server
1316
def get_url(self, relpath=None):
1317
"""Get a URL (or maybe a path) for the readwrite transport.
1319
This will either be backed by '.' or to an equivalent non-file based
1321
relpath provides for clients to get a path relative to the base url.
1322
These should only be downwards relative, not upwards.
1324
base = self.get_server().get_url()
1325
if relpath is not None and relpath != '.':
1326
if not base.endswith('/'):
1328
# XXX: Really base should be a url; we did after all call
1329
# get_url()! But sometimes it's just a path (from
1330
# LocalAbspathServer), and it'd be wrong to append urlescaped data
1331
# to a non-escaped local path.
1332
if base.startswith('./') or base.startswith('/'):
1335
base += urlutils.escape(relpath)
1338
def get_transport(self):
1339
"""Return a writeable transport for the test scratch space"""
1340
t = get_transport(self.get_url())
1341
self.assertFalse(t.is_readonly())
1344
def get_readonly_transport(self):
1345
"""Return a readonly transport for the test scratch space
1347
This can be used to test that operations which should only need
1348
readonly access in fact do not try to write.
1350
t = get_transport(self.get_readonly_url())
1351
self.assertTrue(t.is_readonly())
1354
def make_branch(self, relpath, format=None):
1355
"""Create a branch on the transport at relpath."""
1356
repo = self.make_repository(relpath, format=format)
1357
return repo.bzrdir.create_branch()
1359
def make_bzrdir(self, relpath, format=None):
1361
# might be a relative or absolute path
1362
maybe_a_url = self.get_url(relpath)
1363
segments = maybe_a_url.rsplit('/', 1)
1364
t = get_transport(maybe_a_url)
1365
if len(segments) > 1 and segments[-1] not in ('', '.'):
1368
except errors.FileExists:
1371
format = bzrlib.bzrdir.BzrDirFormat.get_default_format()
1372
return format.initialize_on_transport(t)
1373
except errors.UninitializableFormat:
1374
raise TestSkipped("Format %s is not initializable." % format)
1376
def make_repository(self, relpath, shared=False, format=None):
1377
"""Create a repository on our default transport at relpath."""
1378
made_control = self.make_bzrdir(relpath, format=format)
1379
return made_control.create_repository(shared=shared)
1381
def make_branch_and_memory_tree(self, relpath):
1382
"""Create a branch on the default transport and a MemoryTree for it."""
1383
b = self.make_branch(relpath)
1384
return memorytree.MemoryTree.create_on_branch(b)
1386
1445
def make_branch_and_tree(self, relpath, format=None):
1387
1446
"""Create a branch on the transport and a tree locally.