1
# Copyright (C) 2005, 2006 by 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 version 2 as published by
5
# the Free Software Foundation.
7
# This program is distributed in the hope that it will be useful,
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
# GNU General Public License for more details.
12
# You should have received a copy of the GNU General Public License
13
# along with this program; if not, write to the Free Software
14
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
"""Tests for the test framework."""
19
from StringIO import StringIO
25
from bzrlib import osutils
27
from bzrlib.progress import _BaseProgressBar
28
from bzrlib.tests import (
32
TestCaseWithTransport,
37
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
38
from bzrlib.tests.TestUtil import _load_module_by_name
39
import bzrlib.errors as errors
40
from bzrlib import symbol_versioning
41
from bzrlib.trace import note
42
from bzrlib.transport.memory import MemoryServer, MemoryTransport
43
from bzrlib.version import _get_bzr_source_tree
46
class SelftestTests(TestCase):
48
def test_import_tests(self):
49
mod = _load_module_by_name('bzrlib.tests.test_selftest')
50
self.assertEqual(mod.SelftestTests, SelftestTests)
52
def test_import_test_failure(self):
53
self.assertRaises(ImportError,
58
class MetaTestLog(TestCase):
60
def test_logging(self):
61
"""Test logs are captured when a test fails."""
62
self.log('a test message')
63
self._log_file.flush()
64
self.assertContainsRe(self._get_log(), 'a test message\n')
67
class TestTreeShape(TestCaseInTempDir):
69
def test_unicode_paths(self):
70
filename = u'hell\u00d8'
72
self.build_tree_contents([(filename, 'contents of hello')])
73
except UnicodeEncodeError:
74
raise TestSkipped("can't build unicode working tree in "
75
"filesystem encoding %s" % sys.getfilesystemencoding())
76
self.failUnlessExists(filename)
79
class TestTransportProviderAdapter(TestCase):
80
"""A group of tests that test the transport implementation adaption core.
82
This is a meta test that the tests are applied to all available
85
This will be generalised in the future which is why it is in this
86
test file even though it is specific to transport tests at the moment.
89
def test_get_transport_permutations(self):
90
# this checks that we the module get_test_permutations call
91
# is made by the adapter get_transport_test_permitations method.
92
class MockModule(object):
93
def get_test_permutations(self):
94
return sample_permutation
95
sample_permutation = [(1,2), (3,4)]
96
from bzrlib.transport import TransportTestProviderAdapter
97
adapter = TransportTestProviderAdapter()
98
self.assertEqual(sample_permutation,
99
adapter.get_transport_test_permutations(MockModule()))
101
def test_adapter_checks_all_modules(self):
102
# this checks that the adapter returns as many permurtations as
103
# there are in all the registered# transport modules for there
104
# - we assume if this matches its probably doing the right thing
105
# especially in combination with the tests for setting the right
107
from bzrlib.transport import (TransportTestProviderAdapter,
108
_get_transport_modules
110
modules = _get_transport_modules()
111
permutation_count = 0
112
for module in modules:
114
permutation_count += len(reduce(getattr,
115
(module + ".get_test_permutations").split('.')[1:],
116
__import__(module))())
117
except errors.DependencyNotPresent:
119
input_test = TestTransportProviderAdapter(
120
"test_adapter_sets_transport_class")
121
adapter = TransportTestProviderAdapter()
122
self.assertEqual(permutation_count,
123
len(list(iter(adapter.adapt(input_test)))))
125
def test_adapter_sets_transport_class(self):
126
# Check that the test adapter inserts a transport and server into the
129
# This test used to know about all the possible transports and the
130
# order they were returned but that seems overly brittle (mbp
132
input_test = TestTransportProviderAdapter(
133
"test_adapter_sets_transport_class")
134
from bzrlib.transport import TransportTestProviderAdapter
135
suite = TransportTestProviderAdapter().adapt(input_test)
136
tests = list(iter(suite))
137
self.assertTrue(len(tests) > 6)
138
# there are at least that many builtin transports
140
self.assertTrue(issubclass(one_test.transport_class,
141
bzrlib.transport.Transport))
142
self.assertTrue(issubclass(one_test.transport_server,
143
bzrlib.transport.Server))
146
class TestBranchProviderAdapter(TestCase):
147
"""A group of tests that test the branch implementation test adapter."""
149
def test_adapted_tests(self):
150
# check that constructor parameters are passed through to the adapted
152
from bzrlib.branch import BranchTestProviderAdapter
153
input_test = TestBranchProviderAdapter(
154
"test_adapted_tests")
157
formats = [("c", "C"), ("d", "D")]
158
adapter = BranchTestProviderAdapter(server1, server2, formats)
159
suite = adapter.adapt(input_test)
160
tests = list(iter(suite))
161
self.assertEqual(2, len(tests))
162
self.assertEqual(tests[0].branch_format, formats[0][0])
163
self.assertEqual(tests[0].bzrdir_format, formats[0][1])
164
self.assertEqual(tests[0].transport_server, server1)
165
self.assertEqual(tests[0].transport_readonly_server, server2)
166
self.assertEqual(tests[1].branch_format, formats[1][0])
167
self.assertEqual(tests[1].bzrdir_format, formats[1][1])
168
self.assertEqual(tests[1].transport_server, server1)
169
self.assertEqual(tests[1].transport_readonly_server, server2)
172
class TestBzrDirProviderAdapter(TestCase):
173
"""A group of tests that test the bzr dir implementation test adapter."""
175
def test_adapted_tests(self):
176
# check that constructor parameters are passed through to the adapted
178
from bzrlib.bzrdir import BzrDirTestProviderAdapter
179
input_test = TestBzrDirProviderAdapter(
180
"test_adapted_tests")
184
adapter = BzrDirTestProviderAdapter(server1, server2, formats)
185
suite = adapter.adapt(input_test)
186
tests = list(iter(suite))
187
self.assertEqual(2, len(tests))
188
self.assertEqual(tests[0].bzrdir_format, formats[0])
189
self.assertEqual(tests[0].transport_server, server1)
190
self.assertEqual(tests[0].transport_readonly_server, server2)
191
self.assertEqual(tests[1].bzrdir_format, formats[1])
192
self.assertEqual(tests[1].transport_server, server1)
193
self.assertEqual(tests[1].transport_readonly_server, server2)
196
class TestRepositoryProviderAdapter(TestCase):
197
"""A group of tests that test the repository implementation test adapter."""
199
def test_adapted_tests(self):
200
# check that constructor parameters are passed through to the adapted
202
from bzrlib.repository import RepositoryTestProviderAdapter
203
input_test = TestRepositoryProviderAdapter(
204
"test_adapted_tests")
207
formats = [("c", "C"), ("d", "D")]
208
adapter = RepositoryTestProviderAdapter(server1, server2, formats)
209
suite = adapter.adapt(input_test)
210
tests = list(iter(suite))
211
self.assertEqual(2, len(tests))
212
self.assertEqual(tests[0].bzrdir_format, formats[0][1])
213
self.assertEqual(tests[0].repository_format, formats[0][0])
214
self.assertEqual(tests[0].transport_server, server1)
215
self.assertEqual(tests[0].transport_readonly_server, server2)
216
self.assertEqual(tests[1].bzrdir_format, formats[1][1])
217
self.assertEqual(tests[1].repository_format, formats[1][0])
218
self.assertEqual(tests[1].transport_server, server1)
219
self.assertEqual(tests[1].transport_readonly_server, server2)
222
class TestInterRepositoryProviderAdapter(TestCase):
223
"""A group of tests that test the InterRepository test adapter."""
225
def test_adapted_tests(self):
226
# check that constructor parameters are passed through to the adapted
228
from bzrlib.repository import InterRepositoryTestProviderAdapter
229
input_test = TestInterRepositoryProviderAdapter(
230
"test_adapted_tests")
233
formats = [(str, "C1", "C2"), (int, "D1", "D2")]
234
adapter = InterRepositoryTestProviderAdapter(server1, server2, formats)
235
suite = adapter.adapt(input_test)
236
tests = list(iter(suite))
237
self.assertEqual(2, len(tests))
238
self.assertEqual(tests[0].interrepo_class, formats[0][0])
239
self.assertEqual(tests[0].repository_format, formats[0][1])
240
self.assertEqual(tests[0].repository_format_to, formats[0][2])
241
self.assertEqual(tests[0].transport_server, server1)
242
self.assertEqual(tests[0].transport_readonly_server, server2)
243
self.assertEqual(tests[1].interrepo_class, formats[1][0])
244
self.assertEqual(tests[1].repository_format, formats[1][1])
245
self.assertEqual(tests[1].repository_format_to, formats[1][2])
246
self.assertEqual(tests[1].transport_server, server1)
247
self.assertEqual(tests[1].transport_readonly_server, server2)
250
class TestInterVersionedFileProviderAdapter(TestCase):
251
"""A group of tests that test the InterVersionedFile test adapter."""
253
def test_adapted_tests(self):
254
# check that constructor parameters are passed through to the adapted
256
from bzrlib.versionedfile import InterVersionedFileTestProviderAdapter
257
input_test = TestInterRepositoryProviderAdapter(
258
"test_adapted_tests")
261
formats = [(str, "C1", "C2"), (int, "D1", "D2")]
262
adapter = InterVersionedFileTestProviderAdapter(server1, server2, formats)
263
suite = adapter.adapt(input_test)
264
tests = list(iter(suite))
265
self.assertEqual(2, len(tests))
266
self.assertEqual(tests[0].interversionedfile_class, formats[0][0])
267
self.assertEqual(tests[0].versionedfile_factory, formats[0][1])
268
self.assertEqual(tests[0].versionedfile_factory_to, formats[0][2])
269
self.assertEqual(tests[0].transport_server, server1)
270
self.assertEqual(tests[0].transport_readonly_server, server2)
271
self.assertEqual(tests[1].interversionedfile_class, formats[1][0])
272
self.assertEqual(tests[1].versionedfile_factory, formats[1][1])
273
self.assertEqual(tests[1].versionedfile_factory_to, formats[1][2])
274
self.assertEqual(tests[1].transport_server, server1)
275
self.assertEqual(tests[1].transport_readonly_server, server2)
278
class TestRevisionStoreProviderAdapter(TestCase):
279
"""A group of tests that test the RevisionStore test adapter."""
281
def test_adapted_tests(self):
282
# check that constructor parameters are passed through to the adapted
284
from bzrlib.store.revision import RevisionStoreTestProviderAdapter
285
input_test = TestRevisionStoreProviderAdapter(
286
"test_adapted_tests")
287
# revision stores need a store factory - i.e. RevisionKnit
288
#, a readonly and rw transport
292
store_factories = ["c", "d"]
293
adapter = RevisionStoreTestProviderAdapter(server1, server2, store_factories)
294
suite = adapter.adapt(input_test)
295
tests = list(iter(suite))
296
self.assertEqual(2, len(tests))
297
self.assertEqual(tests[0].store_factory, store_factories[0][0])
298
self.assertEqual(tests[0].transport_server, server1)
299
self.assertEqual(tests[0].transport_readonly_server, server2)
300
self.assertEqual(tests[1].store_factory, store_factories[1][0])
301
self.assertEqual(tests[1].transport_server, server1)
302
self.assertEqual(tests[1].transport_readonly_server, server2)
305
class TestWorkingTreeProviderAdapter(TestCase):
306
"""A group of tests that test the workingtree implementation test adapter."""
308
def test_adapted_tests(self):
309
# check that constructor parameters are passed through to the adapted
311
from bzrlib.workingtree import WorkingTreeTestProviderAdapter
312
input_test = TestWorkingTreeProviderAdapter(
313
"test_adapted_tests")
316
formats = [("c", "C"), ("d", "D")]
317
adapter = WorkingTreeTestProviderAdapter(server1, server2, formats)
318
suite = adapter.adapt(input_test)
319
tests = list(iter(suite))
320
self.assertEqual(2, len(tests))
321
self.assertEqual(tests[0].workingtree_format, formats[0][0])
322
self.assertEqual(tests[0].bzrdir_format, formats[0][1])
323
self.assertEqual(tests[0].transport_server, server1)
324
self.assertEqual(tests[0].transport_readonly_server, server2)
325
self.assertEqual(tests[1].workingtree_format, formats[1][0])
326
self.assertEqual(tests[1].bzrdir_format, formats[1][1])
327
self.assertEqual(tests[1].transport_server, server1)
328
self.assertEqual(tests[1].transport_readonly_server, server2)
331
class TestTreeProviderAdapter(TestCase):
332
"""Test the setup of tree_implementation tests."""
334
def test_adapted_tests(self):
335
# the tree implementation adapter is meant to setup one instance for
336
# each working tree format, and one additional instance that will
337
# use the default wt format, but create a revision tree for the tests.
338
# this means that the wt ones should have the workingtree_to_test_tree
339
# attribute set to 'return_parameter' and the revision one set to
340
# revision_tree_from_workingtree.
342
from bzrlib.tests.tree_implementations import (
343
TreeTestProviderAdapter,
345
revision_tree_from_workingtree
347
from bzrlib.workingtree import WorkingTreeFormat
348
input_test = TestTreeProviderAdapter(
349
"test_adapted_tests")
352
formats = [("c", "C"), ("d", "D")]
353
adapter = TreeTestProviderAdapter(server1, server2, formats)
354
suite = adapter.adapt(input_test)
355
tests = list(iter(suite))
356
self.assertEqual(3, len(tests))
357
default_format = WorkingTreeFormat.get_default_format()
358
self.assertEqual(tests[0].workingtree_format, formats[0][0])
359
self.assertEqual(tests[0].bzrdir_format, formats[0][1])
360
self.assertEqual(tests[0].transport_server, server1)
361
self.assertEqual(tests[0].transport_readonly_server, server2)
362
self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
363
self.assertEqual(tests[1].workingtree_format, formats[1][0])
364
self.assertEqual(tests[1].bzrdir_format, formats[1][1])
365
self.assertEqual(tests[1].transport_server, server1)
366
self.assertEqual(tests[1].transport_readonly_server, server2)
367
self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
368
self.assertEqual(tests[2].workingtree_format, default_format)
369
self.assertEqual(tests[2].bzrdir_format, default_format._matchingbzrdir)
370
self.assertEqual(tests[2].transport_server, server1)
371
self.assertEqual(tests[2].transport_readonly_server, server2)
372
self.assertEqual(tests[2].workingtree_to_test_tree,
373
revision_tree_from_workingtree)
376
class TestInterTreeProviderAdapter(TestCase):
377
"""A group of tests that test the InterTreeTestAdapter."""
379
def test_adapted_tests(self):
380
# check that constructor parameters are passed through to the adapted
382
# for InterTree tests we want the machinery to bring up two trees in
383
# each instance: the base one, and the one we are interacting with.
384
# because each optimiser can be direction specific, we need to test
385
# each optimiser in its chosen direction.
386
# unlike the TestProviderAdapter we dont want to automatically add a
387
# parameterised one for WorkingTree - the optimisers will tell us what
389
from bzrlib.tests.tree_implementations import (
391
revision_tree_from_workingtree
393
from bzrlib.tests.intertree_implementations import (
394
InterTreeTestProviderAdapter,
396
from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
397
input_test = TestInterTreeProviderAdapter(
398
"test_adapted_tests")
401
format1 = WorkingTreeFormat2()
402
format2 = WorkingTreeFormat3()
403
formats = [(str, format1, format2, False, True),
404
(int, format2, format1, False, True)]
405
adapter = InterTreeTestProviderAdapter(server1, server2, formats)
406
suite = adapter.adapt(input_test)
407
tests = list(iter(suite))
408
self.assertEqual(2, len(tests))
409
self.assertEqual(tests[0].intertree_class, formats[0][0])
410
self.assertEqual(tests[0].workingtree_format, formats[0][1])
411
self.assertEqual(tests[0].workingtree_to_test_tree, formats[0][2])
412
self.assertEqual(tests[0].workingtree_format_to, formats[0][3])
413
self.assertEqual(tests[0].workingtree_to_test_tree_to, formats[0][4])
414
self.assertEqual(tests[0].transport_server, server1)
415
self.assertEqual(tests[0].transport_readonly_server, server2)
416
self.assertEqual(tests[1].intertree_class, formats[1][0])
417
self.assertEqual(tests[1].workingtree_format, formats[1][1])
418
self.assertEqual(tests[1].workingtree_to_test_tree, formats[1][2])
419
self.assertEqual(tests[1].workingtree_format_to, formats[1][3])
420
self.assertEqual(tests[1].workingtree_to_test_tree_to, formats[1][4])
421
self.assertEqual(tests[1].transport_server, server1)
422
self.assertEqual(tests[1].transport_readonly_server, server2)
424
class TestTestCaseWithTransport(TestCaseWithTransport):
425
"""Tests for the convenience functions TestCaseWithTransport introduces."""
427
def test_get_readonly_url_none(self):
428
from bzrlib.transport import get_transport
429
from bzrlib.transport.memory import MemoryServer
430
from bzrlib.transport.readonly import ReadonlyTransportDecorator
431
self.transport_server = MemoryServer
432
self.transport_readonly_server = None
433
# calling get_readonly_transport() constructs a decorator on the url
435
url = self.get_readonly_url()
436
url2 = self.get_readonly_url('foo/bar')
437
t = get_transport(url)
438
t2 = get_transport(url2)
439
self.failUnless(isinstance(t, ReadonlyTransportDecorator))
440
self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
441
self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
443
def test_get_readonly_url_http(self):
444
from bzrlib.transport import get_transport
445
from bzrlib.transport.local import LocalRelpathServer
446
from bzrlib.transport.http import HttpServer, HttpTransportBase
447
self.transport_server = LocalRelpathServer
448
self.transport_readonly_server = HttpServer
449
# calling get_readonly_transport() gives us a HTTP server instance.
450
url = self.get_readonly_url()
451
url2 = self.get_readonly_url('foo/bar')
452
# the transport returned may be any HttpTransportBase subclass
453
t = get_transport(url)
454
t2 = get_transport(url2)
455
self.failUnless(isinstance(t, HttpTransportBase))
456
self.failUnless(isinstance(t2, HttpTransportBase))
457
self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
459
def test_is_directory(self):
460
"""Test assertIsDirectory assertion"""
461
t = self.get_transport()
462
self.build_tree(['a_dir/', 'a_file'], transport=t)
463
self.assertIsDirectory('a_dir', t)
464
self.assertRaises(AssertionError, self.assertIsDirectory, 'a_file', t)
465
self.assertRaises(AssertionError, self.assertIsDirectory, 'not_here', t)
468
class TestTestCaseTransports(TestCaseWithTransport):
471
super(TestTestCaseTransports, self).setUp()
472
self.transport_server = MemoryServer
474
def test_make_bzrdir_preserves_transport(self):
475
t = self.get_transport()
476
result_bzrdir = self.make_bzrdir('subdir')
477
self.assertIsInstance(result_bzrdir.transport,
479
# should not be on disk, should only be in memory
480
self.failIfExists('subdir')
483
class TestChrootedTest(ChrootedTestCase):
485
def test_root_is_root(self):
486
from bzrlib.transport import get_transport
487
t = get_transport(self.get_readonly_url())
489
self.assertEqual(url, t.clone('..').base)
492
class MockProgress(_BaseProgressBar):
493
"""Progress-bar standin that records calls.
495
Useful for testing pb using code.
499
_BaseProgressBar.__init__(self)
503
self.calls.append(('tick',))
505
def update(self, msg=None, current=None, total=None):
506
self.calls.append(('update', msg, current, total))
509
self.calls.append(('clear',))
511
def note(self, msg, *args):
512
self.calls.append(('note', msg, args))
515
class TestTestResult(TestCase):
517
def test_progress_bar_style_quiet(self):
518
# test using a progress bar.
519
dummy_test = TestTestResult('test_progress_bar_style_quiet')
520
dummy_error = (Exception, None, [])
521
mypb = MockProgress()
522
mypb.update('Running tests', 0, 4)
523
last_calls = mypb.calls[:]
525
result = bzrlib.tests._MyResult(self._log_file,
529
self.assertEqual(last_calls, mypb.calls)
532
"""Shorten a string based on the terminal width"""
533
return result._ellipsise_unimportant_words(s,
534
osutils.terminal_width())
537
result.startTest(dummy_test)
538
# starting a test prints the test name
539
last_calls += [('update', '...tyle_quiet', 0, None)]
540
self.assertEqual(last_calls, mypb.calls)
541
result.addError(dummy_test, dummy_error)
542
last_calls += [('update', 'ERROR ', 1, None),
543
('note', shorten(dummy_test.id() + ': ERROR'), ())
545
self.assertEqual(last_calls, mypb.calls)
548
result.startTest(dummy_test)
549
last_calls += [('update', '...tyle_quiet', 1, None)]
550
self.assertEqual(last_calls, mypb.calls)
551
last_calls += [('update', 'FAIL ', 2, None),
552
('note', shorten(dummy_test.id() + ': FAIL'), ())
554
result.addFailure(dummy_test, dummy_error)
555
self.assertEqual(last_calls, mypb.calls)
558
result.startTest(dummy_test)
559
last_calls += [('update', '...tyle_quiet', 2, None)]
560
self.assertEqual(last_calls, mypb.calls)
561
result.addSuccess(dummy_test)
562
last_calls += [('update', 'OK ', 3, None)]
563
self.assertEqual(last_calls, mypb.calls)
566
result.startTest(dummy_test)
567
last_calls += [('update', '...tyle_quiet', 3, None)]
568
self.assertEqual(last_calls, mypb.calls)
569
result.addSkipped(dummy_test, dummy_error)
570
last_calls += [('update', 'SKIP ', 4, None)]
571
self.assertEqual(last_calls, mypb.calls)
573
def test_elapsed_time_with_benchmarking(self):
574
result = bzrlib.tests._MyResult(self._log_file,
578
result._recordTestStartTime()
580
result.extractBenchmarkTime(self)
581
timed_string = result._testTimeString()
582
# without explicit benchmarking, we should get a simple time.
583
self.assertContainsRe(timed_string, "^ [ 1-9][0-9]ms$")
584
# if a benchmark time is given, we want a x of y style result.
585
self.time(time.sleep, 0.001)
586
result.extractBenchmarkTime(self)
587
timed_string = result._testTimeString()
588
self.assertContainsRe(timed_string, "^ [ 1-9][0-9]ms/ [ 1-9][0-9]ms$")
589
# extracting the time from a non-bzrlib testcase sets to None
590
result._recordTestStartTime()
591
result.extractBenchmarkTime(
592
unittest.FunctionTestCase(self.test_elapsed_time_with_benchmarking))
593
timed_string = result._testTimeString()
594
self.assertContainsRe(timed_string, "^ [ 1-9][0-9]ms$")
595
# cheat. Yes, wash thy mouth out with soap.
596
self._benchtime = None
598
def test_assigned_benchmark_file_stores_date(self):
600
result = bzrlib.tests._MyResult(self._log_file,
605
output_string = output.getvalue()
606
# if you are wondering about the regexp please read the comment in
607
# test_bench_history (bzrlib.tests.test_selftest.TestRunner)
608
# XXX: what comment? -- Andrew Bennetts
609
self.assertContainsRe(output_string, "--date [0-9.]+")
611
def test_benchhistory_records_test_times(self):
612
result_stream = StringIO()
613
result = bzrlib.tests._MyResult(
617
bench_history=result_stream
620
# we want profile a call and check that its test duration is recorded
621
# make a new test instance that when run will generate a benchmark
622
example_test_case = TestTestResult("_time_hello_world_encoding")
623
# execute the test, which should succeed and record times
624
example_test_case.run(result)
625
lines = result_stream.getvalue().splitlines()
626
self.assertEqual(2, len(lines))
627
self.assertContainsRe(lines[1],
628
" *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
629
"._time_hello_world_encoding")
631
def _time_hello_world_encoding(self):
632
"""Profile two sleep calls
634
This is used to exercise the test framework.
636
self.time(unicode, 'hello', errors='replace')
637
self.time(unicode, 'world', errors='replace')
639
def test_lsprofiling(self):
640
"""Verbose test result prints lsprof statistics from test cases."""
644
raise TestSkipped("lsprof not installed.")
645
result_stream = StringIO()
646
result = bzrlib.tests._MyResult(
647
unittest._WritelnDecorator(result_stream),
651
# we want profile a call of some sort and check it is output by
652
# addSuccess. We dont care about addError or addFailure as they
653
# are not that interesting for performance tuning.
654
# make a new test instance that when run will generate a profile
655
example_test_case = TestTestResult("_time_hello_world_encoding")
656
example_test_case._gather_lsprof_in_benchmarks = True
657
# execute the test, which should succeed and record profiles
658
example_test_case.run(result)
659
# lsprofile_something()
660
# if this worked we want
661
# LSProf output for <built in function unicode> (['hello'], {'errors': 'replace'})
662
# CallCount Recursive Total(ms) Inline(ms) module:lineno(function)
663
# (the lsprof header)
664
# ... an arbitrary number of lines
665
# and the function call which is time.sleep.
666
# 1 0 ??? ??? ???(sleep)
667
# and then repeated but with 'world', rather than 'hello'.
668
# this should appear in the output stream of our test result.
669
output = result_stream.getvalue()
670
self.assertContainsRe(output,
671
r"LSProf output for <type 'unicode'>\(\('hello',\), {'errors': 'replace'}\)")
672
self.assertContainsRe(output,
673
r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n")
674
self.assertContainsRe(output,
675
r"( +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n)?")
676
self.assertContainsRe(output,
677
r"LSProf output for <type 'unicode'>\(\('world',\), {'errors': 'replace'}\)\n")
680
class TestRunner(TestCase):
682
def dummy_test(self):
685
def run_test_runner(self, testrunner, test):
686
"""Run suite in testrunner, saving global state and restoring it.
688
This current saves and restores:
689
TestCaseInTempDir.TEST_ROOT
691
There should be no tests in this file that use bzrlib.tests.TextTestRunner
692
without using this convenience method, because of our use of global state.
694
old_root = TestCaseInTempDir.TEST_ROOT
696
TestCaseInTempDir.TEST_ROOT = None
697
return testrunner.run(test)
699
TestCaseInTempDir.TEST_ROOT = old_root
701
def test_accepts_and_uses_pb_parameter(self):
702
test = TestRunner('dummy_test')
703
mypb = MockProgress()
704
self.assertEqual([], mypb.calls)
705
runner = TextTestRunner(stream=self._log_file, pb=mypb)
706
result = self.run_test_runner(runner, test)
707
self.assertEqual(1, result.testsRun)
708
self.assertEqual(('update', 'Running tests', 0, 1), mypb.calls[0])
709
self.assertEqual(('update', '...dummy_test', 0, None), mypb.calls[1])
710
self.assertEqual(('update', 'OK ', 1, None), mypb.calls[2])
711
self.assertEqual(('update', 'Cleaning up', 0, 1), mypb.calls[3])
712
self.assertEqual(('clear',), mypb.calls[4])
713
self.assertEqual(5, len(mypb.calls))
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.last_revision()
739
self.assertEndsWith(output_string.rstrip(), revision_id)
742
class TestTestCase(TestCase):
743
"""Tests that test the core bzrlib TestCase."""
745
def inner_test(self):
746
# the inner child test
749
def outer_child(self):
750
# the outer child test
752
self.inner_test = TestTestCase("inner_child")
753
result = bzrlib.tests._MyResult(self._log_file,
756
self.inner_test.run(result)
759
def test_trace_nesting(self):
760
# this tests that each test case nests its trace facility correctly.
761
# we do this by running a test case manually. That test case (A)
762
# should setup a new log, log content to it, setup a child case (B),
763
# which should log independently, then case (A) should log a trailer
765
# we do two nested children so that we can verify the state of the
766
# logs after the outer child finishes is correct, which a bad clean
767
# up routine in tearDown might trigger a fault in our test with only
768
# one child, we should instead see the bad result inside our test with
770
# the outer child test
771
original_trace = bzrlib.trace._trace_file
772
outer_test = TestTestCase("outer_child")
773
result = bzrlib.tests._MyResult(self._log_file,
776
outer_test.run(result)
777
self.assertEqual(original_trace, bzrlib.trace._trace_file)
779
def method_that_times_a_bit_twice(self):
780
# call self.time twice to ensure it aggregates
781
self.time(time.sleep, 0.007)
782
self.time(time.sleep, 0.007)
784
def test_time_creates_benchmark_in_result(self):
785
"""Test that the TestCase.time() method accumulates a benchmark time."""
786
sample_test = TestTestCase("method_that_times_a_bit_twice")
787
output_stream = StringIO()
788
result = bzrlib.tests._MyResult(
789
unittest._WritelnDecorator(output_stream),
792
sample_test.run(result)
793
self.assertContainsRe(
794
output_stream.getvalue(),
795
"[1-9][0-9]ms/ [1-9][0-9]ms\n$")
797
def test__gather_lsprof_in_benchmarks(self):
798
"""When _gather_lsprof_in_benchmarks is on, accumulate profile data.
800
Each self.time() call is individually and separately profiled.
805
raise TestSkipped("lsprof not installed.")
806
# overrides the class member with an instance member so no cleanup
808
self._gather_lsprof_in_benchmarks = True
809
self.time(time.sleep, 0.000)
810
self.time(time.sleep, 0.003)
811
self.assertEqual(2, len(self._benchcalls))
812
self.assertEqual((time.sleep, (0.000,), {}), self._benchcalls[0][0])
813
self.assertEqual((time.sleep, (0.003,), {}), self._benchcalls[1][0])
814
self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
815
self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)
818
class TestExtraAssertions(TestCase):
819
"""Tests for new test assertions in bzrlib test suite"""
821
def test_assert_isinstance(self):
822
self.assertIsInstance(2, int)
823
self.assertIsInstance(u'', basestring)
824
self.assertRaises(AssertionError, self.assertIsInstance, None, int)
825
self.assertRaises(AssertionError, self.assertIsInstance, 23.3, int)
827
def test_assertEndsWith(self):
828
self.assertEndsWith('foo', 'oo')
829
self.assertRaises(AssertionError, self.assertEndsWith, 'o', 'oo')
831
def test_callDeprecated(self):
832
def testfunc(be_deprecated, result=None):
833
if be_deprecated is True:
834
symbol_versioning.warn('i am deprecated', DeprecationWarning,
837
result = self.callDeprecated(['i am deprecated'], testfunc, True)
838
self.assertIs(None, result)
839
result = self.callDeprecated([], testfunc, False, 'result')
840
self.assertEqual('result', result)
841
self.callDeprecated(['i am deprecated'], testfunc,
843
self.callDeprecated([], testfunc, be_deprecated=False)
846
class TestConvenienceMakers(TestCaseWithTransport):
847
"""Test for the make_* convenience functions."""
849
def test_make_branch_and_tree_with_format(self):
850
# we should be able to supply a format to make_branch_and_tree
851
self.make_branch_and_tree('a', format=bzrlib.bzrdir.BzrDirMetaFormat1())
852
self.make_branch_and_tree('b', format=bzrlib.bzrdir.BzrDirFormat6())
853
self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('a')._format,
854
bzrlib.bzrdir.BzrDirMetaFormat1)
855
self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
856
bzrlib.bzrdir.BzrDirFormat6)
859
class TestSFTPMakeBranchAndTree(TestCaseWithSFTPServer):
861
def test_make_tree_for_sftp_branch(self):
862
"""Transports backed by local directories create local trees."""
864
tree = self.make_branch_and_tree('t1')
865
base = tree.bzrdir.root_transport.base
866
self.failIf(base.startswith('sftp'),
867
'base %r is on sftp but should be local' % base)
868
self.assertEquals(tree.bzrdir.root_transport,
869
tree.branch.bzrdir.root_transport)
870
self.assertEquals(tree.bzrdir.root_transport,
871
tree.branch.repository.bzrdir.root_transport)
874
class TestSelftest(TestCase):
875
"""Tests of bzrlib.tests.selftest."""
877
def test_selftest_benchmark_parameter_invokes_test_suite__benchmark__(self):
880
factory_called.append(True)
884
self.apply_redirected(out, err, None, bzrlib.tests.selftest,
885
test_suite_factory=factory)
886
self.assertEqual([True], factory_called)