Skip to content

Commit

Permalink
Use ruff as linter
Browse files Browse the repository at this point in the history
  • Loading branch information
jairhenrique committed Jun 26, 2023
1 parent 34d5384 commit d6c0017
Show file tree
Hide file tree
Showing 31 changed files with 188 additions and 114 deletions.
16 changes: 16 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,19 @@ ignore-regex = "\\\\[fnrstv]"
markers = [
"online",
]

[tool.ruff]
select = [
"C4", # flake8-comprehensions
"COM", # flake8-commas
"I", # isort
"ISC", # flake8-implicit-str-concat
"PIE", # flake8-pie
"RUF", # Ruff-specific rules
"UP", # pyupgrade
]
line-length = 110
target-version = "py38"

[tool.ruff.isort]
known-first-party = [ "vcr" ]
20 changes: 12 additions & 8 deletions tests/integration/test_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
asyncio = pytest.importorskip("asyncio")
aiohttp = pytest.importorskip("aiohttp")

import vcr # noqa: E402
import vcr

from .aiohttp_utils import aiohttp_app, aiohttp_request # noqa: E402
from .aiohttp_utils import aiohttp_app, aiohttp_request


def run_in_loop(fn):
Expand Down Expand Up @@ -278,9 +278,7 @@ def test_redirect(tmpdir, mockbin):
# looking request_info.
assert cassette_response.request_info.url == response.request_info.url
assert cassette_response.request_info.method == response.request_info.method
assert {k: v for k, v in cassette_response.request_info.headers.items()} == {
k: v for k, v in response.request_info.headers.items()
}
assert dict(cassette_response.request_info.headers.items()) == dict(response.request_info.headers.items())
assert cassette_response.request_info.real_url == response.request_info.real_url


Expand Down Expand Up @@ -351,7 +349,10 @@ async def run(loop):
async with aiohttp.ClientSession(loop=loop, cookie_jar=aiohttp.CookieJar(unsafe=True)) as session:
cookies_resp = await session.get(cookies_url, ssl=httpbin_ssl_context)
home_resp = await session.get(
home_url, cookies=req_cookies, headers=req_headers, ssl=httpbin_ssl_context
home_url,
cookies=req_cookies,
headers=req_headers,
ssl=httpbin_ssl_context,
)
assert cassette.play_count == 0
assert_responses(cookies_resp, home_resp)
Expand All @@ -361,7 +362,10 @@ async def run(loop):
async with aiohttp.ClientSession(loop=loop, cookie_jar=aiohttp.CookieJar(unsafe=True)) as session:
cookies_resp = await session.get(cookies_url, ssl=httpbin_ssl_context)
home_resp = await session.get(
home_url, cookies=req_cookies, headers=req_headers, ssl=httpbin_ssl_context
home_url,
cookies=req_cookies,
headers=req_headers,
ssl=httpbin_ssl_context,
)
assert cassette.play_count == 2
assert_responses(cookies_resp, home_resp)
Expand Down Expand Up @@ -407,7 +411,7 @@ async def run(loop):
# Assert that it's ignoring expiration date
with vcr.use_cassette(tmp, record_mode=vcr.mode.NONE) as cassette:
cassette.responses[0]["headers"]["set-cookie"] = [
"Cookie_1=Val_1; Expires=Wed, 21 Oct 2015 07:28:00 GMT"
"Cookie_1=Val_1; Expires=Wed, 21 Oct 2015 07:28:00 GMT",
]
async with aiohttp.ClientSession(loop=loop, cookie_jar=aiohttp.CookieJar(unsafe=True)) as session:
cookies_resp = await session.get(cookies_url, ssl=httpbin_ssl_context)
Expand Down
12 changes: 6 additions & 6 deletions tests/integration/test_boto.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

boto = pytest.importorskip("boto")

from configparser import DuplicateSectionError # NOQA
from configparser import DuplicateSectionError

import boto # NOQA
import boto.iam # NOQA
from boto.s3.connection import S3Connection # NOQA
from boto.s3.key import Key # NOQA
import boto
import boto.iam
from boto.s3.connection import S3Connection
from boto.s3.key import Key

import vcr # NOQA
import vcr


def test_boto_stubs(tmpdir):
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/test_boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

boto3 = pytest.importorskip("boto3")

import boto3 # NOQA
import botocore # NOQA
import boto3
import botocore

import vcr # NOQA
import vcr

try:
from botocore import awsrequest # NOQA
from botocore import awsrequest

botocore_awsrequest = True
except ImportError:
Expand Down
12 changes: 5 additions & 7 deletions tests/integration/test_httpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
asyncio = pytest.importorskip("asyncio")
httpx = pytest.importorskip("httpx")

