]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
inject_cassette kwarg.
authorIvan Malison <IvanMalison@gmail.com>
Thu, 2 Apr 2015 00:06:02 +0000 (17:06 -0700)
committerIvan Malison <IvanMalison@gmail.com>
Thu, 2 Apr 2015 00:30:05 +0000 (17:30 -0700)
tests/unit/test_vcr.py
vcr/cassette.py
vcr/config.py

index 0c1c6c7da1a5ec5e6e4e206c94462d9c721eff96..d85b079257665455fc0d24938e393cffe18d1038 100644 (file)
@@ -70,10 +70,11 @@ def test_fixtures_with_use_cassette(random_fixture):
     # problems if the decorator does not preserve the signature of the original
     # test function.
 
-    # This test ensures that use_cassette preserves the signature of the original
-    # test function, and thus that use_cassette is compatible with py.test
-    # fixtures. It is admittedly a bit strange because the test would never even
-    # run if the relevant feature were broken.
+    # This test ensures that use_cassette preserves the signature of
+    # the original test function, and thus that use_cassette is
+    # compatible with py.test fixtures. It is admittedly a bit strange
+    # because the test would never even run if the relevant feature
+    # were broken.
     pass
 
 
@@ -90,3 +91,17 @@ def test_custom_patchers():
         assert issubclass(Test.attribute, VCRHTTPSConnection)
         assert VCRHTTPSConnection is not Test.attribute
         assert Test.attribute is Test.attribute2
+
+
+def test_inject_cassette():
+    vcr = VCR(inject_cassette=True)
+    @vcr.use_cassette('test', record_mode='once')
+    def with_cassette_injected(cassette):
+        assert cassette.record_mode == 'once'
+
+    @vcr.use_cassette('test', record_mode='once', inject_cassette=False)
+    def without_cassette_injected():
+        pass
+
+    with_cassette_injected()
+    without_cassette_injected()
index 5851280387dd44b74305ee4c5e5651541fd6cfdc..1194fcbe5c5585ee55859208b5bc48dceaa04530 100644 (file)
@@ -61,8 +61,11 @@ class CassetteContextDecorator(object):
 
     @wrapt.decorator
     def __call__(self, function, instance, args, kwargs):
-        with self:
-            return function(*args, **kwargs)
+        with self as cassette:
+            if cassette.inject:
+                return function(cassette, *args, **kwargs)
+            else:
+                return function(*args, **kwargs)
 
 
 class Cassette(object):
@@ -85,13 +88,15 @@ class Cassette(object):
 
     def __init__(self, path, serializer=yamlserializer, record_mode='once',
                  match_on=(uri, method),  before_record_request=None,
-                 before_record_response=None, custom_patches=()):
+                 before_record_response=None, custom_patches=(),
+                 inject=False):
 
         self._path = path
         self._serializer = serializer
         self._match_on = match_on
         self._before_record_request = before_record_request or (lambda x: x)
         self._before_record_response = before_record_response or (lambda x: x)
+        self.inject = inject
         self.record_mode = record_mode
         self.custom_patches = custom_patches
 
index dcb59a5db48db22b26cb136bbe5daf2823177ac8..6a66356f3f580e4981908efd8f51a45410106996 100644 (file)
@@ -12,11 +12,12 @@ from . import filters
 class VCR(object):
 
     def __init__(self, serializer='yaml', cassette_library_dir=None,
-                 record_mode="once", filter_headers=(), custom_patches=(),
-                 filter_query_parameters=(), filter_post_data_parameters=(),
-                 before_record_request=None, before_record_response=None,
-                 ignore_hosts=(), ignore_localhost=False, before_record=None,
-                 match_on=('method', 'scheme', 'host', 'port', 'path', 'query')):
+                 record_mode="once", filter_headers=(), ignore_localhost=False,
+                 custom_patches=(), filter_query_parameters=(),
+                 filter_post_data_parameters=(), before_record_request=None,
+                 before_record_response=None, ignore_hosts=(),
+                 match_on=('method', 'scheme', 'host', 'port', 'path', 'query'),
+                 before_record=None, inject_cassette=False):
         self.serializer = serializer
         self.match_on = match_on
         self.cassette_library_dir = cassette_library_dir
@@ -44,6 +45,7 @@ class VCR(object):
         self.before_record_response = before_record_response
         self.ignore_hosts = ignore_hosts
         self.ignore_localhost = ignore_localhost
+        self.inject_cassette = inject_cassette
         self._custom_patches = tuple(custom_patches)
 
     def _get_serializer(self, serializer_name):
@@ -99,7 +101,8 @@ class VCR(object):
             ),
             'custom_patches': self._custom_patches + kwargs.get(
                 'custom_patches', ()
-            )
+            ),
+            'inject': kwargs.get('inject_cassette', self.inject_cassette)
         }
         return path, merged_config