1
# Copyright (C) 2005, 2006, 2007 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
36
from bzrlib.progress import _BaseProgressBar
37
from bzrlib.repofmt import weaverepo
38
from bzrlib.symbol_versioning import zero_ten, zero_eleven
39
from bzrlib.tests import (
46
TestCaseWithMemoryTransport,
47
TestCaseWithTransport,
59
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
60
from bzrlib.tests.TestUtil import _load_module_by_name
61
from bzrlib.trace import note
62
from bzrlib.transport.memory import MemoryServer, MemoryTransport
63
from bzrlib.version import _get_bzr_source_tree
66
class SelftestTests(TestCase):
68
def test_import_tests(self):
69
mod = _load_module_by_name('bzrlib.tests.test_selftest')
70
self.assertEqual(mod.SelftestTests, SelftestTests)
72
def test_import_test_failure(self):
73
self.assertRaises(ImportError,
77
class MetaTestLog(TestCase):
79
def test_logging(self):
80
"""Test logs are captured when a test fails."""
81
self.log('a test message')
82
self._log_file.flush()
83
self.assertContainsRe(self._get_log(keep_log_file=True),
87
class TestTreeShape(TestCaseInTempDir):
89
def test_unicode_paths(self):
90
filename = u'hell\u00d8'
92
self.build_tree_contents([(filename, 'contents of hello')])
93
except UnicodeEncodeError:
94
raise TestSkipped("can't build unicode working tree in "
95
"filesystem encoding %s" % sys.getfilesystemencoding())
96
self.failUnlessExists(filename)
99
class TestTransportProviderAdapter(TestCase):
100
"""A group of tests that test the transport implementation adaption core.
102
This is a meta test that the tests are applied to all available
105
This will be generalised in the future which is why it is in this
106
test file even though it is specific to transport tests at the moment.
109
def test_get_transport_permutations(self):
110
# this checks that we the module get_test_permutations call
111
# is made by the adapter get_transport_test_permitations method.
112
class MockModule(object):
113
def get_test_permutations(self):
114
return sample_permutation
115
sample_permutation = [(1,2), (3,4)]
116
from bzrlib.tests.test_transport_implementations \
117
import TransportTestProviderAdapter
118
adapter = TransportTestProviderAdapter()
119
self.assertEqual(sample_permutation,
120
adapter.get_transport_test_permutations(MockModule()))
122
def test_adapter_checks_all_modules(self):
123
# this checks that the adapter returns as many permurtations as
124
# there are in all the registered# transport modules for there
125
# - we assume if this matches its probably doing the right thing
126
# especially in combination with the tests for setting the right
128
from bzrlib.tests.test_transport_implementations \
129
import TransportTestProviderAdapter
130
from bzrlib.transport import _get_transport_modules
131
modules = _get_transport_modules()
132
permutation_count = 0
133
for module in modules:
135
permutation_count += len(reduce(getattr,
136
(module + ".get_test_permutations").split('.')[1:],
137
__import__(module))())
138
except errors.DependencyNotPresent:
140
input_test = TestTransportProviderAdapter(
141
"test_adapter_sets_transport_class")
142
adapter = TransportTestProviderAdapter()
143
self.assertEqual(permutation_count,
144
len(list(iter(adapter.adapt(input_test)))))
146
def test_adapter_sets_transport_class(self):
147
# Check that the test adapter inserts a transport and server into the
150
# This test used to know about all the possible transports and the
151
# order they were returned but that seems overly brittle (mbp
153
from bzrlib.tests.test_transport_implementations \
154
import TransportTestProviderAdapter
155
scenarios = TransportTestProviderAdapter().scenarios
156
# there are at least that many builtin transports
157
self.assertTrue(len(scenarios) > 6)
158
one_scenario = scenarios[0]
159
self.assertIsInstance(one_scenario[0], str)
160
self.assertTrue(issubclass(one_scenario[1]["transport_class"],
161
bzrlib.transport.Transport))
162
self.assertTrue(issubclass(one_scenario[1]["transport_server"],
163
bzrlib.transport.Server))
166
class TestBranchProviderAdapter(TestCase):
167
"""A group of tests that test the branch implementation test adapter."""
169
def test_constructor(self):
170
# check that constructor parameters are passed through to the adapted
172
from bzrlib.tests.branch_implementations import BranchTestProviderAdapter
175
formats = [("c", "C"), ("d", "D")]
176
adapter = BranchTestProviderAdapter(server1, server2, formats)
177
self.assertEqual(2, len(adapter.scenarios))
180
{'branch_format': 'c',
181
'bzrdir_format': 'C',
182
'transport_readonly_server': 'b',
183
'transport_server': 'a'}),
185
{'branch_format': 'd',
186
'bzrdir_format': 'D',
187
'transport_readonly_server': 'b',
188
'transport_server': 'a'})],
192
class TestBzrDirProviderAdapter(TestCase):
193
"""A group of tests that test the bzr dir implementation test adapter."""
195
def test_adapted_tests(self):
196
# check that constructor parameters are passed through to the adapted
198
from bzrlib.tests.bzrdir_implementations import BzrDirTestProviderAdapter
203
adapter = BzrDirTestProviderAdapter(vfs_factory,
204
server1, server2, formats)
207
{'bzrdir_format': 'c',
208
'transport_readonly_server': 'b',
209
'transport_server': 'a',
210
'vfs_transport_factory': 'v'}),
212
{'bzrdir_format': 'd',
213
'transport_readonly_server': 'b',
214
'transport_server': 'a',
215
'vfs_transport_factory': 'v'})],
219
class TestRepositoryProviderAdapter(TestCase):
220
"""A group of tests that test the repository implementation test adapter."""
222
def test_constructor(self):
223
# check that constructor parameters are passed through to the
225
from bzrlib.tests.repository_implementations import RepositoryTestProviderAdapter
228
formats = [("c", "C"), ("d", "D")]
229
adapter = RepositoryTestProviderAdapter(server1, server2, formats)
232
{'bzrdir_format': 'C',
233
'repository_format': 'c',
234
'transport_readonly_server': 'b',
235
'transport_server': 'a'}),
237
{'bzrdir_format': 'D',
238
'repository_format': 'd',
239
'transport_readonly_server': 'b',
240
'transport_server': 'a'})],
243
def test_setting_vfs_transport(self):
244
"""The vfs_transport_factory can be set optionally."""
245
from bzrlib.tests.repository_implementations import RepositoryTestProviderAdapter
246
formats = [("a", "b"), ("c", "d")]
247
adapter = RepositoryTestProviderAdapter(None, None, formats,
248
vfs_transport_factory="vfs")
251
{'bzrdir_format': 'b',
252
'repository_format': 'a',
253
'transport_readonly_server': None,
254
'transport_server': None,
255
'vfs_transport_factory': 'vfs'}),
257
{'bzrdir_format': 'd',
258
'repository_format': 'c',
259
'transport_readonly_server': None,
260
'transport_server': None,
261
'vfs_transport_factory': 'vfs'})],
264
def test_formats_to_scenarios(self):
265
"""The adapter can generate all the scenarios needed."""
266
from bzrlib.tests.repository_implementations import RepositoryTestProviderAdapter
267
no_vfs_adapter = RepositoryTestProviderAdapter("server", "readonly",
269
vfs_adapter = RepositoryTestProviderAdapter("server", "readonly",
270
[], vfs_transport_factory="vfs")
271
# no_vfs generate scenarios without vfs_transport_factor
272
formats = [("c", "C"), (1, "D")]
275
{'bzrdir_format': 'C',
276
'repository_format': 'c',
277
'transport_readonly_server': 'readonly',
278
'transport_server': 'server'}),
280
{'bzrdir_format': 'D',
281
'repository_format': 1,
282
'transport_readonly_server': 'readonly',
283
'transport_server': 'server'})],
284
no_vfs_adapter.formats_to_scenarios(formats))
287
{'bzrdir_format': 'C',
288
'repository_format': 'c',
289
'transport_readonly_server': 'readonly',
290
'transport_server': 'server',
291
'vfs_transport_factory': 'vfs'}),
293
{'bzrdir_format': 'D',
294
'repository_format': 1,
295
'transport_readonly_server': 'readonly',
296
'transport_server': 'server',
297
'vfs_transport_factory': 'vfs'})],
298
vfs_adapter.formats_to_scenarios(formats))
301
class TestTestScenarioApplier(TestCase):
302
"""Tests for the test adaption facilities."""
304
def test_adapt_applies_scenarios(self):
305
from bzrlib.tests.repository_implementations import TestScenarioApplier
306
input_test = TestTestScenarioApplier("test_adapt_test_to_scenario")
307
adapter = TestScenarioApplier()
308
adapter.scenarios = [("1", "dict"), ("2", "settings")]
310
def capture_call(test, scenario):
311
calls.append((test, scenario))
313
adapter.adapt_test_to_scenario = capture_call
314
adapter.adapt(input_test)
315
self.assertEqual([(input_test, ("1", "dict")),
316
(input_test, ("2", "settings"))], calls)
318
def test_adapt_test_to_scenario(self):
319
from bzrlib.tests.repository_implementations import TestScenarioApplier
320
input_test = TestTestScenarioApplier("test_adapt_test_to_scenario")
321
adapter = TestScenarioApplier()
322
# setup two adapted tests
323
adapted_test1 = adapter.adapt_test_to_scenario(input_test,
325
{"bzrdir_format":"bzr_format",
326
"repository_format":"repo_fmt",
327
"transport_server":"transport_server",
328
"transport_readonly_server":"readonly-server"}))
329
adapted_test2 = adapter.adapt_test_to_scenario(input_test,
330
("new id 2", {"bzrdir_format":None}))
331
# input_test should have been altered.
332
self.assertRaises(AttributeError, getattr, input_test, "bzrdir_format")
333
# the new tests are mutually incompatible, ensuring it has
334
# made new ones, and unspecified elements in the scenario
335
# should not have been altered.
336
self.assertEqual("bzr_format", adapted_test1.bzrdir_format)
337
self.assertEqual("repo_fmt", adapted_test1.repository_format)
338
self.assertEqual("transport_server", adapted_test1.transport_server)
339
self.assertEqual("readonly-server",
340
adapted_test1.transport_readonly_server)
342
"bzrlib.tests.test_selftest.TestTestScenarioApplier."
343
"test_adapt_test_to_scenario(new id)",
345
self.assertEqual(None, adapted_test2.bzrdir_format)
347
"bzrlib.tests.test_selftest.TestTestScenarioApplier."
348
"test_adapt_test_to_scenario(new id 2)",
352
class TestInterRepositoryProviderAdapter(TestCase):
353
"""A group of tests that test the InterRepository test adapter."""
355
def test_adapted_tests(self):
356
# check that constructor parameters are passed through to the adapted
358
from bzrlib.tests.interrepository_implementations import \
359
InterRepositoryTestProviderAdapter
362
formats = [(str, "C1", "C2"), (int, "D1", "D2")]
363
adapter = InterRepositoryTestProviderAdapter(server1, server2, formats)
366
{'interrepo_class': str,
367
'repository_format': 'C1',
368
'repository_format_to': 'C2',
369
'transport_readonly_server': 'b',
370
'transport_server': 'a'}),
372
{'interrepo_class': int,
373
'repository_format': 'D1',
374
'repository_format_to': 'D2',
375
'transport_readonly_server': 'b',
376
'transport_server': 'a'})],
377
adapter.formats_to_scenarios(formats))
380
class TestInterVersionedFileProviderAdapter(TestCase):
381
"""A group of tests that test the InterVersionedFile test adapter."""
383
def test_scenarios(self):
384
# check that constructor parameters are passed through to the adapted
386
from bzrlib.tests.interversionedfile_implementations \
387
import InterVersionedFileTestProviderAdapter
390
formats = [(str, "C1", "C2"), (int, "D1", "D2")]
391
adapter = InterVersionedFileTestProviderAdapter(server1, server2, formats)
394
{'interversionedfile_class':str,
395
'transport_readonly_server': 'b',
396
'transport_server': 'a',
397
'versionedfile_factory': 'C1',
398
'versionedfile_factory_to': 'C2'}),
400
{'interversionedfile_class': int,
401
'transport_readonly_server': 'b',
402
'transport_server': 'a',
403
'versionedfile_factory': 'D1',
404
'versionedfile_factory_to': 'D2'})],
408
class TestRevisionStoreProviderAdapter(TestCase):
409
"""A group of tests that test the RevisionStore test adapter."""
411
def test_scenarios(self):
412
# check that constructor parameters are passed through to the adapted
414
from bzrlib.tests.revisionstore_implementations \
415
import RevisionStoreTestProviderAdapter
416
# revision stores need a store factory - i.e. RevisionKnit
417
#, a readonly and rw transport
421
store_factories = ["c", "d"]
422
adapter = RevisionStoreTestProviderAdapter(server1, server2, store_factories)
425
{'store_factory': 'c',
426
'transport_readonly_server': 'b',
427
'transport_server': 'a'}),
429
{'store_factory': 'd',
430
'transport_readonly_server': 'b',
431
'transport_server': 'a'})],
435
class TestWorkingTreeProviderAdapter(TestCase):
436
"""A group of tests that test the workingtree implementation test adapter."""
438
def test_scenarios(self):
439
# check that constructor parameters are passed through to the adapted
441
from bzrlib.tests.workingtree_implementations \
442
import WorkingTreeTestProviderAdapter
445
formats = [("c", "C"), ("d", "D")]
446
adapter = WorkingTreeTestProviderAdapter(server1, server2, formats)
449
{'bzrdir_format': 'C',
450
'transport_readonly_server': 'b',
451
'transport_server': 'a',
452
'workingtree_format': 'c'}),
454
{'bzrdir_format': 'D',
455
'transport_readonly_server': 'b',
456
'transport_server': 'a',
457
'workingtree_format': 'd'})],
461
class TestTreeProviderAdapter(TestCase):
462
"""Test the setup of tree_implementation tests."""
464
def test_adapted_tests(self):
465
# the tree implementation adapter is meant to setup one instance for
466
# each working tree format, and one additional instance that will
467
# use the default wt format, but create a revision tree for the tests.
468
# this means that the wt ones should have the workingtree_to_test_tree
469
# attribute set to 'return_parameter' and the revision one set to
470
# revision_tree_from_workingtree.
472
from bzrlib.tests.tree_implementations import (
473
TreeTestProviderAdapter,
475
revision_tree_from_workingtree
477
from bzrlib.workingtree import WorkingTreeFormat, WorkingTreeFormat3
478
input_test = TestTreeProviderAdapter(
479
"test_adapted_tests")
482
formats = [("c", "C"), ("d", "D")]
483
adapter = TreeTestProviderAdapter(server1, server2, formats)
484
suite = adapter.adapt(input_test)
485
tests = list(iter(suite))
486
self.assertEqual(4, len(tests))
487
# this must match the default format setp up in
488
# TreeTestProviderAdapter.adapt
489
default_format = WorkingTreeFormat3
490
self.assertEqual(tests[0].workingtree_format, formats[0][0])
491
self.assertEqual(tests[0].bzrdir_format, formats[0][1])
492
self.assertEqual(tests[0].transport_server, server1)
493
self.assertEqual(tests[0].transport_readonly_server, server2)
494
self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
495
self.assertEqual(tests[1].workingtree_format, formats[1][0])
496
self.assertEqual(tests[1].bzrdir_format, formats[1][1])
497
self.assertEqual(tests[1].transport_server, server1)
498
self.assertEqual(tests[1].transport_readonly_server, server2)
499
self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
500
self.assertIsInstance(tests[2].workingtree_format, default_format)
501
#self.assertEqual(tests[2].bzrdir_format,
502
# default_format._matchingbzrdir)
503
self.assertEqual(tests[2].transport_server, server1)
504
self.assertEqual(tests[2].transport_readonly_server, server2)
505
self.assertEqual(tests[2].workingtree_to_test_tree,
506
revision_tree_from_workingtree)
509
class TestInterTreeProviderAdapter(TestCase):
510
"""A group of tests that test the InterTreeTestAdapter."""
512
def test_adapted_tests(self):
513
# check that constructor parameters are passed through to the adapted
515
# for InterTree tests we want the machinery to bring up two trees in
516
# each instance: the base one, and the one we are interacting with.
517
# because each optimiser can be direction specific, we need to test
518
# each optimiser in its chosen direction.
519
# unlike the TestProviderAdapter we dont want to automatically add a
520
# parameterised one for WorkingTree - the optimisers will tell us what
522
from bzrlib.tests.tree_implementations import (
524
revision_tree_from_workingtree
526
from bzrlib.tests.intertree_implementations import (
527
InterTreeTestProviderAdapter,
529
from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
530
input_test = TestInterTreeProviderAdapter(
531
"test_adapted_tests")
534
format1 = WorkingTreeFormat2()
535
format2 = WorkingTreeFormat3()
536
formats = [(str, format1, format2, "converter1"),
537
(int, format2, format1, "converter2")]
538
adapter = InterTreeTestProviderAdapter(server1, server2, formats)
539
suite = adapter.adapt(input_test)
540
tests = list(iter(suite))
541
self.assertEqual(2, len(tests))
542
self.assertEqual(tests[0].intertree_class, formats[0][0])
543
self.assertEqual(tests[0].workingtree_format, formats[0][1])
544
self.assertEqual(tests[0].workingtree_format_to, formats[0][2])
545
self.assertEqual(tests[0].mutable_trees_to_test_trees, formats[0][3])
546
self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
547
self.assertEqual(tests[0].transport_server, server1)
548
self.assertEqual(tests[0].transport_readonly_server, server2)
549
self.assertEqual(tests[1].intertree_class, formats[1][0])
550
self.assertEqual(tests[1].workingtree_format, formats[1][1])
551
self.assertEqual(tests[1].workingtree_format_to, formats[1][2])
552
self.assertEqual(tests[1].mutable_trees_to_test_trees, formats[1][3])
553
self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
554
self.assertEqual(tests[1].transport_server, server1)
555
self.assertEqual(tests[1].transport_readonly_server, server2)
558
class TestTestCaseInTempDir(TestCaseInTempDir):
560
def test_home_is_not_working(self):
561
self.assertNotEqual(self.test_dir, self.test_home_dir)
562
cwd = osutils.getcwd()
563
self.assertEqual(self.test_dir, cwd)
564
self.assertEqual(self.test_home_dir, os.environ['HOME'])
567
class TestTestCaseWithMemoryTransport(TestCaseWithMemoryTransport):
569
def test_home_is_non_existant_dir_under_root(self):
570
"""The test_home_dir for TestCaseWithMemoryTransport is missing.
572
This is because TestCaseWithMemoryTransport is for tests that do not
573
need any disk resources: they should be hooked into bzrlib in such a
574
way that no global settings are being changed by the test (only a
575
few tests should need to do that), and having a missing dir as home is
576
an effective way to ensure that this is the case.
578
self.assertEqual(self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
580
self.assertEqual(self.test_home_dir, os.environ['HOME'])
582
def test_cwd_is_TEST_ROOT(self):
583
self.assertEqual(self.test_dir, self.TEST_ROOT)
584
cwd = osutils.getcwd()
585
self.assertEqual(self.test_dir, cwd)
587
def test_make_branch_and_memory_tree(self):
588
"""In TestCaseWithMemoryTransport we should not make the branch on disk.
590
This is hard to comprehensively robustly test, so we settle for making
591
a branch and checking no directory was created at its relpath.
593
tree = self.make_branch_and_memory_tree('dir')
594
# Guard against regression into MemoryTransport leaking
595
# files to disk instead of keeping them in memory.
596
self.failIf(osutils.lexists('dir'))
597
self.assertIsInstance(tree, memorytree.MemoryTree)
599
def test_make_branch_and_memory_tree_with_format(self):
600
"""make_branch_and_memory_tree should accept a format option."""
601
format = bzrdir.BzrDirMetaFormat1()
602
format.repository_format = weaverepo.RepositoryFormat7()
603
tree = self.make_branch_and_memory_tree('dir', format=format)
604
# Guard against regression into MemoryTransport leaking
605
# files to disk instead of keeping them in memory.
606
self.failIf(osutils.lexists('dir'))
607
self.assertIsInstance(tree, memorytree.MemoryTree)
608
self.assertEqual(format.repository_format.__class__,
609
tree.branch.repository._format.__class__)
612
class TestTestCaseWithTransport(TestCaseWithTransport):
613
"""Tests for the convenience functions TestCaseWithTransport introduces."""
615
def test_get_readonly_url_none(self):
616
from bzrlib.transport import get_transport
617
from bzrlib.transport.memory import MemoryServer
618
from bzrlib.transport.readonly import ReadonlyTransportDecorator
619
self.vfs_transport_factory = MemoryServer
620
self.transport_readonly_server = None
621
# calling get_readonly_transport() constructs a decorator on the url
623
url = self.get_readonly_url()
624
url2 = self.get_readonly_url('foo/bar')
625
t = get_transport(url)
626
t2 = get_transport(url2)
627
self.failUnless(isinstance(t, ReadonlyTransportDecorator))
628
self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
629
self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
631
def test_get_readonly_url_http(self):
632
from bzrlib.tests.HttpServer import HttpServer
633
from bzrlib.transport import get_transport
634
from bzrlib.transport.local import LocalURLServer
635
from bzrlib.transport.http import HttpTransportBase
636
self.transport_server = LocalURLServer
637
self.transport_readonly_server = HttpServer
638
# calling get_readonly_transport() gives us a HTTP server instance.
639
url = self.get_readonly_url()
640
url2 = self.get_readonly_url('foo/bar')
641
# the transport returned may be any HttpTransportBase subclass
642
t = get_transport(url)
643
t2 = get_transport(url2)
644
self.failUnless(isinstance(t, HttpTransportBase))
645
self.failUnless(isinstance(t2, HttpTransportBase))
646
self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
648
def test_is_directory(self):
649
"""Test assertIsDirectory assertion"""
650
t = self.get_transport()
651
self.build_tree(['a_dir/', 'a_file'], transport=t)
652
self.assertIsDirectory('a_dir', t)
653
self.assertRaises(AssertionError, self.assertIsDirectory, 'a_file', t)
654
self.assertRaises(AssertionError, self.assertIsDirectory, 'not_here', t)
657
class TestTestCaseTransports(TestCaseWithTransport):
660
super(TestTestCaseTransports, self).setUp()
661
self.vfs_transport_factory = MemoryServer
663
def test_make_bzrdir_preserves_transport(self):
664
t = self.get_transport()
665
result_bzrdir = self.make_bzrdir('subdir')
666
self.assertIsInstance(result_bzrdir.transport,
668
# should not be on disk, should only be in memory
669
self.failIfExists('subdir')
672
class TestChrootedTest(ChrootedTestCase):
674
def test_root_is_root(self):
675
from bzrlib.transport import get_transport
676
t = get_transport(self.get_readonly_url())
678
self.assertEqual(url, t.clone('..').base)
681
class MockProgress(_BaseProgressBar):
682
"""Progress-bar standin that records calls.
684
Useful for testing pb using code.
688
_BaseProgressBar.__init__(self)
692
self.calls.append(('tick',))
694
def update(self, msg=None, current=None, total=None):
695
self.calls.append(('update', msg, current, total))
698
self.calls.append(('clear',))
700
def note(self, msg, *args):
701
self.calls.append(('note', msg, args))
704
class TestTestResult(TestCase):
706
def test_elapsed_time_with_benchmarking(self):
707
result = bzrlib.tests.TextTestResult(self._log_file,
711
result._recordTestStartTime()
713
result.extractBenchmarkTime(self)
714
timed_string = result._testTimeString()
715
# without explicit benchmarking, we should get a simple time.
716
self.assertContainsRe(timed_string, "^ +[0-9]+ms$")
717
# if a benchmark time is given, we want a x of y style result.
718
self.time(time.sleep, 0.001)
719
result.extractBenchmarkTime(self)
720
timed_string = result._testTimeString()
721
self.assertContainsRe(
722
timed_string, "^ +[0-9]+ms/ +[0-9]+ms$")
723
# extracting the time from a non-bzrlib testcase sets to None
724
result._recordTestStartTime()
725
result.extractBenchmarkTime(
726
unittest.FunctionTestCase(self.test_elapsed_time_with_benchmarking))
727
timed_string = result._testTimeString()
728
self.assertContainsRe(timed_string, "^ +[0-9]+ms$")
729
# cheat. Yes, wash thy mouth out with soap.
730
self._benchtime = None
732
def test_assigned_benchmark_file_stores_date(self):
734
result = bzrlib.tests.TextTestResult(self._log_file,
739
output_string = output.getvalue()
741
# if you are wondering about the regexp please read the comment in
742
# test_bench_history (bzrlib.tests.test_selftest.TestRunner)
743
# XXX: what comment? -- Andrew Bennetts
744
self.assertContainsRe(output_string, "--date [0-9.]+")
746
def test_benchhistory_records_test_times(self):
747
result_stream = StringIO()
748
result = bzrlib.tests.TextTestResult(
752
bench_history=result_stream
755
# we want profile a call and check that its test duration is recorded
756
# make a new test instance that when run will generate a benchmark
757
example_test_case = TestTestResult("_time_hello_world_encoding")
758
# execute the test, which should succeed and record times
759
example_test_case.run(result)
760
lines = result_stream.getvalue().splitlines()
761
self.assertEqual(2, len(lines))
762
self.assertContainsRe(lines[1],
763
" *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
764
"._time_hello_world_encoding")
766
def _time_hello_world_encoding(self):
767
"""Profile two sleep calls
769
This is used to exercise the test framework.
771
self.time(unicode, 'hello', errors='replace')
772
self.time(unicode, 'world', errors='replace')
774
def test_lsprofiling(self):
775
"""Verbose test result prints lsprof statistics from test cases."""
776
self.requireFeature(test_lsprof.LSProfFeature)
777
result_stream = StringIO()
778
result = bzrlib.tests.VerboseTestResult(
779
unittest._WritelnDecorator(result_stream),
783
# we want profile a call of some sort and check it is output by
784
# addSuccess. We dont care about addError or addFailure as they
785
# are not that interesting for performance tuning.
786
# make a new test instance that when run will generate a profile
787
example_test_case = TestTestResult("_time_hello_world_encoding")
788
example_test_case._gather_lsprof_in_benchmarks = True
789
# execute the test, which should succeed and record profiles
790
example_test_case.run(result)
791
# lsprofile_something()
792
# if this worked we want
793
# LSProf output for <built in function unicode> (['hello'], {'errors': 'replace'})
794
# CallCount Recursive Total(ms) Inline(ms) module:lineno(function)
795
# (the lsprof header)
796
# ... an arbitrary number of lines
797
# and the function call which is time.sleep.
798
# 1 0 ??? ??? ???(sleep)
799
# and then repeated but with 'world', rather than 'hello'.
800
# this should appear in the output stream of our test result.
801
output = result_stream.getvalue()
802
self.assertContainsRe(output,
803
r"LSProf output for <type 'unicode'>\(\('hello',\), {'errors': 'replace'}\)")
804
self.assertContainsRe(output,
805
r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n")
806
self.assertContainsRe(output,
807
r"( +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n)?")
808
self.assertContainsRe(output,
809
r"LSProf output for <type 'unicode'>\(\('world',\), {'errors': 'replace'}\)\n")
811
def test_known_failure(self):
812
"""A KnownFailure being raised should trigger several result actions."""
813
class InstrumentedTestResult(ExtendedTestResult):
815
def report_test_start(self, test): pass
816
def report_known_failure(self, test, err):
817
self._call = test, err
818
result = InstrumentedTestResult(None, None, None, None)
820
raise KnownFailure('failed!')
821
test = unittest.FunctionTestCase(test_function)
823
# it should invoke 'report_known_failure'.
824
self.assertEqual(2, len(result._call))
825
self.assertEqual(test, result._call[0])
826
self.assertEqual(KnownFailure, result._call[1][0])
827
self.assertIsInstance(result._call[1][1], KnownFailure)
828
# we dont introspec the traceback, if the rest is ok, it would be
829
# exceptional for it not to be.
830
# it should update the known_failure_count on the object.
831
self.assertEqual(1, result.known_failure_count)
832
# the result should be successful.
833
self.assertTrue(result.wasSuccessful())
835
def test_verbose_report_known_failure(self):
836
# verbose test output formatting
837
result_stream = StringIO()
838
result = bzrlib.tests.VerboseTestResult(
839
unittest._WritelnDecorator(result_stream),
843
test = self.get_passing_test()
844
result.startTest(test)
845
result.extractBenchmarkTime(test)
846
prefix = len(result_stream.getvalue())
847
# the err parameter has the shape:
848
# (class, exception object, traceback)
849
# KnownFailures dont get their tracebacks shown though, so we
851
err = (KnownFailure, KnownFailure('foo'), None)
852
result.report_known_failure(test, err)
853
output = result_stream.getvalue()[prefix:]
854
lines = output.splitlines()
855
self.assertContainsRe(lines[0], r'XFAIL *\d+ms$')
856
self.assertEqual(lines[1], ' foo')
857
self.assertEqual(2, len(lines))
859
def test_text_report_known_failure(self):
860
# text test output formatting
862
result = bzrlib.tests.TextTestResult(
868
test = self.get_passing_test()
869
# this seeds the state to handle reporting the test.
870
result.startTest(test)
871
result.extractBenchmarkTime(test)
872
# the err parameter has the shape:
873
# (class, exception object, traceback)
874
# KnownFailures dont get their tracebacks shown though, so we
876
err = (KnownFailure, KnownFailure('foo'), None)
877
result.report_known_failure(test, err)
880
('update', '[1 in 0s] passing_test', None, None),
881
('note', 'XFAIL: %s\n%s\n', ('passing_test', err[1]))
884
# known_failures should be printed in the summary, so if we run a test
885
# after there are some known failures, the update prefix should match
887
result.known_failure_count = 3
891
('update', '[2 in 0s, 3 known failures] passing_test', None, None),
895
def get_passing_test(self):
896
"""Return a test object that can't be run usefully."""
899
return unittest.FunctionTestCase(passing_test)
901
def test_add_not_supported(self):
902
"""Test the behaviour of invoking addNotSupported."""
903
class InstrumentedTestResult(ExtendedTestResult):
904
def report_test_start(self, test): pass
905
def report_unsupported(self, test, feature):
906
self._call = test, feature
907
result = InstrumentedTestResult(None, None, None, None)
908
test = SampleTestCase('_test_pass')
910
result.startTest(test)
911
result.addNotSupported(test, feature)
912
# it should invoke 'report_unsupported'.
913
self.assertEqual(2, len(result._call))
914
self.assertEqual(test, result._call[0])
915
self.assertEqual(feature, result._call[1])
916
# the result should be successful.
917
self.assertTrue(result.wasSuccessful())
918
# it should record the test against a count of tests not run due to
920
self.assertEqual(1, result.unsupported['Feature'])
921
# and invoking it again should increment that counter
922
result.addNotSupported(test, feature)
923
self.assertEqual(2, result.unsupported['Feature'])
925
def test_verbose_report_unsupported(self):
926
# verbose test output formatting
927
result_stream = StringIO()
928
result = bzrlib.tests.VerboseTestResult(
929
unittest._WritelnDecorator(result_stream),
933
test = self.get_passing_test()
935
result.startTest(test)
936
result.extractBenchmarkTime(test)
937
prefix = len(result_stream.getvalue())
938
result.report_unsupported(test, feature)
939
output = result_stream.getvalue()[prefix:]
940
lines = output.splitlines()
941
self.assertEqual(lines, ['NODEP 0ms', " The feature 'Feature' is not available."])
943
def test_text_report_unsupported(self):
944
# text test output formatting
946
result = bzrlib.tests.TextTestResult(
952
test = self.get_passing_test()
954
# this seeds the state to handle reporting the test.
955
result.startTest(test)
956
result.extractBenchmarkTime(test)
957
result.report_unsupported(test, feature)
958
# no output on unsupported features
960
[('update', '[1 in 0s] passing_test', None, None)
963
# the number of missing features should be printed in the progress
964
# summary, so check for that.
965
result.unsupported = {'foo':0, 'bar':0}
969
('update', '[2 in 0s, 2 missing features] passing_test', None, None),
973
def test_unavailable_exception(self):
974
"""An UnavailableFeature being raised should invoke addNotSupported."""
975
class InstrumentedTestResult(ExtendedTestResult):
977
def report_test_start(self, test): pass
978
def addNotSupported(self, test, feature):
979
self._call = test, feature
980
result = InstrumentedTestResult(None, None, None, None)
983
raise UnavailableFeature(feature)
984
test = unittest.FunctionTestCase(test_function)
986
# it should invoke 'addNotSupported'.
987
self.assertEqual(2, len(result._call))
988
self.assertEqual(test, result._call[0])
989
self.assertEqual(feature, result._call[1])
990
# and not count as an error
991
self.assertEqual(0, result.error_count)
993
def test_strict_with_unsupported_feature(self):
994
result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
996
test = self.get_passing_test()
997
feature = "Unsupported Feature"
998
result.addNotSupported(test, feature)
999
self.assertFalse(result.wasStrictlySuccessful())
1001
def test_strict_with_known_failure(self):
1002
result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
1004
test = self.get_passing_test()
1005
err = (KnownFailure, KnownFailure('foo'), None)
1006
result.addKnownFailure(test, err)
1007
self.assertFalse(result.wasStrictlySuccessful())
1009
def test_strict_with_success(self):
1010
result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
1012
test = self.get_passing_test()
1013
result.addSuccess(test)
1014
self.assertTrue(result.wasStrictlySuccessful())
1017
class TestRunner(TestCase):
1019
def dummy_test(self):
1022
def run_test_runner(self, testrunner, test):
1023
"""Run suite in testrunner, saving global state and restoring it.
1025
This current saves and restores:
1026
TestCaseInTempDir.TEST_ROOT
1028
There should be no tests in this file that use bzrlib.tests.TextTestRunner
1029
without using this convenience method, because of our use of global state.
1031
old_root = TestCaseInTempDir.TEST_ROOT
1033
TestCaseInTempDir.TEST_ROOT = None
1034
return testrunner.run(test)
1036
TestCaseInTempDir.TEST_ROOT = old_root
1038
def test_known_failure_failed_run(self):
1039
# run a test that generates a known failure which should be printed in
1040
# the final output when real failures occur.
1041
def known_failure_test():
1042
raise KnownFailure('failed')
1043
test = unittest.TestSuite()
1044
test.addTest(unittest.FunctionTestCase(known_failure_test))
1046
raise AssertionError('foo')
1047
test.addTest(unittest.FunctionTestCase(failing_test))
1049
runner = TextTestRunner(stream=stream)
1050
result = self.run_test_runner(runner, test)
1051
lines = stream.getvalue().splitlines()
1054
'======================================================================',
1055
'FAIL: unittest.FunctionTestCase (failing_test)',
1056
'----------------------------------------------------------------------',
1057
'Traceback (most recent call last):',
1058
' raise AssertionError(\'foo\')',
1059
'AssertionError: foo',
1061
'----------------------------------------------------------------------',
1063
'FAILED (failures=1, known_failure_count=1)'],
1064
lines[0:5] + lines[6:10] + lines[11:])
1066
def test_known_failure_ok_run(self):
1067
# run a test that generates a known failure which should be printed in the final output.
1068
def known_failure_test():
1069
raise KnownFailure('failed')
1070
test = unittest.FunctionTestCase(known_failure_test)
1072
runner = TextTestRunner(stream=stream)
1073
result = self.run_test_runner(runner, test)
1074
self.assertContainsRe(stream.getvalue(),
1077
'Ran 1 test in .*\n'
1079
'OK \\(known_failures=1\\)\n')
1081
def test_skipped_test(self):
1082
# run a test that is skipped, and check the suite as a whole still
1084
# skipping_test must be hidden in here so it's not run as a real test
1085
def skipping_test():
1086
raise TestSkipped('test intentionally skipped')
1088
runner = TextTestRunner(stream=self._log_file)
1089
test = unittest.FunctionTestCase(skipping_test)
1090
result = self.run_test_runner(runner, test)
1091
self.assertTrue(result.wasSuccessful())
1093
def test_skipped_from_setup(self):
1094
class SkippedSetupTest(TestCase):
1098
self.addCleanup(self.cleanup)
1099
raise TestSkipped('skipped setup')
1101
def test_skip(self):
1102
self.fail('test reached')
1107
runner = TextTestRunner(stream=self._log_file)
1108
test = SkippedSetupTest('test_skip')
1109
result = self.run_test_runner(runner, test)
1110
self.assertTrue(result.wasSuccessful())
1111
# Check if cleanup was called the right number of times.
1112
self.assertEqual(0, test.counter)
1114
def test_skipped_from_test(self):
1115
class SkippedTest(TestCase):
1119
self.addCleanup(self.cleanup)
1121
def test_skip(self):
1122
raise TestSkipped('skipped test')
1127
runner = TextTestRunner(stream=self._log_file)
1128
test = SkippedTest('test_skip')
1129
result = self.run_test_runner(runner, test)
1130
self.assertTrue(result.wasSuccessful())
1131
# Check if cleanup was called the right number of times.
1132
self.assertEqual(0, test.counter)
1134
def test_unsupported_features_listed(self):
1135
"""When unsupported features are encountered they are detailed."""
1136
class Feature1(Feature):
1137
def _probe(self): return False
1138
class Feature2(Feature):
1139
def _probe(self): return False
1140
# create sample tests
1141
test1 = SampleTestCase('_test_pass')
1142
test1._test_needs_features = [Feature1()]
1143
test2 = SampleTestCase('_test_pass')
1144
test2._test_needs_features = [Feature2()]
1145
test = unittest.TestSuite()
1149
runner = TextTestRunner(stream=stream)
1150
result = self.run_test_runner(runner, test)
1151
lines = stream.getvalue().splitlines()
1154
"Missing feature 'Feature1' skipped 1 tests.",
1155
"Missing feature 'Feature2' skipped 1 tests.",
1159
def test_bench_history(self):
1160
# tests that the running the benchmark produces a history file
1161
# containing a timestamp and the revision id of the bzrlib source which
1163
workingtree = _get_bzr_source_tree()
1164
test = TestRunner('dummy_test')
1166
runner = TextTestRunner(stream=self._log_file, bench_history=output)
1167
result = self.run_test_runner(runner, test)
1168
output_string = output.getvalue()
1169
self.assertContainsRe(output_string, "--date [0-9.]+")
1170
if workingtree is not None:
1171
revision_id = workingtree.get_parent_ids()[0]
1172
self.assertEndsWith(output_string.rstrip(), revision_id)
1174
def test_success_log_deleted(self):
1175
"""Successful tests have their log deleted"""
1177
class LogTester(TestCase):
1179
def test_success(self):
1180
self.log('this will be removed\n')
1182
sio = cStringIO.StringIO()
1183
runner = TextTestRunner(stream=sio)
1184
test = LogTester('test_success')
1185
result = self.run_test_runner(runner, test)
1187
log = test._get_log()
1188
self.assertEqual("DELETED log file to reduce memory footprint", log)
1189
self.assertEqual('', test._log_contents)
1190
self.assertIs(None, test._log_file_name)
1192
def test_fail_log_kept(self):
1193
"""Failed tests have their log kept"""
1195
class LogTester(TestCase):
1197
def test_fail(self):
1198
self.log('this will be kept\n')
1199
self.fail('this test fails')
1201
sio = cStringIO.StringIO()
1202
runner = TextTestRunner(stream=sio)
1203
test = LogTester('test_fail')
1204
result = self.run_test_runner(runner, test)
1206
text = sio.getvalue()
1207
self.assertContainsRe(text, 'this will be kept')
1208
self.assertContainsRe(text, 'this test fails')
1210
log = test._get_log()
1211
self.assertContainsRe(log, 'this will be kept')
1212
self.assertEqual(log, test._log_contents)
1214
def test_error_log_kept(self):
1215
"""Tests with errors have their log kept"""
1217
class LogTester(TestCase):
1219
def test_error(self):
1220
self.log('this will be kept\n')
1221
raise ValueError('random exception raised')
1223
sio = cStringIO.StringIO()
1224
runner = TextTestRunner(stream=sio)
1225
test = LogTester('test_error')
1226
result = self.run_test_runner(runner, test)
1228
text = sio.getvalue()
1229
self.assertContainsRe(text, 'this will be kept')
1230
self.assertContainsRe(text, 'random exception raised')
1232
log = test._get_log()
1233
self.assertContainsRe(log, 'this will be kept')
1234
self.assertEqual(log, test._log_contents)
1237
class SampleTestCase(TestCase):
1239
def _test_pass(self):
1243
class TestTestCase(TestCase):
1244
"""Tests that test the core bzrlib TestCase."""
1246
def test_debug_flags_sanitised(self):
1247
"""The bzrlib debug flags should be sanitised by setUp."""
1248
# we could set something and run a test that will check
1249
# it gets santised, but this is probably sufficient for now:
1250
# if someone runs the test with -Dsomething it will error.
1251
self.assertEqual(set(), bzrlib.debug.debug_flags)
1253
def inner_test(self):
1254
# the inner child test
1257
def outer_child(self):
1258
# the outer child test
1260
self.inner_test = TestTestCase("inner_child")
1261
result = bzrlib.tests.TextTestResult(self._log_file,
1264
self.inner_test.run(result)
1265
note("outer finish")
1267
def test_trace_nesting(self):
1268
# this tests that each test case nests its trace facility correctly.
1269
# we do this by running a test case manually. That test case (A)
1270
# should setup a new log, log content to it, setup a child case (B),
1271
# which should log independently, then case (A) should log a trailer
1273
# we do two nested children so that we can verify the state of the
1274
# logs after the outer child finishes is correct, which a bad clean
1275
# up routine in tearDown might trigger a fault in our test with only
1276
# one child, we should instead see the bad result inside our test with
1278
# the outer child test
1279
original_trace = bzrlib.trace._trace_file
1280
outer_test = TestTestCase("outer_child")
1281
result = bzrlib.tests.TextTestResult(self._log_file,
1284
outer_test.run(result)
1285
self.assertEqual(original_trace, bzrlib.trace._trace_file)
1287
def method_that_times_a_bit_twice(self):
1288
# call self.time twice to ensure it aggregates
1289
self.time(time.sleep, 0.007)
1290
self.time(time.sleep, 0.007)
1292
def test_time_creates_benchmark_in_result(self):
1293
"""Test that the TestCase.time() method accumulates a benchmark time."""
1294
sample_test = TestTestCase("method_that_times_a_bit_twice")
1295
output_stream = StringIO()
1296
result = bzrlib.tests.VerboseTestResult(
1297
unittest._WritelnDecorator(output_stream),
1300
num_tests=sample_test.countTestCases())
1301
sample_test.run(result)
1302
self.assertContainsRe(
1303
output_stream.getvalue(),
1304
r"\d+ms/ +\d+ms\n$")
1306
def test_hooks_sanitised(self):
1307
"""The bzrlib hooks should be sanitised by setUp."""
1308
self.assertEqual(bzrlib.branch.BranchHooks(),
1309
bzrlib.branch.Branch.hooks)
1310
self.assertEqual(bzrlib.smart.server.SmartServerHooks(),
1311
bzrlib.smart.server.SmartTCPServer.hooks)
1313
def test__gather_lsprof_in_benchmarks(self):
1314
"""When _gather_lsprof_in_benchmarks is on, accumulate profile data.
1316
Each self.time() call is individually and separately profiled.
1318
self.requireFeature(test_lsprof.LSProfFeature)
1319
# overrides the class member with an instance member so no cleanup
1321
self._gather_lsprof_in_benchmarks = True
1322
self.time(time.sleep, 0.000)
1323
self.time(time.sleep, 0.003)
1324
self.assertEqual(2, len(self._benchcalls))
1325
self.assertEqual((time.sleep, (0.000,), {}), self._benchcalls[0][0])
1326
self.assertEqual((time.sleep, (0.003,), {}), self._benchcalls[1][0])
1327
self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
1328
self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)
1330
def test_knownFailure(self):
1331
"""Self.knownFailure() should raise a KnownFailure exception."""
1332
self.assertRaises(KnownFailure, self.knownFailure, "A Failure")
1334
def test_requireFeature_available(self):
1335
"""self.requireFeature(available) is a no-op."""
1336
class Available(Feature):
1337
def _probe(self):return True
1338
feature = Available()
1339
self.requireFeature(feature)
1341
def test_requireFeature_unavailable(self):
1342
"""self.requireFeature(unavailable) raises UnavailableFeature."""
1343
class Unavailable(Feature):
1344
def _probe(self):return False
1345
feature = Unavailable()
1346
self.assertRaises(UnavailableFeature, self.requireFeature, feature)
1348
def test_run_no_parameters(self):
1349
test = SampleTestCase('_test_pass')
1352
def test_run_enabled_unittest_result(self):
1353
"""Test we revert to regular behaviour when the test is enabled."""
1354
test = SampleTestCase('_test_pass')
1355
class EnabledFeature(object):
1356
def available(self):
1358
test._test_needs_features = [EnabledFeature()]
1359
result = unittest.TestResult()
1361
self.assertEqual(1, result.testsRun)
1362
self.assertEqual([], result.errors)
1363
self.assertEqual([], result.failures)
1365
def test_run_disabled_unittest_result(self):
1366
"""Test our compatability for disabled tests with unittest results."""
1367
test = SampleTestCase('_test_pass')
1368
class DisabledFeature(object):
1369
def available(self):
1371
test._test_needs_features = [DisabledFeature()]
1372
result = unittest.TestResult()
1374
self.assertEqual(1, result.testsRun)
1375
self.assertEqual([], result.errors)
1376
self.assertEqual([], result.failures)
1378
def test_run_disabled_supporting_result(self):
1379
"""Test disabled tests behaviour with support aware results."""
1380
test = SampleTestCase('_test_pass')
1381
class DisabledFeature(object):
1382
def available(self):
1384
the_feature = DisabledFeature()
1385
test._test_needs_features = [the_feature]
1386
class InstrumentedTestResult(unittest.TestResult):
1388
unittest.TestResult.__init__(self)
1390
def startTest(self, test):
1391
self.calls.append(('startTest', test))
1392
def stopTest(self, test):
1393
self.calls.append(('stopTest', test))
1394
def addNotSupported(self, test, feature):
1395
self.calls.append(('addNotSupported', test, feature))
1396
result = InstrumentedTestResult()
1399
('startTest', test),
1400
('addNotSupported', test, the_feature),
1406
@symbol_versioning.deprecated_function(zero_eleven)
1407
def sample_deprecated_function():
1408
"""A deprecated function to test applyDeprecated with."""
1412
def sample_undeprecated_function(a_param):
1413
"""A undeprecated function to test applyDeprecated with."""
1416
class ApplyDeprecatedHelper(object):
1417
"""A helper class for ApplyDeprecated tests."""
1419
@symbol_versioning.deprecated_method(zero_eleven)
1420
def sample_deprecated_method(self, param_one):
1421
"""A deprecated method for testing with."""
1424
def sample_normal_method(self):
1425
"""A undeprecated method."""
1427
@symbol_versioning.deprecated_method(zero_ten)
1428
def sample_nested_deprecation(self):
1429
return sample_deprecated_function()
1432
class TestExtraAssertions(TestCase):
1433
"""Tests for new test assertions in bzrlib test suite"""
1435
def test_assert_isinstance(self):
1436
self.assertIsInstance(2, int)
1437
self.assertIsInstance(u'', basestring)
1438
self.assertRaises(AssertionError, self.assertIsInstance, None, int)
1439
self.assertRaises(AssertionError, self.assertIsInstance, 23.3, int)
1441
def test_assertEndsWith(self):
1442
self.assertEndsWith('foo', 'oo')
1443
self.assertRaises(AssertionError, self.assertEndsWith, 'o', 'oo')
1445
def test_applyDeprecated_not_deprecated(self):
1446
sample_object = ApplyDeprecatedHelper()
1447
# calling an undeprecated callable raises an assertion
1448
self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
1449
sample_object.sample_normal_method)
1450
self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
1451
sample_undeprecated_function, "a param value")
1452
# calling a deprecated callable (function or method) with the wrong
1453
# expected deprecation fails.
1454
self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
1455
sample_object.sample_deprecated_method, "a param value")
1456
self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
1457
sample_deprecated_function)
1458
# calling a deprecated callable (function or method) with the right
1459
# expected deprecation returns the functions result.
1460
self.assertEqual("a param value", self.applyDeprecated(zero_eleven,
1461
sample_object.sample_deprecated_method, "a param value"))
1462
self.assertEqual(2, self.applyDeprecated(zero_eleven,
1463
sample_deprecated_function))
1464
# calling a nested deprecation with the wrong deprecation version
1465
# fails even if a deeper nested function was deprecated with the
1467
self.assertRaises(AssertionError, self.applyDeprecated,
1468
zero_eleven, sample_object.sample_nested_deprecation)
1469
# calling a nested deprecation with the right deprecation value
1470
# returns the calls result.
1471
self.assertEqual(2, self.applyDeprecated(zero_ten,
1472
sample_object.sample_nested_deprecation))
1474
def test_callDeprecated(self):
1475
def testfunc(be_deprecated, result=None):
1476
if be_deprecated is True:
1477
symbol_versioning.warn('i am deprecated', DeprecationWarning,
1480
result = self.callDeprecated(['i am deprecated'], testfunc, True)
1481
self.assertIs(None, result)
1482
result = self.callDeprecated([], testfunc, False, 'result')
1483
self.assertEqual('result', result)
1484
self.callDeprecated(['i am deprecated'], testfunc, be_deprecated=True)
1485
self.callDeprecated([], testfunc, be_deprecated=False)
1488
class TestConvenienceMakers(TestCaseWithTransport):
1489
"""Test for the make_* convenience functions."""
1491
def test_make_branch_and_tree_with_format(self):
1492
# we should be able to supply a format to make_branch_and_tree
1493
self.make_branch_and_tree('a', format=bzrlib.bzrdir.BzrDirMetaFormat1())
1494
self.make_branch_and_tree('b', format=bzrlib.bzrdir.BzrDirFormat6())
1495
self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('a')._format,
1496
bzrlib.bzrdir.BzrDirMetaFormat1)
1497
self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
1498
bzrlib.bzrdir.BzrDirFormat6)
1500
def test_make_branch_and_memory_tree(self):
1501
# we should be able to get a new branch and a mutable tree from
1502
# TestCaseWithTransport
1503
tree = self.make_branch_and_memory_tree('a')
1504
self.assertIsInstance(tree, bzrlib.memorytree.MemoryTree)
1507
class TestSFTPMakeBranchAndTree(TestCaseWithSFTPServer):
1509
def test_make_tree_for_sftp_branch(self):
1510
"""Transports backed by local directories create local trees."""
1512
tree = self.make_branch_and_tree('t1')
1513
base = tree.bzrdir.root_transport.base
1514
self.failIf(base.startswith('sftp'),
1515
'base %r is on sftp but should be local' % base)
1516
self.assertEquals(tree.bzrdir.root_transport,
1517
tree.branch.bzrdir.root_transport)
1518
self.assertEquals(tree.bzrdir.root_transport,
1519
tree.branch.repository.bzrdir.root_transport)
1522
class TestSelftest(TestCase):
1523
"""Tests of bzrlib.tests.selftest."""
1525
def test_selftest_benchmark_parameter_invokes_test_suite__benchmark__(self):
1528
factory_called.append(True)
1532
self.apply_redirected(out, err, None, bzrlib.tests.selftest,
1533
test_suite_factory=factory)
1534
self.assertEqual([True], factory_called)
1537
class TestKnownFailure(TestCase):
1539
def test_known_failure(self):
1540
"""Check that KnownFailure is defined appropriately."""
1541
# a KnownFailure is an assertion error for compatability with unaware
1543
self.assertIsInstance(KnownFailure(""), AssertionError)
1545
def test_expect_failure(self):
1547
self.expectFailure("Doomed to failure", self.assertTrue, False)
1548
except KnownFailure, e:
1549
self.assertEqual('Doomed to failure', e.args[0])
1551
self.expectFailure("Doomed to failure", self.assertTrue, True)
1552
except AssertionError, e:
1553
self.assertEqual('Unexpected success. Should have failed:'
1554
' Doomed to failure', e.args[0])
1556
self.fail('Assertion not raised')
1559
class TestFeature(TestCase):
1561
def test_caching(self):
1562
"""Feature._probe is called by the feature at most once."""
1563
class InstrumentedFeature(Feature):
1565
Feature.__init__(self)
1568
self.calls.append('_probe')
1570
feature = InstrumentedFeature()
1572
self.assertEqual(['_probe'], feature.calls)
1574
self.assertEqual(['_probe'], feature.calls)
1576
def test_named_str(self):
1577
"""Feature.__str__ should thunk to feature_name()."""
1578
class NamedFeature(Feature):
1579
def feature_name(self):
1581
feature = NamedFeature()
1582
self.assertEqual('symlinks', str(feature))
1584
def test_default_str(self):
1585
"""Feature.__str__ should default to __class__.__name__."""
1586
class NamedFeature(Feature):
1588
feature = NamedFeature()
1589
self.assertEqual('NamedFeature', str(feature))
1592
class TestUnavailableFeature(TestCase):
1594
def test_access_feature(self):
1596
exception = UnavailableFeature(feature)
1597
self.assertIs(feature, exception.args[0])
1600
class TestSelftestFiltering(TestCase):
1603
self.suite = TestUtil.TestSuite()
1604
self.loader = TestUtil.TestLoader()
1605
self.suite.addTest(self.loader.loadTestsFromModuleNames([
1606
'bzrlib.tests.test_selftest']))
1607
self.all_names = [t.id() for t in iter_suite_tests(self.suite)]
1609
def test_filter_suite_by_re(self):
1610
filtered_suite = filter_suite_by_re(self.suite, 'test_filter')
1611
filtered_names = [t.id() for t in iter_suite_tests(filtered_suite)]
1612
self.assertEqual(filtered_names, ['bzrlib.tests.test_selftest.'
1613
'TestSelftestFiltering.test_filter_suite_by_re'])
1615
def test_sort_suite_by_re(self):
1616
sorted_suite = sort_suite_by_re(self.suite, 'test_filter')
1617
sorted_names = [t.id() for t in iter_suite_tests(sorted_suite)]
1618
self.assertEqual(sorted_names[0], 'bzrlib.tests.test_selftest.'
1619
'TestSelftestFiltering.test_filter_suite_by_re')
1620
self.assertEquals(sorted(self.all_names), sorted(sorted_names))
1623
class TestCheckInventoryShape(TestCaseWithTransport):
1625
def test_check_inventory_shape(self):
1626
files = ['a', 'b/', 'b/c']
1627
tree = self.make_branch_and_tree('.')
1628
self.build_tree(files)
1632
self.check_inventory_shape(tree.inventory, files)