import vcr # noqa: E402
from vcr.stubs.httpx_stubs import HTTPX_REDIRECT_PARAM # noqa: E402
import vcr
from vcr.stubs.httpx_stubs import HTTPX_REDIRECT_PARAM


class BaseDoRequest:
Expand Down Expand Up @@ -185,9 +185,7 @@ def test_redirect(mockbin, yml, do_request):
# looking request_info.
assert cassette_response.request.url == response.request.url
assert cassette_response.request.method == response.request.method
assert {k: v for k, v in cassette_response.request.headers.items()} == {
k: v for k, v in response.request.headers.items()
}
assert dict(cassette_response.request.headers.items()) == dict(response.request.headers.items())


@pytest.mark.online
Expand Down Expand Up @@ -242,10 +240,10 @@ def test_behind_proxy(do_request):
@pytest.mark.online
def test_cookies(tmpdir, mockbin, do_request):
def client_cookies(client):
return [c for c in client.client.cookies]
return list(client.client.cookies)

def response_cookies(response):
return [c for c in response.cookies]
return list(response.cookies)

url = mockbin + "/bin/26148652-fe25-4f21-aaf5-689b5b4bf65f"
headers = {"cookie": "k1=v1;k2=v2"}
Expand Down
7 changes: 6 additions & 1 deletion tests/integration/test_matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ def test_method_matcher(cassette, httpbin, httpbin_secure):


@pytest.mark.parametrize(
"uri", [DEFAULT_URI, "http://httpbin.org/get?p2=q2&p1=q1", "http://httpbin.org/get?p2=q2&p1=q1"]
"uri",
(
DEFAULT_URI,
"http://httpbin.org/get?p2=q2&p1=q1",
"http://httpbin.org/get?p2=q2&p1=q1",
),
)
def test_default_matcher_matches(cassette, uri, httpbin, httpbin_secure):
uri = _replace_httpbin(uri, httpbin, httpbin_secure)
Expand Down
5 changes: 3 additions & 2 deletions tests/integration/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import vcr

requests = pytest.importorskip("requests")
from requests.exceptions import ConnectionError # noqa E402
from requests.exceptions import ConnectionError # E402


def test_status_code(httpbin_both, tmpdir):
Expand Down Expand Up @@ -171,7 +171,8 @@ def test_gzip__decode_compressed_response_true(tmpdir, httpbin_both):
assert expected_response.headers["content-encoding"] == "gzip" # self-test

with vcr.use_cassette(
str(tmpdir.join("decode_compressed.yaml")), decode_compressed_response=True
str(tmpdir.join("decode_compressed.yaml")),
decode_compressed_response=True,
) as cassette:
r = requests.get(url)
assert r.headers["content-encoding"] == "gzip" # i.e. not removed
Expand Down
5 changes: 4 additions & 1 deletion tests/integration/test_tornado.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ def callback(chunk):

with vcr.use_cassette(str(tmpdir.join("invalid.yaml"))):
response = yield get(
get_client(), "http://httpbin.org", streaming_callback=callback, raise_error=False
get_client(),
"http://httpbin.org",
streaming_callback=callback,
raise_error=False,
)

assert "not yet supported by VCR" in str(response.error)
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/test_urllib3.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
@pytest.fixture(scope="module")
def verify_pool_mgr():
return urllib3.PoolManager(
cert_reqs="CERT_REQUIRED", ca_certs=pytest_httpbin.certs.where() # Force certificate check.
cert_reqs="CERT_REQUIRED",
ca_certs=pytest_httpbin.certs.where(), # Force certificate check.
)


Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_wild.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

requests = pytest.importorskip("requests")

import vcr # NOQA
import vcr


def test_domain_redirect():
Expand Down
13 changes: 8 additions & 5 deletions tests/unit/test_cassettes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ def test_cassette_load(tmpdir):
yaml.dump(
{
"interactions": [
{"request": {"body": "", "uri": "foo", "method": "GET", "headers": {}}, "response": "bar"}
]
}
)
{
"request": {"body": "", "uri": "foo", "method": "GET", "headers": {}},
"response": "bar",
},
],
},
),
)
a_cassette = Cassette.load(path=str(a_file))
assert len(a_cassette) == 1
Expand Down Expand Up @@ -218,7 +221,7 @@ def test_nesting_cassette_context_managers(*args):
with contextlib.ExitStack() as exit_stack:
first_cassette = exit_stack.enter_context(Cassette.use(path="test"))
exit_stack.enter_context(
mock.patch.object(first_cassette, "play_response", return_value=first_response)
mock.patch.object(first_cassette, "play_response", return_value=first_response),
)
assert_get_response_body_is("first_response")

