1049
1050
BzrTestBase = TestCase
1052
class TestCaseInTempDir(TestCase):
1053
"""Derived class that runs a test within a temporary directory.
1055
This is useful for tests that need to create a branch, etc.
1057
The directory is created in a slightly complex way: for each
1058
Python invocation, a new temporary top-level directory is created.
1059
All test cases create their own directory within that. If the
1060
tests complete successfully, the directory is removed.
1062
InTempDir is an old alias for FunctionalTestCase.
1053
class TestCaseWithMemoryTransport(TestCase):
1054
"""Common test class for tests that do not need disk resources.
1056
Tests that need disk resources should derive from TestCaseWithTransport.
1058
TestCaseWithMemoryTransport sets the TEST_ROOT variable for all bzr tests.
1060
For TestCaseWithMemoryTransport the test_home_dir is set to the name of
1061
a directory which does not exist. This serves to help ensure test isolation
1062
is preserved. test_dir is set to the TEST_ROOT, as is cwd, because they
1063
must exist. However, TestCaseWithMemoryTransport does not offer local
1064
file defaults for the transport in tests, nor does it obey the command line
1065
override, so tests that accidentally write to the common directory should
1065
1069
TEST_ROOT = None
1066
1070
_TEST_NAME = 'test'
1067
OVERRIDE_PYTHON = 'python'
1069
def check_file_contents(self, filename, expect):
1070
self.log("check contents of file %s" % filename)
1071
contents = file(filename, 'r').read()
1072
if contents != expect:
1073
self.log("expected: %r" % expect)
1074
self.log("actually: %r" % contents)
1075
self.fail("contents of %s not as expected" % filename)
1072
def failUnlessExists(self, path):
1073
"""Fail unless path, which may be abs or relative, exists."""
1074
self.failUnless(osutils.lexists(path))
1076
def failIfExists(self, path):
1077
"""Fail if path, which may be abs or relative, exists."""
1078
self.failIf(osutils.lexists(path))
1080
def get_transport(self):
1081
"""Return a writeable transport for the test scratch space"""
1082
t = get_transport(self.get_url())
1083
self.assertFalse(t.is_readonly())
1086
def get_readonly_transport(self):
1087
"""Return a readonly transport for the test scratch space
1089
This can be used to test that operations which should only need
1090
readonly access in fact do not try to write.
1092
t = get_transport(self.get_readonly_url())
1093
self.assertTrue(t.is_readonly())
1096
def get_readonly_server(self):
1097
"""Get the server instance for the readonly transport
1099
This is useful for some tests with specific servers to do diagnostics.
1101
if self.__readonly_server is None:
1102
if self.transport_readonly_server is None:
1103
# readonly decorator requested
1104
# bring up the server
1106
self.__readonly_server = ReadonlyServer()
1107
self.__readonly_server.setUp(self.__server)
1109
self.__readonly_server = self.transport_readonly_server()
1110
self.__readonly_server.setUp()
1111
self.addCleanup(self.__readonly_server.tearDown)
1112
return self.__readonly_server
1114
def get_readonly_url(self, relpath=None):
1115
"""Get a URL for the readonly transport.
1117
This will either be backed by '.' or a decorator to the transport
1118
used by self.get_url()
1119
relpath provides for clients to get a path relative to the base url.
1120
These should only be downwards relative, not upwards.
1122
base = self.get_readonly_server().get_url()
1123
if relpath is not None:
1124
if not base.endswith('/'):
1126
base = base + relpath
1129
def get_server(self):
1130
"""Get the read/write server instance.
1132
This is useful for some tests with specific servers that need
1135
For TestCaseWithMemoryTransport this is always a MemoryServer, and there
1136
is no means to override it.
1138
if self.__server is None:
1139
self.__server = MemoryServer()
1140
self.__server.setUp()
1141
self.addCleanup(self.__server.tearDown)
1142
return self.__server
1144
def get_url(self, relpath=None):
1145
"""Get a URL (or maybe a path) for the readwrite transport.
1147
This will either be backed by '.' or to an equivalent non-file based
1149
relpath provides for clients to get a path relative to the base url.
1150
These should only be downwards relative, not upwards.
1152
base = self.get_server().get_url()
1153
if relpath is not None and relpath != '.':
1154
if not base.endswith('/'):
1156
# XXX: Really base should be a url; we did after all call
1157
# get_url()! But sometimes it's just a path (from
1158
# LocalAbspathServer), and it'd be wrong to append urlescaped data
1159
# to a non-escaped local path.
1160
if base.startswith('./') or base.startswith('/'):
1163
base += urlutils.escape(relpath)
1077
1166
def _make_test_root(self):
1078
if TestCaseInTempDir.TEST_ROOT is not None:
1167
if TestCaseWithMemoryTransport.TEST_ROOT is not None:
1091
1180
# successfully created
1092
TestCaseInTempDir.TEST_ROOT = osutils.abspath(root)
1181
TestCaseWithMemoryTransport.TEST_ROOT = osutils.abspath(root)
1094
1183
# make a fake bzr directory there to prevent any tests propagating
1095
1184
# up onto the source directory's real branch
1096
bzrdir.BzrDir.create_standalone_workingtree(TestCaseInTempDir.TEST_ROOT)
1185
bzrdir.BzrDir.create_standalone_workingtree(
1186
TestCaseWithMemoryTransport.TEST_ROOT)
1188
def makeAndChdirToTestDir(self):
1189
"""Create a temporary directories for this one test.
1191
This must set self.test_home_dir and self.test_dir and chdir to
1194
For TestCaseWithMemoryTransport we chdir to the TEST_ROOT for this test.
1196
os.chdir(TestCaseWithMemoryTransport.TEST_ROOT)
1197
self.test_dir = TestCaseWithMemoryTransport.TEST_ROOT
1198
self.test_home_dir = self.test_dir + "/MemoryTransportMissingHomeDir"
1200
def make_branch(self, relpath, format=None):
1201
"""Create a branch on the transport at relpath."""
1202
repo = self.make_repository(relpath, format=format)
1203
return repo.bzrdir.create_branch()
1205
def make_bzrdir(self, relpath, format=None):
1207
# might be a relative or absolute path
1208
maybe_a_url = self.get_url(relpath)
1209
segments = maybe_a_url.rsplit('/', 1)
1210
t = get_transport(maybe_a_url)
1211
if len(segments) > 1 and segments[-1] not in ('', '.'):
1214
except errors.FileExists:
1217
format = bzrlib.bzrdir.BzrDirFormat.get_default_format()
1218
return format.initialize_on_transport(t)
1219
except errors.UninitializableFormat:
1220
raise TestSkipped("Format %s is not initializable." % format)
1222
def make_repository(self, relpath, shared=False, format=None):
1223
"""Create a repository on our default transport at relpath."""
1224
made_control = self.make_bzrdir(relpath, format=format)
1225
return made_control.create_repository(shared=shared)
1227
def make_branch_and_memory_tree(self, relpath):
1228
"""Create a branch on the default transport and a MemoryTree for it."""
1229
b = self.make_branch(relpath)
1230
return memorytree.MemoryTree.create_on_branch(b)
1232
def overrideEnvironmentForTesting(self):
1233
os.environ['HOME'] = self.test_home_dir
1234
os.environ['APPDATA'] = self.test_home_dir
1098
1236
def setUp(self):
1099
super(TestCaseInTempDir, self).setUp()
1237
super(TestCaseWithMemoryTransport, self).setUp()
1100
1238
self._make_test_root()
1101
1239
_currentdir = os.getcwdu()
1240
def _leaveDirectory():
1241
os.chdir(_currentdir)
1242
self.addCleanup(_leaveDirectory)
1243
self.makeAndChdirToTestDir()
1244
self.overrideEnvironmentForTesting()
1245
self.__readonly_server = None
1246
self.__server = None
1247
self.transport_server = default_transport
1248
self.transport_readonly_server = None
1251
class TestCaseInTempDir(TestCaseWithMemoryTransport):
1252
"""Derived class that runs a test within a temporary directory.
1254
This is useful for tests that need to create a branch, etc.
1256
The directory is created in a slightly complex way: for each
1257
Python invocation, a new temporary top-level directory is created.
1258
All test cases create their own directory within that. If the
1259
tests complete successfully, the directory is removed.
1261
InTempDir is an old alias for FunctionalTestCase.
1264
OVERRIDE_PYTHON = 'python'
1266
def check_file_contents(self, filename, expect):
1267
self.log("check contents of file %s" % filename)
1268
contents = file(filename, 'r').read()
1269
if contents != expect:
1270
self.log("expected: %r" % expect)
1271
self.log("actually: %r" % contents)
1272
self.fail("contents of %s not as expected" % filename)
1274
def makeAndChdirToTestDir(self):
1275
"""See TestCaseWithMemoryTransport.makeAndChdirToTestDir().
1277
For TestCaseInTempDir we create a temporary directory based on the test
1278
name and then create two subdirs - test and home under it.
1102
1280
# shorten the name, to avoid test failures due to path length
1103
1281
short_id = self.id().replace('bzrlib.tests.', '') \
1104
1282
.replace('__main__.', '')[-100:]
1202
1367
readwrite one must both define get_url() as resolving to os.getcwd().
1205
def __init__(self, methodName='testMethod'):
1206
super(TestCaseWithTransport, self).__init__(methodName)
1207
self.__readonly_server = None
1208
self.__server = None
1209
self.transport_server = default_transport
1210
self.transport_readonly_server = None
1212
def get_readonly_url(self, relpath=None):
1213
"""Get a URL for the readonly transport.
1215
This will either be backed by '.' or a decorator to the transport
1216
used by self.get_url()
1217
relpath provides for clients to get a path relative to the base url.
1218
These should only be downwards relative, not upwards.
1220
base = self.get_readonly_server().get_url()
1221
if relpath is not None:
1222
if not base.endswith('/'):
1224
base = base + relpath
1227
def get_readonly_server(self):
1228
"""Get the server instance for the readonly transport
1230
This is useful for some tests with specific servers to do diagnostics.
1232
if self.__readonly_server is None:
1233
if self.transport_readonly_server is None:
1234
# readonly decorator requested
1235
# bring up the server
1237
self.__readonly_server = ReadonlyServer()
1238
self.__readonly_server.setUp(self.__server)
1240
self.__readonly_server = self.transport_readonly_server()
1241
self.__readonly_server.setUp()
1242
self.addCleanup(self.__readonly_server.tearDown)
1243
return self.__readonly_server
1245
1370
def get_server(self):
1246
"""Get the read/write server instance.
1371
"""See TestCaseWithMemoryTransport.
1248
1373
This is useful for some tests with specific servers that need
1254
1379
self.addCleanup(self.__server.tearDown)
1255
1380
return self.__server
1257
def get_url(self, relpath=None):
1258
"""Get a URL (or maybe a path) for the readwrite transport.
1260
This will either be backed by '.' or to an equivalent non-file based
1262
relpath provides for clients to get a path relative to the base url.
1263
These should only be downwards relative, not upwards.
1265
base = self.get_server().get_url()
1266
if relpath is not None and relpath != '.':
1267
if not base.endswith('/'):
1269
# XXX: Really base should be a url; we did after all call
1270
# get_url()! But sometimes it's just a path (from
1271
# LocalAbspathServer), and it'd be wrong to append urlescaped data
1272
# to a non-escaped local path.
1273
if base.startswith('./') or base.startswith('/'):
1276
base += urlutils.escape(relpath)
1279
def get_transport(self):
1280
"""Return a writeable transport for the test scratch space"""
1281
t = get_transport(self.get_url())
1282
self.assertFalse(t.is_readonly())
1285
def get_readonly_transport(self):
1286
"""Return a readonly transport for the test scratch space
1288
This can be used to test that operations which should only need
1289
readonly access in fact do not try to write.
1291
t = get_transport(self.get_readonly_url())
1292
self.assertTrue(t.is_readonly())
1295
def make_branch(self, relpath, format=None):
1296
"""Create a branch on the transport at relpath."""
1297
repo = self.make_repository(relpath, format=format)
1298
return repo.bzrdir.create_branch()
1300
def make_bzrdir(self, relpath, format=None):
1302
# might be a relative or absolute path
1303
maybe_a_url = self.get_url(relpath)
1304
segments = maybe_a_url.rsplit('/', 1)
1305
t = get_transport(maybe_a_url)
1306
if len(segments) > 1 and segments[-1] not in ('', '.'):
1309
except errors.FileExists:
1312
format = bzrlib.bzrdir.BzrDirFormat.get_default_format()
1313
return format.initialize_on_transport(t)
1314
except errors.UninitializableFormat:
1315
raise TestSkipped("Format %s is not initializable." % format)
1317
def make_repository(self, relpath, shared=False, format=None):
1318
"""Create a repository on our default transport at relpath."""
1319
made_control = self.make_bzrdir(relpath, format=format)
1320
return made_control.create_repository(shared=shared)
1322
def make_branch_and_memory_tree(self, relpath):
1323
"""Create a branch on the default transport and a MemoryTree for it."""
1324
b = self.make_branch(relpath)
1325
return memorytree.MemoryTree.create_on_branch(b)
1327
1382
def make_branch_and_tree(self, relpath, format=None):
1328
1383
"""Create a branch on the transport and a tree locally.