mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 01:03:24 +00:00
Add record_on_exception flag.
Defaults to True, which maintains historical behavior. Fixes #205.
This commit is contained in:
committed by
Jair Henrique
parent
423ccaa40b
commit
995020bf06
@@ -60,3 +60,23 @@ def test_missing_matcher():
|
|||||||
with pytest.raises(KeyError):
|
with pytest.raises(KeyError):
|
||||||
with my_vcr.use_cassette("test.yaml", match_on=["notawesome"]):
|
with my_vcr.use_cassette("test.yaml", match_on=["notawesome"]):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def test_dont_record_on_exception(tmpdir):
|
||||||
|
my_vcr = vcr.VCR(record_on_exception=False)
|
||||||
|
|
||||||
|
@my_vcr.use_cassette(str(tmpdir.join('dontsave.yml')))
|
||||||
|
def some_test():
|
||||||
|
assert 'Not in content' in urlopen('http://httpbin.org/get')
|
||||||
|
|
||||||
|
with pytest.raises(AssertionError):
|
||||||
|
some_test()
|
||||||
|
|
||||||
|
assert not os.path.exists(str(tmpdir.join('dontsave.yml')))
|
||||||
|
|
||||||
|
# Make sure context decorator has the same behavior
|
||||||
|
with pytest.raises(AssertionError):
|
||||||
|
with my_vcr.use_cassette(str(tmpdir.join('dontsave2.yml'))):
|
||||||
|
assert 'Not in content' in urlopen('http://httpbin.org/get').read()
|
||||||
|
|
||||||
|
assert not os.path.exists(str(tmpdir.join('dontsave2.yml')))
|
||||||
|
|||||||
@@ -45,7 +45,9 @@ class CassetteContextDecorator:
|
|||||||
this class as a context manager in ``__exit__``.
|
this class as a context manager in ``__exit__``.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
_non_cassette_arguments = ("path_transformer", "func_path_generator")
|
_non_cassette_arguments = (
|
||||||
|
"path_transformer", "func_path_generator", "record_on_exception",
|
||||||
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_args(cls, cassette_class, **kwargs):
|
def from_args(cls, cassette_class, **kwargs):
|
||||||
@@ -87,8 +89,13 @@ class CassetteContextDecorator:
|
|||||||
self.__finish = self._patch_generator(self.cls.load(**cassette_kwargs))
|
self.__finish = self._patch_generator(self.cls.load(**cassette_kwargs))
|
||||||
return next(self.__finish)
|
return next(self.__finish)
|
||||||
|
|
||||||
def __exit__(self, *args):
|
def __exit__(self, *exc_info):
|
||||||
next(self.__finish, None)
|
exception_was_raised = any(exc_info)
|
||||||
|
record_on_exception = self._args_getter().get(
|
||||||
|
'record_on_exception', True
|
||||||
|
)
|
||||||
|
if record_on_exception or not exception_was_raised:
|
||||||
|
next(self.__finish, None)
|
||||||
self.__finish = None
|
self.__finish = None
|
||||||
|
|
||||||
@wrapt.decorator
|
@wrapt.decorator
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ class VCR:
|
|||||||
cassette_library_dir=None,
|
cassette_library_dir=None,
|
||||||
func_path_generator=None,
|
func_path_generator=None,
|
||||||
decode_compressed_response=False,
|
decode_compressed_response=False,
|
||||||
|
record_on_exception=True,
|
||||||
):
|
):
|
||||||
self.serializer = serializer
|
self.serializer = serializer
|
||||||
self.match_on = match_on
|
self.match_on = match_on
|
||||||
@@ -80,6 +81,7 @@ class VCR:
|
|||||||
self.path_transformer = path_transformer
|
self.path_transformer = path_transformer
|
||||||
self.func_path_generator = func_path_generator
|
self.func_path_generator = func_path_generator
|
||||||
self.decode_compressed_response = decode_compressed_response
|
self.decode_compressed_response = decode_compressed_response
|
||||||
|
self.record_on_exception = record_on_exception
|
||||||
self._custom_patches = tuple(custom_patches)
|
self._custom_patches = tuple(custom_patches)
|
||||||
|
|
||||||
def _get_serializer(self, serializer_name):
|
def _get_serializer(self, serializer_name):
|
||||||
@@ -123,6 +125,7 @@ class VCR:
|
|||||||
func_path_generator = kwargs.get("func_path_generator", self.func_path_generator)
|
func_path_generator = kwargs.get("func_path_generator", self.func_path_generator)
|
||||||
cassette_library_dir = kwargs.get("cassette_library_dir", self.cassette_library_dir)
|
cassette_library_dir = kwargs.get("cassette_library_dir", self.cassette_library_dir)
|
||||||
additional_matchers = kwargs.get("additional_matchers", ())
|
additional_matchers = kwargs.get("additional_matchers", ())
|
||||||
|
record_on_exception = kwargs.get("record_on_exception", self.record_on_exception)
|
||||||
|
|
||||||
if cassette_library_dir:
|
if cassette_library_dir:
|
||||||
|
|
||||||
@@ -149,6 +152,7 @@ class VCR:
|
|||||||
"path_transformer": path_transformer,
|
"path_transformer": path_transformer,
|
||||||
"func_path_generator": func_path_generator,
|
"func_path_generator": func_path_generator,
|
||||||
"allow_playback_repeats": kwargs.get("allow_playback_repeats", False),
|
"allow_playback_repeats": kwargs.get("allow_playback_repeats", False),
|
||||||
|
"record_on_exception": record_on_exception,
|
||||||
}
|
}
|
||||||
path = kwargs.get("path")
|
path = kwargs.get("path")
|
||||||
if path:
|
if path:
|
||||||
|
|||||||
Reference in New Issue
Block a user