~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Vincent Ladeuil
  • Date: 2007-11-04 15:29:17 UTC
  • mfrom: (2961 +trunk)
  • mto: (2961.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 2962.
  • Revision ID: v.ladeuil+lp@free.fr-20071104152917-nrsumxpk3dikso2c
Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1032
1032
        else:
1033
1033
            self.fail('Unexpected success.  Should have failed: %s' % reason)
1034
1034
 
1035
 
    def _capture_warnings(self, a_callable, *args, **kwargs):
 
1035
    def _capture_deprecation_warnings(self, a_callable, *args, **kwargs):
1036
1036
        """A helper for callDeprecated and applyDeprecated.
1037
1037
 
1038
1038
        :param a_callable: A callable to call.
1080
1080
        :param kwargs: The keyword arguments for the callable
1081
1081
        :return: The result of a_callable(``*args``, ``**kwargs``)
1082
1082
        """
1083
 
        call_warnings, result = self._capture_warnings(a_callable,
 
1083
        call_warnings, result = self._capture_deprecation_warnings(a_callable,
1084
1084
            *args, **kwargs)
1085
1085
        expected_first_warning = symbol_versioning.deprecation_string(
1086
1086
            a_callable, deprecation_format)
1090
1090
        self.assertEqual(expected_first_warning, call_warnings[0])
1091
1091
        return result
1092
1092
 
 
1093
    def callCatchWarnings(self, fn, *args, **kw):
 
1094
        """Call a callable that raises python warnings.
 
1095
 
 
1096
        The caller's responsible for examining the returned warnings.
 
1097
 
 
1098
        If the callable raises an exception, the exception is not
 
1099
        caught and propagates up to the caller.  In that case, the list
 
1100
        of warnings is not available.
 
1101
 
 
1102
        :returns: ([warning_object, ...], fn_result)
 
1103
        """
 
1104
        # XXX: This is not perfect, because it completely overrides the
 
1105
        # warnings filters, and some code may depend on suppressing particular
 
1106
        # warnings.  It's the easiest way to insulate ourselves from -Werror,
 
1107
        # though.  -- Andrew, 20071062
 
1108
        wlist = []
 
1109
        def _catcher(message, category, filename, lineno, file=None):
 
1110
            # despite the name, 'message' is normally(?) a Warning subclass
 
1111
            # instance
 
1112
            wlist.append(message)
 
1113
        saved_showwarning = warnings.showwarning
 
1114
        saved_filters = warnings.filters
 
1115
        try:
 
1116
            warnings.showwarning = _catcher
 
1117
            warnings.filters = []
 
1118
            result = fn(*args, **kw)
 
1119
        finally:
 
1120
            warnings.showwarning = saved_showwarning
 
1121
            warnings.filters = saved_filters
 
1122
        return wlist, result
 
1123
 
1093
1124
    def callDeprecated(self, expected, callable, *args, **kwargs):
1094
1125
        """Assert that a callable is deprecated in a particular way.
1095
1126
 
1099
1130
        and will ensure that that is issued for the function being called.
1100
1131
 
1101
1132
        Note that this only captures warnings raised by symbol_versioning.warn,
1102
 
        not other callers that go direct to the warning module.
 
1133
        not other callers that go direct to the warning module.  To catch
 
1134
        general warnings, use callCatchWarnings.
1103
1135
 
1104
1136
        :param expected: a list of the deprecation warnings expected, in order
1105
1137
        :param callable: The callable to call
1106
1138
        :param args: The positional arguments for the callable
1107
1139
        :param kwargs: The keyword arguments for the callable
1108
1140
        """
1109
 
        call_warnings, result = self._capture_warnings(callable,
 
1141
        call_warnings, result = self._capture_deprecation_warnings(callable,
1110
1142
            *args, **kwargs)
1111
1143
        self.assertEqual(expected, call_warnings)
1112
1144
        return result
2587
2619
        if sys.platform == 'win32' and e.errno == errno.EACCES:
2588
2620
            sys.stderr.write(('Permission denied: '
2589
2621
                                 'unable to remove testing dir '
2590
 
                                 '%\n' % os.path.basename(dirname)))
 
2622
                                 '%s\n' % os.path.basename(dirname)))
2591
2623
        else:
2592
2624
            raise
2593
2625
 
2631
2663
SymlinkFeature = _SymlinkFeature()
2632
2664
 
2633
2665
 
 
2666
class _OsFifoFeature(Feature):
 
2667
 
 
2668
    def _probe(self):
 
2669
        return getattr(os, 'mkfifo', None)
 
2670
 
 
2671
    def feature_name(self):
 
2672
        return 'filesystem fifos'
 
2673
 
 
2674
OsFifoFeature = _OsFifoFeature()
 
2675
 
 
2676
 
2634
2677
class TestScenarioApplier(object):
2635
2678
    """A tool to apply scenarios to tests."""
2636
2679
 
2691
2734
        except UnicodeDecodeError:
2692
2735
            return char
2693
2736
    return None
 
2737
 
 
2738
 
 
2739
class _FTPServerFeature(Feature):
 
2740
    """Some tests want an FTP Server, check if one is available.
 
2741
 
 
2742
    Right now, the only way this is available is if 'medusa' is installed.
 
2743
    http://www.amk.ca/python/code/medusa.html
 
2744
    """
 
2745
 
 
2746
    def _probe(self):
 
2747
        try:
 
2748
            import bzrlib.tests.ftp_server
 
2749
            return True
 
2750
        except ImportError:
 
2751
            return False
 
2752
 
 
2753
    def feature_name(self):
 
2754
        return 'FTPServer'
 
2755
 
 
2756
FTPServerFeature = _FTPServerFeature()