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,
53
clean_selftest_output,
60
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
61
from bzrlib.tests.TestUtil import _load_module_by_name
62
from bzrlib.trace import note
63
from bzrlib.transport.memory import MemoryServer, MemoryTransport
64
from bzrlib.version import _get_bzr_source_tree
67
class SelftestTests(TestCase):
69
def test_import_tests(self):
70
mod = _load_module_by_name('bzrlib.tests.test_selftest')
71
self.assertEqual(mod.SelftestTests, SelftestTests)
73
def test_import_test_failure(self):
74
self.assertRaises(ImportError,
78
class MetaTestLog(TestCase):
80
def test_logging(self):
81
"""Test logs are captured when a test fails."""
82
self.log('a test message')
83
self._log_file.flush()
84
self.assertContainsRe(self._get_log(keep_log_file=True),
88
class TestTreeShape(TestCaseInTempDir):
90
def test_unicode_paths(self):
91
filename = u'hell\u00d8'
93
self.build_tree_contents([(filename, 'contents of hello')])
94
except UnicodeEncodeError:
95
raise TestSkipped("can't build unicode working tree in "
96
"filesystem encoding %s" % sys.getfilesystemencoding())
97
self.failUnlessExists(filename)
100
class TestTransportProviderAdapter(TestCase):
101
"""A group of tests that test the transport implementation adaption core.
103
This is a meta test that the tests are applied to all available
106
This will be generalised in the future which is why it is in this
107
test file even though it is specific to transport tests at the moment.
110
def test_get_transport_permutations(self):
111
# this checks that we the module get_test_permutations call
112
# is made by the adapter get_transport_test_permitations method.
113
class MockModule(object):
114
def get_test_permutations(self):
115
return sample_permutation
116
sample_permutation = [(1,2), (3,4)]
117
from bzrlib.tests.test_transport_implementations \
118
import TransportTestProviderAdapter
119
adapter = TransportTestProviderAdapter()
120
self.assertEqual(sample_permutation,
121
adapter.get_transport_test_permutations(MockModule()))
123
def test_adapter_checks_all_modules(self):
124
# this checks that the adapter returns as many permurtations as
125
# there are in all the registered# transport modules for there
126
# - we assume if this matches its probably doing the right thing
127
# especially in combination with the tests for setting the right
129
from bzrlib.tests.test_transport_implementations \
130
import TransportTestProviderAdapter
131
from bzrlib.transport import _get_transport_modules
132
modules = _get_transport_modules()
133
permutation_count = 0
134
for module in modules:
136
permutation_count += len(reduce(getattr,
137
(module + ".get_test_permutations").split('.')[1:],
138
__import__(module))())
139
except errors.DependencyNotPresent:
141
input_test = TestTransportProviderAdapter(
142
"test_adapter_sets_transport_class")
143
adapter = TransportTestProviderAdapter()
144
self.assertEqual(permutation_count,
145
len(list(iter(adapter.adapt(input_test)))))
147
def test_adapter_sets_transport_class(self):
148
# Check that the test adapter inserts a transport and server into the
151
# This test used to know about all the possible transports and the
152
# order they were returned but that seems overly brittle (mbp
154
from bzrlib.tests.test_transport_implementations \
155
import TransportTestProviderAdapter
156
scenarios = TransportTestProviderAdapter().scenarios
157
# there are at least that many builtin transports
158
self.assertTrue(len(scenarios) > 6)
159
one_scenario = scenarios[0]
160
self.assertIsInstance(one_scenario[0], str)
161
self.assertTrue(issubclass(one_scenario[1]["transport_class"],
162
bzrlib.transport.Transport))
163
self.assertTrue(issubclass(one_scenario[1]["transport_server"],
164
bzrlib.transport.Server))
167
class TestBranchProviderAdapter(TestCase):
168
"""A group of tests that test the branch implementation test adapter."""
170
def test_constructor(self):
171
# check that constructor parameters are passed through to the adapted
173
from bzrlib.tests.branch_implementations import BranchTestProviderAdapter
176
formats = [("c", "C"), ("d", "D")]
177
adapter = BranchTestProviderAdapter(server1, server2, formats)
178
self.assertEqual(2, len(adapter.scenarios))
181
{'branch_format': 'c',
182
'bzrdir_format': 'C',
183
'transport_readonly_server': 'b',
184
'transport_server': 'a'}),
186
{'branch_format': 'd',
187
'bzrdir_format': 'D',
188
'transport_readonly_server': 'b',
189
'transport_server': 'a'})],
193
class TestBzrDirProviderAdapter(TestCase):
194
"""A group of tests that test the bzr dir implementation test adapter."""
196
def test_adapted_tests(self):
197
# check that constructor parameters are passed through to the adapted
199
from bzrlib.tests.bzrdir_implementations import BzrDirTestProviderAdapter
204
adapter = BzrDirTestProviderAdapter(vfs_factory,
205
server1, server2, formats)
208
{'bzrdir_format': 'c',
209
'transport_readonly_server': 'b',
210
'transport_server': 'a',
211
'vfs_transport_factory': 'v'}),
213
{'bzrdir_format': 'd',
214
'transport_readonly_server': 'b',
215
'transport_server': 'a',
216
'vfs_transport_factory': 'v'})],
220
class TestRepositoryProviderAdapter(TestCase):
221
"""A group of tests that test the repository implementation test adapter."""
223
def test_constructor(self):
224
# check that constructor parameters are passed through to the
226
from bzrlib.tests.repository_implementations import RepositoryTestProviderAdapter
229
formats = [("c", "C"), ("d", "D")]
230
adapter = RepositoryTestProviderAdapter(server1, server2, formats)
233
{'bzrdir_format': 'C',
234
'repository_format': 'c',
235
'transport_readonly_server': 'b',
236
'transport_server': 'a'}),
238
{'bzrdir_format': 'D',
239
'repository_format': 'd',
240
'transport_readonly_server': 'b',
241
'transport_server': 'a'})],
244
def test_setting_vfs_transport(self):
245
"""The vfs_transport_factory can be set optionally."""
246
from bzrlib.tests.repository_implementations import RepositoryTestProviderAdapter
247
formats = [("a", "b"), ("c", "d")]
248
adapter = RepositoryTestProviderAdapter(None, None, formats,
249
vfs_transport_factory="vfs")
252
{'bzrdir_format': 'b',
253
'repository_format': 'a',
254
'transport_readonly_server': None,
255
'transport_server': None,
256
'vfs_transport_factory': 'vfs'}),
258
{'bzrdir_format': 'd',
259
'repository_format': 'c',
260
'transport_readonly_server': None,
261
'transport_server': None,
262
'vfs_transport_factory': 'vfs'})],
265
def test_formats_to_scenarios(self):
266
"""The adapter can generate all the scenarios needed."""
267
from bzrlib.tests.repository_implementations import RepositoryTestProviderAdapter
268
no_vfs_adapter = RepositoryTestProviderAdapter("server", "readonly",
270
vfs_adapter = RepositoryTestProviderAdapter("server", "readonly",
271
[], vfs_transport_factory="vfs")
272
# no_vfs generate scenarios without vfs_transport_factor
273
formats = [("c", "C"), (1, "D")]
276
{'bzrdir_format': 'C',
277
'repository_format': 'c',
278
'transport_readonly_server': 'readonly',
279
'transport_server': 'server'}),
281
{'bzrdir_format': 'D',
282
'repository_format': 1,
283
'transport_readonly_server': 'readonly',
284
'transport_server': 'server'})],
285
no_vfs_adapter.formats_to_scenarios(formats))
288
{'bzrdir_format': 'C',
289
'repository_format': 'c',
290
'transport_readonly_server': 'readonly',
291
'transport_server': 'server',
292
'vfs_transport_factory': 'vfs'}),
294
{'bzrdir_format': 'D',
295
'repository_format': 1,
296
'transport_readonly_server': 'readonly',
297
'transport_server': 'server',
298
'vfs_transport_factory': 'vfs'})],
299
vfs_adapter.formats_to_scenarios(formats))
302
class TestTestScenarioApplier(TestCase):
303
"""Tests for the test adaption facilities."""
305
def test_adapt_applies_scenarios(self):
306
from bzrlib.tests.repository_implementations import TestScenarioApplier
307
input_test = TestTestScenarioApplier("test_adapt_test_to_scenario")
308
adapter = TestScenarioApplier()
309
adapter.scenarios = [("1", "dict"), ("2", "settings")]
311
def capture_call(test, scenario):
312
calls.append((test, scenario))
314
adapter.adapt_test_to_scenario = capture_call
315
adapter.adapt(input_test)
316
self.assertEqual([(input_test, ("1", "dict")),
317
(input_test, ("2", "settings"))], calls)
319
def test_adapt_test_to_scenario(self):
320
from bzrlib.tests.repository_implementations import TestScenarioApplier
321
input_test = TestTestScenarioApplier("test_adapt_test_to_scenario")
322
adapter = TestScenarioApplier()
323
# setup two adapted tests
324
adapted_test1 = adapter.adapt_test_to_scenario(input_test,
326
{"bzrdir_format":"bzr_format",
327
"repository_format":"repo_fmt",
328
"transport_server":"transport_server",
329
"transport_readonly_server":"readonly-server"}))
330
adapted_test2 = adapter.adapt_test_to_scenario(input_test,
331
("new id 2", {"bzrdir_format":None}))
332
# input_test should have been altered.
333
self.assertRaises(AttributeError, getattr, input_test, "bzrdir_format")
334
# the new tests are mutually incompatible, ensuring it has
335
# made new ones, and unspecified elements in the scenario
336
# should not have been altered.
337
self.assertEqual("bzr_format", adapted_test1.bzrdir_format)
338
self.assertEqual("repo_fmt", adapted_test1.repository_format)
339
self.assertEqual("transport_server", adapted_test1.transport_server)
340
self.assertEqual("readonly-server",
341
adapted_test1.transport_readonly_server)
343
"bzrlib.tests.test_selftest.TestTestScenarioApplier."
344
"test_adapt_test_to_scenario(new id)",
346
self.assertEqual(None, adapted_test2.bzrdir_format)
348
"bzrlib.tests.test_selftest.TestTestScenarioApplier."
349
"test_adapt_test_to_scenario(new id 2)",
353
class TestInterRepositoryProviderAdapter(TestCase):
354
"""A group of tests that test the InterRepository test adapter."""
356
def test_adapted_tests(self):
357
# check that constructor parameters are passed through to the adapted
359
from bzrlib.tests.interrepository_implementations import \
360
InterRepositoryTestProviderAdapter
363
formats = [(str, "C1", "C2"), (int, "D1", "D2")]
364
adapter = InterRepositoryTestProviderAdapter(server1, server2, formats)
367
{'interrepo_class': str,
368
'repository_format': 'C1',
369
'repository_format_to': 'C2',
370
'transport_readonly_server': 'b',
371
'transport_server': 'a'}),
373
{'interrepo_class': int,
374
'repository_format': 'D1',
375
'repository_format_to': 'D2',
376
'transport_readonly_server': 'b',
377
'transport_server': 'a'})],
378
adapter.formats_to_scenarios(formats))
381
class TestInterVersionedFileProviderAdapter(TestCase):
382
"""A group of tests that test the InterVersionedFile test adapter."""
384
def test_scenarios(self):
385
# check that constructor parameters are passed through to the adapted
387
from bzrlib.tests.interversionedfile_implementations \
388
import InterVersionedFileTestProviderAdapter
391
formats = [(str, "C1", "C2"), (int, "D1", "D2")]
392
adapter = InterVersionedFileTestProviderAdapter(server1, server2, formats)
395
{'interversionedfile_class':str,
396
'transport_readonly_server': 'b',
397
'transport_server': 'a',
398
'versionedfile_factory': 'C1',
399
'versionedfile_factory_to': 'C2'}),
401
{'interversionedfile_class': int,
402
'transport_readonly_server': 'b',
403
'transport_server': 'a',
404
'versionedfile_factory': 'D1',
405
'versionedfile_factory_to': 'D2'})],
409
class TestRevisionStoreProviderAdapter(TestCase):
410
"""A group of tests that test the RevisionStore test adapter."""
412
def test_scenarios(self):
413
# check that constructor parameters are passed through to the adapted
415
from bzrlib.tests.revisionstore_implementations \
416
import RevisionStoreTestProviderAdapter
417
# revision stores need a store factory - i.e. RevisionKnit
418
#, a readonly and rw transport
422
store_factories = ["c", "d"]
423
adapter = RevisionStoreTestProviderAdapter(server1, server2, store_factories)
426
{'store_factory': 'c',
427
'transport_readonly_server': 'b',
428
'transport_server': 'a'}),
430
{'store_factory': 'd',
431
'transport_readonly_server': 'b',
432
'transport_server': 'a'})],
436
class TestWorkingTreeProviderAdapter(TestCase):
437
"""A group of tests that test the workingtree implementation test adapter."""
439
def test_scenarios(self):
440
# check that constructor parameters are passed through to the adapted
442
from bzrlib.tests.workingtree_implementations \
443
import WorkingTreeTestProviderAdapter
446
formats = [("c", "C"), ("d", "D")]
447
adapter = WorkingTreeTestProviderAdapter(server1, server2, formats)
450
{'bzrdir_format': 'C',
451
'transport_readonly_server': 'b',
452
'transport_server': 'a',
453
'workingtree_format': 'c'}),
455
{'bzrdir_format': 'D',
456
'transport_readonly_server': 'b',
457
'transport_server': 'a',
458
'workingtree_format': 'd'})],
462
class TestTreeProviderAdapter(TestCase):
463
"""Test the setup of tree_implementation tests."""
465
def test_adapted_tests(self):
466
# the tree implementation adapter is meant to setup one instance for
467
# each working tree format, and one additional instance that will
468
# use the default wt format, but create a revision tree for the tests.
469
# this means that the wt ones should have the workingtree_to_test_tree
470
# attribute set to 'return_parameter' and the revision one set to
471
# revision_tree_from_workingtree.
473
from bzrlib.tests.tree_implementations import (
474
TreeTestProviderAdapter,
476
revision_tree_from_workingtree
478
from bzrlib.workingtree import WorkingTreeFormat, WorkingTreeFormat3
479
input_test = TestTreeProviderAdapter(
480
"test_adapted_tests")
483
formats = [("c", "C"), ("d", "D")]
484
adapter = TreeTestProviderAdapter(server1, server2, formats)
485
suite = adapter.adapt(input_test)
486
tests = list(iter(suite))
487
self.assertEqual(4, len(tests))
488
# this must match the default format setp up in
489
# TreeTestProviderAdapter.adapt
490
default_format = WorkingTreeFormat3
491
self.assertEqual(tests[0].workingtree_format, formats[0][0])
492
self.assertEqual(tests[0].bzrdir_format, formats[0][1])
493
self.assertEqual(tests[0].transport_server, server1)
494
self.assertEqual(tests[0].transport_readonly_server, server2)
495
self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
496
self.assertEqual(tests[1].workingtree_format, formats[1][0])
497
self.assertEqual(tests[1].bzrdir_format, formats[1][1])
498
self.assertEqual(tests[1].transport_server, server1)
499
self.assertEqual(tests[1].transport_readonly_server, server2)
500
self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
501
self.assertIsInstance(tests[2].workingtree_format, default_format)
502
#self.assertEqual(tests[2].bzrdir_format,
503
# default_format._matchingbzrdir)
504
self.assertEqual(tests[2].transport_server, server1)
505
self.assertEqual(tests[2].transport_readonly_server, server2)
506
self.assertEqual(tests[2].workingtree_to_test_tree,
507
revision_tree_from_workingtree)
510
class TestInterTreeProviderAdapter(TestCase):
511
"""A group of tests that test the InterTreeTestAdapter."""
513
def test_adapted_tests(self):
514
# check that constructor parameters are passed through to the adapted
516
# for InterTree tests we want the machinery to bring up two trees in
517
# each instance: the base one, and the one we are interacting with.
518
# because each optimiser can be direction specific, we need to test
519
# each optimiser in its chosen direction.
520
# unlike the TestProviderAdapter we dont want to automatically add a
521
# parameterised one for WorkingTree - the optimisers will tell us what
523
from bzrlib.tests.tree_implementations import (
525
revision_tree_from_workingtree
527
from bzrlib.tests.intertree_implementations import (
528
InterTreeTestProviderAdapter,
530
from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
531
input_test = TestInterTreeProviderAdapter(
532
"test_adapted_tests")
535
format1 = WorkingTreeFormat2()
536
format2 = WorkingTreeFormat3()
537
formats = [(str, format1, format2, "converter1"),
538
(int, format2, format1, "converter2")]
539
adapter = InterTreeTestProviderAdapter(server1, server2, formats)
540
suite = adapter.adapt(input_test)
541
tests = list(iter(suite))
542
self.assertEqual(2, len(tests))
543
self.assertEqual(tests[0].intertree_class, formats[0][0])
544
self.assertEqual(tests[0].workingtree_format, formats[0][1])
545
self.assertEqual(tests[0].workingtree_format_to, formats[0][2])
546
self.assertEqual(tests[0].mutable_trees_to_test_trees, formats[0][3])
547
self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
548
self.assertEqual(tests[0].transport_server, server1)
549
self.assertEqual(tests[0].transport_readonly_server, server2)
550
self.assertEqual(tests[1].intertree_class, formats[1][0])
551
self.assertEqual(tests[1].workingtree_format, formats[1][1])
552
self.assertEqual(tests[1].workingtree_format_to, formats[1][2])
553
self.assertEqual(tests[1].mutable_trees_to_test_trees, formats[1][3])
554
self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
555
self.assertEqual(tests[1].transport_server, server1)
556
self.assertEqual(tests[1].transport_readonly_server, server2)
559
class TestTestCaseInTempDir(TestCaseInTempDir):
561
def test_home_is_not_working(self):
562
self.assertNotEqual(self.test_dir, self.test_home_dir)
563
cwd = osutils.getcwd()
564
self.assertEqual(self.test_dir, cwd)
565
self.assertEqual(self.test_home_dir, os.environ['HOME'])
568
class TestTestCaseWithMemoryTransport(TestCaseWithMemoryTransport):
570
def test_home_is_non_existant_dir_under_root(self):
571
"""The test_home_dir for TestCaseWithMemoryTransport is missing.
573
This is because TestCaseWithMemoryTransport is for tests that do not
574
need any disk resources: they should be hooked into bzrlib in such a
575
way that no global settings are being changed by the test (only a
576
few tests should need to do that), and having a missing dir as home is
577
an effective way to ensure that this is the case.
579
self.assertEqual(self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
581
self.assertEqual(self.test_home_dir, os.environ['HOME'])
583
def test_cwd_is_TEST_ROOT(self):
584
self.assertEqual(self.test_dir, self.TEST_ROOT)
585
cwd = osutils.getcwd()
586
self.assertEqual(self.test_dir, cwd)
588
def test_make_branch_and_memory_tree(self):
589
"""In TestCaseWithMemoryTransport we should not make the branch on disk.
591
This is hard to comprehensively robustly test, so we settle for making
592
a branch and checking no directory was created at its relpath.
594
tree = self.make_branch_and_memory_tree('dir')
595
# Guard against regression into MemoryTransport leaking
596
# files to disk instead of keeping them in memory.
597
self.failIf(osutils.lexists('dir'))
598
self.assertIsInstance(tree, memorytree.MemoryTree)
600
def test_make_branch_and_memory_tree_with_format(self):
601
"""make_branch_and_memory_tree should accept a format option."""
602
format = bzrdir.BzrDirMetaFormat1()
603
format.repository_format = weaverepo.RepositoryFormat7()
604
tree = self.make_branch_and_memory_tree('dir', format=format)
605
# Guard against regression into MemoryTransport leaking
606
# files to disk instead of keeping them in memory.
607
self.failIf(osutils.lexists('dir'))
608
self.assertIsInstance(tree, memorytree.MemoryTree)
609
self.assertEqual(format.repository_format.__class__,
610
tree.branch.repository._format.__class__)
613
class TestTestCaseWithTransport(TestCaseWithTransport):
614
"""Tests for the convenience functions TestCaseWithTransport introduces."""
616
def test_get_readonly_url_none(self):
617
from bzrlib.transport import get_transport
618
from bzrlib.transport.memory import MemoryServer
619
from bzrlib.transport.readonly import ReadonlyTransportDecorator
620
self.vfs_transport_factory = MemoryServer
621
self.transport_readonly_server = None
622
# calling get_readonly_transport() constructs a decorator on the url
624
url = self.get_readonly_url()
625
url2 = self.get_readonly_url('foo/bar')
626
t = get_transport(url)
627
t2 = get_transport(url2)
628
self.failUnless(isinstance(t, ReadonlyTransportDecorator))
629
self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
630
self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
632
def test_get_readonly_url_http(self):
633
from bzrlib.tests.HttpServer import HttpServer
634
from bzrlib.transport import get_transport
635
from bzrlib.transport.local import LocalURLServer
636
from bzrlib.transport.http import HttpTransportBase
637
self.transport_server = LocalURLServer
638
self.transport_readonly_server = HttpServer
639
# calling get_readonly_transport() gives us a HTTP server instance.
640
url = self.get_readonly_url()
641
url2 = self.get_readonly_url('foo/bar')
642
# the transport returned may be any HttpTransportBase subclass
643
t = get_transport(url)
644
t2 = get_transport(url2)
645
self.failUnless(isinstance(t, HttpTransportBase))
646
self.failUnless(isinstance(t2, HttpTransportBase))
647
self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
649
def test_is_directory(self):
650
"""Test assertIsDirectory assertion"""
651
t = self.get_transport()
652
self.build_tree(['a_dir/', 'a_file'], transport=t)
653
self.assertIsDirectory('a_dir', t)
654
self.assertRaises(AssertionError, self.assertIsDirectory, 'a_file', t)
655
self.assertRaises(AssertionError, self.assertIsDirectory, 'not_here', t)
658
class TestTestCaseTransports(TestCaseWithTransport):
661
super(TestTestCaseTransports, self).setUp()
662
self.vfs_transport_factory = MemoryServer
664
def test_make_bzrdir_preserves_transport(self):
665
t = self.get_transport()
666
result_bzrdir = self.make_bzrdir('subdir')
667
self.assertIsInstance(result_bzrdir.transport,
669
# should not be on disk, should only be in memory
670
self.failIfExists('subdir')
673
class TestChrootedTest(ChrootedTestCase):
675
def test_root_is_root(self):
676
from bzrlib.transport import get_transport
677
t = get_transport(self.get_readonly_url())
679
self.assertEqual(url, t.clone('..').base)
682
class MockProgress(_BaseProgressBar):
683
"""Progress-bar standin that records calls.
685
Useful for testing pb using code.
689
_BaseProgressBar.__init__(self)
693
self.calls.append(('tick',))
695
def update(self, msg=None, current=None, total=None):
696
self.calls.append(('update', msg, current, total))
699
self.calls.append(('clear',))
701
def note(self, msg, *args):
702
self.calls.append(('note', msg, args))
705
class TestTestResult(TestCase):
707
def test_elapsed_time_with_benchmarking(self):
708
result = bzrlib.tests.TextTestResult(self._log_file,
712
result._recordTestStartTime()
714
result.extractBenchmarkTime(self)
715
timed_string = result._testTimeString()
716
# without explicit benchmarking, we should get a simple time.
717
self.assertContainsRe(timed_string, "^ +[0-9]+ms$")
718
# if a benchmark time is given, we want a x of y style result.
719
self.time(time.sleep, 0.001)
720
result.extractBenchmarkTime(self)
721
timed_string = result._testTimeString()
722
self.assertContainsRe(
723
timed_string, "^ +[0-9]+ms/ +[0-9]+ms$")
724
# extracting the time from a non-bzrlib testcase sets to None
725
result._recordTestStartTime()
726
result.extractBenchmarkTime(
727
unittest.FunctionTestCase(self.test_elapsed_time_with_benchmarking))
728
timed_string = result._testTimeString()
729
self.assertContainsRe(timed_string, "^ +[0-9]+ms$")
730
# cheat. Yes, wash thy mouth out with soap.
731
self._benchtime = None
733
def test_assigned_benchmark_file_stores_date(self):
735
result = bzrlib.tests.TextTestResult(self._log_file,
740
output_string = output.getvalue()
742
# if you are wondering about the regexp please read the comment in
743
# test_bench_history (bzrlib.tests.test_selftest.TestRunner)
744
# XXX: what comment? -- Andrew Bennetts
745
self.assertContainsRe(output_string, "--date [0-9.]+")
747
def test_benchhistory_records_test_times(self):
748
result_stream = StringIO()
749
result = bzrlib.tests.TextTestResult(
753
bench_history=result_stream
756
# we want profile a call and check that its test duration is recorded
757
# make a new test instance that when run will generate a benchmark
758
example_test_case = TestTestResult("_time_hello_world_encoding")
759
# execute the test, which should succeed and record times
760
example_test_case.run(result)
761
lines = result_stream.getvalue().splitlines()
762
self.assertEqual(2, len(lines))
763
self.assertContainsRe(lines[1],
764
" *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
765
"._time_hello_world_encoding")
767
def _time_hello_world_encoding(self):
768
"""Profile two sleep calls
770
This is used to exercise the test framework.
772
self.time(unicode, 'hello', errors='replace')
773
self.time(unicode, 'world', errors='replace')
775
def test_lsprofiling(self):
776
"""Verbose test result prints lsprof statistics from test cases."""
777
self.requireFeature(test_lsprof.LSProfFeature)
778
result_stream = StringIO()
779
result = bzrlib.tests.VerboseTestResult(
780
unittest._WritelnDecorator(result_stream),
784
# we want profile a call of some sort and check it is output by
785
# addSuccess. We dont care about addError or addFailure as they
786
# are not that interesting for performance tuning.
787
# make a new test instance that when run will generate a profile
788
example_test_case = TestTestResult("_time_hello_world_encoding")
789
example_test_case._gather_lsprof_in_benchmarks = True
790
# execute the test, which should succeed and record profiles
791
example_test_case.run(result)
792
# lsprofile_something()
793
# if this worked we want
794
# LSProf output for <built in function unicode> (['hello'], {'errors': 'replace'})
795
# CallCount Recursive Total(ms) Inline(ms) module:lineno(function)
796
# (the lsprof header)
797
# ... an arbitrary number of lines
798
# and the function call which is time.sleep.
799
# 1 0 ??? ??? ???(sleep)
800
# and then repeated but with 'world', rather than 'hello'.
801
# this should appear in the output stream of our test result.
802
output = result_stream.getvalue()
803
self.assertContainsRe(output,
804
r"LSProf output for <type 'unicode'>\(\('hello',\), {'errors': 'replace'}\)")
805
self.assertContainsRe(output,
806
r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n")
807
self.assertContainsRe(output,
808
r"( +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n)?")
809
self.assertContainsRe(output,
810
r"LSProf output for <type 'unicode'>\(\('world',\), {'errors': 'replace'}\)\n")
812
def test_known_failure(self):
813
"""A KnownFailure being raised should trigger several result actions."""
814
class InstrumentedTestResult(ExtendedTestResult):
816
def report_test_start(self, test): pass
817
def report_known_failure(self, test, err):
818
self._call = test, err
819
result = InstrumentedTestResult(None, None, None, None)
821
raise KnownFailure('failed!')
822
test = unittest.FunctionTestCase(test_function)
824
# it should invoke 'report_known_failure'.
825
self.assertEqual(2, len(result._call))
826
self.assertEqual(test, result._call[0])
827
self.assertEqual(KnownFailure, result._call[1][0])
828
self.assertIsInstance(result._call[1][1], KnownFailure)
829
# we dont introspec the traceback, if the rest is ok, it would be
830
# exceptional for it not to be.
831
# it should update the known_failure_count on the object.
832
self.assertEqual(1, result.known_failure_count)
833
# the result should be successful.
834
self.assertTrue(result.wasSuccessful())
836
def test_verbose_report_known_failure(self):
837
# verbose test output formatting
838
result_stream = StringIO()
839
result = bzrlib.tests.VerboseTestResult(
840
unittest._WritelnDecorator(result_stream),
844
test = self.get_passing_test()
845
result.startTest(test)
846
result.extractBenchmarkTime(test)
847
prefix = len(result_stream.getvalue())
848
# the err parameter has the shape:
849
# (class, exception object, traceback)
850
# KnownFailures dont get their tracebacks shown though, so we
852
err = (KnownFailure, KnownFailure('foo'), None)
853
result.report_known_failure(test, err)
854
output = result_stream.getvalue()[prefix:]
855
lines = output.splitlines()
856
self.assertContainsRe(lines[0], r'XFAIL *\d+ms$')
857
self.assertEqual(lines[1], ' foo')
858
self.assertEqual(2, len(lines))
860
def test_text_report_known_failure(self):
861
# text test output formatting
863
result = bzrlib.tests.TextTestResult(
869
test = self.get_passing_test()
870
# this seeds the state to handle reporting the test.
871
result.startTest(test)
872
result.extractBenchmarkTime(test)
873
# the err parameter has the shape:
874
# (class, exception object, traceback)
875
# KnownFailures dont get their tracebacks shown though, so we
877
err = (KnownFailure, KnownFailure('foo'), None)
878
result.report_known_failure(test, err)
881
('update', '[1 in 0s] passing_test', None, None),
882
('note', 'XFAIL: %s\n%s\n', ('passing_test', err[1]))
885
# known_failures should be printed in the summary, so if we run a test
886
# after there are some known failures, the update prefix should match
888
result.known_failure_count = 3
892
('update', '[2 in 0s, 3 known failures] passing_test', None, None),
896
def get_passing_test(self):
897
"""Return a test object that can't be run usefully."""
900
return unittest.FunctionTestCase(passing_test)
902
def test_add_not_supported(self):
903
"""Test the behaviour of invoking addNotSupported."""
904
class InstrumentedTestResult(ExtendedTestResult):
905
def report_test_start(self, test): pass
906
def report_unsupported(self, test, feature):
907
self._call = test, feature
908
result = InstrumentedTestResult(None, None, None, None)
909
test = SampleTestCase('_test_pass')
911
result.startTest(test)
912
result.addNotSupported(test, feature)
913
# it should invoke 'report_unsupported'.
914
self.assertEqual(2, len(result._call))
915
self.assertEqual(test, result._call[0])
916
self.assertEqual(feature, result._call[1])
917
# the result should be successful.
918
self.assertTrue(result.wasSuccessful())
919
# it should record the test against a count of tests not run due to
921
self.assertEqual(1, result.unsupported['Feature'])
922
# and invoking it again should increment that counter
923
result.addNotSupported(test, feature)
924
self.assertEqual(2, result.unsupported['Feature'])
926
def test_verbose_report_unsupported(self):
927
# verbose test output formatting
928
result_stream = StringIO()
929
result = bzrlib.tests.VerboseTestResult(
930
unittest._WritelnDecorator(result_stream),
934
test = self.get_passing_test()
936
result.startTest(test)
937
result.extractBenchmarkTime(test)
938
prefix = len(result_stream.getvalue())
939
result.report_unsupported(test, feature)
940
output = result_stream.getvalue()[prefix:]
941
lines = output.splitlines()
942
self.assertEqual(lines, ['NODEP 0ms', " The feature 'Feature' is not available."])
944
def test_text_report_unsupported(self):
945
# text test output formatting
947
result = bzrlib.tests.TextTestResult(
953
test = self.get_passing_test()
955
# this seeds the state to handle reporting the test.
956
result.startTest(test)
957
result.extractBenchmarkTime(test)
958
result.report_unsupported(test, feature)
959
# no output on unsupported features
961
[('update', '[1 in 0s] passing_test', None, None)
964
# the number of missing features should be printed in the progress
965
# summary, so check for that.
966
result.unsupported = {'foo':0, 'bar':0}
970
('update', '[2 in 0s, 2 missing features] passing_test', None, None),
974
def test_unavailable_exception(self):
975
"""An UnavailableFeature being raised should invoke addNotSupported."""
976
class InstrumentedTestResult(ExtendedTestResult):
978
def report_test_start(self, test): pass
979
def addNotSupported(self, test, feature):
980
self._call = test, feature
981
result = InstrumentedTestResult(None, None, None, None)
984
raise UnavailableFeature(feature)
985
test = unittest.FunctionTestCase(test_function)
987
# it should invoke 'addNotSupported'.
988
self.assertEqual(2, len(result._call))
989
self.assertEqual(test, result._call[0])
990
self.assertEqual(feature, result._call[1])
991
# and not count as an error
992
self.assertEqual(0, result.error_count)
995
class TestRunner(TestCase):
997
def dummy_test(self):
1000
def run_test_runner(self, testrunner, test):
1001
"""Run suite in testrunner, saving global state and restoring it.
1003
This current saves and restores:
1004
TestCaseInTempDir.TEST_ROOT
1006
There should be no tests in this file that use bzrlib.tests.TextTestRunner
1007
without using this convenience method, because of our use of global state.
1009
old_root = TestCaseInTempDir.TEST_ROOT
1011
TestCaseInTempDir.TEST_ROOT = None
1012
return testrunner.run(test)
1014
TestCaseInTempDir.TEST_ROOT = old_root
1016
def test_known_failure_failed_run(self):
1017
# run a test that generates a known failure which should be printed in
1018
# the final output when real failures occur.
1019
def known_failure_test():
1020
raise KnownFailure('failed')
1021
test = unittest.TestSuite()
1022
test.addTest(unittest.FunctionTestCase(known_failure_test))
1024
raise AssertionError('foo')
1025
test.addTest(unittest.FunctionTestCase(failing_test))
1027
runner = TextTestRunner(stream=stream)
1028
result = self.run_test_runner(runner, test)
1029
lines = stream.getvalue().splitlines()
1032
'======================================================================',
1033
'FAIL: unittest.FunctionTestCase (failing_test)',
1034
'----------------------------------------------------------------------',
1035
'Traceback (most recent call last):',
1036
' raise AssertionError(\'foo\')',
1037
'AssertionError: foo',
1039
'----------------------------------------------------------------------',
1041
'FAILED (failures=1, known_failure_count=1)'],
1042
lines[0:5] + lines[6:10] + lines[11:])
1044
def test_known_failure_ok_run(self):
1045
# run a test that generates a known failure which should be printed in the final output.
1046
def known_failure_test():
1047
raise KnownFailure('failed')
1048
test = unittest.FunctionTestCase(known_failure_test)
1050
runner = TextTestRunner(stream=stream)
1051
result = self.run_test_runner(runner, test)
1052
self.assertContainsRe(stream.getvalue(),
1055
'Ran 1 test in .*\n'
1057
'OK \\(known_failures=1\\)\n')
1059
def test_skipped_test(self):
1060
# run a test that is skipped, and check the suite as a whole still
1062
# skipping_test must be hidden in here so it's not run as a real test
1063
def skipping_test():
1064
raise TestSkipped('test intentionally skipped')
1066
runner = TextTestRunner(stream=self._log_file)
1067
test = unittest.FunctionTestCase(skipping_test)
1068
result = self.run_test_runner(runner, test)
1069
self.assertTrue(result.wasSuccessful())
1071
def test_skipped_from_setup(self):
1072
class SkippedSetupTest(TestCase):
1076
self.addCleanup(self.cleanup)
1077
raise TestSkipped('skipped setup')
1079
def test_skip(self):
1080
self.fail('test reached')
1085
runner = TextTestRunner(stream=self._log_file)
1086
test = SkippedSetupTest('test_skip')
1087
result = self.run_test_runner(runner, test)
1088
self.assertTrue(result.wasSuccessful())
1089
# Check if cleanup was called the right number of times.
1090
self.assertEqual(0, test.counter)
1092
def test_skipped_from_test(self):
1093
class SkippedTest(TestCase):
1097
self.addCleanup(self.cleanup)
1099
def test_skip(self):
1100
raise TestSkipped('skipped test')
1105
runner = TextTestRunner(stream=self._log_file)
1106
test = SkippedTest('test_skip')
1107
result = self.run_test_runner(runner, test)
1108
self.assertTrue(result.wasSuccessful())
1109
# Check if cleanup was called the right number of times.
1110
self.assertEqual(0, test.counter)
1112
def test_unsupported_features_listed(self):
1113
"""When unsupported features are encountered they are detailed."""
1114
class Feature1(Feature):
1115
def _probe(self): return False
1116
class Feature2(Feature):
1117
def _probe(self): return False
1118
# create sample tests
1119
test1 = SampleTestCase('_test_pass')
1120
test1._test_needs_features = [Feature1()]
1121
test2 = SampleTestCase('_test_pass')
1122
test2._test_needs_features = [Feature2()]
1123
test = unittest.TestSuite()
1127
runner = TextTestRunner(stream=stream)
1128
result = self.run_test_runner(runner, test)
1129
lines = stream.getvalue().splitlines()
1132
"Missing feature 'Feature1' skipped 1 tests.",
1133
"Missing feature 'Feature2' skipped 1 tests.",
1137
def test_bench_history(self):
1138
# tests that the running the benchmark produces a history file
1139
# containing a timestamp and the revision id of the bzrlib source which
1141
workingtree = _get_bzr_source_tree()
1142
test = TestRunner('dummy_test')
1144
runner = TextTestRunner(stream=self._log_file, bench_history=output)
1145
result = self.run_test_runner(runner, test)
1146
output_string = output.getvalue()
1147
self.assertContainsRe(output_string, "--date [0-9.]+")
1148
if workingtree is not None:
1149
revision_id = workingtree.get_parent_ids()[0]
1150
self.assertEndsWith(output_string.rstrip(), revision_id)
1152
def test_success_log_deleted(self):
1153
"""Successful tests have their log deleted"""
1155
class LogTester(TestCase):
1157
def test_success(self):
1158
self.log('this will be removed\n')
1160
sio = cStringIO.StringIO()
1161
runner = TextTestRunner(stream=sio)
1162
test = LogTester('test_success')
1163
result = self.run_test_runner(runner, test)
1165
log = test._get_log()
1166
self.assertEqual("DELETED log file to reduce memory footprint", log)
1167
self.assertEqual('', test._log_contents)
1168
self.assertIs(None, test._log_file_name)
1170
def test_fail_log_kept(self):
1171
"""Failed tests have their log kept"""
1173
class LogTester(TestCase):
1175
def test_fail(self):
1176
self.log('this will be kept\n')
1177
self.fail('this test fails')
1179
sio = cStringIO.StringIO()
1180
runner = TextTestRunner(stream=sio)
1181
test = LogTester('test_fail')
1182
result = self.run_test_runner(runner, test)
1184
text = sio.getvalue()
1185
self.assertContainsRe(text, 'this will be kept')
1186
self.assertContainsRe(text, 'this test fails')
1188
log = test._get_log()
1189
self.assertContainsRe(log, 'this will be kept')
1190
self.assertEqual(log, test._log_contents)
1192
def test_error_log_kept(self):
1193
"""Tests with errors have their log kept"""
1195
class LogTester(TestCase):
1197
def test_error(self):
1198
self.log('this will be kept\n')
1199
raise ValueError('random exception raised')
1201
sio = cStringIO.StringIO()
1202
runner = TextTestRunner(stream=sio)
1203
test = LogTester('test_error')
1204
result = self.run_test_runner(runner, test)
1206
text = sio.getvalue()
1207
self.assertContainsRe(text, 'this will be kept')
1208
self.assertContainsRe(text, 'random exception raised')
1210
log = test._get_log()
1211
self.assertContainsRe(log, 'this will be kept')
1212
self.assertEqual(log, test._log_contents)
1215
class SampleTestCase(TestCase):
1217
def _test_pass(self):
1221
class TestTestCase(TestCase):
1222
"""Tests that test the core bzrlib TestCase."""
1224
def test_debug_flags_sanitised(self):
1225
"""The bzrlib debug flags should be sanitised by setUp."""
1226
# we could set something and run a test that will check
1227
# it gets santised, but this is probably sufficient for now:
1228
# if someone runs the test with -Dsomething it will error.
1229
self.assertEqual(set(), bzrlib.debug.debug_flags)
1231
def inner_test(self):
1232
# the inner child test
1235
def outer_child(self):
1236
# the outer child test
1238
self.inner_test = TestTestCase("inner_child")
1239
result = bzrlib.tests.TextTestResult(self._log_file,
1242
self.inner_test.run(result)
1243
note("outer finish")
1245
def test_trace_nesting(self):
1246
# this tests that each test case nests its trace facility correctly.
1247
# we do this by running a test case manually. That test case (A)
1248
# should setup a new log, log content to it, setup a child case (B),
1249
# which should log independently, then case (A) should log a trailer
1251
# we do two nested children so that we can verify the state of the
1252
# logs after the outer child finishes is correct, which a bad clean
1253
# up routine in tearDown might trigger a fault in our test with only
1254
# one child, we should instead see the bad result inside our test with
1256
# the outer child test
1257
original_trace = bzrlib.trace._trace_file
1258
outer_test = TestTestCase("outer_child")
1259
result = bzrlib.tests.TextTestResult(self._log_file,
1262
outer_test.run(result)
1263
self.assertEqual(original_trace, bzrlib.trace._trace_file)
1265
def method_that_times_a_bit_twice(self):
1266
# call self.time twice to ensure it aggregates
1267
self.time(time.sleep, 0.007)
1268
self.time(time.sleep, 0.007)
1270
def test_time_creates_benchmark_in_result(self):
1271
"""Test that the TestCase.time() method accumulates a benchmark time."""
1272
sample_test = TestTestCase("method_that_times_a_bit_twice")
1273
output_stream = StringIO()
1274
result = bzrlib.tests.VerboseTestResult(
1275
unittest._WritelnDecorator(output_stream),
1278
num_tests=sample_test.countTestCases())
1279
sample_test.run(result)
1280
self.assertContainsRe(
1281
output_stream.getvalue(),
1282
r"\d+ms/ +\d+ms\n$")
1284
def test_hooks_sanitised(self):
1285
"""The bzrlib hooks should be sanitised by setUp."""
1286
self.assertEqual(bzrlib.branch.BranchHooks(),
1287
bzrlib.branch.Branch.hooks)
1288
self.assertEqual(bzrlib.smart.server.SmartServerHooks(),
1289
bzrlib.smart.server.SmartTCPServer.hooks)
1291
def test__gather_lsprof_in_benchmarks(self):
1292
"""When _gather_lsprof_in_benchmarks is on, accumulate profile data.
1294
Each self.time() call is individually and separately profiled.
1296
self.requireFeature(test_lsprof.LSProfFeature)
1297
# overrides the class member with an instance member so no cleanup
1299
self._gather_lsprof_in_benchmarks = True
1300
self.time(time.sleep, 0.000)
1301
self.time(time.sleep, 0.003)
1302
self.assertEqual(2, len(self._benchcalls))
1303
self.assertEqual((time.sleep, (0.000,), {}), self._benchcalls[0][0])
1304
self.assertEqual((time.sleep, (0.003,), {}), self._benchcalls[1][0])
1305
self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
1306
self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)
1308
def test_knownFailure(self):
1309
"""Self.knownFailure() should raise a KnownFailure exception."""
1310
self.assertRaises(KnownFailure, self.knownFailure, "A Failure")
1312
def test_requireFeature_available(self):
1313
"""self.requireFeature(available) is a no-op."""
1314
class Available(Feature):
1315
def _probe(self):return True
1316
feature = Available()
1317
self.requireFeature(feature)
1319
def test_requireFeature_unavailable(self):
1320
"""self.requireFeature(unavailable) raises UnavailableFeature."""
1321
class Unavailable(Feature):
1322
def _probe(self):return False
1323
feature = Unavailable()
1324
self.assertRaises(UnavailableFeature, self.requireFeature, feature)
1326
def test_run_no_parameters(self):
1327
test = SampleTestCase('_test_pass')
1330
def test_run_enabled_unittest_result(self):
1331
"""Test we revert to regular behaviour when the test is enabled."""
1332
test = SampleTestCase('_test_pass')
1333
class EnabledFeature(object):
1334
def available(self):
1336
test._test_needs_features = [EnabledFeature()]
1337
result = unittest.TestResult()
1339
self.assertEqual(1, result.testsRun)
1340
self.assertEqual([], result.errors)
1341
self.assertEqual([], result.failures)
1343
def test_run_disabled_unittest_result(self):
1344
"""Test our compatability for disabled tests with unittest results."""
1345
test = SampleTestCase('_test_pass')
1346
class DisabledFeature(object):
1347
def available(self):
1349
test._test_needs_features = [DisabledFeature()]
1350
result = unittest.TestResult()
1352
self.assertEqual(1, result.testsRun)
1353
self.assertEqual([], result.errors)
1354
self.assertEqual([], result.failures)
1356
def test_run_disabled_supporting_result(self):
1357
"""Test disabled tests behaviour with support aware results."""
1358
test = SampleTestCase('_test_pass')
1359
class DisabledFeature(object):
1360
def available(self):
1362
the_feature = DisabledFeature()
1363
test._test_needs_features = [the_feature]
1364
class InstrumentedTestResult(unittest.TestResult):
1366
unittest.TestResult.__init__(self)
1368
def startTest(self, test):
1369
self.calls.append(('startTest', test))
1370
def stopTest(self, test):
1371
self.calls.append(('stopTest', test))
1372
def addNotSupported(self, test, feature):
1373
self.calls.append(('addNotSupported', test, feature))
1374
result = InstrumentedTestResult()
1377
('startTest', test),
1378
('addNotSupported', test, the_feature),
1384
@symbol_versioning.deprecated_function(zero_eleven)
1385
def sample_deprecated_function():
1386
"""A deprecated function to test applyDeprecated with."""
1390
def sample_undeprecated_function(a_param):
1391
"""A undeprecated function to test applyDeprecated with."""
1394
class ApplyDeprecatedHelper(object):
1395
"""A helper class for ApplyDeprecated tests."""
1397
@symbol_versioning.deprecated_method(zero_eleven)
1398
def sample_deprecated_method(self, param_one):
1399
"""A deprecated method for testing with."""
1402
def sample_normal_method(self):
1403
"""A undeprecated method."""
1405
@symbol_versioning.deprecated_method(zero_ten)
1406
def sample_nested_deprecation(self):
1407
return sample_deprecated_function()
1410
class TestExtraAssertions(TestCase):
1411
"""Tests for new test assertions in bzrlib test suite"""
1413
def test_assert_isinstance(self):
1414
self.assertIsInstance(2, int)
1415
self.assertIsInstance(u'', basestring)
1416
self.assertRaises(AssertionError, self.assertIsInstance, None, int)
1417
self.assertRaises(AssertionError, self.assertIsInstance, 23.3, int)
1419
def test_assertEndsWith(self):
1420
self.assertEndsWith('foo', 'oo')
1421
self.assertRaises(AssertionError, self.assertEndsWith, 'o', 'oo')
1423
def test_applyDeprecated_not_deprecated(self):
1424
sample_object = ApplyDeprecatedHelper()
1425
# calling an undeprecated callable raises an assertion
1426
self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
1427
sample_object.sample_normal_method)
1428
self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
1429
sample_undeprecated_function, "a param value")
1430
# calling a deprecated callable (function or method) with the wrong
1431
# expected deprecation fails.
1432
self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
1433
sample_object.sample_deprecated_method, "a param value")
1434
self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
1435
sample_deprecated_function)
1436
# calling a deprecated callable (function or method) with the right
1437
# expected deprecation returns the functions result.
1438
self.assertEqual("a param value", self.applyDeprecated(zero_eleven,
1439
sample_object.sample_deprecated_method, "a param value"))
1440
self.assertEqual(2, self.applyDeprecated(zero_eleven,
1441
sample_deprecated_function))
1442
# calling a nested deprecation with the wrong deprecation version
1443
# fails even if a deeper nested function was deprecated with the
1445
self.assertRaises(AssertionError, self.applyDeprecated,
1446
zero_eleven, sample_object.sample_nested_deprecation)
1447
# calling a nested deprecation with the right deprecation value
1448
# returns the calls result.
1449
self.assertEqual(2, self.applyDeprecated(zero_ten,
1450
sample_object.sample_nested_deprecation))
1452
def test_callDeprecated(self):
1453
def testfunc(be_deprecated, result=None):
1454
if be_deprecated is True:
1455
symbol_versioning.warn('i am deprecated', DeprecationWarning,
1458
result = self.callDeprecated(['i am deprecated'], testfunc, True)
1459
self.assertIs(None, result)
1460
result = self.callDeprecated([], testfunc, False, 'result')
1461
self.assertEqual('result', result)
1462
self.callDeprecated(['i am deprecated'], testfunc, be_deprecated=True)
1463
self.callDeprecated([], testfunc, be_deprecated=False)
1466
class TestConvenienceMakers(TestCaseWithTransport):
1467
"""Test for the make_* convenience functions."""
1469
def test_make_branch_and_tree_with_format(self):
1470
# we should be able to supply a format to make_branch_and_tree
1471
self.make_branch_and_tree('a', format=bzrlib.bzrdir.BzrDirMetaFormat1())
1472
self.make_branch_and_tree('b', format=bzrlib.bzrdir.BzrDirFormat6())
1473
self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('a')._format,
1474
bzrlib.bzrdir.BzrDirMetaFormat1)
1475
self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
1476
bzrlib.bzrdir.BzrDirFormat6)
1478
def test_make_branch_and_memory_tree(self):
1479
# we should be able to get a new branch and a mutable tree from
1480
# TestCaseWithTransport
1481
tree = self.make_branch_and_memory_tree('a')
1482
self.assertIsInstance(tree, bzrlib.memorytree.MemoryTree)
1485
class TestSFTPMakeBranchAndTree(TestCaseWithSFTPServer):
1487
def test_make_tree_for_sftp_branch(self):
1488
"""Transports backed by local directories create local trees."""
1490
tree = self.make_branch_and_tree('t1')
1491
base = tree.bzrdir.root_transport.base
1492
self.failIf(base.startswith('sftp'),
1493
'base %r is on sftp but should be local' % base)
1494
self.assertEquals(tree.bzrdir.root_transport,
1495
tree.branch.bzrdir.root_transport)
1496
self.assertEquals(tree.bzrdir.root_transport,
1497
tree.branch.repository.bzrdir.root_transport)
1500
class TestSelftest(TestCase):
1501
"""Tests of bzrlib.tests.selftest."""
1503
def test_selftest_benchmark_parameter_invokes_test_suite__benchmark__(self):
1506
factory_called.append(True)
1510
self.apply_redirected(out, err, None, bzrlib.tests.selftest,
1511
test_suite_factory=factory)
1512
self.assertEqual([True], factory_called)
1515
class TestSelftestCleanOutput(TestCaseInTempDir):
1517
def test_clean_output(self):
1518
# test functionality of clean_selftest_output()
1519
self.build_tree(['test0000.tmp/', 'test0001.tmp/',
1520
'bzrlib/', 'tests/',
1521
'bzr', 'setup.py', 'test9999.tmp'])
1524
before = os.listdir(root)
1526
self.assertEquals(['bzr','bzrlib','setup.py',
1527
'test0000.tmp','test0001.tmp',
1528
'test9999.tmp','tests'],
1530
clean_selftest_output(root, quiet=True)
1531
after = os.listdir(root)
1533
self.assertEquals(['bzr','bzrlib','setup.py',
1534
'test9999.tmp','tests'],
1537
def test_clean_readonly(self):
1538
# test for delete read-only files
1539
self.build_tree(['test0000.tmp/', 'test0000.tmp/foo'])
1540
osutils.make_readonly('test0000.tmp/foo')
1542
before = os.listdir(root); before.sort()
1543
self.assertEquals(['test0000.tmp'], before)
1544
clean_selftest_output(root, quiet=True)
1545
after = os.listdir(root); after.sort()
1546
self.assertEquals([], after)
1549
class TestKnownFailure(TestCase):
1551
def test_known_failure(self):
1552
"""Check that KnownFailure is defined appropriately."""
1553
# a KnownFailure is an assertion error for compatability with unaware
1555
self.assertIsInstance(KnownFailure(""), AssertionError)
1557
def test_expect_failure(self):
1559
self.expectFailure("Doomed to failure", self.assertTrue, False)
1560
except KnownFailure, e:
1561
self.assertEqual('Doomed to failure', e.args[0])
1563
self.expectFailure("Doomed to failure", self.assertTrue, True)
1564
except AssertionError, e:
1565
self.assertEqual('Unexpected success. Should have failed:'
1566
' Doomed to failure', e.args[0])
1568
self.fail('Assertion not raised')
1571
class TestFeature(TestCase):
1573
def test_caching(self):
1574
"""Feature._probe is called by the feature at most once."""
1575
class InstrumentedFeature(Feature):
1577
Feature.__init__(self)
1580
self.calls.append('_probe')
1582
feature = InstrumentedFeature()
1584
self.assertEqual(['_probe'], feature.calls)
1586
self.assertEqual(['_probe'], feature.calls)
1588
def test_named_str(self):
1589
"""Feature.__str__ should thunk to feature_name()."""
1590
class NamedFeature(Feature):
1591
def feature_name(self):
1593
feature = NamedFeature()
1594
self.assertEqual('symlinks', str(feature))
1596
def test_default_str(self):
1597
"""Feature.__str__ should default to __class__.__name__."""
1598
class NamedFeature(Feature):
1600
feature = NamedFeature()
1601
self.assertEqual('NamedFeature', str(feature))
1604
class TestUnavailableFeature(TestCase):
1606
def test_access_feature(self):
1608
exception = UnavailableFeature(feature)
1609
self.assertIs(feature, exception.args[0])
1612
class TestSelftestFiltering(TestCase):
1615
self.suite = TestUtil.TestSuite()
1616
self.loader = TestUtil.TestLoader()
1617
self.suite.addTest(self.loader.loadTestsFromModuleNames([
1618
'bzrlib.tests.test_selftest']))
1619
self.all_names = [t.id() for t in iter_suite_tests(self.suite)]
1621
def test_filter_suite_by_re(self):
1622
filtered_suite = filter_suite_by_re(self.suite, 'test_filter')
1623
filtered_names = [t.id() for t in iter_suite_tests(filtered_suite)]
1624
self.assertEqual(filtered_names, ['bzrlib.tests.test_selftest.'
1625
'TestSelftestFiltering.test_filter_suite_by_re'])
1627
def test_sort_suite_by_re(self):
1628
sorted_suite = sort_suite_by_re(self.suite, 'test_filter')
1629
sorted_names = [t.id() for t in iter_suite_tests(sorted_suite)]
1630
self.assertEqual(sorted_names[0], 'bzrlib.tests.test_selftest.'
1631
'TestSelftestFiltering.test_filter_suite_by_re')
1632
self.assertEquals(sorted(self.all_names), sorted(sorted_names))
1635
class TestCheckInventoryShape(TestCaseWithTransport):
1637
def test_check_inventory_shape(self):
1638
files = ['a', 'b/', 'b/c']
1639
tree = self.make_branch_and_tree('.')
1640
self.build_tree(files)
1644
self.check_inventory_shape(tree.inventory, files)