~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Martin Pool
  • Date: 2011-08-02 01:10:27 UTC
  • mfrom: (6015.9.4 2.4)
  • mto: This revision was merged to the branch mainline in revision 6047.
  • Revision ID: mbp@canonical.com-20110802011027-cv8b6h9q18ctxle4
merge up 2.3 and 2.4 into trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1728
1728
    def overrideAttr(self, obj, attr_name, new=_unitialized_attr):
1729
1729
        """Overrides an object attribute restoring it after the test.
1730
1730
 
 
1731
        :note: This should be used with discretion; you should think about
 
1732
        whether it's better to make the code testable without monkey-patching.
 
1733
 
1731
1734
        :param obj: The object that will be mutated.
1732
1735
 
1733
1736
        :param attr_name: The attribute name we want to preserve/override in
1758
1761
        self.addCleanup(osutils.set_or_unset_env, name, value)
1759
1762
        return value
1760
1763
 
 
1764
    def recordCalls(self, obj, attr_name):
 
1765
        """Monkeypatch in a wrapper that will record calls.
 
1766
 
 
1767
        The monkeypatch is automatically removed when the test concludes.
 
1768
 
 
1769
        :param obj: The namespace holding the reference to be replaced;
 
1770
            typically a module, class, or object.
 
1771
        :param attr_name: A string for the name of the attribute to 
 
1772
            patch.
 
1773
        :returns: A list that will be extended with one item every time the
 
1774
            function is called, with a tuple of (args, kwargs).
 
1775
        """
 
1776
        calls = []
 
1777
 
 
1778
        def decorator(*args, **kwargs):
 
1779
            calls.append((args, kwargs))
 
1780
            return orig(*args, **kwargs)
 
1781
        orig = self.overrideAttr(obj, attr_name, decorator)
 
1782
        return calls
 
1783
 
1761
1784
    def _cleanEnvironment(self):
1762
1785
        for name, value in isolated_environ.iteritems():
1763
1786
            self.overrideEnv(name, value)