From 1def947dd15e9842b12af7d62ec7b6b1f729b2e8 Mon Sep 17 00:00:00 2001 From: Martin Valgur Date: Mon, 10 May 2021 09:23:12 +0300 Subject: [PATCH] use importlib_resources to find test fixtures --- setup.py | 9 ++++++++- tests/__init__.py | 0 tests/integration/test_filter.py | 2 +- tests/integration/test_httplib2.py | 2 +- tests/integration/test_requests.py | 2 +- tests/integration/test_stubs.py | 2 +- tests/integration/test_tornado.py | 2 +- tests/integration/test_urllib2.py | 2 +- tests/integration/test_urllib3.py | 2 +- tests/integration/test_wild.py | 7 ++++++- tests/unit/__init__.py | 0 tests/unit/test_migration.py | 18 +++++++++++------- tests/unit/test_persist.py | 16 ++++++++++------ tests/unit/test_serialize.py | 13 +++++++++---- 14 files changed, 51 insertions(+), 26 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/unit/__init__.py diff --git a/setup.py b/setup.py index 0eece40e..d7ca6779 100644 --- a/setup.py +++ b/setup.py @@ -62,8 +62,15 @@ def run_tests(self): packages=find_packages(exclude=["tests*"]), python_requires=">=3.5", install_requires=install_requires, + extras_require={ + "tests": [ + "pytest", + "mock", + "pytest-httpbin", + "importlib-resources" + ], + }, license="MIT", - tests_require=["pytest", "mock", "pytest-httpbin"], classifiers=[ "Development Status :: 5 - Production/Stable", "Environment :: Console", diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/integration/test_filter.py b/tests/integration/test_filter.py index b42bb342..f18eadb7 100644 --- a/tests/integration/test_filter.py +++ b/tests/integration/test_filter.py @@ -5,7 +5,7 @@ from urllib.error import HTTPError import vcr import json -from assertions import assert_cassette_has_one_response, assert_is_json +from ..assertions import assert_cassette_has_one_response, assert_is_json def _request_with_auth(url, username, password): diff --git a/tests/integration/test_httplib2.py b/tests/integration/test_httplib2.py index f5ce7c0b..295f17e4 100644 --- a/tests/integration/test_httplib2.py +++ b/tests/integration/test_httplib2.py @@ -9,7 +9,7 @@ import vcr -from assertions import assert_cassette_has_one_response +from ..assertions import assert_cassette_has_one_response httplib2 = pytest.importorskip("httplib2") diff --git a/tests/integration/test_requests.py b/tests/integration/test_requests.py index 8665bdf2..12237fdc 100644 --- a/tests/integration/test_requests.py +++ b/tests/integration/test_requests.py @@ -4,7 +4,7 @@ import pytest import sys import vcr -from assertions import assert_cassette_empty, assert_is_json +from ..assertions import assert_cassette_empty, assert_is_json requests = pytest.importorskip("requests") from requests.exceptions import ConnectionError # noqa E402 diff --git a/tests/integration/test_stubs.py b/tests/integration/test_stubs.py index bb6b5627..b2d951ca 100644 --- a/tests/integration/test_stubs.py +++ b/tests/integration/test_stubs.py @@ -3,7 +3,7 @@ import json import http.client as httplib -from assertions import assert_is_json +from ..assertions import assert_is_json def _headers_are_case_insensitive(host, port): diff --git a/tests/integration/test_tornado.py b/tests/integration/test_tornado.py index f8fdb046..f39120f1 100644 --- a/tests/integration/test_tornado.py +++ b/tests/integration/test_tornado.py @@ -7,7 +7,7 @@ import vcr from vcr.errors import CannotOverwriteExistingCassetteException -from assertions import assert_cassette_empty, assert_is_json +from ..assertions import assert_cassette_empty, assert_is_json tornado = pytest.importorskip("tornado") http = pytest.importorskip("tornado.httpclient") diff --git a/tests/integration/test_urllib2.py b/tests/integration/test_urllib2.py index 2582e0cb..a164febe 100644 --- a/tests/integration/test_urllib2.py +++ b/tests/integration/test_urllib2.py @@ -9,7 +9,7 @@ # Internal imports import vcr -from assertions import assert_cassette_has_one_response +from ..assertions import assert_cassette_has_one_response def urlopen_with_cafile(*args, **kwargs): diff --git a/tests/integration/test_urllib3.py b/tests/integration/test_urllib3.py index 110c05af..7eda6379 100644 --- a/tests/integration/test_urllib3.py +++ b/tests/integration/test_urllib3.py @@ -6,7 +6,7 @@ import pytest_httpbin import vcr from vcr.patch import force_reset -from assertions import assert_cassette_empty, assert_is_json +from ..assertions import assert_cassette_empty, assert_is_json urllib3 = pytest.importorskip("urllib3") diff --git a/tests/integration/test_wild.py b/tests/integration/test_wild.py index 997b092e..eefe1ff0 100644 --- a/tests/integration/test_wild.py +++ b/tests/integration/test_wild.py @@ -4,10 +4,15 @@ from xmlrpc.client import ServerProxy from xmlrpc.server import SimpleXMLRPCServer +from ..fixtures import wild +import importlib_resources + requests = pytest.importorskip("requests") import vcr # NOQA +wild_fixtures = importlib_resources.files(wild) + def test_domain_redirect(): """Ensure that redirects across domains are considered unique""" @@ -15,7 +20,7 @@ def test_domain_redirect(): # requests are considered identical, then we'll be stuck in a redirect # loop. url = "http://seomoz.org/" - with vcr.use_cassette("tests/fixtures/wild/domain_redirect.yaml") as cass: + with vcr.use_cassette(wild_fixtures / "domain_redirect.yaml") as cass: requests.get(url, headers={"User-Agent": "vcrpy-test"}) # Ensure that we've now served two responses. One for the original # redirect, and a second for the actual fetch diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/test_migration.py b/tests/unit/test_migration.py index 63e8aa4f..623a1364 100644 --- a/tests/unit/test_migration.py +++ b/tests/unit/test_migration.py @@ -5,18 +5,22 @@ import vcr.migration +from ..fixtures import migration +import importlib_resources + # Use the libYAML versions if possible try: from yaml import CLoader as Loader except ImportError: from yaml import Loader +migration_fixtures = importlib_resources.files(migration) def test_try_migrate_with_json(tmpdir): cassette = tmpdir.join("cassette.json").strpath - shutil.copy("tests/fixtures/migration/old_cassette.json", cassette) + shutil.copy(migration_fixtures / "old_cassette.json", cassette) assert vcr.migration.try_migrate(cassette) - with open("tests/fixtures/migration/new_cassette.json", "r") as f: + with open(migration_fixtures / "new_cassette.json", "r") as f: expected_json = json.load(f) with open(cassette, "r") as f: actual_json = json.load(f) @@ -25,9 +29,9 @@ def test_try_migrate_with_json(tmpdir): def test_try_migrate_with_yaml(tmpdir): cassette = tmpdir.join("cassette.yaml").strpath - shutil.copy("tests/fixtures/migration/old_cassette.yaml", cassette) + shutil.copy(migration_fixtures / "old_cassette.yaml", cassette) assert vcr.migration.try_migrate(cassette) - with open("tests/fixtures/migration/new_cassette.yaml", "r") as f: + with open(migration_fixtures / "new_cassette.yaml", "r") as f: expected_yaml = yaml.load(f, Loader=Loader) with open(cassette, "r") as f: actual_yaml = yaml.load(f, Loader=Loader) @@ -37,9 +41,9 @@ def test_try_migrate_with_yaml(tmpdir): def test_try_migrate_with_invalid_or_new_cassettes(tmpdir): cassette = tmpdir.join("cassette").strpath files = [ - "tests/fixtures/migration/not_cassette.txt", - "tests/fixtures/migration/new_cassette.yaml", - "tests/fixtures/migration/new_cassette.json", + migration_fixtures / "not_cassette.txt", + migration_fixtures / "new_cassette.yaml", + migration_fixtures / "new_cassette.json", ] for file_path in files: shutil.copy(file_path, cassette) diff --git a/tests/unit/test_persist.py b/tests/unit/test_persist.py index 025ad968..f9d51a00 100644 --- a/tests/unit/test_persist.py +++ b/tests/unit/test_persist.py @@ -3,28 +3,32 @@ from vcr.persisters.filesystem import FilesystemPersister from vcr.serializers import jsonserializer, yamlserializer +from ..fixtures import migration +import importlib_resources + +migration_fixtures = importlib_resources.files(migration) @pytest.mark.parametrize( "cassette_path, serializer", [ - ("tests/fixtures/migration/old_cassette.json", jsonserializer), - ("tests/fixtures/migration/old_cassette.yaml", yamlserializer), + ("old_cassette.json", jsonserializer), + ("old_cassette.yaml", yamlserializer), ], ) def test_load_cassette_with_old_cassettes(cassette_path, serializer): with pytest.raises(ValueError) as excinfo: - FilesystemPersister.load_cassette(cassette_path, serializer) + FilesystemPersister.load_cassette(migration_fixtures / cassette_path, serializer) assert "run the migration script" in excinfo.exconly() @pytest.mark.parametrize( "cassette_path, serializer", [ - ("tests/fixtures/migration/not_cassette.txt", jsonserializer), - ("tests/fixtures/migration/not_cassette.txt", yamlserializer), + ("not_cassette.txt", jsonserializer), + ("not_cassette.txt", yamlserializer), ], ) def test_load_cassette_with_invalid_cassettes(cassette_path, serializer): with pytest.raises(Exception) as excinfo: - FilesystemPersister.load_cassette(cassette_path, serializer) + FilesystemPersister.load_cassette(migration_fixtures / cassette_path, serializer) assert "run the migration script" not in excinfo.exconly() diff --git a/tests/unit/test_serialize.py b/tests/unit/test_serialize.py index c06d6d3f..ccbf1210 100644 --- a/tests/unit/test_serialize.py +++ b/tests/unit/test_serialize.py @@ -7,26 +7,31 @@ from vcr.serialize import deserialize, serialize from vcr.serializers import yamlserializer, jsonserializer, compat +from ..fixtures import migration +import importlib_resources + +migration_fixtures = importlib_resources.files(migration) + def test_deserialize_old_yaml_cassette(): - with open("tests/fixtures/migration/old_cassette.yaml", "r") as f: + with open(migration_fixtures / "old_cassette.yaml", "r") as f: with pytest.raises(ValueError): deserialize(f.read(), yamlserializer) def test_deserialize_old_json_cassette(): - with open("tests/fixtures/migration/old_cassette.json", "r") as f: + with open(migration_fixtures / "old_cassette.json", "r") as f: with pytest.raises(ValueError): deserialize(f.read(), jsonserializer) def test_deserialize_new_yaml_cassette(): - with open("tests/fixtures/migration/new_cassette.yaml", "r") as f: + with open(migration_fixtures / "new_cassette.yaml", "r") as f: deserialize(f.read(), yamlserializer) def test_deserialize_new_json_cassette(): - with open("tests/fixtures/migration/new_cassette.json", "r") as f: + with open(migration_fixtures / "new_cassette.json", "r") as f: deserialize(f.read(), jsonserializer)