1
# Copyright (C) 2005, 2006 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Tests for the test framework."""
21
from StringIO import StringIO
34
from bzrlib.progress import _BaseProgressBar
35
from bzrlib.tests import (
39
TestCaseWithMemoryTransport,
40
TestCaseWithTransport,
45
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
46
from bzrlib.tests.TestUtil import _load_module_by_name
47
import bzrlib.errors as errors
48
from bzrlib import symbol_versioning
49
from bzrlib.symbol_versioning import zero_ten, zero_eleven
50
from bzrlib.trace import note
51
from bzrlib.transport.memory import MemoryServer, MemoryTransport
52
from bzrlib.version import _get_bzr_source_tree
55
class SelftestTests(TestCase):
57
def test_import_tests(self):
58
mod = _load_module_by_name('bzrlib.tests.test_selftest')
59
self.assertEqual(mod.SelftestTests, SelftestTests)
61
def test_import_test_failure(self):
62
self.assertRaises(ImportError,
66
class MetaTestLog(TestCase):
68
def test_logging(self):
69
"""Test logs are captured when a test fails."""
70
self.log('a test message')
71
self._log_file.flush()
72
self.assertContainsRe(self._get_log(keep_log_file=True),
76
class TestTreeShape(TestCaseInTempDir):
78
def test_unicode_paths(self):
79
filename = u'hell\u00d8'
81
self.build_tree_contents([(filename, 'contents of hello')])
82
except UnicodeEncodeError:
83
raise TestSkipped("can't build unicode working tree in "
84
"filesystem encoding %s" % sys.getfilesystemencoding())
85
self.failUnlessExists(filename)
88
class TestTransportProviderAdapter(TestCase):
89
"""A group of tests that test the transport implementation adaption core.
91
This is a meta test that the tests are applied to all available
94
This will be generalised in the future which is why it is in this
95
test file even though it is specific to transport tests at the moment.
98
def test_get_transport_permutations(self):
99
# this checks that we the module get_test_permutations call
100
# is made by the adapter get_transport_test_permitations method.
101
class MockModule(object):
102
def get_test_permutations(self):
103
return sample_permutation
104
sample_permutation = [(1,2), (3,4)]
105
from bzrlib.transport import TransportTestProviderAdapter
106
adapter = TransportTestProviderAdapter()
107
self.assertEqual(sample_permutation,
108
adapter.get_transport_test_permutations(MockModule()))
110
def test_adapter_checks_all_modules(self):
111
# this checks that the adapter returns as many permurtations as
112
# there are in all the registered# transport modules for there
113
# - we assume if this matches its probably doing the right thing
114
# especially in combination with the tests for setting the right
116
from bzrlib.transport import (TransportTestProviderAdapter,
117
_get_transport_modules
119
modules = _get_transport_modules()
120
permutation_count = 0
121
for module in modules:
123
permutation_count += len(reduce(getattr,
124
(module + ".get_test_permutations").split('.')[1:],
125
__import__(module))())
126
except errors.DependencyNotPresent:
128
input_test = TestTransportProviderAdapter(
129
"test_adapter_sets_transport_class")
130
adapter = TransportTestProviderAdapter()
131
self.assertEqual(permutation_count,
132
len(list(iter(adapter.adapt(input_test)))))
134
def test_adapter_sets_transport_class(self):
135
# Check that the test adapter inserts a transport and server into the
138
# This test used to know about all the possible transports and the
139
# order they were returned but that seems overly brittle (mbp
141
input_test = TestTransportProviderAdapter(
142
"test_adapter_sets_transport_class")
143
from bzrlib.transport import TransportTestProviderAdapter
144
suite = TransportTestProviderAdapter().adapt(input_test)
145
tests = list(iter(suite))
146
self.assertTrue(len(tests) > 6)
147
# there are at least that many builtin transports
149
self.assertTrue(issubclass(one_test.transport_class,
150
bzrlib.transport.Transport))
151
self.assertTrue(issubclass(one_test.transport_server,
152
bzrlib.transport.Server))
155
class TestBranchProviderAdapter(TestCase):
156
"""A group of tests that test the branch implementation test adapter."""
158
def test_adapted_tests(self):
159
# check that constructor parameters are passed through to the adapted
161
from bzrlib.branch import BranchTestProviderAdapter
162
input_test = TestBranchProviderAdapter(
163
"test_adapted_tests")
166
formats = [("c", "C"), ("d", "D")]
167
adapter = BranchTestProviderAdapter(server1, server2, formats)
168
suite = adapter.adapt(input_test)
169
tests = list(iter(suite))
170
self.assertEqual(2, len(tests))
171
self.assertEqual(tests[0].branch_format, formats[0][0])
172
self.assertEqual(tests[0].bzrdir_format, formats[0][1])
173
self.assertEqual(tests[0].transport_server, server1)
174
self.assertEqual(tests[0].transport_readonly_server, server2)
175
self.assertEqual(tests[1].branch_format, formats[1][0])
176
self.assertEqual(tests[1].bzrdir_format, formats[1][1])
177
self.assertEqual(tests[1].transport_server, server1)
178
self.assertEqual(tests[1].transport_readonly_server, server2)
181
class TestBzrDirProviderAdapter(TestCase):
182
"""A group of tests that test the bzr dir implementation test adapter."""
184
def test_adapted_tests(self):
185
# check that constructor parameters are passed through to the adapted
187
from bzrlib.bzrdir import BzrDirTestProviderAdapter
188
input_test = TestBzrDirProviderAdapter(
189
"test_adapted_tests")
194
adapter = BzrDirTestProviderAdapter(vfs_factory,
195
server1, server2, formats)
196
suite = adapter.adapt(input_test)
197
tests = list(iter(suite))
198
self.assertEqual(2, len(tests))
199
self.assertEqual(tests[0].bzrdir_format, formats[0])
200
self.assertEqual(tests[0].vfs_transport_factory, vfs_factory)
201
self.assertEqual(tests[0].transport_server, server1)
202
self.assertEqual(tests[0].transport_readonly_server, server2)
203
self.assertEqual(tests[1].bzrdir_format, formats[1])
204
self.assertEqual(tests[1].vfs_transport_factory, vfs_factory)
205
self.assertEqual(tests[1].transport_server, server1)
206
self.assertEqual(tests[1].transport_readonly_server, server2)
209
class TestRepositoryProviderAdapter(TestCase):
210
"""A group of tests that test the repository implementation test adapter."""
212
def test_adapted_tests(self):
213
# check that constructor parameters are passed through to the adapted
215
from bzrlib.repository import RepositoryTestProviderAdapter
216
input_test = TestRepositoryProviderAdapter(
217
"test_adapted_tests")
220
formats = [("c", "C"), ("d", "D")]
221
adapter = RepositoryTestProviderAdapter(server1, server2, formats)
222
suite = adapter.adapt(input_test)
223
tests = list(iter(suite))
224
self.assertEqual(2, len(tests))
225
self.assertEqual(tests[0].bzrdir_format, formats[0][1])
226
self.assertEqual(tests[0].repository_format, formats[0][0])
227
self.assertEqual(tests[0].transport_server, server1)
228
self.assertEqual(tests[0].transport_readonly_server, server2)
229
self.assertEqual(tests[1].bzrdir_format, formats[1][1])
230
self.assertEqual(tests[1].repository_format, formats[1][0])
231
self.assertEqual(tests[1].transport_server, server1)
232
self.assertEqual(tests[1].transport_readonly_server, server2)
235
class TestInterRepositoryProviderAdapter(TestCase):
236
"""A group of tests that test the InterRepository test adapter."""
238
def test_adapted_tests(self):
239
# check that constructor parameters are passed through to the adapted
241
from bzrlib.repository import InterRepositoryTestProviderAdapter
242
input_test = TestInterRepositoryProviderAdapter(
243
"test_adapted_tests")
246
formats = [(str, "C1", "C2"), (int, "D1", "D2")]
247
adapter = InterRepositoryTestProviderAdapter(server1, server2, formats)
248
suite = adapter.adapt(input_test)
249
tests = list(iter(suite))
250
self.assertEqual(2, len(tests))
251
self.assertEqual(tests[0].interrepo_class, formats[0][0])
252
self.assertEqual(tests[0].repository_format, formats[0][1])
253
self.assertEqual(tests[0].repository_format_to, formats[0][2])
254
self.assertEqual(tests[0].transport_server, server1)
255
self.assertEqual(tests[0].transport_readonly_server, server2)
256
self.assertEqual(tests[1].interrepo_class, formats[1][0])
257
self.assertEqual(tests[1].repository_format, formats[1][1])
258
self.assertEqual(tests[1].repository_format_to, formats[1][2])
259
self.assertEqual(tests[1].transport_server, server1)
260
self.assertEqual(tests[1].transport_readonly_server, server2)
263
class TestInterVersionedFileProviderAdapter(TestCase):
264
"""A group of tests that test the InterVersionedFile test adapter."""
266
def test_adapted_tests(self):
267
# check that constructor parameters are passed through to the adapted
269
from bzrlib.versionedfile import InterVersionedFileTestProviderAdapter
270
input_test = TestInterRepositoryProviderAdapter(
271
"test_adapted_tests")
274
formats = [(str, "C1", "C2"), (int, "D1", "D2")]
275
adapter = InterVersionedFileTestProviderAdapter(server1, server2, formats)
276
suite = adapter.adapt(input_test)
277
tests = list(iter(suite))
278
self.assertEqual(2, len(tests))
279
self.assertEqual(tests[0].interversionedfile_class, formats[0][0])
280
self.assertEqual(tests[0].versionedfile_factory, formats[0][1])
281
self.assertEqual(tests[0].versionedfile_factory_to, formats[0][2])
282
self.assertEqual(tests[0].transport_server, server1)
283
self.assertEqual(tests[0].transport_readonly_server, server2)
284
self.assertEqual(tests[1].interversionedfile_class, formats[1][0])
285
self.assertEqual(tests[1].versionedfile_factory, formats[1][1])
286
self.assertEqual(tests[1].versionedfile_factory_to, formats[1][2])
287
self.assertEqual(tests[1].transport_server, server1)
288
self.assertEqual(tests[1].transport_readonly_server, server2)
291
class TestRevisionStoreProviderAdapter(TestCase):
292
"""A group of tests that test the RevisionStore test adapter."""
294
def test_adapted_tests(self):
295
# check that constructor parameters are passed through to the adapted
297
from bzrlib.store.revision import RevisionStoreTestProviderAdapter
298
input_test = TestRevisionStoreProviderAdapter(
299
"test_adapted_tests")
300
# revision stores need a store factory - i.e. RevisionKnit
301
#, a readonly and rw transport
305
store_factories = ["c", "d"]
306
adapter = RevisionStoreTestProviderAdapter(server1, server2, store_factories)
307
suite = adapter.adapt(input_test)
308
tests = list(iter(suite))
309
self.assertEqual(2, len(tests))
310
self.assertEqual(tests[0].store_factory, store_factories[0][0])
311
self.assertEqual(tests[0].transport_server, server1)
312
self.assertEqual(tests[0].transport_readonly_server, server2)
313
self.assertEqual(tests[1].store_factory, store_factories[1][0])
314
self.assertEqual(tests[1].transport_server, server1)
315
self.assertEqual(tests[1].transport_readonly_server, server2)
318
class TestWorkingTreeProviderAdapter(TestCase):
319
"""A group of tests that test the workingtree implementation test adapter."""
321
def test_adapted_tests(self):
322
# check that constructor parameters are passed through to the adapted
324
from bzrlib.workingtree import WorkingTreeTestProviderAdapter
325
input_test = TestWorkingTreeProviderAdapter(
326
"test_adapted_tests")
329
formats = [("c", "C"), ("d", "D")]
330
adapter = WorkingTreeTestProviderAdapter(server1, server2, formats)
331
suite = adapter.adapt(input_test)
332
tests = list(iter(suite))
333
self.assertEqual(2, len(tests))
334
self.assertEqual(tests[0].workingtree_format, formats[0][0])
335
self.assertEqual(tests[0].bzrdir_format, formats[0][1])
336
self.assertEqual(tests[0].transport_server, server1)
337
self.assertEqual(tests[0].transport_readonly_server, server2)
338
self.assertEqual(tests[1].workingtree_format, formats[1][0])
339
self.assertEqual(tests[1].bzrdir_format, formats[1][1])
340
self.assertEqual(tests[1].transport_server, server1)
341
self.assertEqual(tests[1].transport_readonly_server, server2)
344
class TestTreeProviderAdapter(TestCase):
345
"""Test the setup of tree_implementation tests."""
347
def test_adapted_tests(self):
348
# the tree implementation adapter is meant to setup one instance for
349
# each working tree format, and one additional instance that will
350
# use the default wt format, but create a revision tree for the tests.
351
# this means that the wt ones should have the workingtree_to_test_tree
352
# attribute set to 'return_parameter' and the revision one set to
353
# revision_tree_from_workingtree.
355
from bzrlib.tests.tree_implementations import (
356
TreeTestProviderAdapter,
358
revision_tree_from_workingtree
360
from bzrlib.workingtree import WorkingTreeFormat
361
input_test = TestTreeProviderAdapter(
362
"test_adapted_tests")
365
formats = [("c", "C"), ("d", "D")]
366
adapter = TreeTestProviderAdapter(server1, server2, formats)
367
suite = adapter.adapt(input_test)
368
tests = list(iter(suite))
369
self.assertEqual(3, len(tests))
370
default_format = WorkingTreeFormat.get_default_format()
371
self.assertEqual(tests[0].workingtree_format, formats[0][0])
372
self.assertEqual(tests[0].bzrdir_format, formats[0][1])
373
self.assertEqual(tests[0].transport_server, server1)
374
self.assertEqual(tests[0].transport_readonly_server, server2)
375
self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
376
self.assertEqual(tests[1].workingtree_format, formats[1][0])
377
self.assertEqual(tests[1].bzrdir_format, formats[1][1])
378
self.assertEqual(tests[1].transport_server, server1)
379
self.assertEqual(tests[1].transport_readonly_server, server2)
380
self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
381
self.assertEqual(tests[2].workingtree_format, default_format)
382
self.assertEqual(tests[2].bzrdir_format, default_format._matchingbzrdir)
383
self.assertEqual(tests[2].transport_server, server1)
384
self.assertEqual(tests[2].transport_readonly_server, server2)
385
self.assertEqual(tests[2].workingtree_to_test_tree,
386
revision_tree_from_workingtree)
389
class TestInterTreeProviderAdapter(TestCase):
390
"""A group of tests that test the InterTreeTestAdapter."""
392
def test_adapted_tests(self):
393
# check that constructor parameters are passed through to the adapted
395
# for InterTree tests we want the machinery to bring up two trees in
396
# each instance: the base one, and the one we are interacting with.
397
# because each optimiser can be direction specific, we need to test
398
# each optimiser in its chosen direction.
399
# unlike the TestProviderAdapter we dont want to automatically add a
400
# parameterised one for WorkingTree - the optimisers will tell us what
402
from bzrlib.tests.tree_implementations import (
404
revision_tree_from_workingtree
406
from bzrlib.tests.intertree_implementations import (
407
InterTreeTestProviderAdapter,
409
from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
410
input_test = TestInterTreeProviderAdapter(
411
"test_adapted_tests")
414
format1 = WorkingTreeFormat2()
415
format2 = WorkingTreeFormat3()
416
formats = [(str, format1, format2, False, True),
417
(int, format2, format1, False, True)]
418
adapter = InterTreeTestProviderAdapter(server1, server2, formats)
419
suite = adapter.adapt(input_test)
420
tests = list(iter(suite))
421
self.assertEqual(2, len(tests))
422
self.assertEqual(tests[0].intertree_class, formats[0][0])
423
self.assertEqual(tests[0].workingtree_format, formats[0][1])
424
self.assertEqual(tests[0].workingtree_to_test_tree, formats[0][2])
425
self.assertEqual(tests[0].workingtree_format_to, formats[0][3])
426
self.assertEqual(tests[0].workingtree_to_test_tree_to, formats[0][4])
427
self.assertEqual(tests[0].transport_server, server1)
428
self.assertEqual(tests[0].transport_readonly_server, server2)
429
self.assertEqual(tests[1].intertree_class, formats[1][0])
430
self.assertEqual(tests[1].workingtree_format, formats[1][1])
431
self.assertEqual(tests[1].workingtree_to_test_tree, formats[1][2])
432
self.assertEqual(tests[1].workingtree_format_to, formats[1][3])
433
self.assertEqual(tests[1].workingtree_to_test_tree_to, formats[1][4])
434
self.assertEqual(tests[1].transport_server, server1)
435
self.assertEqual(tests[1].transport_readonly_server, server2)
438
class TestTestCaseInTempDir(TestCaseInTempDir):
440
def test_home_is_not_working(self):
441
self.assertNotEqual(self.test_dir, self.test_home_dir)
442
cwd = osutils.getcwd()
443
self.assertEqual(self.test_dir, cwd)
444
self.assertEqual(self.test_home_dir, os.environ['HOME'])
447
class TestTestCaseWithMemoryTransport(TestCaseWithMemoryTransport):
449
def test_home_is_non_existant_dir_under_root(self):
450
"""The test_home_dir for TestCaseWithMemoryTransport is missing.
452
This is because TestCaseWithMemoryTransport is for tests that do not
453
need any disk resources: they should be hooked into bzrlib in such a
454
way that no global settings are being changed by the test (only a
455
few tests should need to do that), and having a missing dir as home is
456
an effective way to ensure that this is the case.
458
self.assertEqual(self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
460
self.assertEqual(self.test_home_dir, os.environ['HOME'])
462
def test_cwd_is_TEST_ROOT(self):
463
self.assertEqual(self.test_dir, self.TEST_ROOT)
464
cwd = osutils.getcwd()
465
self.assertEqual(self.test_dir, cwd)
467
def test_make_branch_and_memory_tree(self):
468
"""In TestCaseWithMemoryTransport we should not make the branch on disk.
470
This is hard to comprehensively robustly test, so we settle for making
471
a branch and checking no directory was created at its relpath.
473
tree = self.make_branch_and_memory_tree('dir')
474
# Guard against regression into MemoryTransport leaking
475
# files to disk instead of keeping them in memory.
476
self.failIf(osutils.lexists('dir'))
477
self.assertIsInstance(tree, memorytree.MemoryTree)
479
def test_make_branch_and_memory_tree_with_format(self):
480
"""make_branch_and_memory_tree should accept a format option."""
481
format = bzrdir.BzrDirMetaFormat1()
482
format.repository_format = repository.RepositoryFormat7()
483
tree = self.make_branch_and_memory_tree('dir', format=format)
484
# Guard against regression into MemoryTransport leaking
485
# files to disk instead of keeping them in memory.
486
self.failIf(osutils.lexists('dir'))
487
self.assertIsInstance(tree, memorytree.MemoryTree)
488
self.assertEqual(format.repository_format.__class__,
489
tree.branch.repository._format.__class__)
492
class TestTestCaseWithTransport(TestCaseWithTransport):
493
"""Tests for the convenience functions TestCaseWithTransport introduces."""
495
def test_get_readonly_url_none(self):
496
from bzrlib.transport import get_transport
497
from bzrlib.transport.memory import MemoryServer
498
from bzrlib.transport.readonly import ReadonlyTransportDecorator
499
self.vfs_transport_factory = MemoryServer
500
self.transport_readonly_server = None
501
# calling get_readonly_transport() constructs a decorator on the url
503
url = self.get_readonly_url()
504
url2 = self.get_readonly_url('foo/bar')
505
t = get_transport(url)
506
t2 = get_transport(url2)
507
self.failUnless(isinstance(t, ReadonlyTransportDecorator))
508
self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
509
self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
511
def test_get_readonly_url_http(self):
512
from bzrlib.tests.HttpServer import HttpServer
513
from bzrlib.transport import get_transport
514
from bzrlib.transport.local import LocalURLServer
515
from bzrlib.transport.http import HttpTransportBase
516
self.transport_server = LocalURLServer
517
self.transport_readonly_server = HttpServer
518
# calling get_readonly_transport() gives us a HTTP server instance.
519
url = self.get_readonly_url()
520
url2 = self.get_readonly_url('foo/bar')
521
# the transport returned may be any HttpTransportBase subclass
522
t = get_transport(url)
523
t2 = get_transport(url2)
524
self.failUnless(isinstance(t, HttpTransportBase))
525
self.failUnless(isinstance(t2, HttpTransportBase))
526
self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
528
def test_is_directory(self):
529
"""Test assertIsDirectory assertion"""
530
t = self.get_transport()
531
self.build_tree(['a_dir/', 'a_file'], transport=t)
532
self.assertIsDirectory('a_dir', t)
533
self.assertRaises(AssertionError, self.assertIsDirectory, 'a_file', t)
534
self.assertRaises(AssertionError, self.assertIsDirectory, 'not_here', t)
537
class TestTestCaseTransports(TestCaseWithTransport):
540
super(TestTestCaseTransports, self).setUp()
541
self.vfs_transport_factory = MemoryServer
543
def test_make_bzrdir_preserves_transport(self):
544
t = self.get_transport()
545
result_bzrdir = self.make_bzrdir('subdir')
546
self.assertIsInstance(result_bzrdir.transport,
548
# should not be on disk, should only be in memory
549
self.failIfExists('subdir')
552
class TestChrootedTest(ChrootedTestCase):
554
def test_root_is_root(self):
555
from bzrlib.transport import get_transport
556
t = get_transport(self.get_readonly_url())
558
self.assertEqual(url, t.clone('..').base)
561
class MockProgress(_BaseProgressBar):
562
"""Progress-bar standin that records calls.
564
Useful for testing pb using code.
568
_BaseProgressBar.__init__(self)
572
self.calls.append(('tick',))
574
def update(self, msg=None, current=None, total=None):
575
self.calls.append(('update', msg, current, total))
578
self.calls.append(('clear',))
580
def note(self, msg, *args):
581
self.calls.append(('note', msg, args))
584
class TestTestResult(TestCase):
586
def test_elapsed_time_with_benchmarking(self):
587
result = bzrlib.tests.TextTestResult(self._log_file,
591
result._recordTestStartTime()
593
result.extractBenchmarkTime(self)
594
timed_string = result._testTimeString()
595
# without explicit benchmarking, we should get a simple time.
596
self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms$")
597
# if a benchmark time is given, we want a x of y style result.
598
self.time(time.sleep, 0.001)
599
result.extractBenchmarkTime(self)
600
timed_string = result._testTimeString()
601
self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms/ *[ 1-9][0-9]ms$")
602
# extracting the time from a non-bzrlib testcase sets to None
603
result._recordTestStartTime()
604
result.extractBenchmarkTime(
605
unittest.FunctionTestCase(self.test_elapsed_time_with_benchmarking))
606
timed_string = result._testTimeString()
607
self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms$")
608
# cheat. Yes, wash thy mouth out with soap.
609
self._benchtime = None
611
def test_assigned_benchmark_file_stores_date(self):
613
result = bzrlib.tests.TextTestResult(self._log_file,
618
output_string = output.getvalue()
620
# if you are wondering about the regexp please read the comment in
621
# test_bench_history (bzrlib.tests.test_selftest.TestRunner)
622
# XXX: what comment? -- Andrew Bennetts
623
self.assertContainsRe(output_string, "--date [0-9.]+")
625
def test_benchhistory_records_test_times(self):
626
result_stream = StringIO()
627
result = bzrlib.tests.TextTestResult(
631
bench_history=result_stream
634
# we want profile a call and check that its test duration is recorded
635
# make a new test instance that when run will generate a benchmark
636
example_test_case = TestTestResult("_time_hello_world_encoding")
637
# execute the test, which should succeed and record times
638
example_test_case.run(result)
639
lines = result_stream.getvalue().splitlines()
640
self.assertEqual(2, len(lines))
641
self.assertContainsRe(lines[1],
642
" *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
643
"._time_hello_world_encoding")
645
def _time_hello_world_encoding(self):
646
"""Profile two sleep calls
648
This is used to exercise the test framework.
650
self.time(unicode, 'hello', errors='replace')
651
self.time(unicode, 'world', errors='replace')
653
def test_lsprofiling(self):
654
"""Verbose test result prints lsprof statistics from test cases."""
658
raise TestSkipped("lsprof not installed.")
659
result_stream = StringIO()
660
result = bzrlib.tests.VerboseTestResult(
661
unittest._WritelnDecorator(result_stream),
665
# we want profile a call of some sort and check it is output by
666
# addSuccess. We dont care about addError or addFailure as they
667
# are not that interesting for performance tuning.
668
# make a new test instance that when run will generate a profile
669
example_test_case = TestTestResult("_time_hello_world_encoding")
670
example_test_case._gather_lsprof_in_benchmarks = True
671
# execute the test, which should succeed and record profiles
672
example_test_case.run(result)
673
# lsprofile_something()
674
# if this worked we want
675
# LSProf output for <built in function unicode> (['hello'], {'errors': 'replace'})
676
# CallCount Recursive Total(ms) Inline(ms) module:lineno(function)
677
# (the lsprof header)
678
# ... an arbitrary number of lines
679
# and the function call which is time.sleep.
680
# 1 0 ??? ??? ???(sleep)
681
# and then repeated but with 'world', rather than 'hello'.
682
# this should appear in the output stream of our test result.
683
output = result_stream.getvalue()
684
self.assertContainsRe(output,
685
r"LSProf output for <type 'unicode'>\(\('hello',\), {'errors': 'replace'}\)")
686
self.assertContainsRe(output,
687
r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n")
688
self.assertContainsRe(output,
689
r"( +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n)?")
690
self.assertContainsRe(output,
691
r"LSProf output for <type 'unicode'>\(\('world',\), {'errors': 'replace'}\)\n")
694
class TestRunner(TestCase):
696
def dummy_test(self):
699
def run_test_runner(self, testrunner, test):
700
"""Run suite in testrunner, saving global state and restoring it.
702
This current saves and restores:
703
TestCaseInTempDir.TEST_ROOT
705
There should be no tests in this file that use bzrlib.tests.TextTestRunner
706
without using this convenience method, because of our use of global state.
708
old_root = TestCaseInTempDir.TEST_ROOT
710
TestCaseInTempDir.TEST_ROOT = None
711
return testrunner.run(test)
713
TestCaseInTempDir.TEST_ROOT = old_root
715
def test_skipped_test(self):
716
# run a test that is skipped, and check the suite as a whole still
718
# skipping_test must be hidden in here so it's not run as a real test
720
raise TestSkipped('test intentionally skipped')
721
runner = TextTestRunner(stream=self._log_file, keep_output=True)
722
test = unittest.FunctionTestCase(skipping_test)
723
result = self.run_test_runner(runner, test)
724
self.assertTrue(result.wasSuccessful())
726
def test_bench_history(self):
727
# tests that the running the benchmark produces a history file
728
# containing a timestamp and the revision id of the bzrlib source which
730
workingtree = _get_bzr_source_tree()
731
test = TestRunner('dummy_test')
733
runner = TextTestRunner(stream=self._log_file, bench_history=output)
734
result = self.run_test_runner(runner, test)
735
output_string = output.getvalue()
736
self.assertContainsRe(output_string, "--date [0-9.]+")
737
if workingtree is not None:
738
revision_id = workingtree.get_parent_ids()[0]
739
self.assertEndsWith(output_string.rstrip(), revision_id)
741
def test_success_log_deleted(self):
742
"""Successful tests have their log deleted"""
744
class LogTester(TestCase):
746
def test_success(self):
747
self.log('this will be removed\n')
749
sio = cStringIO.StringIO()
750
runner = TextTestRunner(stream=sio)
751
test = LogTester('test_success')
752
result = self.run_test_runner(runner, test)
754
log = test._get_log()
755
self.assertEqual("DELETED log file to reduce memory footprint", log)
756
self.assertEqual('', test._log_contents)
757
self.assertIs(None, test._log_file_name)
759
def test_fail_log_kept(self):
760
"""Failed tests have their log kept"""
762
class LogTester(TestCase):
765
self.log('this will be kept\n')
766
self.fail('this test fails')
768
sio = cStringIO.StringIO()
769
runner = TextTestRunner(stream=sio)
770
test = LogTester('test_fail')
771
result = self.run_test_runner(runner, test)
773
text = sio.getvalue()
774
self.assertContainsRe(text, 'this will be kept')
775
self.assertContainsRe(text, 'this test fails')
777
log = test._get_log()
778
self.assertContainsRe(log, 'this will be kept')
779
self.assertEqual(log, test._log_contents)
781
def test_error_log_kept(self):
782
"""Tests with errors have their log kept"""
784
class LogTester(TestCase):
786
def test_error(self):
787
self.log('this will be kept\n')
788
raise ValueError('random exception raised')
790
sio = cStringIO.StringIO()
791
runner = TextTestRunner(stream=sio)
792
test = LogTester('test_error')
793
result = self.run_test_runner(runner, test)
795
text = sio.getvalue()
796
self.assertContainsRe(text, 'this will be kept')
797
self.assertContainsRe(text, 'random exception raised')
799
log = test._get_log()
800
self.assertContainsRe(log, 'this will be kept')
801
self.assertEqual(log, test._log_contents)
804
class TestTestCase(TestCase):
805
"""Tests that test the core bzrlib TestCase."""
807
def inner_test(self):
808
# the inner child test
811
def outer_child(self):
812
# the outer child test
814
self.inner_test = TestTestCase("inner_child")
815
result = bzrlib.tests.TextTestResult(self._log_file,
818
self.inner_test.run(result)
821
def test_trace_nesting(self):
822
# this tests that each test case nests its trace facility correctly.
823
# we do this by running a test case manually. That test case (A)
824
# should setup a new log, log content to it, setup a child case (B),
825
# which should log independently, then case (A) should log a trailer
827
# we do two nested children so that we can verify the state of the
828
# logs after the outer child finishes is correct, which a bad clean
829
# up routine in tearDown might trigger a fault in our test with only
830
# one child, we should instead see the bad result inside our test with
832
# the outer child test
833
original_trace = bzrlib.trace._trace_file
834
outer_test = TestTestCase("outer_child")
835
result = bzrlib.tests.TextTestResult(self._log_file,
838
outer_test.run(result)
839
self.assertEqual(original_trace, bzrlib.trace._trace_file)
841
def method_that_times_a_bit_twice(self):
842
# call self.time twice to ensure it aggregates
843
self.time(time.sleep, 0.007)
844
self.time(time.sleep, 0.007)
846
def test_time_creates_benchmark_in_result(self):
847
"""Test that the TestCase.time() method accumulates a benchmark time."""
848
sample_test = TestTestCase("method_that_times_a_bit_twice")
849
output_stream = StringIO()
850
result = bzrlib.tests.VerboseTestResult(
851
unittest._WritelnDecorator(output_stream),
854
num_tests=sample_test.countTestCases())
855
sample_test.run(result)
856
self.assertContainsRe(
857
output_stream.getvalue(),
860
def test_hooks_sanitised(self):
861
"""The bzrlib hooks should be sanitised by setUp."""
862
self.assertEqual(bzrlib.branch.BranchHooks(),
863
bzrlib.branch.Branch.hooks)
865
def test__gather_lsprof_in_benchmarks(self):
866
"""When _gather_lsprof_in_benchmarks is on, accumulate profile data.
868
Each self.time() call is individually and separately profiled.
873
raise TestSkipped("lsprof not installed.")
874
# overrides the class member with an instance member so no cleanup
876
self._gather_lsprof_in_benchmarks = True
877
self.time(time.sleep, 0.000)
878
self.time(time.sleep, 0.003)
879
self.assertEqual(2, len(self._benchcalls))
880
self.assertEqual((time.sleep, (0.000,), {}), self._benchcalls[0][0])
881
self.assertEqual((time.sleep, (0.003,), {}), self._benchcalls[1][0])
882
self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
883
self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)
886
@symbol_versioning.deprecated_function(zero_eleven)
887
def sample_deprecated_function():
888
"""A deprecated function to test applyDeprecated with."""
892
def sample_undeprecated_function(a_param):
893
"""A undeprecated function to test applyDeprecated with."""
896
class ApplyDeprecatedHelper(object):
897
"""A helper class for ApplyDeprecated tests."""
899
@symbol_versioning.deprecated_method(zero_eleven)
900
def sample_deprecated_method(self, param_one):
901
"""A deprecated method for testing with."""
904
def sample_normal_method(self):
905
"""A undeprecated method."""
907
@symbol_versioning.deprecated_method(zero_ten)
908
def sample_nested_deprecation(self):
909
return sample_deprecated_function()
912
class TestExtraAssertions(TestCase):
913
"""Tests for new test assertions in bzrlib test suite"""
915
def test_assert_isinstance(self):
916
self.assertIsInstance(2, int)
917
self.assertIsInstance(u'', basestring)
918
self.assertRaises(AssertionError, self.assertIsInstance, None, int)
919
self.assertRaises(AssertionError, self.assertIsInstance, 23.3, int)
921
def test_assertEndsWith(self):
922
self.assertEndsWith('foo', 'oo')
923
self.assertRaises(AssertionError, self.assertEndsWith, 'o', 'oo')
925
def test_applyDeprecated_not_deprecated(self):
926
sample_object = ApplyDeprecatedHelper()
927
# calling an undeprecated callable raises an assertion
928
self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
929
sample_object.sample_normal_method)
930
self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
931
sample_undeprecated_function, "a param value")
932
# calling a deprecated callable (function or method) with the wrong
933
# expected deprecation fails.
934
self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
935
sample_object.sample_deprecated_method, "a param value")
936
self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
937
sample_deprecated_function)
938
# calling a deprecated callable (function or method) with the right
939
# expected deprecation returns the functions result.
940
self.assertEqual("a param value", self.applyDeprecated(zero_eleven,
941
sample_object.sample_deprecated_method, "a param value"))
942
self.assertEqual(2, self.applyDeprecated(zero_eleven,
943
sample_deprecated_function))
944
# calling a nested deprecation with the wrong deprecation version
945
# fails even if a deeper nested function was deprecated with the
947
self.assertRaises(AssertionError, self.applyDeprecated,
948
zero_eleven, sample_object.sample_nested_deprecation)
949
# calling a nested deprecation with the right deprecation value
950
# returns the calls result.
951
self.assertEqual(2, self.applyDeprecated(zero_ten,
952
sample_object.sample_nested_deprecation))
954
def test_callDeprecated(self):
955
def testfunc(be_deprecated, result=None):
956
if be_deprecated is True:
957
symbol_versioning.warn('i am deprecated', DeprecationWarning,
960
result = self.callDeprecated(['i am deprecated'], testfunc, True)
961
self.assertIs(None, result)
962
result = self.callDeprecated([], testfunc, False, 'result')
963
self.assertEqual('result', result)
964
self.callDeprecated(['i am deprecated'], testfunc, be_deprecated=True)
965
self.callDeprecated([], testfunc, be_deprecated=False)
968
class TestConvenienceMakers(TestCaseWithTransport):
969
"""Test for the make_* convenience functions."""
971
def test_make_branch_and_tree_with_format(self):
972
# we should be able to supply a format to make_branch_and_tree
973
self.make_branch_and_tree('a', format=bzrlib.bzrdir.BzrDirMetaFormat1())
974
self.make_branch_and_tree('b', format=bzrlib.bzrdir.BzrDirFormat6())
975
self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('a')._format,
976
bzrlib.bzrdir.BzrDirMetaFormat1)
977
self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
978
bzrlib.bzrdir.BzrDirFormat6)
980
def test_make_branch_and_memory_tree(self):
981
# we should be able to get a new branch and a mutable tree from
982
# TestCaseWithTransport
983
tree = self.make_branch_and_memory_tree('a')
984
self.assertIsInstance(tree, bzrlib.memorytree.MemoryTree)
987
class TestSFTPMakeBranchAndTree(TestCaseWithSFTPServer):
989
def test_make_tree_for_sftp_branch(self):
990
"""Transports backed by local directories create local trees."""
992
tree = self.make_branch_and_tree('t1')
993
base = tree.bzrdir.root_transport.base
994
self.failIf(base.startswith('sftp'),
995
'base %r is on sftp but should be local' % base)
996
self.assertEquals(tree.bzrdir.root_transport,
997
tree.branch.bzrdir.root_transport)
998
self.assertEquals(tree.bzrdir.root_transport,
999
tree.branch.repository.bzrdir.root_transport)
1002
class TestSelftest(TestCase):
1003
"""Tests of bzrlib.tests.selftest."""
1005
def test_selftest_benchmark_parameter_invokes_test_suite__benchmark__(self):
1008
factory_called.append(True)
1012
self.apply_redirected(out, err, None, bzrlib.tests.selftest,
1013
test_suite_factory=factory)
1014
self.assertEqual([True], factory_called)
1017
class TestSelftestCleanOutput(TestCaseInTempDir):
1019
def test_clean_output(self):
1020
# test functionality of clean_selftest_output()
1021
from bzrlib.tests import clean_selftest_output
1023
dirs = ('test0000.tmp', 'test0001.tmp', 'bzrlib', 'tests')
1024
files = ('bzr', 'setup.py', 'test9999.tmp')
1029
f.write('content of ')
1034
before = os.listdir(root)
1036
self.assertEquals(['bzr','bzrlib','setup.py',
1037
'test0000.tmp','test0001.tmp',
1038
'test9999.tmp','tests'],
1040
clean_selftest_output(root, quiet=True)
1041
after = os.listdir(root)
1043
self.assertEquals(['bzr','bzrlib','setup.py',
1044
'test9999.tmp','tests'],