Expand Down
10 changes: 6 additions & 4 deletions tests/unit/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,17 @@
],
)
def test_CannotOverwriteExistingCassetteException_get_message(
mock_find_requests_with_most_matches, most_matches, expected_message
mock_find_requests_with_most_matches,
most_matches,
expected_message,
):
mock_find_requests_with_most_matches.return_value = most_matches
cassette = Cassette("path")
failed_request = "request"
exception_message = errors.CannotOverwriteExistingCassetteException._get_message(cassette, "request")
expected = (
"Can't overwrite existing cassette (%r) in your current record mode (%r).\n"
"No match for the request (%r) was found.\n"
"%s" % (cassette._path, cassette.record_mode, failed_request, expected_message)
"Can't overwrite existing cassette ({!r}) in your current record mode ({!r}).\n"
"No match for the request ({!r}) was found.\n"
"{}".format(cassette._path, cassette.record_mode, failed_request, expected_message)
)
assert exception_message == expected
55 changes: 44 additions & 11 deletions tests/unit/test_matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,16 @@ def test_uri_matcher():
),
(
request.Request(
"POST", "http://host.com/", "a=1&b=2", {"Content-Type": "application/x-www-form-urlencoded"}
"POST",
"http://host.com/",
"a=1&b=2",
{"Content-Type": "application/x-www-form-urlencoded"},
),
request.Request(
"POST", "http://host.com/", "b=2&a=1", {"Content-Type": "application/x-www-form-urlencoded"}
"POST",
"http://host.com/",
"b=2&a=1",
{"Content-Type": "application/x-www-form-urlencoded"},
),
),
(
Expand All @@ -86,23 +92,38 @@ def test_uri_matcher():
),
(
request.Request(
"POST", "http://host.com/", "a=1&b=2", {"Content-Type": "application/x-www-form-urlencoded"}
"POST",
"http://host.com/",
"a=1&b=2",
{"Content-Type": "application/x-www-form-urlencoded"},
),
request.Request(
"POST", "http://host.com/", "b=2&a=1", {"Content-Type": "application/x-www-form-urlencoded"}
"POST",
"http://host.com/",
"b=2&a=1",
{"Content-Type": "application/x-www-form-urlencoded"},
),
),
(
request.Request(
"POST", "http://host.com/", '{"a": 1, "b": 2}', {"Content-Type": "application/json"}
"POST",
"http://host.com/",
'{"a": 1, "b": 2}',
{"Content-Type": "application/json"},
),
request.Request(
"POST", "http://host.com/", '{"b": 2, "a": 1}', {"content-type": "application/json"}
"POST",
"http://host.com/",
'{"b": 2, "a": 1}',
{"content-type": "application/json"},
),
),
(
request.Request(
"POST", "http://host.com/", req1_body, {"User-Agent": "xmlrpclib", "Content-Type": "text/xml"}
"POST",
"http://host.com/",
req1_body,
{"User-Agent": "xmlrpclib", "Content-Type": "text/xml"},
),
request.Request(
"POST",
Expand All @@ -113,10 +134,16 @@ def test_uri_matcher():
),
(
request.Request(
"POST", "http://host.com/", '{"a": 1, "b": 2}', {"Content-Type": "application/json"}
"POST",
"http://host.com/",
'{"a": 1, "b": 2}',
{"Content-Type": "application/json"},
),
request.Request(
"POST", "http://host.com/", '{"b": 2, "a": 1}', {"content-type": "application/json"}
"POST",
"http://host.com/",
'{"b": 2, "a": 1}',
{"content-type": "application/json"},
),
),
(
Expand All @@ -139,10 +166,16 @@ def test_body_matcher_does_match(r1, r2):
),
(
request.Request(
"POST", "http://host.com/", '{"a": 1, "b": 3}', {"Content-Type": "application/json"}
"POST",
"http://host.com/",
'{"a": 1, "b": 3}',
{"Content-Type": "application/json"},
),
request.Request(
"POST", "http://host.com/", '{"b": 2, "a": 1}', {"content-type": "application/json"}
"POST",
"http://host.com/",
'{"b": 2, "a": 1}',
{"content-type": "application/json"},
),
),
(
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ def test_response_parses_correctly_and_fp_attribute_error_is_not_thrown():
b"different types of cancer cells. Recently, the first HDACi was\n "
b"approved for the "
b"treatment of cutaneous T cell lymphomas. Most HDACi currently in\n "
b"clinical "
b"clinical ",
},
}
vcr_response = VCRHTTPResponse(recorded_response)
handle = io.TextIOWrapper(vcr_response, encoding="utf-8")
handle = iter(handle)
articles = [line for line in handle]
articles = list(handle)
assert len(articles) > 1
Loading

0 comments on commit d6c0017

Please sign in to comment.