1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-09 01:03:24 +00:00

Add Path handling to use_cassette and to filesystem.py persister

* now it is possible to use path from pathlib
This commit is contained in:
Josef
2022-09-24 23:57:26 +02:00
committed by Jair Henrique
parent 511d0ab855
commit 526fdbb194
3 changed files with 28 additions and 15 deletions

View File

@@ -1,5 +1,6 @@
import http.client as httplib import http.client as httplib
import os import os
from pathlib import Path
from unittest import mock from unittest import mock
import pytest import pytest
@@ -95,7 +96,6 @@ def test_vcr_before_record_response_iterable():
# Prevent actually saving the cassette # Prevent actually saving the cassette
with mock.patch("vcr.cassette.FilesystemPersister.save_cassette"): with mock.patch("vcr.cassette.FilesystemPersister.save_cassette"):
# Baseline: non-iterable before_record_response should work # Baseline: non-iterable before_record_response should work
mock_filter = mock.Mock() mock_filter = mock.Mock()
vcr = VCR(before_record_response=mock_filter) vcr = VCR(before_record_response=mock_filter)
@@ -119,7 +119,6 @@ def test_before_record_response_as_filter():
# Prevent actually saving the cassette # Prevent actually saving the cassette
with mock.patch("vcr.cassette.FilesystemPersister.save_cassette"): with mock.patch("vcr.cassette.FilesystemPersister.save_cassette"):
filter_all = mock.Mock(return_value=None) filter_all = mock.Mock(return_value=None)
vcr = VCR(before_record_response=filter_all) vcr = VCR(before_record_response=filter_all)
with vcr.use_cassette("test") as cassette: with vcr.use_cassette("test") as cassette:
@@ -133,7 +132,6 @@ def test_vcr_path_transformer():
# Prevent actually saving the cassette # Prevent actually saving the cassette
with mock.patch("vcr.cassette.FilesystemPersister.save_cassette"): with mock.patch("vcr.cassette.FilesystemPersister.save_cassette"):
# Baseline: path should be unchanged # Baseline: path should be unchanged
vcr = VCR() vcr = VCR()
with vcr.use_cassette("test") as cassette: with vcr.use_cassette("test") as cassette:
@@ -360,3 +358,11 @@ def test_dynamically_added(self):
TestVCRClass.test_dynamically_added = test_dynamically_added TestVCRClass.test_dynamically_added = test_dynamically_added
del test_dynamically_added del test_dynamically_added
def test_path_class_as_cassette():
path = Path(__file__).parent.parent.joinpath(
"integration/cassettes/test_httpx_test_test_behind_proxy.yml"
)
with use_cassette(path):
pass

View File

@@ -4,6 +4,7 @@ import inspect
import os import os
import types import types
from collections import abc as collections_abc from collections import abc as collections_abc
from pathlib import Path
import six import six
@@ -98,7 +99,7 @@ class VCR:
return matchers return matchers
def use_cassette(self, path=None, **kwargs): def use_cassette(self, path=None, **kwargs):
if path is not None and not isinstance(path, str): if path is not None and not isinstance(path, (str, Path)):
function = path function = path
# Assume this is an attempt to decorate a function # Assume this is an attempt to decorate a function
return self._use_cassette(**kwargs)(function) return self._use_cassette(**kwargs)(function)

View File

@@ -1,6 +1,6 @@
# .. _persister_example: # .. _persister_example:
import os from pathlib import Path
from ..serialize import deserialize, serialize from ..serialize import deserialize, serialize
@@ -8,19 +8,25 @@ from ..serialize import deserialize, serialize
class FilesystemPersister: class FilesystemPersister:
@classmethod @classmethod
def load_cassette(cls, cassette_path, serializer): def load_cassette(cls, cassette_path, serializer):
try: cassette_path = Path(cassette_path) # if cassette path is already Path this is no operation
with open(cassette_path) as f: if not cassette_path.is_file():
cassette_content = f.read()
except OSError:
raise ValueError("Cassette not found.") raise ValueError("Cassette not found.")
cassette = deserialize(cassette_content, serializer) try:
return cassette with cassette_path.open() as f:
data = f.read()
except UnicodeEncodeError as err:
raise ValueError("Can't read Cassette, Encoding is broken") from err
return deserialize(data, serializer)
@staticmethod @staticmethod
def save_cassette(cassette_path, cassette_dict, serializer): def save_cassette(cassette_path, cassette_dict, serializer):
data = serialize(cassette_dict, serializer) data = serialize(cassette_dict, serializer)
dirname, filename = os.path.split(cassette_path) cassette_path = Path(cassette_path) # if cassette path is already Path this is no operation
if dirname and not os.path.exists(dirname):
os.makedirs(dirname) cassette_folder = cassette_path.parent
with open(cassette_path, "w") as f: if not cassette_folder.exists():
cassette_folder.mkdir(parents=True)
with cassette_path.open("w") as f:
f.write(data) f.write(data)