~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Kent Gibson
  • Date: 2007-03-11 13:44:18 UTC
  • mfrom: (2334 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2350.
  • Revision ID: warthog618@gmail.com-20070311134418-nu57arul94zawbj4
merge bzr.dev

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
571
572
            return setattr(self._cstring, name, val)
572
573
 
573
574
 
 
575
class TestUIFactory(ui.CLIUIFactory):
 
576
    """A UI Factory for testing.
 
577
 
 
578
    Hide the progress bar but emit note()s.
 
579
    Redirect stdin.
 
580
    Allows get_password to be tested without real tty attached.
 
581
    """
 
582
 
 
583
    def __init__(self,
 
584
                 stdout=None,
 
585
                 stderr=None,
 
586
                 stdin=None):
 
587
        super(TestUIFactory, self).__init__()
 
588
        if stdin is not None:
 
589
            # We use a StringIOWrapper to be able to test various
 
590
            # encodings, but the user is still responsible to
 
591
            # encode the string and to set the encoding attribute
 
592
            # of StringIOWrapper.
 
593
            self.stdin = StringIOWrapper(stdin)
 
594
        if stdout is None:
 
595
            self.stdout = sys.stdout
 
596
        else:
 
597
            self.stdout = stdout
 
598
        if stderr is None:
 
599
            self.stderr = sys.stderr
 
600
        else:
 
601
            self.stderr = stderr
 
602
 
 
603
    def clear(self):
 
604
        """See progress.ProgressBar.clear()."""
 
605
 
 
606
    def clear_term(self):
 
607
        """See progress.ProgressBar.clear_term()."""
 
608
 
 
609
    def clear_term(self):
 
610
        """See progress.ProgressBar.clear_term()."""
 
611
 
 
612
    def finished(self):
 
613
        """See progress.ProgressBar.finished()."""
 
614
 
 
615
    def note(self, fmt_string, *args, **kwargs):
 
616
        """See progress.ProgressBar.note()."""
 
617
        self.stdout.write((fmt_string + "\n") % args)
 
618
 
 
619
    def progress_bar(self):
 
620
        return self
 
621
 
 
622
    def nested_progress_bar(self):
 
623
        return self
 
624
 
 
625
    def update(self, message, count=None, total=None):
 
626
        """See progress.ProgressBar.update()."""
 
627
 
 
628
    def get_non_echoed_password(self, prompt):
 
629
        """Get password from stdin without trying to handle the echo mode"""
 
630
        if prompt:
 
631
            self.stdout.write(prompt)
 
632
        password = self.stdin.readline()
 
633
        if not password:
 
634
            raise EOFError
 
635
        if password[-1] == '\n':
 
636
            password = password[:-1]
 
637
        return password
 
638
 
 
639
 
574
640
class TestCase(unittest.TestCase):
575
641
    """Base class for bzr unit tests.
576
642
    
620
686
    def _silenceUI(self):
621
687
        """Turn off UI for duration of test"""
622
688
        # by default the UI is off; tests can turn it on if they want it.
623
 
        saved = bzrlib.ui.ui_factory
 
689
        saved = ui.ui_factory
624
690
        def _restore():
625
 
            bzrlib.ui.ui_factory = saved
626
 
        bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
 
691
            ui.ui_factory = saved
 
692
        ui.ui_factory = ui.SilentUIFactory()
627
693
        self.addCleanup(_restore)
628
694
 
629
695
    def _ndiff_strings(self, a, b):
994
1060
        """
995
1061
        if encoding is None:
996
1062
            encoding = bzrlib.user_encoding
997
 
        if stdin is not None:
998
 
            stdin = StringIO(stdin)
999
1063
        stdout = StringIOWrapper()
1000
1064
        stderr = StringIOWrapper()
1001
1065
        stdout.encoding = encoding
1007
1071
        handler.setLevel(logging.INFO)
1008
1072
        logger = logging.getLogger('')
1009
1073
        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
 
1074
        old_ui_factory = ui.ui_factory
 
1075
        ui.ui_factory = TestUIFactory(stdin=stdin, stdout=stdout, stderr=stderr)
1015
1076
 
1016
1077
        cwd = None
1017
1078
        if working_dir is not None:
1022
1083
            saved_debug_flags = frozenset(debug.debug_flags)
1023
1084
            debug.debug_flags.clear()
1024
1085
            try:
1025
 
                result = self.apply_redirected(stdin, stdout, stderr,
 
1086
                result = self.apply_redirected(ui.ui_factory.stdin,
 
1087
                                               stdout, stderr,
1026
1088
                                               bzrlib.commands.run_bzr_catch_errors,
1027
1089
                                               argv)
1028
1090
            finally:
1029
1091
                debug.debug_flags.update(saved_debug_flags)
1030
1092
        finally:
1031
1093
            logger.removeHandler(handler)
1032
 
            bzrlib.ui.ui_factory = old_ui_factory
 
1094
            ui.ui_factory = old_ui_factory
1033
1095
            if cwd is not None:
1034
1096
                os.chdir(cwd)
1035
1097
 
1893
1955
                   'bzrlib.tests.test_merge',
1894
1956
                   'bzrlib.tests.test_merge3',
1895
1957
                   'bzrlib.tests.test_merge_core',
 
1958
                   'bzrlib.tests.test_merge_directive',
1896
1959
                   'bzrlib.tests.test_missing',
1897
1960
                   'bzrlib.tests.test_msgeditor',
1898
1961
                   'bzrlib.tests.test_nonascii',
1919
1982
                   'bzrlib.tests.test_smart_add',
1920
1983
                   'bzrlib.tests.test_smart_transport',
1921
1984
                   'bzrlib.tests.test_source',
 
1985
                   'bzrlib.tests.test_ssh_transport',
1922
1986
                   'bzrlib.tests.test_status',
1923
1987
                   'bzrlib.tests.test_store',
1924
1988
                   'bzrlib.tests.test_subsume',
1927
1991
                   'bzrlib.tests.test_testament',
1928
1992
                   'bzrlib.tests.test_textfile',
1929
1993
                   'bzrlib.tests.test_textmerge',
 
1994
                   'bzrlib.tests.test_timestamp',
1930
1995
                   'bzrlib.tests.test_trace',
1931
1996
                   'bzrlib.tests.test_transactions',
1932
1997
                   'bzrlib.tests.test_transform',