1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-08 16:53:23 +00:00

Enables SIM ruff lint

This commit is contained in:
Jair Henrique
2025-11-19 10:14:34 -03:00
parent ab550d9a7a
commit 93737a0e73
16 changed files with 84 additions and 75 deletions

View File

@@ -25,6 +25,7 @@ select = [
"RUF", # Ruff-specific rules "RUF", # Ruff-specific rules
"UP", # pyupgrade "UP", # pyupgrade
"W", # pycodestyle warning "W", # pycodestyle warning
"SIM",
] ]
[tool.ruff.lint.isort] [tool.ruff.lint.isort]

View File

@@ -3,10 +3,11 @@
import codecs import codecs
import os import os
import re import re
from pathlib import Path
from setuptools import find_packages, setup from setuptools import find_packages, setup
long_description = open("README.rst").read() long_description = Path("README.rst").read_text()
here = os.path.abspath(os.path.dirname(__file__)) here = os.path.abspath(os.path.dirname(__file__))
@@ -29,16 +30,17 @@ def find_version(*file_paths):
install_requires = [ install_requires = [
"PyYAML", "PyYAML",
"wrapt", "wrapt",
"yarl",
] ]
extras_require = { extras_require = {
"tests": [ "tests": [
"aiohttp", "aiohttp",
"boto3", "boto3",
"httplib2", "cryptography",
"httpbin", "httpbin",
"httplib2",
"httpx", "httpx",
"pycurl; platform_python_implementation !='PyPy'",
"pytest", "pytest",
"pytest-aiohttp", "pytest-aiohttp",
"pytest-asyncio", "pytest-asyncio",
@@ -47,6 +49,7 @@ extras_require = {
"requests>=2.22.0", "requests>=2.22.0",
"tornado", "tornado",
"urllib3", "urllib3",
"werkzeug==2.0.3",
], ],
} }

View File

@@ -193,9 +193,11 @@ def test_params_same_url_distinct_params(tmpdir, httpbin):
assert cassette.play_count == 1 assert cassette.play_count == 1
other_params = {"other": "params"} other_params = {"other": "params"}
with vcr.use_cassette(str(tmpdir.join("get.yaml"))) as cassette: with (
with pytest.raises(vcr.errors.CannotOverwriteExistingCassetteException): vcr.use_cassette(str(tmpdir.join("get.yaml"))) as cassette,
get(url, output="text", params=other_params) pytest.raises(vcr.errors.CannotOverwriteExistingCassetteException),
):
get(url, output="text", params=other_params)
@pytest.mark.online @pytest.mark.online

View File

@@ -62,9 +62,8 @@ def test_override_match_on(tmpdir, httpbin):
def test_missing_matcher(): def test_missing_matcher():
my_vcr = vcr.VCR() my_vcr = vcr.VCR()
my_vcr.register_matcher("awesome", object) my_vcr.register_matcher("awesome", object)
with pytest.raises(KeyError): with pytest.raises(KeyError), my_vcr.use_cassette("test.yaml", match_on=["notawesome"]):
with my_vcr.use_cassette("test.yaml", match_on=["notawesome"]): pass
pass
@pytest.mark.online @pytest.mark.online
@@ -81,9 +80,8 @@ def test_dont_record_on_exception(tmpdir, httpbin):
assert not os.path.exists(str(tmpdir.join("dontsave.yml"))) assert not os.path.exists(str(tmpdir.join("dontsave.yml")))
# Make sure context decorator has the same behavior # Make sure context decorator has the same behavior
with pytest.raises(AssertionError): with pytest.raises(AssertionError), my_vcr.use_cassette(str(tmpdir.join("dontsave2.yml"))):
with my_vcr.use_cassette(str(tmpdir.join("dontsave2.yml"))): assert b"Not in content" in urlopen(httpbin.url).read()
assert b"Not in content" in urlopen(httpbin.url).read()
assert not os.path.exists(str(tmpdir.join("dontsave2.yml"))) assert not os.path.exists(str(tmpdir.join("dontsave2.yml")))

View File

@@ -60,9 +60,8 @@ class DoSyncRequest(BaseDoRequest):
return b"".join(response.iter_bytes()) return b"".join(response.iter_bytes())
# Use one-time context and dispose of the client afterwards # Use one-time context and dispose of the client afterwards
with self: with self, self.client.stream(*args, **kwargs) as response:
with self.client.stream(*args, **kwargs) as response: return b"".join(response.iter_bytes())
return b"".join(response.iter_bytes())
class DoAsyncRequest(BaseDoRequest): class DoAsyncRequest(BaseDoRequest):
@@ -195,9 +194,11 @@ def test_params_same_url_distinct_params(tmpdir, httpbin, do_request):
assert cassette.play_count == 1 assert cassette.play_count == 1
params = {"other": "params"} params = {"other": "params"}
with vcr.use_cassette(str(tmpdir.join("get.yaml"))) as cassette: with (
with pytest.raises(vcr.errors.CannotOverwriteExistingCassetteException): vcr.use_cassette(str(tmpdir.join("get.yaml"))) as cassette,
do_request()("GET", url, params=params, headers=headers) pytest.raises(vcr.errors.CannotOverwriteExistingCassetteException),
):
do_request()("GET", url, params=params, headers=headers)
@pytest.mark.online @pytest.mark.online

View File

@@ -51,9 +51,11 @@ def test_matchers(httpbin, httpbin_secure, cassette, matcher, matching_uri, not_
assert cass.play_count == 1 assert cass.play_count == 1
# play cassette with not matching on uri, it should fail # play cassette with not matching on uri, it should fail
with pytest.raises(vcr.errors.CannotOverwriteExistingCassetteException): with (
with vcr.use_cassette(cassette, match_on=[matcher]) as cass: pytest.raises(vcr.errors.CannotOverwriteExistingCassetteException),
urlopen(not_matching_uri) vcr.use_cassette(cassette, match_on=[matcher]) as cass,
):
urlopen(not_matching_uri)
def test_method_matcher(cassette, httpbin, httpbin_secure): def test_method_matcher(cassette, httpbin, httpbin_secure):
@@ -65,10 +67,12 @@ def test_method_matcher(cassette, httpbin, httpbin_secure):
assert cass.play_count == 1 assert cass.play_count == 1
# should fail if method does not match # should fail if method does not match
with pytest.raises(vcr.errors.CannotOverwriteExistingCassetteException): with (
with vcr.use_cassette(cassette, match_on=["method"]) as cass: pytest.raises(vcr.errors.CannotOverwriteExistingCassetteException),
# is a POST request vcr.use_cassette(cassette, match_on=["method"]) as cass,
urlopen(default_uri, data=b"") ):
# is a POST request
urlopen(default_uri, data=b"")
@pytest.mark.parametrize( @pytest.mark.parametrize(
@@ -98,14 +102,12 @@ def test_default_matcher_matches(cassette, uri, httpbin, httpbin_secure):
) )
def test_default_matcher_does_not_match(cassette, uri, httpbin, httpbin_secure): def test_default_matcher_does_not_match(cassette, uri, httpbin, httpbin_secure):
uri = _replace_httpbin(uri, httpbin, httpbin_secure) uri = _replace_httpbin(uri, httpbin, httpbin_secure)
with pytest.raises(vcr.errors.CannotOverwriteExistingCassetteException): with pytest.raises(vcr.errors.CannotOverwriteExistingCassetteException), vcr.use_cassette(cassette):
with vcr.use_cassette(cassette): urlopen(uri)
urlopen(uri)
def test_default_matcher_does_not_match_on_method(cassette, httpbin, httpbin_secure): def test_default_matcher_does_not_match_on_method(cassette, httpbin, httpbin_secure):
default_uri = _replace_httpbin(DEFAULT_URI, httpbin, httpbin_secure) default_uri = _replace_httpbin(DEFAULT_URI, httpbin, httpbin_secure)
with pytest.raises(vcr.errors.CannotOverwriteExistingCassetteException): with pytest.raises(vcr.errors.CannotOverwriteExistingCassetteException), vcr.use_cassette(cassette):
with vcr.use_cassette(cassette): # is a POST request
# is a POST request urlopen(default_uri, data=b"")
urlopen(default_uri, data=b"")

View File

@@ -124,9 +124,11 @@ def test_none_record_mode(tmpdir, httpbin):
# Cassette file doesn't exist, yet we are trying to make a request. # Cassette file doesn't exist, yet we are trying to make a request.
# raise hell. # raise hell.
testfile = str(tmpdir.join("recordmode.yml")) testfile = str(tmpdir.join("recordmode.yml"))
with vcr.use_cassette(testfile, record_mode=vcr.mode.NONE): with (
with pytest.raises(CannotOverwriteExistingCassetteException): vcr.use_cassette(testfile, record_mode=vcr.mode.NONE),
urlopen(httpbin.url).read() pytest.raises(CannotOverwriteExistingCassetteException),
):
urlopen(httpbin.url).read()
def test_none_record_mode_with_existing_cassette(tmpdir, httpbin): def test_none_record_mode_with_existing_cassette(tmpdir, httpbin):

View File

@@ -83,6 +83,5 @@ def test_load_cassette_persister_exception_handling(tmpdir, httpbin):
with my_vcr.use_cassette("bad/encoding") as cass: with my_vcr.use_cassette("bad/encoding") as cass:
assert len(cass) == 0 assert len(cass) == 0
with pytest.raises(ValueError): with pytest.raises(ValueError), my_vcr.use_cassette("bad/buggy") as cass:
with my_vcr.use_cassette("bad/buggy") as cass: pass
pass

View File

@@ -66,7 +66,7 @@ def test_original_decoded_response_is_not_modified(tmpdir, httpbin):
# Assert that we do not modify the original response while appending # Assert that we do not modify the original response while appending
# to the cassette. # to the cassette.
assert "gzip" == inside.headers["content-encoding"] assert inside.headers["content-encoding"] == "gzip"
# They should effectively be the same response. # They should effectively be the same response.
inside_headers = (h for h in inside.headers.items() if h[0].lower() != "date") inside_headers = (h for h in inside.headers.items() if h[0].lower() != "date")
@@ -122,7 +122,7 @@ def test_original_response_is_not_modified_by_before_filter(tmpdir, httpbin):
# Furthermore, the responses should be identical. # Furthermore, the responses should be identical.
inside_body = json.loads(inside.read()) inside_body = json.loads(inside.read())
outside_body = json.loads(outside.read()) outside_body = json.loads(outside.read())
assert not inside_body[field_to_scrub] == replacement assert inside_body[field_to_scrub] != replacement
assert inside_body[field_to_scrub] == outside_body[field_to_scrub] assert inside_body[field_to_scrub] == outside_body[field_to_scrub]
# Ensure that when a cassette exists, the scrubbed response is returned. # Ensure that when a cassette exists, the scrubbed response is returned.

View File

@@ -42,8 +42,13 @@ def scheme(request):
return request.param return request.param
@pytest.fixture(params=["curl", "default"]) @pytest.fixture(params=["simple", "curl", "default"])
def get_client(request): def get_client(request):
if request.param == "simple":
from tornado import simple_httpclient as simple
return lambda: simple.SimpleAsyncHTTPClient()
if request.param == "curl": if request.param == "curl":
curl = pytest.importorskip("tornado.curl_httpclient") curl = pytest.importorskip("tornado.curl_httpclient")
return lambda: curl.CurlAsyncHTTPClient() return lambda: curl.CurlAsyncHTTPClient()
@@ -75,7 +80,7 @@ def test_status_code(get_client, scheme, tmpdir):
with vcr.use_cassette(str(tmpdir.join("atts.yaml"))) as cass: with vcr.use_cassette(str(tmpdir.join("atts.yaml"))) as cass:
assert status_code == (yield get(get_client(), url)).code assert status_code == (yield get(get_client(), url)).code
assert 1 == cass.play_count assert cass.play_count == 1
@pytest.mark.online @pytest.mark.online
@@ -88,7 +93,7 @@ def test_headers(get_client, scheme, tmpdir):
with vcr.use_cassette(str(tmpdir.join("headers.yaml"))) as cass: with vcr.use_cassette(str(tmpdir.join("headers.yaml"))) as cass:
assert headers == (yield get(get_client(), url)).headers assert headers == (yield get(get_client(), url)).headers
assert 1 == cass.play_count assert cass.play_count == 1
@pytest.mark.online @pytest.mark.online
@@ -102,7 +107,7 @@ def test_body(get_client, tmpdir, scheme):
with vcr.use_cassette(str(tmpdir.join("body.yaml"))) as cass: with vcr.use_cassette(str(tmpdir.join("body.yaml"))) as cass:
assert content == (yield get(get_client(), url)).body assert content == (yield get(get_client(), url)).body
assert 1 == cass.play_count assert cass.play_count == 1
@gen_test @gen_test
@@ -115,7 +120,7 @@ def test_effective_url(get_client, tmpdir, httpbin):
with vcr.use_cassette(str(tmpdir.join("url.yaml"))) as cass: with vcr.use_cassette(str(tmpdir.join("url.yaml"))) as cass:
assert effective_url == (yield get(get_client(), url)).effective_url assert effective_url == (yield get(get_client(), url)).effective_url
assert 1 == cass.play_count assert cass.play_count == 1
@pytest.mark.online @pytest.mark.online
@@ -131,7 +136,7 @@ def test_auth(get_client, tmpdir, scheme):
two = yield get(get_client(), url, auth_username=auth[0], auth_password=auth[1]) two = yield get(get_client(), url, auth_username=auth[0], auth_password=auth[1])
assert one.body == two.body assert one.body == two.body
assert one.code == two.code assert one.code == two.code
assert 1 == cass.play_count assert cass.play_count == 1
@pytest.mark.online @pytest.mark.online
@@ -155,7 +160,7 @@ def test_auth_failed(get_client, tmpdir, scheme):
assert exc_info.value.code == 401 assert exc_info.value.code == 401
assert one.body == two.body assert one.body == two.body
assert one.code == two.code == 401 assert one.code == two.code == 401
assert 1 == cass.play_count assert cass.play_count == 1
@pytest.mark.online @pytest.mark.online
@@ -171,7 +176,7 @@ def test_post(get_client, tmpdir, scheme):
req2 = (yield post(get_client(), url, data)).body req2 = (yield post(get_client(), url, data)).body
assert req1 == req2 assert req1 == req2
assert 1 == cass.play_count assert cass.play_count == 1
@gen_test @gen_test
@@ -229,7 +234,7 @@ def test_gzip(get_client, tmpdir, scheme):
with vcr.use_cassette(str(tmpdir.join("gzip.yaml"))) as cass: with vcr.use_cassette(str(tmpdir.join("gzip.yaml"))) as cass:
response = yield get(get_client(), url, **kwargs) response = yield get(get_client(), url, **kwargs)
assert_is_json_bytes(response.body) assert_is_json_bytes(response.body)
assert 1 == cass.play_count assert cass.play_count == 1
@pytest.mark.online @pytest.mark.online
@@ -242,7 +247,7 @@ def test_https_with_cert_validation_disabled(get_client, tmpdir):
with vcr.use_cassette(cass_path) as cass: with vcr.use_cassette(cass_path) as cass:
yield get(get_client(), "https://httpbin.org", validate_cert=False) yield get(get_client(), "https://httpbin.org", validate_cert=False)
assert 1 == cass.play_count assert cass.play_count == 1
@gen_test @gen_test

View File

@@ -62,13 +62,12 @@ def test_flickr_should_respond_with_200(tmpdir):
def test_cookies(tmpdir, httpbin): def test_cookies(tmpdir, httpbin):
testfile = str(tmpdir.join("cookies.yml")) testfile = str(tmpdir.join("cookies.yml"))
with vcr.use_cassette(testfile): with vcr.use_cassette(testfile), requests.Session() as s:
with requests.Session() as s: s.get(httpbin.url + "/cookies/set?k1=v1&k2=v2")
s.get(httpbin.url + "/cookies/set?k1=v1&k2=v2") assert s.cookies.keys() == ["k1", "k2"]
assert s.cookies.keys() == ["k1", "k2"]
r2 = s.get(httpbin.url + "/cookies") r2 = s.get(httpbin.url + "/cookies")
assert sorted(r2.json()["cookies"].keys()) == ["k1", "k2"] assert sorted(r2.json()["cookies"].keys()) == ["k1", "k2"]
@pytest.mark.online @pytest.mark.online

View File

@@ -227,9 +227,11 @@ def test_nesting_cassette_context_managers(*args):
assert_get_response_body_is("first_response") assert_get_response_body_is("first_response")
# Make sure a second cassette can supersede the first # Make sure a second cassette can supersede the first
with Cassette.use(path="test") as second_cassette: with (
with mock.patch.object(second_cassette, "play_response", return_value=second_response): Cassette.use(path="test") as second_cassette,
assert_get_response_body_is("second_response") mock.patch.object(second_cassette, "play_response", return_value=second_response),
):
assert_get_response_body_is("second_response")
# Now the first cassette should be back in effect # Now the first cassette should be back in effect
assert_get_response_body_is("first_response") assert_get_response_body_is("first_response")

View File

@@ -8,15 +8,13 @@ from vcr.serializers import compat, jsonserializer, yamlserializer
def test_deserialize_old_yaml_cassette(): def test_deserialize_old_yaml_cassette():
with open("tests/fixtures/migration/old_cassette.yaml") as f: with open("tests/fixtures/migration/old_cassette.yaml") as f, pytest.raises(ValueError):
with pytest.raises(ValueError): deserialize(f.read(), yamlserializer)
deserialize(f.read(), yamlserializer)
def test_deserialize_old_json_cassette(): def test_deserialize_old_json_cassette():
with open("tests/fixtures/migration/old_cassette.json") as f: with open("tests/fixtures/migration/old_cassette.json") as f, pytest.raises(ValueError):
with pytest.raises(ValueError): deserialize(f.read(), jsonserializer)
deserialize(f.read(), jsonserializer)
def test_deserialize_new_yaml_cassette(): def test_deserialize_new_yaml_cassette():

View File

@@ -1,5 +1,6 @@
import logging import logging
import warnings import warnings
from contextlib import suppress
from io import BytesIO from io import BytesIO
from urllib.parse import parse_qsl, urlparse from urllib.parse import parse_qsl, urlparse
@@ -80,10 +81,9 @@ class Request:
def port(self): def port(self):
port = self.parsed_uri.port port = self.parsed_uri.port
if port is None: if port is None:
try: with suppress(KeyError):
port = {"https": 443, "http": 80}[self.parsed_uri.scheme] port = {"https": 443, "http": 80}[self.parsed_uri.scheme]
except KeyError:
pass
return port return port
@property @property

View File

@@ -1,6 +1,7 @@
"""Stubs for patching HTTP and HTTPS requests""" """Stubs for patching HTTP and HTTPS requests"""
import logging import logging
from contextlib import suppress
from http.client import HTTPConnection, HTTPResponse, HTTPSConnection from http.client import HTTPConnection, HTTPResponse, HTTPSConnection
from io import BytesIO from io import BytesIO
@@ -77,7 +78,7 @@ class VCRHTTPResponse(HTTPResponse):
# libraries trying to process a chunked response. By removing the # libraries trying to process a chunked response. By removing the
# transfer-encoding: chunked header, this should cause the downstream # transfer-encoding: chunked header, this should cause the downstream
# libraries to process this as a non-chunked response. # libraries to process this as a non-chunked response.
te_key = [h for h in headers.keys() if h.upper() == "TRANSFER-ENCODING"] te_key = [h for h in headers if h.upper() == "TRANSFER-ENCODING"]
if te_key: if te_key:
del headers[te_key[0]] del headers[te_key[0]]
self.headers = self.msg = parse_headers(headers) self.headers = self.msg = parse_headers(headers)
@@ -370,12 +371,8 @@ class VCRConnection:
TODO: Separately setting the attribute on the two instances is not TODO: Separately setting the attribute on the two instances is not
ideal. We should switch to a proxying implementation. ideal. We should switch to a proxying implementation.
""" """
try: with suppress(AttributeError):
setattr(self.real_connection, name, value) setattr(self.real_connection, name, value)
except AttributeError:
# raised if real_connection has not been set yet, such as when
# we're setting the real_connection itself for the first time
pass
super().__setattr__(name, value) super().__setattr__(name, value)

View File

@@ -74,7 +74,7 @@ def vcr_fetch_impl(cassette, real_fetch_impl):
return callback(response) return callback(response)
def new_callback(response): def new_callback(response):
headers = [(k, response.headers.get_list(k)) for k in response.headers.keys()] headers = [(k, response.headers.get_list(k)) for k in response.headers]
vcr_response = { vcr_response = {
"status": {"code": response.code, "message": response.reason}, "status": {"code": response.code, "message": response.reason},