/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

  • Committer: Aaron Bentley
  • Date: 2006-09-09 18:52:57 UTC
  • mfrom: (1996 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1997.
  • Revision ID: aaron.bentley@utoronto.ca-20060909185257-ce0ee03ee5125ff1
Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
63
63
import bzrlib.plugin
64
64
import bzrlib.progress as progress
65
65
from bzrlib.revision import common_ancestor
66
 
from bzrlib.revisionspec import RevisionSpec
67
66
import bzrlib.store
68
67
from bzrlib import symbol_versioning
69
68
import bzrlib.trace
151
150
            from bzrlib.version import _get_bzr_source_tree
152
151
            src_tree = _get_bzr_source_tree()
153
152
            if src_tree:
154
 
                revision_id = src_tree.last_revision()
 
153
                try:
 
154
                    revision_id = src_tree.get_parent_ids()[0]
 
155
                except IndexError:
 
156
                    # XXX: if this is a brand new tree, do the same as if there
 
157
                    # is no branch.
 
158
                    revision_id = ''
155
159
            else:
156
160
                # XXX: If there's no branch, what should we do?
157
161
                revision_id = ''
577
581
            self.fail("%r is an instance of %s rather than %s" % (
578
582
                obj, obj.__class__, kls))
579
583
 
580
 
    def callDeprecated(self, expected, callable, *args, **kwargs):
581
 
        """Assert that a callable is deprecated in a particular way.
 
584
    def _capture_warnings(self, a_callable, *args, **kwargs):
 
585
        """A helper for callDeprecated and applyDeprecated.
582
586
 
583
 
        :param expected: a list of the deprecation warnings expected, in order
584
 
        :param callable: The callable to call
 
587
        :param a_callable: A callable to call.
585
588
        :param args: The positional arguments for the callable
586
589
        :param kwargs: The keyword arguments for the callable
 
590
        :return: A tuple (warnings, result). result is the result of calling
 
591
            a_callable(*args, **kwargs).
587
592
        """
588
593
        local_warnings = []
589
594
        def capture_warnings(msg, cls, stacklevel=None):
 
595
            # we've hooked into a deprecation specific callpath,
 
596
            # only deprecations should getting sent via it.
590
597
            self.assertEqual(cls, DeprecationWarning)
591
598
            local_warnings.append(msg)
592
 
        method = symbol_versioning.warn
 
599
        original_warning_method = symbol_versioning.warn
593
600
        symbol_versioning.set_warning_method(capture_warnings)
594
601
        try:
595
 
            result = callable(*args, **kwargs)
 
602
            result = a_callable(*args, **kwargs)
596
603
        finally:
597
 
            symbol_versioning.set_warning_method(method)
598
 
        self.assertEqual(expected, local_warnings)
 
604
            symbol_versioning.set_warning_method(original_warning_method)
 
605
        return (local_warnings, result)
 
606
 
 
607
    def applyDeprecated(self, deprecation_format, a_callable, *args, **kwargs):
 
608
        """Call a deprecated callable without warning the user.
 
609
 
 
610
        :param deprecation_format: The deprecation format that the callable
 
611
            should have been deprecated with. This is the same type as the 
 
612
            parameter to deprecated_method/deprecated_function. If the 
 
613
            callable is not deprecated with this format, an assertion error
 
614
            will be raised.
 
615
        :param a_callable: A callable to call. This may be a bound method or
 
616
            a regular function. It will be called with *args and **kwargs.
 
617
        :param args: The positional arguments for the callable
 
618
        :param kwargs: The keyword arguments for the callable
 
619
        :return: The result of a_callable(*args, **kwargs)
 
620
        """
 
621
        call_warnings, result = self._capture_warnings(a_callable,
 
622
            *args, **kwargs)
 
623
        expected_first_warning = symbol_versioning.deprecation_string(
 
624
            a_callable, deprecation_format)
 
625
        if len(call_warnings) == 0:
 
626
            self.fail("No assertion generated by call to %s" %
 
627
                a_callable)
 
628
        self.assertEqual(expected_first_warning, call_warnings[0])
 
629
        return result
 
630
 
 
631
    def callDeprecated(self, expected, callable, *args, **kwargs):
 
632
        """Assert that a callable is deprecated in a particular way.
 
633
 
 
634
        This is a very precise test for unusual requirements. The 
 
635
        applyDeprecated helper function is probably more suited for most tests
 
636
        as it allows you to simply specify the deprecation format being used
 
637
        and will ensure that that is issued for the function being called.
 
638
 
 
639
        :param expected: a list of the deprecation warnings expected, in order
 
640
        :param callable: The callable to call
 
641
        :param args: The positional arguments for the callable
 
642
        :param kwargs: The keyword arguments for the callable
 
643
        """
 
644
        call_warnings, result = self._capture_warnings(callable,
 
645
            *args, **kwargs)
 
646
        self.assertEqual(expected, call_warnings)
599
647
        return result
600
648
 
601
649
    def _startLogFile(self):
604
652
        The file is removed as the test is torn down.
605
653
        """
606
654
        fileno, name = tempfile.mkstemp(suffix='.log', prefix='testbzr')
607
 
        encoder, decoder, stream_reader, stream_writer = codecs.lookup('UTF-8')
608
 
        self._log_file = stream_writer(os.fdopen(fileno, 'w+'))
 
655
        self._log_file = os.fdopen(fileno, 'w+')
609
656
        self._log_nonce = bzrlib.trace.enable_test_log(self._log_file)
610
657
        self._log_file_name = name
611
658
        self.addCleanup(self._finishLogFile)
642
689
            'BZR_EMAIL': None,
643
690
            'BZREMAIL': None, # may still be present in the environment
644
691
            'EMAIL': None,
 
692
            'BZR_PROGRESS_BAR': None,
645
693
        }
