1
# Copyright (C) 2005, 2006 by Canonical Ltd
1
# Copyright (C) 2005, 2006 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License version 2 as published by
5
# the Free Software Foundation.
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.
7
8
# This program is distributed in the hope that it will be useful,
8
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
432
440
self.assertEqual(self.test_home_dir, os.environ['HOME'])
443
class TestTestCaseWithMemoryTransport(TestCaseWithMemoryTransport):
445
def test_home_is_non_existant_dir_under_root(self):
446
"""The test_home_dir for TestCaseWithMemoryTransport is missing.
448
This is because TestCaseWithMemoryTransport is for tests that do not
449
need any disk resources: they should be hooked into bzrlib in such a
450
way that no global settings are being changed by the test (only a
451
few tests should need to do that), and having a missing dir as home is
452
an effective way to ensure that this is the case.
454
self.assertEqual(self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
456
self.assertEqual(self.test_home_dir, os.environ['HOME'])
458
def test_cwd_is_TEST_ROOT(self):
459
self.assertEqual(self.test_dir, self.TEST_ROOT)
460
cwd = osutils.getcwd()
461
self.assertEqual(self.test_dir, cwd)
463
def test_make_branch_and_memory_tree(self):
464
"""In TestCaseWithMemoryTransport we should not make the branch on disk.
466
This is hard to comprehensively robustly test, so we settle for making
467
a branch and checking no directory was created at its relpath.
469
tree = self.make_branch_and_memory_tree('dir')
470
self.failIfExists('dir')
471
self.assertIsInstance(tree, memorytree.MemoryTree)
473
def test_make_branch_and_memory_tree_with_format(self):
474
"""make_branch_and_memory_tree should accept a format option."""
475
format = bzrdir.BzrDirMetaFormat1()
476
format.repository_format = repository.RepositoryFormat7()
477
tree = self.make_branch_and_memory_tree('dir', format=format)
478
self.failIfExists('dir')
479
self.assertIsInstance(tree, memorytree.MemoryTree)
480
self.assertEqual(format.repository_format.__class__,
481
tree.branch.repository._format.__class__)
435
484
class TestTestCaseWithTransport(TestCaseWithTransport):
436
485
"""Tests for the convenience functions TestCaseWithTransport introduces."""
454
503
def test_get_readonly_url_http(self):
455
504
from bzrlib.transport import get_transport
456
from bzrlib.transport.local import LocalRelpathServer
505
from bzrlib.transport.local import LocalURLServer
457
506
from bzrlib.transport.http import HttpServer, HttpTransportBase
458
self.transport_server = LocalRelpathServer
507
self.transport_server = LocalURLServer
459
508
self.transport_readonly_server = HttpServer
460
509
# calling get_readonly_transport() gives us a HTTP server instance.
461
510
url = self.get_readonly_url()
526
575
class TestTestResult(TestCase):
528
def test_progress_bar_style_quiet(self):
529
# test using a progress bar.
530
dummy_test = TestTestResult('test_progress_bar_style_quiet')
531
dummy_error = (Exception, None, [])
532
mypb = MockProgress()
533
mypb.update('Running tests', 0, 4)
534
last_calls = mypb.calls[:]
536
result = bzrlib.tests._MyResult(self._log_file,
540
self.assertEqual(last_calls, mypb.calls)
543
"""Shorten a string based on the terminal width"""
544
return result._ellipsise_unimportant_words(s,
545
osutils.terminal_width())
548
result.startTest(dummy_test)
549
# starting a test prints the test name
550
last_calls += [('update', '...tyle_quiet', 0, None)]
551
self.assertEqual(last_calls, mypb.calls)
552
result.addError(dummy_test, dummy_error)
553
last_calls += [('update', 'ERROR ', 1, None),
554
('note', shorten(dummy_test.id() + ': ERROR'), ())
556
self.assertEqual(last_calls, mypb.calls)
559
result.startTest(dummy_test)
560
last_calls += [('update', '...tyle_quiet', 1, None)]
561
self.assertEqual(last_calls, mypb.calls)
562
last_calls += [('update', 'FAIL ', 2, None),
563
('note', shorten(dummy_test.id() + ': FAIL'), ())
565
result.addFailure(dummy_test, dummy_error)
566
self.assertEqual(last_calls, mypb.calls)
569
result.startTest(dummy_test)
570
last_calls += [('update', '...tyle_quiet', 2, None)]
571
self.assertEqual(last_calls, mypb.calls)
572
result.addSuccess(dummy_test)
573
last_calls += [('update', 'OK ', 3, None)]
574
self.assertEqual(last_calls, mypb.calls)
577
result.startTest(dummy_test)
578
last_calls += [('update', '...tyle_quiet', 3, None)]
579
self.assertEqual(last_calls, mypb.calls)
580
result.addSkipped(dummy_test, dummy_error)
581
last_calls += [('update', 'SKIP ', 4, None)]
582
self.assertEqual(last_calls, mypb.calls)
584
577
def test_elapsed_time_with_benchmarking(self):
585
result = bzrlib.tests._MyResult(self._log_file,
578
result = bzrlib.tests.TextTestResult(self._log_file,
710
704
TestCaseInTempDir.TEST_ROOT = old_root
712
def test_accepts_and_uses_pb_parameter(self):
713
test = TestRunner('dummy_test')
714
mypb = MockProgress()
715
self.assertEqual([], mypb.calls)
716
runner = TextTestRunner(stream=self._log_file, pb=mypb)
717
result = self.run_test_runner(runner, test)
718
self.assertEqual(1, result.testsRun)
719
self.assertEqual(('update', 'Running tests', 0, 1), mypb.calls[0])
720
self.assertEqual(('update', '...dummy_test', 0, None), mypb.calls[1])
721
self.assertEqual(('update', 'OK ', 1, None), mypb.calls[2])
722
self.assertEqual(('update', 'Cleaning up', 0, 1), mypb.calls[3])
723
self.assertEqual(('clear',), mypb.calls[4])
724
self.assertEqual(5, len(mypb.calls))
726
706
def test_skipped_test(self):
727
707
# run a test that is skipped, and check the suite as a whole still
749
729
revision_id = workingtree.get_parent_ids()[0]
750
730
self.assertEndsWith(output_string.rstrip(), revision_id)
732
def test_success_log_deleted(self):
733
"""Successful tests have their log deleted"""
735
class LogTester(TestCase):
737
def test_success(self):
738
self.log('this will be removed\n')
740
sio = cStringIO.StringIO()
741
runner = TextTestRunner(stream=sio)
742
test = LogTester('test_success')
743
result = self.run_test_runner(runner, test)
745
log = test._get_log()
746
self.assertEqual("DELETED log file to reduce memory footprint", log)
747
self.assertEqual('', test._log_contents)
748
self.assertIs(None, test._log_file_name)
750
def test_fail_log_kept(self):
751
"""Failed tests have their log kept"""
753
class LogTester(TestCase):
756
self.log('this will be kept\n')
757
self.fail('this test fails')
759
sio = cStringIO.StringIO()
760
runner = TextTestRunner(stream=sio)
761
test = LogTester('test_fail')
762
result = self.run_test_runner(runner, test)
764
text = sio.getvalue()
765
self.assertContainsRe(text, 'this will be kept')
766
self.assertContainsRe(text, 'this test fails')
768
log = test._get_log()
769
self.assertContainsRe(log, 'this will be kept')
770
self.assertEqual(log, test._log_contents)
772
def test_error_log_kept(self):
773
"""Tests with errors have their log kept"""
775
class LogTester(TestCase):
777
def test_error(self):
778
self.log('this will be kept\n')
779
raise ValueError('random exception raised')
781
sio = cStringIO.StringIO()
782
runner = TextTestRunner(stream=sio)
783
test = LogTester('test_error')
784
result = self.run_test_runner(runner, test)
786
text = sio.getvalue()
787
self.assertContainsRe(text, 'this will be kept')
788
self.assertContainsRe(text, 'random exception raised')
790
log = test._get_log()
791
self.assertContainsRe(log, 'this will be kept')
792
self.assertEqual(log, test._log_contents)
753
795
class TestTestCase(TestCase):
754
796
"""Tests that test the core bzrlib TestCase."""
796
838
"""Test that the TestCase.time() method accumulates a benchmark time."""
797
839
sample_test = TestTestCase("method_that_times_a_bit_twice")
798
840
output_stream = StringIO()
799
result = bzrlib.tests._MyResult(
841
result = bzrlib.tests.VerboseTestResult(
800
842
unittest._WritelnDecorator(output_stream),
845
num_tests=sample_test.countTestCases())
803
846
sample_test.run(result)
804
847
self.assertContainsRe(
805
848
output_stream.getvalue(),
806
"[1-9][0-9]ms/ [1-9][0-9]ms\n$")
808
851
def test__gather_lsprof_in_benchmarks(self):
809
852
"""When _gather_lsprof_in_benchmarks is on, accumulate profile data.