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
27
from bzrlib import osutils
29
from bzrlib.progress import _BaseProgressBar
30
from bzrlib.tests import (
34
TestCaseWithTransport,
39
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
40
from bzrlib.tests.TestUtil import _load_module_by_name
41
import bzrlib.errors as errors
42
from bzrlib import symbol_versioning
43
from bzrlib.symbol_versioning import zero_ten, zero_eleven
44
from bzrlib.trace import note
45
from bzrlib.transport.memory import MemoryServer, MemoryTransport
46
from bzrlib.version import _get_bzr_source_tree
49
class SelftestTests(TestCase):
51
def test_import_tests(self):
52
mod = _load_module_by_name('bzrlib.tests.test_selftest')
53
self.assertEqual(mod.SelftestTests, SelftestTests)
55
def test_import_test_failure(self):
56
self.assertRaises(ImportError,
61
class MetaTestLog(TestCase):
63
def test_logging(self):
64
"""Test logs are captured when a test fails."""
65
self.log('a test message')
66
self._log_file.flush()
67
self.assertContainsRe(self._get_log(keep_log_file=True),
71
class TestTreeShape(TestCaseInTempDir):
73
def test_unicode_paths(self):
74
filename = u'hell\u00d8'
76
self.build_tree_contents([(filename, 'contents of hello')])
77
except UnicodeEncodeError:
78
raise TestSkipped("can't build unicode working tree in "
79
"filesystem encoding %s" % sys.getfilesystemencoding())
80
self.failUnlessExists(filename)
83
class TestTransportProviderAdapter(TestCase):
84
"""A group of tests that test the transport implementation adaption core.
86
This is a meta test that the tests are applied to all available
89
This will be generalised in the future which is why it is in this
90
test file even though it is specific to transport tests at the moment.
93
def test_get_transport_permutations(self):
94
# this checks that we the module get_test_permutations call
95
# is made by the adapter get_transport_test_permitations method.
96
class MockModule(object):
97
def get_test_permutations(self):
98
return sample_permutation
99
sample_permutation = [(1,2), (3,4)]
100
from bzrlib.transport import TransportTestProviderAdapter
101
adapter = TransportTestProviderAdapter()
102
self.assertEqual(sample_permutation,
103
adapter.get_transport_test_permutations(MockModule()))
105
def test_adapter_checks_all_modules(self):
106
# this checks that the adapter returns as many permurtations as
107
# there are in all the registered# transport modules for there
108
# - we assume if this matches its probably doing the right thing
109
# especially in combination with the tests for setting the right
111
from bzrlib.transport import (TransportTestProviderAdapter,
112
_get_transport_modules
114
modules = _get_transport_modules()
115
permutation_count = 0
116
for module in modules:
118
permutation_count += len(reduce(getattr,
119
(module + ".get_test_permutations").split('.')[1:],
120
__import__(module))())
121
except errors.DependencyNotPresent:
123
input_test = TestTransportProviderAdapter(
124
"test_adapter_sets_transport_class")
125
adapter = TransportTestProviderAdapter()
126
self.assertEqual(permutation_count,
127
len(list(iter(adapter.adapt(input_test)))))
129
def test_adapter_sets_transport_class(self):
130
# Check that the test adapter inserts a transport and server into the
133
# This test used to know about all the possible transports and the
134
# order they were returned but that seems overly brittle (mbp
136
input_test = TestTransportProviderAdapter(
137
"test_adapter_sets_transport_class")
138
from bzrlib.transport import TransportTestProviderAdapter
139
suite = TransportTestProviderAdapter().adapt(input_test)
140
tests = list(iter(suite))
141
self.assertTrue(len(tests) > 6)
142
# there are at least that many builtin transports
144
self.assertTrue(issubclass(one_test.transport_class,
145
bzrlib.transport.Transport))
146
self.assertTrue(issubclass(one_test.transport_server,
147
bzrlib.transport.Server))
150
class TestBranchProviderAdapter(TestCase):
151
"""A group of tests that test the branch implementation test adapter."""
153
def test_adapted_tests(self):
154
# check that constructor parameters are passed through to the adapted
156
from bzrlib.branch import BranchTestProviderAdapter
157
input_test = TestBranchProviderAdapter(
158
"test_adapted_tests")
161
formats = [("c", "C"), ("d", "D")]
162
adapter = BranchTestProviderAdapter(server1, server2, formats)
163
suite = adapter.adapt(input_test)
164
tests = list(iter(suite))
165
self.assertEqual(2, len(tests))
166
self.assertEqual(tests[0].branch_format, formats[0][0])
167
self.assertEqual(tests[0].bzrdir_format, formats[0][1])
168
self.assertEqual(tests[0].transport_server, server1)
169
self.assertEqual(tests[0].transport_readonly_server, server2)
170
self.assertEqual(tests[1].branch_format, formats[1][0])
171
self.assertEqual(tests[1].bzrdir_format, formats[1][1])
172
self.assertEqual(tests[1].transport_server, server1)
173
self.assertEqual(tests[1].transport_readonly_server, server2)
176
class TestBzrDirProviderAdapter(TestCase):
177
"""A group of tests that test the bzr dir implementation test adapter."""
179
def test_adapted_tests(self):
180
# check that constructor parameters are passed through to the adapted
182
from bzrlib.bzrdir import BzrDirTestProviderAdapter
183
input_test = TestBzrDirProviderAdapter(
184
"test_adapted_tests")
188
adapter = BzrDirTestProviderAdapter(server1, server2, formats)
189
suite = adapter.adapt(input_test)
190
tests = list(iter(suite))
191
self.assertEqual(2, len(tests))
192
self.assertEqual(tests[0].bzrdir_format, formats[0])
193
self.assertEqual(tests[0].transport_server, server1)
194
self.assertEqual(tests[0].transport_readonly_server, server2)
195
self.assertEqual(tests[1].bzrdir_format, formats[1])
196
self.assertEqual(tests[1].transport_server, server1)
197
self.assertEqual(tests[1].transport_readonly_server, server2)
200
class TestRepositoryProviderAdapter(TestCase):
201
"""A group of tests that test the repository implementation test adapter."""
203
def test_adapted_tests(self):
204
# check that constructor parameters are passed through to the adapted
206
from bzrlib.repository import RepositoryTestProviderAdapter
207
input_test = TestRepositoryProviderAdapter(
208
"test_adapted_tests")
211
formats = [("c", "C"), ("d", "D")]
212
adapter = RepositoryTestProviderAdapter(server1, server2, formats)
213
suite = adapter.adapt(input_test)
214
tests = list(iter(suite))
215
self.assertEqual(2, len(tests))
216
self.assertEqual(tests[0].bzrdir_format, formats[0][1])
217
self.assertEqual(tests[0].repository_format, formats[0][0])
218
self.assertEqual(tests[0].transport_server, server1)
219
self.assertEqual(tests[0].transport_readonly_server, server2)
220
self.assertEqual(tests[1].bzrdir_format, formats[1][1])
221
self.assertEqual(tests[1].repository_format, formats[1][0])
222
self.assertEqual(tests[1].transport_server, server1)
223
self.assertEqual(tests[1].transport_readonly_server, server2)
226
class TestInterRepositoryProviderAdapter(TestCase):
227
"""A group of tests that test the InterRepository test adapter."""
229
def test_adapted_tests(self):
230
# check that constructor parameters are passed through to the adapted
232
from bzrlib.repository import InterRepositoryTestProviderAdapter
233
input_test = TestInterRepositoryProviderAdapter(
234
"test_adapted_tests")
237
formats = [(str, "C1", "C2"), (int, "D1", "D2")]
238
adapter = InterRepositoryTestProviderAdapter(server1, server2, formats)
239
suite = adapter.adapt(input_test)
240
tests = list(iter(suite))
241
self.assertEqual(2, len(tests))
242
self.assertEqual(tests[0].interrepo_class, formats[0][0])
243
self.assertEqual(tests[0].repository_format, formats[0][1])
244
self.assertEqual(tests[0].repository_format_to, formats[0][2])
245
self.assertEqual(tests[0].transport_server, server1)
246
self.assertEqual(tests[0].transport_readonly_server, server2)
247
self.assertEqual(tests[1].interrepo_class, formats[1][0])
248
self.assertEqual(tests[1].repository_format, formats[1][1])
249
self.assertEqual(tests[1].repository_format_to, formats[1][2])
250
self.assertEqual(tests[1].transport_server, server1)
251
self.assertEqual(tests[1].transport_readonly_server, server2)
254
class TestInterVersionedFileProviderAdapter(TestCase):
255
"""A group of tests that test the InterVersionedFile test adapter."""
257
def test_adapted_tests(self):
258
# check that constructor parameters are passed through to the adapted
260
from bzrlib.versionedfile import InterVersionedFileTestProviderAdapter
261
input_test = TestInterRepositoryProviderAdapter(
262
"test_adapted_tests")
265
formats = [(str, "C1", "C2"), (int, "D1", "D2")]
266
adapter = InterVersionedFileTestProviderAdapter(server1, server2, formats)
267
suite = adapter.adapt(input_test)
268
tests = list(iter(suite))
269
self.assertEqual(2, len(tests))
270
self.assertEqual(tests[0].interversionedfile_class, formats[0][0])
271
self.assertEqual(tests[0].versionedfile_factory, formats[0][1])
272
self.assertEqual(tests[0].versionedfile_factory_to, formats[0][2])
273
self.assertEqual(tests[0].transport_server, server1)
274
self.assertEqual(tests[0].transport_readonly_server, server2)
275
self.assertEqual(tests[1].interversionedfile_class, formats[1][0])
276
self.assertEqual(tests[1].versionedfile_factory, formats[1][1])
277
self.assertEqual(tests[1].versionedfile_factory_to, formats[1][2])
278
self.assertEqual(tests[1].transport_server, server1)
279
self.assertEqual(tests[1].transport_readonly_server, server2)
282
class TestRevisionStoreProviderAdapter(TestCase):
283
"""A group of tests that test the RevisionStore test adapter."""
285
def test_adapted_tests(self):
286
# check that constructor parameters are passed through to the adapted
288
from bzrlib.store.revision import RevisionStoreTestProviderAdapter
289
input_test = TestRevisionStoreProviderAdapter(
290
"test_adapted_tests")
291
# revision stores need a store factory - i.e. RevisionKnit
292
#, a readonly and rw transport
296
store_factories = ["c", "d"]
297
adapter = RevisionStoreTestProviderAdapter(server1, server2, store_factories)
298
suite = adapter.adapt(input_test)
299
tests = list(iter(suite))
300
self.assertEqual(2, len(tests))
301
self.assertEqual(tests[0].store_factory, store_factories[0][0])
302
self.assertEqual(tests[0].transport_server, server1)
303
self.assertEqual(tests[0].transport_readonly_server, server2)
304
self.assertEqual(tests[1].store_factory, store_factories[1][0])
305
self.assertEqual(tests[1].transport_server, server1)
306
self.assertEqual(tests[1].transport_readonly_server, server2)
309
class TestWorkingTreeProviderAdapter(TestCase):
310
"""A group of tests that test the workingtree implementation test adapter."""
312
def test_adapted_tests(self):
313
# check that constructor parameters are passed through to the adapted
315
from bzrlib.workingtree import WorkingTreeTestProviderAdapter
316
input_test = TestWorkingTreeProviderAdapter(
317
"test_adapted_tests")
320
formats = [("c", "C"), ("d", "D")]
321
adapter = WorkingTreeTestProviderAdapter(server1, server2, formats)
322
suite = adapter.adapt(input_test)
323
tests = list(iter(suite))
324
self.assertEqual(2, len(tests))
325
self.assertEqual(tests[0].workingtree_format, formats[0][0])
326
self.assertEqual(tests[0].bzrdir_format, formats[0][1])
327
self.assertEqual(tests[0].transport_server, server1)
328
self.assertEqual(tests[0].transport_readonly_server, server2)
329
self.assertEqual(tests[1].workingtree_format, formats[1][0])
330
self.assertEqual(tests[1].bzrdir_format, formats[1][1])
331
self.assertEqual(tests[1].transport_server, server1)
332
self.assertEqual(tests[1].transport_readonly_server, server2)
335
class TestTreeProviderAdapter(TestCase):
336
"""Test the setup of tree_implementation tests."""
338
def test_adapted_tests(self):
339
# the tree implementation adapter is meant to setup one instance for
340
# each working tree format, and one additional instance that will
341
# use the default wt format, but create a revision tree for the tests.
342
# this means that the wt ones should have the workingtree_to_test_tree
343
# attribute set to 'return_parameter' and the revision one set to
344
# revision_tree_from_workingtree.
346
from bzrlib.tests.tree_implementations import (
347
TreeTestProviderAdapter,
349
revision_tree_from_workingtree
351
from bzrlib.workingtree import WorkingTreeFormat
352
input_test = TestTreeProviderAdapter(
353
"test_adapted_tests")
356
formats = [("c", "C"), ("d", "D")]
357
adapter = TreeTestProviderAdapter(server1, server2, formats)
358
suite = adapter.adapt(input_test)
359
tests = list(iter(suite))
360
self.assertEqual(3, len(tests))
361
default_format = WorkingTreeFormat.get_default_format()
362
self.assertEqual(tests[0].workingtree_format, formats[0][0])
363
self.assertEqual(tests[0].bzrdir_format, formats[0][1])
364
self.assertEqual(tests[0].transport_server, server1)
365
self.assertEqual(tests[0].transport_readonly_server, server2)
366
self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
367
self.assertEqual(tests[1].workingtree_format, formats[1][0])
368
self.assertEqual(tests[1].bzrdir_format, formats[1][1])
369
self.assertEqual(tests[1].transport_server, server1)
370
self.assertEqual(tests[1].transport_readonly_server, server2)
371
self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
372
self.assertEqual(tests[2].workingtree_format, default_format)
373
self.assertEqual(tests[2].bzrdir_format, default_format._matchingbzrdir)
374
self.assertEqual(tests[2].transport_server, server1)
375
self.assertEqual(tests[2].transport_readonly_server, server2)
376
self.assertEqual(tests[2].workingtree_to_test_tree,
377
revision_tree_from_workingtree)
380
class TestInterTreeProviderAdapter(TestCase):
381
"""A group of tests that test the InterTreeTestAdapter."""
383
def test_adapted_tests(self):
384
# check that constructor parameters are passed through to the adapted
386
# for InterTree tests we want the machinery to bring up two trees in
387
# each instance: the base one, and the one we are interacting with.
388
# because each optimiser can be direction specific, we need to test
389
# each optimiser in its chosen direction.
390
# unlike the TestProviderAdapter we dont want to automatically add a
391
# parameterised one for WorkingTree - the optimisers will tell us what
393
from bzrlib.tests.tree_implementations import (
395
revision_tree_from_workingtree
397
from bzrlib.tests.intertree_implementations import (
398
InterTreeTestProviderAdapter,
400
from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
401
input_test = TestInterTreeProviderAdapter(
402
"test_adapted_tests")
405
format1 = WorkingTreeFormat2()
406
format2 = WorkingTreeFormat3()
407
formats = [(str, format1, format2, False, True),
408
(int, format2, format1, False, True)]
409
adapter = InterTreeTestProviderAdapter(server1, server2, formats)
410
suite = adapter.adapt(input_test)
411
tests = list(iter(suite))
412
self.assertEqual(2, len(tests))
413
self.assertEqual(tests[0].intertree_class, formats[0][0])
414
self.assertEqual(tests[0].workingtree_format, formats[0][1])
415
self.assertEqual(tests[0].workingtree_to_test_tree, formats[0][2])
416
self.assertEqual(tests[0].workingtree_format_to, formats[0][3])
417
self.assertEqual(tests[0].workingtree_to_test_tree_to, formats[0][4])
418
self.assertEqual(tests[0].transport_server, server1)
419
self.assertEqual(tests[0].transport_readonly_server, server2)
420
self.assertEqual(tests[1].intertree_class, formats[1][0])
421
self.assertEqual(tests[1].workingtree_format, formats[1][1])
422
self.assertEqual(tests[1].workingtree_to_test_tree, formats[1][2])
423
self.assertEqual(tests[1].workingtree_format_to, formats[1][3])
424
self.assertEqual(tests[1].workingtree_to_test_tree_to, formats[1][4])
425
self.assertEqual(tests[1].transport_server, server1)
426
self.assertEqual(tests[1].transport_readonly_server, server2)
429
class TestTestCaseInTempDir(TestCaseInTempDir):
431
def test_home_is_not_working(self):
432
self.assertNotEqual(self.test_dir, self.test_home_dir)
433
cwd = osutils.getcwd()
434
self.assertEqual(self.test_dir, cwd)
435
self.assertEqual(self.test_home_dir, os.environ['HOME'])
438
class TestTestCaseWithTransport(TestCaseWithTransport):
439
"""Tests for the convenience functions TestCaseWithTransport introduces."""
441
def test_get_readonly_url_none(self):
442
from bzrlib.transport import get_transport
443
from bzrlib.transport.memory import MemoryServer
444
from bzrlib.transport.readonly import ReadonlyTransportDecorator
445
self.transport_server = MemoryServer
446
self.transport_readonly_server = None
447
# calling get_readonly_transport() constructs a decorator on the url
449
url = self.get_readonly_url()
450
url2 = self.get_readonly_url('foo/bar')
451
t = get_transport(url)
452
t2 = get_transport(url2)
453
self.failUnless(isinstance(t, ReadonlyTransportDecorator))
454
self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
455
self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
457
def test_get_readonly_url_http(self):
458
from bzrlib.transport import get_transport
459
from bzrlib.transport.local import LocalRelpathServer
460
from bzrlib.transport.http import HttpServer, HttpTransportBase
461
self.transport_server = LocalRelpathServer
462
self.transport_readonly_server = HttpServer
463
# calling get_readonly_transport() gives us a HTTP server instance.
464
url = self.get_readonly_url()
465
url2 = self.get_readonly_url('foo/bar')
466
# the transport returned may be any HttpTransportBase subclass
467
t = get_transport(url)
468
t2 = get_transport(url2)
469
self.failUnless(isinstance(t, HttpTransportBase))
470
self.failUnless(isinstance(t2, HttpTransportBase))
471
self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
473
def test_is_directory(self):
474
"""Test assertIsDirectory assertion"""
475
t = self.get_transport()
476
self.build_tree(['a_dir/', 'a_file'], transport=t)
477
self.assertIsDirectory('a_dir', t)
478
self.assertRaises(AssertionError, self.assertIsDirectory, 'a_file', t)
479
self.assertRaises(AssertionError, self.assertIsDirectory, 'not_here', t)
482
class TestTestCaseTransports(TestCaseWithTransport):
485
super(TestTestCaseTransports, self).setUp()
486
self.transport_server = MemoryServer
488
def test_make_bzrdir_preserves_transport(self):
489
t = self.get_transport()
490
result_bzrdir = self.make_bzrdir('subdir')
491
self.assertIsInstance(result_bzrdir.transport,
493
# should not be on disk, should only be in memory
494
self.failIfExists('subdir')
497
class TestChrootedTest(ChrootedTestCase):
499
def test_root_is_root(self):
500
from bzrlib.transport import get_transport
501
t = get_transport(self.get_readonly_url())
503
self.assertEqual(url, t.clone('..').base)
506
class MockProgress(_BaseProgressBar):
507
"""Progress-bar standin that records calls.
509
Useful for testing pb using code.
513
_BaseProgressBar.__init__(self)
517
self.calls.append(('tick',))
519
def update(self, msg=None, current=None, total=None):
520
self.calls.append(('update', msg, current, total))
523
self.calls.append(('clear',))
525
def note(self, msg, *args):
526
self.calls.append(('note', msg, args))
529
class TestTestResult(TestCase):
531
def test_progress_bar_style_quiet(self):
532
# test using a progress bar.
533
dummy_test = TestTestResult('test_progress_bar_style_quiet')
534
dummy_error = (Exception, None, [])
535
mypb = MockProgress()
536
mypb.update('Running tests', 0, 4)
537
last_calls = mypb.calls[:]
539
result = bzrlib.tests._MyResult(self._log_file,
543
self.assertEqual(last_calls, mypb.calls)
546
"""Shorten a string based on the terminal width"""
547
return result._ellipsise_unimportant_words(s,
548
osutils.terminal_width())
551
result.startTest(dummy_test)
552
# starting a test prints the test name
553
last_calls += [('update', '...tyle_quiet', 0, None)]
554
self.assertEqual(last_calls, mypb.calls)
555
result.addError(dummy_test, dummy_error)
556
last_calls += [('update', 'ERROR ', 1, None),
557
('note', shorten(dummy_test.id() + ': ERROR'), ())
559
self.assertEqual(last_calls, mypb.calls)
562
result.startTest(dummy_test)
563
last_calls += [('update', '...tyle_quiet', 1, None)]
564
self.assertEqual(last_calls, mypb.calls)
565
last_calls += [('update', 'FAIL ', 2, None),
566
('note', shorten(dummy_test.id() + ': FAIL'), ())
568
result.addFailure(dummy_test, dummy_error)
569
self.assertEqual(last_calls, mypb.calls)
572
result.startTest(dummy_test)
573
last_calls += [('update', '...tyle_quiet', 2, None)]
574
self.assertEqual(last_calls, mypb.calls)
575
result.addSuccess(dummy_test)
576
last_calls += [('update', 'OK ', 3, None)]
577
self.assertEqual(last_calls, mypb.calls)
580
result.startTest(dummy_test)
581
last_calls += [('update', '...tyle_quiet', 3, None)]
582
self.assertEqual(last_calls, mypb.calls)
583
result.addSkipped(dummy_test, dummy_error)
584
last_calls += [('update', 'SKIP ', 4, None)]
585
self.assertEqual(last_calls, mypb.calls)
587
def test_elapsed_time_with_benchmarking(self):
588
result = bzrlib.tests._MyResult(self._log_file,
592
result._recordTestStartTime()
594
result.extractBenchmarkTime(self)
595
timed_string = result._testTimeString()
596
# without explicit benchmarking, we should get a simple time.
597
self.assertContainsRe(timed_string, "^ [ 1-9][0-9]ms$")
598
# if a benchmark time is given, we want a x of y style result.
599
self.time(time.sleep, 0.001)
600
result.extractBenchmarkTime(self)
601
timed_string = result._testTimeString()
602
self.assertContainsRe(timed_string, "^ [ 1-9][0-9]ms/ [ 1-9][0-9]ms$")
603
# extracting the time from a non-bzrlib testcase sets to None
604
result._recordTestStartTime()
605
result.extractBenchmarkTime(
606
unittest.FunctionTestCase(self.test_elapsed_time_with_benchmarking))
607
timed_string = result._testTimeString()
608
self.assertContainsRe(timed_string, "^ [ 1-9][0-9]ms$")
609
# cheat. Yes, wash thy mouth out with soap.
610
self._benchtime = None
612
def test_assigned_benchmark_file_stores_date(self):
614
result = bzrlib.tests._MyResult(self._log_file,
619
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._MyResult(
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._MyResult(
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_accepts_and_uses_pb_parameter(self):
716
test = TestRunner('dummy_test')
717
mypb = MockProgress()
718
self.assertEqual([], mypb.calls)
719
runner = TextTestRunner(stream=self._log_file, pb=mypb)
720
result = self.run_test_runner(runner, test)
721
self.assertEqual(1, result.testsRun)
722
self.assertEqual(('update', 'Running tests', 0, 1), mypb.calls[0])
723
self.assertEqual(('update', '...dummy_test', 0, None), mypb.calls[1])
724
self.assertEqual(('update', 'OK ', 1, None), mypb.calls[2])
725
self.assertEqual(('update', 'Cleaning up', 0, 1), mypb.calls[3])
726
self.assertEqual(('clear',), mypb.calls[4])
727
self.assertEqual(5, len(mypb.calls))
729
def test_skipped_test(self):
730
# run a test that is skipped, and check the suite as a whole still
732
# skipping_test must be hidden in here so it's not run as a real test
734
raise TestSkipped('test intentionally skipped')
735
runner = TextTestRunner(stream=self._log_file, keep_output=True)
736
test = unittest.FunctionTestCase(skipping_test)
737
result = self.run_test_runner(runner, test)
738
self.assertTrue(result.wasSuccessful())
740
def test_bench_history(self):
741
# tests that the running the benchmark produces a history file
742
# containing a timestamp and the revision id of the bzrlib source which
744
workingtree = _get_bzr_source_tree()
745
test = TestRunner('dummy_test')
747
runner = TextTestRunner(stream=self._log_file, bench_history=output)
748
result = self.run_test_runner(runner, test)
749
output_string = output.getvalue()
750
self.assertContainsRe(output_string, "--date [0-9.]+")
751
if workingtree is not None:
752
revision_id = workingtree.get_parent_ids()[0]
753
self.assertEndsWith(output_string.rstrip(), revision_id)
755
def test_success_log_deleted(self):
756
"""Successful tests have their log deleted"""
758
class LogTester(TestCase):
760
def test_success(self):
761
self.log('this will be removed\n')
763
sio = cStringIO.StringIO()
764
runner = TextTestRunner(stream=sio)
765
test = LogTester('test_success')
766
result = self.run_test_runner(runner, test)
768
log = test._get_log()
769
self.assertEqual("DELETED log file to reduce memory footprint", log)
770
self.assertEqual('', test._log_contents)
771
self.assertIs(None, test._log_file_name)
773
def test_fail_log_kept(self):
774
"""Failed tests have their log kept"""
776
class LogTester(TestCase):
779
self.log('this will be kept\n')
780
self.fail('this test fails')
782
sio = cStringIO.StringIO()
783
runner = TextTestRunner(stream=sio)
784
test = LogTester('test_fail')
785
result = self.run_test_runner(runner, test)
787
text = sio.getvalue()
788
self.assertContainsRe(text, 'this will be kept')
789
self.assertContainsRe(text, 'this test fails')
791
log = test._get_log()
792
self.assertContainsRe(log, 'this will be kept')
793
self.assertEqual(log, test._log_contents)
795
def test_error_log_kept(self):
796
"""Tests with errors have their log kept"""
798
class LogTester(TestCase):
800
def test_error(self):
801
self.log('this will be kept\n')
802
raise ValueError('random exception raised')
804
sio = cStringIO.StringIO()
805
runner = TextTestRunner(stream=sio)
806
test = LogTester('test_error')
807
result = self.run_test_runner(runner, test)
809
text = sio.getvalue()
810
self.assertContainsRe(text, 'this will be kept')
811
self.assertContainsRe(text, 'random exception raised')
813
log = test._get_log()
814
self.assertContainsRe(log, 'this will be kept')
815
self.assertEqual(log, test._log_contents)
818
class TestTestCase(TestCase):
819
"""Tests that test the core bzrlib TestCase."""
821
def inner_test(self):
822
# the inner child test
825
def outer_child(self):
826
# the outer child test
828
self.inner_test = TestTestCase("inner_child")
829
result = bzrlib.tests._MyResult(self._log_file,
832
self.inner_test.run(result)
835
def test_trace_nesting(self):
836
# this tests that each test case nests its trace facility correctly.
837
# we do this by running a test case manually. That test case (A)
838
# should setup a new log, log content to it, setup a child case (B),
839
# which should log independently, then case (A) should log a trailer
841
# we do two nested children so that we can verify the state of the
842
# logs after the outer child finishes is correct, which a bad clean
843
# up routine in tearDown might trigger a fault in our test with only
844
# one child, we should instead see the bad result inside our test with
846
# the outer child test
847
original_trace = bzrlib.trace._trace_file
848
outer_test = TestTestCase("outer_child")
849
result = bzrlib.tests._MyResult(self._log_file,
852
outer_test.run(result)
853
self.assertEqual(original_trace, bzrlib.trace._trace_file)
855
def method_that_times_a_bit_twice(self):
856
# call self.time twice to ensure it aggregates
857
self.time(time.sleep, 0.007)
858
self.time(time.sleep, 0.007)
860
def test_time_creates_benchmark_in_result(self):
861
"""Test that the TestCase.time() method accumulates a benchmark time."""
862
sample_test = TestTestCase("method_that_times_a_bit_twice")
863
output_stream = StringIO()
864
result = bzrlib.tests._MyResult(
865
unittest._WritelnDecorator(output_stream),
868
sample_test.run(result)
869
self.assertContainsRe(
870
output_stream.getvalue(),
871
"[1-9][0-9]ms/ [1-9][0-9]ms\n$")
873
def test__gather_lsprof_in_benchmarks(self):
874
"""When _gather_lsprof_in_benchmarks is on, accumulate profile data.
876
Each self.time() call is individually and separately profiled.
881
raise TestSkipped("lsprof not installed.")
882
# overrides the class member with an instance member so no cleanup
884
self._gather_lsprof_in_benchmarks = True
885
self.time(time.sleep, 0.000)
886
self.time(time.sleep, 0.003)
887
self.assertEqual(2, len(self._benchcalls))
888
self.assertEqual((time.sleep, (0.000,), {}), self._benchcalls[0][0])
889
self.assertEqual((time.sleep, (0.003,), {}), self._benchcalls[1][0])
890
self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
891
self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)
894
@symbol_versioning.deprecated_function(zero_eleven)
895
def sample_deprecated_function():
896
"""A deprecated function to test applyDeprecated with."""
900
def sample_undeprecated_function(a_param):
901
"""A undeprecated function to test applyDeprecated with."""
904
class ApplyDeprecatedHelper(object):
905
"""A helper class for ApplyDeprecated tests."""
907
@symbol_versioning.deprecated_method(zero_eleven)
908
def sample_deprecated_method(self, param_one):
909
"""A deprecated method for testing with."""
912
def sample_normal_method(self):
913
"""A undeprecated method."""
915
@symbol_versioning.deprecated_method(zero_ten)
916
def sample_nested_deprecation(self):
917
return sample_deprecated_function()
920
class TestExtraAssertions(TestCase):
921
"""Tests for new test assertions in bzrlib test suite"""
923
def test_assert_isinstance(self):
924
self.assertIsInstance(2, int)
925
self.assertIsInstance(u'', basestring)
926
self.assertRaises(AssertionError, self.assertIsInstance, None, int)
927
self.assertRaises(AssertionError, self.assertIsInstance, 23.3, int)
929
def test_assertEndsWith(self):
930
self.assertEndsWith('foo', 'oo')
931
self.assertRaises(AssertionError, self.assertEndsWith, 'o', 'oo')
933
def test_applyDeprecated_not_deprecated(self):
934
sample_object = ApplyDeprecatedHelper()
935
# calling an undeprecated callable raises an assertion
936
self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
937
sample_object.sample_normal_method)
938
self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
939
sample_undeprecated_function, "a param value")
940
# calling a deprecated callable (function or method) with the wrong
941
# expected deprecation fails.
942
self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
943
sample_object.sample_deprecated_method, "a param value")
944
self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
945
sample_deprecated_function)
946
# calling a deprecated callable (function or method) with the right
947
# expected deprecation returns the functions result.
948
self.assertEqual("a param value", self.applyDeprecated(zero_eleven,
949
sample_object.sample_deprecated_method, "a param value"))
950
self.assertEqual(2, self.applyDeprecated(zero_eleven,
951
sample_deprecated_function))
952
# calling a nested deprecation with the wrong deprecation version
953
# fails even if a deeper nested function was deprecated with the
955
self.assertRaises(AssertionError, self.applyDeprecated,
956
zero_eleven, sample_object.sample_nested_deprecation)
957
# calling a nested deprecation with the right deprecation value
958
# returns the calls result.
959
self.assertEqual(2, self.applyDeprecated(zero_ten,
960
sample_object.sample_nested_deprecation))
962
def test_callDeprecated(self):
963
def testfunc(be_deprecated, result=None):
964
if be_deprecated is True:
965
symbol_versioning.warn('i am deprecated', DeprecationWarning,
968
result = self.callDeprecated(['i am deprecated'], testfunc, True)
969
self.assertIs(None, result)
970
result = self.callDeprecated([], testfunc, False, 'result')
971
self.assertEqual('result', result)
972
self.callDeprecated(['i am deprecated'], testfunc, be_deprecated=True)
973
self.callDeprecated([], testfunc, be_deprecated=False)
976
class TestConvenienceMakers(TestCaseWithTransport):
977
"""Test for the make_* convenience functions."""
979
def test_make_branch_and_tree_with_format(self):
980
# we should be able to supply a format to make_branch_and_tree
981
self.make_branch_and_tree('a', format=bzrlib.bzrdir.BzrDirMetaFormat1())
982
self.make_branch_and_tree('b', format=bzrlib.bzrdir.BzrDirFormat6())
983
self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('a')._format,
984
bzrlib.bzrdir.BzrDirMetaFormat1)
985
self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
986
bzrlib.bzrdir.BzrDirFormat6)
988
def test_make_branch_and_mutable_tree(self):
989
# we should be able to get a new branch and a mutable tree from
990
# TestCaseWithTransport
991
tree = self.make_branch_and_memory_tree('a')
992
self.assertIsInstance(tree, bzrlib.memorytree.MemoryTree)
995
class TestSFTPMakeBranchAndTree(TestCaseWithSFTPServer):
997
def test_make_tree_for_sftp_branch(self):
998
"""Transports backed by local directories create local trees."""
1000
tree = self.make_branch_and_tree('t1')
1001
base = tree.bzrdir.root_transport.base
1002
self.failIf(base.startswith('sftp'),
1003
'base %r is on sftp but should be local' % base)
1004
self.assertEquals(tree.bzrdir.root_transport,
1005
tree.branch.bzrdir.root_transport)
1006
self.assertEquals(tree.bzrdir.root_transport,
1007
tree.branch.repository.bzrdir.root_transport)
1010
class TestSelftest(TestCase):
1011
"""Tests of bzrlib.tests.selftest."""
1013
def test_selftest_benchmark_parameter_invokes_test_suite__benchmark__(self):
1016
factory_called.append(True)
1020
self.apply_redirected(out, err, None, bzrlib.tests.selftest,
1021
test_suite_factory=factory)
1022
self.assertEqual([True], factory_called)