646
694
        self.__old_env = {}
647
695
        self.addCleanup(self._restoreEnvironment)
648
696
        for name, value in new_env.iteritems():
649
697
            self._captureVar(name, value)
650
698
 
651
 
 
652
699
    def _captureVar(self, name, newvalue):
653
 
        """Set an environment variable, preparing it to be reset when finished."""
654
 
        self.__old_env[name] = os.environ.get(name, None)
655
 
        if newvalue is None:
656
 
            if name in os.environ:
657
 
                del os.environ[name]
658
 
        else:
659
 
            os.environ[name] = newvalue
660
 
 
661
 
    @staticmethod
662
 
    def _restoreVar(name, value):
663
 
        if value is None:
664
 
            if name in os.environ:
665
 
                del os.environ[name]
666
 
        else:
667
 
            os.environ[name] = value
 
700
        """Set an environment variable, and reset it when finished."""
 
701
        self.__old_env[name] = osutils.set_or_unset_env(name, newvalue)
668
702
 
669
703
    def _restoreEnvironment(self):
670
704
        for name, value in self.__old_env.iteritems():
671
 
            self._restoreVar(name, value)
 
705
            osutils.set_or_unset_env(name, value)
672
706
 
673
707
    def tearDown(self):
674
708
        self._runCleanups()
799
833
        return self.run_bzr_captured(args, retcode=retcode, encoding=encoding, stdin=stdin)
800
834
 
801
835
    def run_bzr_decode(self, *args, **kwargs):
802
 
        if kwargs.has_key('encoding'):
 
836
        if 'encoding' in kwargs:
803
837
            encoding = kwargs['encoding']
804
838
        else:
805
839
            encoding = bzrlib.user_encoding
844
878
        profiled or debugged so easily.
845
879
 
846
880
        :param retcode: The status code that is expected.  Defaults to 0.  If
847
 
        None is supplied, the status code is not checked.
 
881
            None is supplied, the status code is not checked.
 
882
        :param env_changes: A dictionary which lists changes to environment
 
883
            variables. A value of None will unset the env variable.
 
884
            The values must be strings. The change will only occur in the
 
885
            child, so you don't need to fix the environment after running.
 
886
        :param universal_newlines: Convert CRLF => LF
