55import os
66from urllib .request import urlopen
77
8+ import pytest
9+
810# Internal imports
911import vcr
10- from vcr .persisters .filesystem import FilesystemPersister
12+ from vcr .persisters .filesystem import CassetteDecodeError , CassetteNotFoundError , FilesystemPersister
1113
1214
1315class CustomFilesystemPersister :
@@ -25,6 +27,19 @@ def save_cassette(cassette_path, cassette_dict, serializer):
2527 FilesystemPersister .save_cassette (cassette_path , cassette_dict , serializer )
2628
2729
30+ class BadPersister (FilesystemPersister ):
31+ """A bad persister that raises different errors."""
32+
33+ @staticmethod
34+ def load_cassette (cassette_path , serializer ):
35+ if "nonexistent" in cassette_path :
36+ raise CassetteNotFoundError ()
37+ elif "encoding" in cassette_path :
38+ raise CassetteDecodeError ()
39+ else :
40+ raise ValueError ("buggy persister" )
41+
42+
2843def test_save_cassette_with_custom_persister (tmpdir , httpbin ):
2944 """Ensure you can save a cassette using custom persister"""
3045 my_vcr = vcr .VCR ()
@@ -53,3 +68,22 @@ def test_load_cassette_with_custom_persister(tmpdir, httpbin):
5368 with my_vcr .use_cassette (test_fixture , serializer = "json" ):
5469 response = urlopen (httpbin .url ).read ()
5570 assert b"difficult sometimes" in response
71+
72+
73+ def test_load_cassette_persister_exception_handling (tmpdir , httpbin ):
74+ """
75+ Ensure expected errors from persister are swallowed while unexpected ones
76+ are passed up the call stack.
77+ """
78+ my_vcr = vcr .VCR ()
79+ my_vcr .register_persister (BadPersister )
80+
81+ with my_vcr .use_cassette ("bad/nonexistent" ) as cass :
82+ assert len (cass ) == 0
83+
84+ with my_vcr .use_cassette ("bad/encoding" ) as cass :
85+ assert len (cass ) == 0
86+
87+ with pytest .raises (ValueError ):
88+ with my_vcr .use_cassette ("bad/buggy" ) as cass :
89+ pass
0 commit comments