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
475
477
format = bzrdir.BzrDirMetaFormat1()
476
478
format.repository_format = repository.RepositoryFormat7()
477
479
tree = self.make_branch_and_memory_tree('dir', format=format)
478
self.failIfExists('dir')
480
# Guard against regression into MemoryTransport leaking
481
# files to disk instead of keeping them in memory.
482
self.failIf(osutils.lexists('dir'))
479
483
self.assertIsInstance(tree, memorytree.MemoryTree)
480
484
self.assertEqual(format.repository_format.__class__,
481
485
tree.branch.repository._format.__class__)
501
505
self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
503
507
def test_get_readonly_url_http(self):
508
from bzrlib.tests.HttpServer import HttpServer
504
509
from bzrlib.transport import get_transport
505
from bzrlib.transport.local import LocalRelpathServer
506
from bzrlib.transport.http import HttpServer, HttpTransportBase
507
self.transport_server = LocalRelpathServer
510
from bzrlib.transport.local import LocalURLServer
511
from bzrlib.transport.http import HttpTransportBase
512
self.transport_server = LocalURLServer
508
513
self.transport_readonly_server = HttpServer
509
514
# calling get_readonly_transport() gives us a HTTP server instance.
510
515
url = self.get_readonly_url()
575
580
class TestTestResult(TestCase):
577
def test_progress_bar_style_quiet(self):
578
# test using a progress bar.
579
dummy_test = TestTestResult('test_progress_bar_style_quiet')
580
dummy_error = (Exception, None, [])
581
mypb = MockProgress()
582
mypb.update('Running tests', 0, 4)
583
last_calls = mypb.calls[:]
585
result = bzrlib.tests._MyResult(self._log_file,
589
self.assertEqual(last_calls, mypb.calls)
592
"""Shorten a string based on the terminal width"""
593
return result._ellipsise_unimportant_words(s,
594
osutils.terminal_width())
597
result.startTest(dummy_test)
598
# starting a test prints the test name
599
last_calls += [('update', '...tyle_quiet', 0, None)]
600
self.assertEqual(last_calls, mypb.calls)
601
result.addError(dummy_test, dummy_error)
602
last_calls += [('update', 'ERROR ', 1, None),
603
('note', shorten(dummy_test.id() + ': ERROR'), ())
605
self.assertEqual(last_calls, mypb.calls)
608
result.startTest(dummy_test)
609
last_calls += [('update', '...tyle_quiet', 1, None)]
610
self.assertEqual(last_calls, mypb.calls)
611
last_calls += [('update', 'FAIL ', 2, None),
612
('note', shorten(dummy_test.id() + ': FAIL'), ())
614
result.addFailure(dummy_test, dummy_error)
615
self.assertEqual(last_calls, mypb.calls)
618
result.startTest(dummy_test)
619
last_calls += [('update', '...tyle_quiet', 2, None)]
620
self.assertEqual(last_calls, mypb.calls)
621
result.addSuccess(dummy_test)
622
last_calls += [('update', 'OK ', 3, None)]
623
self.assertEqual(last_calls, mypb.calls)
626
result.startTest(dummy_test)
627
last_calls += [('update', '...tyle_quiet', 3, None)]
628
self.assertEqual(last_calls, mypb.calls)
629
result.addSkipped(dummy_test, dummy_error)
630
last_calls += [('update', 'SKIP ', 4, None)]
631
self.assertEqual(last_calls, mypb.calls)
633
582
def test_elapsed_time_with_benchmarking(self):
634
result = bzrlib.tests._MyResult(self._log_file,
583
result = bzrlib.tests.TextTestResult(self._log_file,
640
589
result.extractBenchmarkTime(self)
641
590
timed_string = result._testTimeString()
642
591
# without explicit benchmarking, we should get a simple time.
643
self.assertContainsRe(timed_string, "^ [ 1-9][0-9]ms$")
592
self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms$")
644
593
# if a benchmark time is given, we want a x of y style result.
645
594
self.time(time.sleep, 0.001)
646
595
result.extractBenchmarkTime(self)
647
596
timed_string = result._testTimeString()
648
self.assertContainsRe(timed_string, "^ [ 1-9][0-9]ms/ [ 1-9][0-9]ms$")
597
self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms/ *[ 1-9][0-9]ms$")
649
598
# extracting the time from a non-bzrlib testcase sets to None
650
599
result._recordTestStartTime()
651
600
result.extractBenchmarkTime(
652
601
unittest.FunctionTestCase(self.test_elapsed_time_with_benchmarking))
653
602
timed_string = result._testTimeString()
654
self.assertContainsRe(timed_string, "^ [ 1-9][0-9]ms$")
603
self.assertContainsRe(timed_string, "^ *[ 1-9][0-9]ms$")
655
604
# cheat. Yes, wash thy mouth out with soap.
656
605
self._benchtime = None
658
607
def test_assigned_benchmark_file_stores_date(self):
659
608
output = StringIO()
660
result = bzrlib.tests._MyResult(self._log_file,
609
result = bzrlib.tests.TextTestResult(self._log_file,
663
612
bench_history=output
665
614
output_string = output.getvalue()
666
616
# if you are wondering about the regexp please read the comment in
667
617
# test_bench_history (bzrlib.tests.test_selftest.TestRunner)
668
618
# XXX: what comment? -- Andrew Bennetts
759
709
TestCaseInTempDir.TEST_ROOT = old_root
761
def test_accepts_and_uses_pb_parameter(self):
762
test = TestRunner('dummy_test')
763
mypb = MockProgress()
764
self.assertEqual([], mypb.calls)
765
runner = TextTestRunner(stream=self._log_file, pb=mypb)
766
result = self.run_test_runner(runner, test)
767
self.assertEqual(1, result.testsRun)
768
self.assertEqual(('update', 'Running tests', 0, 1), mypb.calls[0])
769
self.assertEqual(('update', '...dummy_test', 0, None), mypb.calls[1])
770
self.assertEqual(('update', 'OK ', 1, None), mypb.calls[2])
771
self.assertEqual(('update', 'Cleaning up', 0, 1), mypb.calls[3])
772
self.assertEqual(('clear',), mypb.calls[4])
773
self.assertEqual(5, len(mypb.calls))
775
711
def test_skipped_test(self):
776
712
# run a test that is skipped, and check the suite as a whole still
907
843
"""Test that the TestCase.time() method accumulates a benchmark time."""
908
844
sample_test = TestTestCase("method_that_times_a_bit_twice")
909
845
output_stream = StringIO()
910
result = bzrlib.tests._MyResult(
846
result = bzrlib.tests.VerboseTestResult(
911
847
unittest._WritelnDecorator(output_stream),
850
num_tests=sample_test.countTestCases())
914
851
sample_test.run(result)
915
852
self.assertContainsRe(
916
853
output_stream.getvalue(),
917
"[1-9][0-9]ms/ [1-9][0-9]ms\n$")
856
def test_hooks_sanitised(self):
857
"""The bzrlib hooks should be sanitised by setUp."""
858
self.assertEqual(bzrlib.branch.BranchHooks(),
859
bzrlib.branch.Branch.hooks)
919
861
def test__gather_lsprof_in_benchmarks(self):
920
862
"""When _gather_lsprof_in_benchmarks is on, accumulate profile data.
1066
1008
self.apply_redirected(out, err, None, bzrlib.tests.selftest,
1067
1009
test_suite_factory=factory)
1068
1010
self.assertEqual([True], factory_called)
1013
class TestSelftestCleanOutput(TestCaseInTempDir):
1015
def test_clean_output(self):
1016
# test functionality of clean_selftest_output()
1017
from bzrlib.tests import clean_selftest_output
1019
dirs = ('test0000.tmp', 'test0001.tmp', 'bzrlib', 'tests')
1020
files = ('bzr', 'setup.py', 'test9999.tmp')
1025
f.write('content of ')
1030
before = os.listdir(root)
1032
self.assertEquals(['bzr','bzrlib','setup.py',
1033
'test0000.tmp','test0001.tmp',
1034
'test9999.tmp','tests'],
1036
clean_selftest_output(root, quiet=True)
1037
after = os.listdir(root)
1039
self.assertEquals(['bzr','bzrlib','setup.py',
1040
'test9999.tmp','tests'],