174
175
bench_history.write("--date %s %s\n" % (time.time(), revision_id))
175
176
self._bench_history = bench_history
176
self.ui = bzrlib.ui.ui_factory
177
self.ui = ui.ui_factory
177
178
self.num_tests = num_tests
178
179
self.error_count = 0
179
180
self.failure_count = 0
271
272
def printErrorList(self, flavour, errors):
272
273
for test, err in errors:
273
274
self.stream.writeln(self.separator1)
274
self.stream.writeln("%s: %s" % (flavour, self.getDescription(test)))
275
self.stream.write("%s: " % flavour)
277
self.stream.write('#%d ' % test.number)
278
self.stream.writeln(self.getDescription(test))
275
279
if getattr(test, '_get_log', None) is not None:
276
280
print >>self.stream
277
281
print >>self.stream, \
422
426
def report_success(self, test):
423
427
self.stream.writeln(' OK %s' % self._testTimeString())
424
428
for bench_called, stats in getattr(test, '_benchcalls', []):
426
self.stream.write(' ' * 6)
427
429
self.stream.writeln('LSProf output for %s(%s, %s)' % bench_called)
428
430
stats.pprint(file=self.stream)
429
431
self.stream.flush()
571
573
return setattr(self._cstring, name, val)
576
class TestUIFactory(ui.CLIUIFactory):
577
"""A UI Factory for testing.
579
Hide the progress bar but emit note()s.
581
Allows get_password to be tested without real tty attached.
588
super(TestUIFactory, self).__init__()
589
if stdin is not None:
590
# We use a StringIOWrapper to be able to test various
591
# encodings, but the user is still responsible to
592
# encode the string and to set the encoding attribute
593
# of StringIOWrapper.
594
self.stdin = StringIOWrapper(stdin)
596
self.stdout = sys.stdout
600
self.stderr = sys.stderr
605
"""See progress.ProgressBar.clear()."""
607
def clear_term(self):
608
"""See progress.ProgressBar.clear_term()."""
610
def clear_term(self):
611
"""See progress.ProgressBar.clear_term()."""
614
"""See progress.ProgressBar.finished()."""
616
def note(self, fmt_string, *args, **kwargs):
617
"""See progress.ProgressBar.note()."""
618
self.stdout.write((fmt_string + "\n") % args)
620
def progress_bar(self):
623
def nested_progress_bar(self):
626
def update(self, message, count=None, total=None):
627
"""See progress.ProgressBar.update()."""
629
def get_non_echoed_password(self, prompt):
630
"""Get password from stdin without trying to handle the echo mode"""
632
self.stdout.write(prompt)
633
password = self.stdin.readline()
636
if password[-1] == '\n':
637
password = password[:-1]
574
641
class TestCase(unittest.TestCase):
575
642
"""Base class for bzr unit tests.
620
687
def _silenceUI(self):
621
688
"""Turn off UI for duration of test"""
622
689
# by default the UI is off; tests can turn it on if they want it.
623
saved = bzrlib.ui.ui_factory
690
saved = ui.ui_factory
625
bzrlib.ui.ui_factory = saved
626
bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
692
ui.ui_factory = saved
693
ui.ui_factory = ui.SilentUIFactory()
627
694
self.addCleanup(_restore)
629
696
def _ndiff_strings(self, a, b):
930
997
# TODO: Perhaps this should keep running cleanups even if
931
998
# one of them fails?
932
for cleanup_fn in reversed(self._cleanups):
1000
# Actually pop the cleanups from the list so tearDown running
1001
# twice is safe (this happens for skipped tests).
1002
while self._cleanups:
1003
self._cleanups.pop()()
935
1005
def log(self, *args):
1007
1075
handler.setLevel(logging.INFO)
1008
1076
logger = logging.getLogger('')
1009
1077
logger.addHandler(handler)
1010
old_ui_factory = bzrlib.ui.ui_factory
1011
bzrlib.ui.ui_factory = bzrlib.tests.blackbox.TestUIFactory(
1014
bzrlib.ui.ui_factory.stdin = stdin
1078
old_ui_factory = ui.ui_factory
1079
ui.ui_factory = TestUIFactory(stdin=stdin, stdout=stdout, stderr=stderr)
1017
1082
if working_dir is not None:
1022
1087
saved_debug_flags = frozenset(debug.debug_flags)
1023
1088
debug.debug_flags.clear()
1025
result = self.apply_redirected(stdin, stdout, stderr,
1090
result = self.apply_redirected(ui.ui_factory.stdin,
1026
1092
bzrlib.commands.run_bzr_catch_errors,
1029
1095
debug.debug_flags.update(saved_debug_flags)
1031
1097
logger.removeHandler(handler)
1032
bzrlib.ui.ui_factory = old_ui_factory
1098
ui.ui_factory = old_ui_factory
1033
1099
if cwd is not None:
1632
1698
"""Fail if path does not contain 'content'."""
1633
1699
self.failUnlessExists(path)
1634
1700
# TODO: jam 20060427 Shouldn't this be 'rb'?
1635
self.assertEqualDiff(content, open(path, 'r').read())
1706
self.assertEqualDiff(content, s)
1637
1708
def failUnlessExists(self, path):
1638
1709
"""Fail unless path, which may be abs or relative, exists."""
1780
1851
stop_on_failure=False, keep_output=False,
1781
1852
transport=None, lsprof_timed=None, bench_history=None,
1782
1853
matching_tests_first=None,
1783
numbered_dirs=False):
1854
numbered_dirs=None):
1784
1855
global NUMBERED_DIRS
1785
NUMBERED_DIRS = bool(numbered_dirs)
1856
if numbered_dirs is not None:
1857
NUMBERED_DIRS = bool(numbered_dirs)
1787
1859
TestCase._gather_lsprof_in_benchmarks = lsprof_timed
1811
1883
lsprof_timed=None,
1812
1884
bench_history=None,
1813
1885
matching_tests_first=None,
1814
numbered_dirs=False):
1886
numbered_dirs=None):
1815
1887
"""Run the whole test suite under the enhanced runner"""
1816
1888
# XXX: Very ugly way to do this...
1817
1889
# Disable warning about old formats because we don't want it to disturb
1893
1965
'bzrlib.tests.test_merge',
1894
1966
'bzrlib.tests.test_merge3',
1895
1967
'bzrlib.tests.test_merge_core',
1968
'bzrlib.tests.test_merge_directive',
1896
1969
'bzrlib.tests.test_missing',
1897
1970
'bzrlib.tests.test_msgeditor',
1898
1971
'bzrlib.tests.test_nonascii',
1919
1992
'bzrlib.tests.test_smart_add',
1920
1993
'bzrlib.tests.test_smart_transport',
1921
1994
'bzrlib.tests.test_source',
1995
'bzrlib.tests.test_ssh_transport',
1922
1996
'bzrlib.tests.test_status',
1923
1997
'bzrlib.tests.test_store',
1924
1998
'bzrlib.tests.test_subsume',
1927
2001
'bzrlib.tests.test_testament',
1928
2002
'bzrlib.tests.test_textfile',
1929
2003
'bzrlib.tests.test_textmerge',
2004
'bzrlib.tests.test_timestamp',
1930
2005
'bzrlib.tests.test_trace',
1931
2006
'bzrlib.tests.test_transactions',
1932
2007
'bzrlib.tests.test_transform',