~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

Merge improved test deprecation helpers, simplifying handling of deprecated WorkingTree function tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
581
581
            self.fail("%r is an instance of %s rather than %s" % (
582
582
                obj, obj.__class__, kls))
583
583
 
584
 
    def callDeprecated(self, expected, callable, *args, **kwargs):
585
 
        """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.
586
586
 
587
 
        :param expected: a list of the deprecation warnings expected, in order
588
 
        :param callable: The callable to call
 
587
        :param a_callable: A callable to call.
589
588
        :param args: The positional arguments for the callable
590
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).
591
592
        """
592
593
        local_warnings = []
593
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.
594
597
            self.assertEqual(cls, DeprecationWarning)
595
598
            local_warnings.append(msg)
596
 
        method = symbol_versioning.warn
 
599
        original_warning_method = symbol_versioning.warn
597
600
        symbol_versioning.set_warning_method(capture_warnings)
598
601
        try:
599
 
            result = callable(*args, **kwargs)
 
602
            result = a_callable(*args, **kwargs)
600
603
        finally:
601
 
            symbol_versioning.set_warning_method(method)
602
 
        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
            raise AssertionError("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)
603
647
        return result
604
648
 
605
649
    def _startLogFile(self):