/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

Merge Martins 0.15rc2 release branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
51
51
    memorytree,
52
52
    osutils,
53
53
    progress,
 
54
    ui,
54
55
    urlutils,
55
56
    )
56
57
import bzrlib.branch
57
58
import bzrlib.commands
58
 
import bzrlib.bundle.serializer
 
59
import bzrlib.timestamp
59
60
import bzrlib.export
60
61
import bzrlib.inventory
61
62
import bzrlib.iterablefile
92
93
 
93
94
MODULES_TO_TEST = []
94
95
MODULES_TO_DOCTEST = [
95
 
                      bzrlib.bundle.serializer,
 
96
                      bzrlib.timestamp,
96
97
                      bzrlib.errors,
97
98
                      bzrlib.export,
98
99
                      bzrlib.inventory,
173
174
                revision_id = ''
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)
 
276
            if NUMBERED_DIRS:
 
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', []):
425
 
            if NUMBERED_DIRS:
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)
572
574
 
573
575
 
 
576
class TestUIFactory(ui.CLIUIFactory):
 
577
    """A UI Factory for testing.
 
578
 
 
579
    Hide the progress bar but emit note()s.
 
580
    Redirect stdin.
 
581
    Allows get_password to be tested without real tty attached.
 
582
    """
 
583
 
 
584
    def __init__(self,
 
585
                 stdout=None,
 
586
                 stderr=None,
 
587
                 stdin=None):
 
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)
 
595
        if stdout is None:
 
596
            self.stdout = sys.stdout
 
597
        else:
 
598
            self.stdout = stdout
 
599
        if stderr is None:
 
600
            self.stderr = sys.stderr
 
601
        else:
 
602
            self.stderr = stderr
 
603
 
 
604
    def clear(self):
 
605
        """See progress.ProgressBar.clear()."""
 
606
 
 
607
    def clear_term(self):
 
608
        """See progress.ProgressBar.clear_term()."""
 
609
 
 
610
    def clear_term(self):
 
611
        """See progress.ProgressBar.clear_term()."""
 
612
 
 
613
    def finished(self):
 
614
        """See progress.ProgressBar.finished()."""
 
615
 
 
616
    def note(self, fmt_string, *args, **kwargs):
 
617
        """See progress.ProgressBar.note()."""
 
618
        self.stdout.write((fmt_string + "\n") % args)
 
619
 
 
620
    def progress_bar(self):
 
621
        return self
 
622
 
 
623
    def nested_progress_bar(self):
 
624
        return self
 
625
 
 
626
    def update(self, message, count=None, total=None):
 
627
        """See progress.ProgressBar.update()."""
 
628
 
 
629
    def get_non_echoed_password(self, prompt):
 
630
        """Get password from stdin without trying to handle the echo mode"""
 
631
        if prompt:
 
632
            self.stdout.write(prompt)
 
633
        password = self.stdin.readline()
 
634
        if not password:
 
635
            raise EOFError
 
636
        if password[-1] == '\n':
 
637
            password = password[:-1]
 
638
        return password
 
639
 
 
640
 
574
641
class TestCase(unittest.TestCase):
575
642
    """Base class for bzr unit tests.
576
643
    
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
624
691
        def _restore():
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)
628
695
 
629
696
    def _ndiff_strings(self, a, b):
929
996
        """
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):
933
 
            cleanup_fn()
 
999
 
 
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()()
934
1004
 
935
1005
    def log(self, *args):
936
1006
        mutter(*args)
994
1064
        """
995
1065
        if encoding is None:
996
1066
            encoding = bzrlib.user_encoding
997
 
        if stdin is not None:
998
 
            stdin = StringIO(stdin)
999
1067
        stdout = StringIOWrapper()
1000
1068
        stderr = StringIOWrapper()
1001
1069
        stdout.encoding = encoding
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(
1012
 
            stdout=stdout,
1013
 
            stderr=stderr)
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)
1015
1080
 
1016
1081
        cwd = None
1017
1082
        if working_dir is not None:
1022
1087
            saved_debug_flags = frozenset(debug.debug_flags)
1023
1088
            debug.debug_flags.clear()
1024
1089
            try:
1025
 
                result = self.apply_redirected(stdin, stdout, stderr,
 
1090
                result = self.apply_redirected(ui.ui_factory.stdin,
 
1091
                                               stdout, stderr,
1026
1092
                                               bzrlib.commands.run_bzr_catch_errors,
1027
1093
                                               argv)
1028
1094
            finally:
1029
1095
                debug.debug_flags.update(saved_debug_flags)
1030
1096
        finally:
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:
1034
1100
                os.chdir(cwd)
1035
1101
 
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())
 
1701
        f = file(path, 'r')
 
1702
        try:
 
1703
            s = f.read()
 
1704
        finally:
 
1705
            f.close()
 
1706
        self.assertEqualDiff(content, s)
1636
1707
 
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)
1786
1858
 
1787
1859
    TestCase._gather_lsprof_in_benchmarks = lsprof_timed
1788
1860
    if verbose:
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',