848
887
        """
 
888
        env_changes = kwargs.get('env_changes', {})
 
889
 
 
890
        old_env = {}
 
891
 
 
892
        def cleanup_environment():
 
893
            for env_var, value in env_changes.iteritems():
 
894
                old_env[env_var] = osutils.set_or_unset_env(env_var, value)
 
895
 
 
896
        def restore_environment():
 
897
            for env_var, value in old_env.iteritems():
 
898
                osutils.set_or_unset_env(env_var, value)
 
899
 
849
900
        bzr_path = os.path.dirname(os.path.dirname(bzrlib.__file__))+'/bzr'
850
901
        args = list(args)
851
 
        process = Popen([sys.executable, bzr_path]+args, stdout=PIPE, 
852
 
                         stderr=PIPE)
 
902
 
 
903
        try:
 
904
            # win32 subprocess doesn't support preexec_fn
 
905
            # so we will avoid using it on all platforms, just to
 
906
            # make sure the code path is used, and we don't break on win32
 
907
            cleanup_environment()
 
908
            process = Popen([sys.executable, bzr_path]+args,
 
909
                             stdout=PIPE, stderr=PIPE)
 
910
        finally:
 
911
            restore_environment()
 
912
            
853
913
        out = process.stdout.read()
854
914
        err = process.stderr.read()
 
915
 
 
916
        if kwargs.get('universal_newlines', False):
 
917
            out = out.replace('\r\n', '\n')
 
918
            err = err.replace('\r\n', '\n')
 
919
 
855
920
        retcode = process.wait()
856
921
        supplied_retcode = kwargs.get('retcode', 0)
857
922
        if supplied_retcode is not None:
910
975
            sys.stderr = real_stderr
911
976
            sys.stdin = real_stdin
912
977
 
 
978
    @symbol_versioning.deprecated_method(symbol_versioning.zero_eleven)
913
979
    def merge(self, branch_from, wt_to):
914
980
        """A helper for tests to do a ui-less merge.
915
981
 
921
987
        base_rev = common_ancestor(branch_from.last_revision(),
922
988
                                   wt_to.branch.last_revision(),
923
989
                                   wt_to.branch.repository)
924
 
        merge_inner(wt_to.branch, branch_from.basis_tree(), 
 
990
        merge_inner(wt_to.branch, branch_from.basis_tree(),
925
991
                    wt_to.branch.repository.revision_tree(base_rev),
926
992
                    this_tree=wt_to)
927
 
        wt_to.add_pending_merge(branch_from.last_revision())
 
993
        wt_to.add_parent_tree_id(branch_from.last_revision())
928
994
 
929
995
 
930
996
BzrTestBase = TestCase
995
1061
                i = i + 1
996
1062
                continue
997
1063
            else:
998
 
                self.test_dir = candidate_dir
 
1064
                os.mkdir(candidate_dir)
 
1065
                self.test_home_dir = candidate_dir + '/home'
 
1066
                os.mkdir(self.test_home_dir)
 
1067
                self.test_dir = candidate_dir + '/work'
999
1068
                os.mkdir(self.test_dir)
1000
1069
                os.chdir(self.test_dir)
1001
1070
                break
1002
 
        os.environ['HOME'] = self.test_dir
1003
 
        os.environ['APPDATA'] = self.test_dir
 
1071
        os.environ['HOME'] = self.test_home_dir
 
1072
        os.environ['APPDATA'] = self.test_home_dir
1004
1073
        def _leaveDirectory():
1005
1074
            os.chdir(_currentdir)
1006
1075
        self.addCleanup(_leaveDirectory)
1045
1114
                # On jam's machine, make_kernel_like_tree is:
1046
1115
                #   put:    4.5-7.5s (averaging 6s)
1047
1116
                #   append: 2.9-4.5s
1048
 
                transport.append(urlutils.escape(name), StringIO(content))
 
1117
                #   put_non_atomic: 2.9-4.5s
 
1118
                transport.put_bytes_non_atomic(urlutils.escape(name), content)
1049
1119
 
1050
1120
    def build_tree_contents(self, shape):
1051
1121
        build_tree_contents(shape)
1362
1432
                   'bzrlib.tests.test_progress',
1363
1433
                   'bzrlib.tests.test_reconcile',
1364
1434
                   'bzrlib.tests.test_repository',
 
1435
                   'bzrlib.tests.test_revert',
1365
1436
                   'bzrlib.tests.test_revision',
1366
1437
                   'bzrlib.tests.test_revisionnamespaces',
1367
1438
                   'bzrlib.tests.test_revisiontree',
1370
1441
                   'bzrlib.tests.test_selftest',
1371
1442
                   'bzrlib.tests.test_setup',
1372
1443
                   'bzrlib.tests.test_sftp_transport',
 
1444
                   'bzrlib.tests.test_ftp_transport',
1373
1445
                   'bzrlib.tests.test_smart_add',
1374
1446
                   'bzrlib.tests.test_source',
1375
1447
                   'bzrlib.tests.test_status',