From 98a1ed9104f5d4ce8117b2fad0e41d8c882543c0 Mon Sep 17 00:00:00 2001 From: Giorgio Salluzzo Date: Sun, 12 May 2024 23:05:20 +0200 Subject: [PATCH] Switching to using `ruff` for linting (#232) --- .github/workflows/main.yml | 5 -- .gitignore | 1 - .pre-commit-config.yaml | 2 +- Makefile | 11 ++- mocket/compat.py | 2 +- mocket/mocket.py | 94 ++++++++++----------- mocket/mockhttp.py | 32 ++----- mocket/mockredis.py | 16 ++-- mocket/plugins/httpretty/__init__.py | 6 +- mocket/plugins/pook_mock_engine.py | 2 +- mocket/utils.py | 5 +- pyproject.toml | 25 ++++++ scripts/patch_hosts.sh | 5 ++ tests/main/test_http.py | 121 ++++++++++++++------------- tests/main/test_http_with_xxhash.py | 6 +- tests/main/test_httpretty.py | 3 - tests/main/test_https.py | 26 +++--- tests/main/test_mocket.py | 24 +++--- tests/main/test_mode.py | 5 +- tests/main/test_redis.py | 24 ++---- tests/tests37/test_asyncio.py | 3 +- tests/tests38/test_http_aiohttp.py | 29 ++++--- 22 files changed, 217 insertions(+), 230 deletions(-) create mode 100644 scripts/patch_hosts.sh diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 82fec09..c23dd8c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -42,11 +42,6 @@ jobs: run: | make develop make services-up - - name: Setup hostname - run: | - export CONTAINER_ID=$(docker compose ps -q proxy) - export CONTAINER_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $CONTAINER_ID) - echo "$CONTAINER_IP httpbin.local" | sudo tee -a /etc/hosts - name: Test run: | make test diff --git a/.gitignore b/.gitignore index 7cfa1dd..564b8ce 100644 --- a/.gitignore +++ b/.gitignore @@ -20,7 +20,6 @@ Documentation/ .tox/ .ropeproject/ .idea/ -*.sh doc/build .buildinfo .coverage diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d8b01c1..eae15d1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,7 +15,7 @@ repos: exclude: helm/ args: [ --unsafe ] - repo: https://github.com/charliermarsh/ruff-pre-commit - rev: "v0.4.3" + rev: "v0.4.4" hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix] diff --git a/Makefile b/Makefile index edcdec8..6b30484 100644 --- a/Makefile +++ b/Makefile @@ -7,9 +7,14 @@ install-dev-requirements: install-test-requirements: uv pip install --editable .[test] -services-up: +prepare-hosts: _services-up + @bash scripts/patch_hosts.sh + +_services-up: docker compose up -d +services-up: _services-up prepare-hosts + services-down: docker compose down --remove-orphans @@ -39,5 +44,5 @@ clean: rm -rf *.egg-info dist/ requirements.txt Pipfile.lock find . -type d -name __pycache__ -exec rm -rf {} \; -.PHONY: clean publish safetest test setup develop lint-python test-python -.PHONY: services-up services-down install-test-requirements install-dev-requirements +.PHONY: clean publish safetest test setup develop lint-python test-python _services-up +.PHONY: prepare-hosts services-up services-down install-test-requirements install-dev-requirements diff --git a/mocket/compat.py b/mocket/compat.py index 4651b8e..8457c27 100644 --- a/mocket/compat.py +++ b/mocket/compat.py @@ -3,7 +3,7 @@ import codecs import os import shlex -from typing import Any, Final +from typing import Final ENCODING: Final[str] = os.getenv("MOCKET_ENCODING", "utf-8") diff --git a/mocket/mocket.py b/mocket/mocket.py index 94581f1..e328f15 100644 --- a/mocket/mocket.py +++ b/mocket/mocket.py @@ -1,8 +1,8 @@ import collections import collections.abc as collections_abc +import contextlib import errno import hashlib -import io import itertools import json import os @@ -21,6 +21,7 @@ except ImportError: urllib3_wrap_socket = None + from .compat import basestring, byte_type, decode_from_bytes, encode_to_bytes, text_type from .utils import ( SSL_PROTOCOL, @@ -35,10 +36,8 @@ try: from xxhash import xxh32 except ImportError: # pragma: no cover - try: + with contextlib.suppress(ImportError): from xxhash_cffi import xxh32 - except ImportError: - pass hasher = xxh32 or hashlib.md5 try: # pragma: no cover @@ -192,9 +191,7 @@ def __init__( self.kwargs = kwargs def __str__(self): - return "({})(family={} type={} protocol={})".format( - self.__class__.__name__, self.family, self.type, self.proto - ) + return f"({self.__class__.__name__})(family={self.family} type={self.type} protocol={self.proto})" def __enter__(self): return self @@ -250,14 +247,14 @@ def getpeercert(self, *args, **kwargs): return { "notAfter": shift.strftime("%b %d %H:%M:%S GMT"), "subjectAltName": ( - ("DNS", "*.%s" % self._host), + ("DNS", f"*.{self._host}"), ("DNS", self._host), ("DNS", "*"), ), "subject": ( - (("organizationName", "*.%s" % self._host),), + (("organizationName", f"*.{self._host}"),), (("organizationalUnitName", "Domain Control Validated"),), - (("commonName", "*.%s" % self._host),), + (("commonName", f"*.{self._host}"),), ), } @@ -292,10 +289,7 @@ def sendall(self, data, entry=None, *args, **kwargs): if entry: consume_response = entry.collect(data) - if consume_response is not False: - response = entry.get_response() - else: - response = None + response = entry.get_response() if consume_response is not False else None else: response = self.true_sendall(data, *args, **kwargs) @@ -353,7 +347,7 @@ def true_sendall(self, data, *args, **kwargs): ) # check if there's already a recorded session dumped to a JSON file try: - with io.open(path) as f: + with open(path) as f: responses = json.load(f) # if not, create a new dictionary except (FileNotFoundError, JSONDecodeError): @@ -390,11 +384,9 @@ def true_sendall(self, data, *args, **kwargs): **self.kwargs, ) - try: - self.true_socket.connect((host, port)) - except (OSError, socket.error, ValueError): + with contextlib.suppress(OSError, ValueError): # already connected - pass + self.true_socket.connect((host, port)) self.true_socket.sendall(data, *args, **kwargs) encoded_response = b"" # https://github.com/kennethreitz/requests/blob/master/tests/testserver/server.py#L13 @@ -416,7 +408,7 @@ def true_sendall(self, data, *args, **kwargs): response_dict["request"] = req response_dict["response"] = hexdump(encoded_response) - with io.open(path, mode="w") as f: + with open(path, mode="w") as f: f.write( decode_from_bytes( json.dumps(responses, indent=4, sort_keys=True) @@ -428,8 +420,9 @@ def true_sendall(self, data, *args, **kwargs): def send(self, data, *args, **kwargs): # pragma: no cover entry = self.get_entry(data) + kwargs["entry"] = entry if not entry or (entry and self._entry != entry): - self.sendall(data, entry=entry, *args, **kwargs) + self.sendall(data, *args, **kwargs) else: req = Mocket.last_request() if hasattr(req, "add_data"): @@ -512,41 +505,40 @@ def enable(namespace=None, truesocket_recording_dir=None): Mocket._namespace = namespace Mocket._truesocket_recording_dir = truesocket_recording_dir - if truesocket_recording_dir: + if truesocket_recording_dir and not os.path.isdir(truesocket_recording_dir): # JSON dumps will be saved here - if not os.path.isdir(truesocket_recording_dir): - raise AssertionError + raise AssertionError socket.socket = socket.__dict__["socket"] = MocketSocket socket._socketobject = socket.__dict__["_socketobject"] = MocketSocket socket.SocketType = socket.__dict__["SocketType"] = MocketSocket - socket.create_connection = socket.__dict__[ - "create_connection" - ] = create_connection + socket.create_connection = socket.__dict__["create_connection"] = ( + create_connection + ) socket.gethostname = socket.__dict__["gethostname"] = lambda: "localhost" - socket.gethostbyname = socket.__dict__[ - "gethostbyname" - ] = lambda host: "127.0.0.1" - socket.getaddrinfo = socket.__dict__[ - "getaddrinfo" - ] = lambda host, port, family=None, socktype=None, proto=None, flags=None: [ - (2, 1, 6, "", (host, port)) - ] + socket.gethostbyname = socket.__dict__["gethostbyname"] = ( + lambda host: "127.0.0.1" + ) + socket.getaddrinfo = socket.__dict__["getaddrinfo"] = ( + lambda host, port, family=None, socktype=None, proto=None, flags=None: [ + (2, 1, 6, "", (host, port)) + ] + ) socket.socketpair = socket.__dict__["socketpair"] = socketpair ssl.wrap_socket = ssl.__dict__["wrap_socket"] = FakeSSLContext.wrap_socket ssl.SSLContext = ssl.__dict__["SSLContext"] = FakeSSLContext socket.inet_pton = socket.__dict__["inet_pton"] = lambda family, ip: byte_type( "\x7f\x00\x00\x01", "utf-8" ) - urllib3.util.ssl_.wrap_socket = urllib3.util.ssl_.__dict__[ - "wrap_socket" - ] = FakeSSLContext.wrap_socket + urllib3.util.ssl_.wrap_socket = urllib3.util.ssl_.__dict__["wrap_socket"] = ( + FakeSSLContext.wrap_socket + ) urllib3.util.ssl_.ssl_wrap_socket = urllib3.util.ssl_.__dict__[ "ssl_wrap_socket" ] = FakeSSLContext.wrap_socket - urllib3.util.ssl_wrap_socket = urllib3.util.__dict__[ - "ssl_wrap_socket" - ] = FakeSSLContext.wrap_socket + urllib3.util.ssl_wrap_socket = urllib3.util.__dict__["ssl_wrap_socket"] = ( + FakeSSLContext.wrap_socket + ) urllib3.connection.ssl_wrap_socket = urllib3.connection.__dict__[ "ssl_wrap_socket" ] = FakeSSLContext.wrap_socket @@ -564,9 +556,9 @@ def disable(): socket.socket = socket.__dict__["socket"] = true_socket socket._socketobject = socket.__dict__["_socketobject"] = true_socket socket.SocketType = socket.__dict__["SocketType"] = true_socket - socket.create_connection = socket.__dict__[ - "create_connection" - ] = true_create_connection + socket.create_connection = socket.__dict__["create_connection"] = ( + true_create_connection + ) socket.gethostname = socket.__dict__["gethostname"] = true_gethostname socket.gethostbyname = socket.__dict__["gethostbyname"] = true_gethostbyname socket.getaddrinfo = socket.__dict__["getaddrinfo"] = true_getaddrinfo @@ -575,15 +567,15 @@ def disable(): ssl.wrap_socket = ssl.__dict__["wrap_socket"] = true_ssl_wrap_socket ssl.SSLContext = ssl.__dict__["SSLContext"] = true_ssl_context socket.inet_pton = socket.__dict__["inet_pton"] = true_inet_pton - urllib3.util.ssl_.wrap_socket = urllib3.util.ssl_.__dict__[ - "wrap_socket" - ] = true_urllib3_wrap_socket + urllib3.util.ssl_.wrap_socket = urllib3.util.ssl_.__dict__["wrap_socket"] = ( + true_urllib3_wrap_socket + ) urllib3.util.ssl_.ssl_wrap_socket = urllib3.util.ssl_.__dict__[ "ssl_wrap_socket" ] = true_urllib3_ssl_wrap_socket - urllib3.util.ssl_wrap_socket = urllib3.util.__dict__[ - "ssl_wrap_socket" - ] = true_urllib3_ssl_wrap_socket + urllib3.util.ssl_wrap_socket = urllib3.util.__dict__["ssl_wrap_socket"] = ( + true_urllib3_ssl_wrap_socket + ) urllib3.connection.ssl_wrap_socket = urllib3.connection.__dict__[ "ssl_wrap_socket" ] = true_urllib3_ssl_wrap_socket @@ -645,7 +637,7 @@ def __init__(self, location, responses): self.responses.append(r) def __repr__(self): - return "{}(location={})".format(self.__class__.__name__, self.location) + return f"{self.__class__.__name__}(location={self.location})" @staticmethod def can_handle(data): diff --git a/mocket/mockhttp.py b/mocket/mockhttp.py index 4ab3345..1a05b7f 100644 --- a/mocket/mockhttp.py +++ b/mocket/mockhttp.py @@ -76,7 +76,7 @@ def body(self): return self._protocol.body def __str__(self): - return "{} - {} - {}".format(self.method, self.path, self.headers) + return f"{self.method} - {self.path} - {self.headers}" class Response: @@ -104,16 +104,14 @@ def __init__(self, body="", status=200, headers=None, lib_magic=magic): self.data = self.get_protocol_data() + self.body def get_protocol_data(self, str_format_fun_name="capitalize"): - status_line = "HTTP/1.1 {status_code} {status}".format( - status_code=self.status, status=STATUS[self.status] - ) + status_line = f"HTTP/1.1 {self.status} {STATUS[self.status]}" header_lines = CRLF.join( ( - "{0}: {1}".format(getattr(k, str_format_fun_name)(), v) + f"{getattr(k, str_format_fun_name)()}: {v}" for k, v in self.headers.items() ) ) - return "{0}\r\n{1}\r\n\r\n".format(status_line, header_lines).encode(ENCODING) + return f"{status_line}\r\n{header_lines}\r\n\r\n".encode(ENCODING) def set_base_headers(self): self.headers = { @@ -140,7 +138,7 @@ def set_extra_headers(self, headers): True """ for k, v in headers.items(): - self.headers["-".join((token.capitalize() for token in k.split("-")))] = v + self.headers["-".join(token.capitalize() for token in k.split("-"))] = v class Entry(MocketEntry): @@ -164,12 +162,9 @@ def __init__(self, uri, method, responses, match_querystring=True): port = uri.port if not port: - if uri.scheme == "https": - port = 443 - else: - port = 80 + port = 443 if uri.scheme == "https" else 80 - super(Entry, self).__init__((uri.hostname, port), responses) + super().__init__((uri.hostname, port), responses) self.schema = uri.scheme self.path = uri.path self.query = uri.query @@ -178,16 +173,7 @@ def __init__(self, uri, method, responses, match_querystring=True): self._match_querystring = match_querystring def __repr__(self): - return ( - "{}(method={!r}, schema={!r}, location={!r}, path={!r}, query={!r})".format( - self.__class__.__name__, - self.method, - self.schema, - self.location, - self.path, - self.query, - ) - ) + return f"{self.__class__.__name__}(method={self.method!r}, schema={self.schema!r}, location={self.location!r}, path={self.path!r}, query={self.query!r})" def collect(self, data): consume_response = True @@ -200,7 +186,7 @@ def collect(self, data): else: self._sent_data = data - super(Entry, self).collect(self._sent_data) + super().collect(self._sent_data) return consume_response diff --git a/mocket/mockredis.py b/mocket/mockredis.py index 15bba6b..1a0c51e 100644 --- a/mocket/mockredis.py +++ b/mocket/mockredis.py @@ -18,12 +18,8 @@ class Redisizer(byte_type): @staticmethod def tokens(iterable): iterable = [encode_to_bytes(x) for x in iterable] - return ["*{0}".format(len(iterable)).encode("utf-8")] + list( - chain( - *zip( - ["${0}".format(len(x)).encode("utf-8") for x in iterable], iterable - ) - ) + return [f"*{len(iterable)}".encode()] + list( + chain(*zip([f"${len(x)}".encode() for x in iterable], iterable)) ) @staticmethod @@ -33,8 +29,8 @@ def get_conversion(t): dict: lambda x: b"\r\n".join( Redisizer.tokens(list(chain(*tuple(x.items())))) ), - int: lambda x: ":{0}".format(x).encode("utf-8"), - text_type: lambda x: "${0}\r\n{1}".format( + int: lambda x: f":{x}".encode(), + text_type: lambda x: "${}\r\n{}".format( len(x.encode("utf-8")), x ).encode("utf-8"), list: lambda x: b"\r\n".join(Redisizer.tokens(x)), @@ -48,7 +44,7 @@ def get_conversion(t): @staticmethod def command(description, _type="+"): - return Redisizer("{0}{1}{2}".format(_type, description, "\r\n").encode("utf-8")) + return Redisizer("{}{}{}".format(_type, description, "\r\n").encode("utf-8")) @staticmethod def error(description): @@ -65,7 +61,7 @@ class Entry(MocketEntry): response_cls = Response def __init__(self, addr, command, responses): - super(Entry, self).__init__(addr or ("localhost", 6379), responses) + super().__init__(addr or ("localhost", 6379), responses) d = shsplit(command) d[0] = d[0].upper() self.command = Redisizer.tokens(d) diff --git a/mocket/plugins/httpretty/__init__.py b/mocket/plugins/httpretty/__init__.py index bf1e7e2..9d61ae2 100644 --- a/mocket/plugins/httpretty/__init__.py +++ b/mocket/plugins/httpretty/__init__.py @@ -24,12 +24,10 @@ class Response(MocketHttpResponse): def get_protocol_data(self, str_format_fun_name="lower"): if "server" in self.headers and self.headers["server"] == "Python/Mocket": self.headers["server"] = "Python/HTTPretty" - return super(Response, self).get_protocol_data( - str_format_fun_name=str_format_fun_name - ) + return super().get_protocol_data(str_format_fun_name=str_format_fun_name) def set_base_headers(self): - super(Response, self).set_base_headers() + super().set_base_headers() self.headers = httprettifier_headers(self.headers) original_set_base_headers = set_base_headers diff --git a/mocket/plugins/pook_mock_engine.py b/mocket/plugins/pook_mock_engine.py index 3dff211..99cb07e 100644 --- a/mocket/plugins/pook_mock_engine.py +++ b/mocket/plugins/pook_mock_engine.py @@ -10,7 +10,7 @@ class MocketPookEntry(Entry): pook_engine = None def can_handle(self, data): - can_handle = super(MocketPookEntry, self).can_handle(data) + can_handle = super().can_handle(data) if can_handle: self.pook_engine.match(self.pook_request) diff --git a/mocket/utils.py b/mocket/utils.py index 7d4bf58..4858604 100644 --- a/mocket/utils.py +++ b/mocket/utils.py @@ -10,8 +10,9 @@ from .exceptions import StrictMocketException if TYPE_CHECKING: + from typing import NoReturn + from _typeshed import ReadableBuffer - from typing_extensions import NoReturn SSL_PROTOCOL = ssl.PROTOCOL_TLSv1_2 @@ -21,7 +22,7 @@ def write( # type: ignore[override] # BytesIO returns int self, content: ReadableBuffer, ) -> None: - super(MocketSocketCore, self).write(content) + super().write(content) from mocket import Mocket diff --git a/pyproject.toml b/pyproject.toml index 2e48ec1..1ffe831 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -84,6 +84,31 @@ exclude = [ ".*", ] +[tool.ruff] +src = ["mocket", "tests"] + +[tool.ruff.lint] +extend-ignore = ["E731", "E501"] +select = [ + # pycodestyle + "E", + # Pyflakes + "F", + # pyupgrade + "UP", + # flake8-bugbear + "B", + # flake8-simplify + "SIM", + # isort + "I", +] + +[tool.ruff.lint.mccabe] +# Unlike Flake8, default to a complexity level of 10. +# https://en.wikipedia.org/wiki/Cyclomatic_complexity#Limiting_complexity_during_development +max-complexity = 8 + [tool.mypy] python_version = "3.8" files = [ diff --git a/scripts/patch_hosts.sh b/scripts/patch_hosts.sh new file mode 100644 index 0000000..b329108 --- /dev/null +++ b/scripts/patch_hosts.sh @@ -0,0 +1,5 @@ +sudo cp /etc/hosts /etc/hosts.mocket +export CONTAINER_ID=$(docker compose ps -q proxy) +export CONTAINER_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $CONTAINER_ID) +echo "$CONTAINER_IP httpbin.local" | sudo tee -a /etc/hosts.mocket +sudo mv /etc/hosts.mocket /etc/hosts diff --git a/tests/main/test_http.py b/tests/main/test_http.py index bde954d..f8b6491 100644 --- a/tests/main/test_http.py +++ b/tests/main/test_http.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- -import io import json import os import socket @@ -35,60 +33,63 @@ def test_truesendall(self): self.assertEqual(resp.status_code, 200) def test_truesendall_with_recording(self): - with tempfile.TemporaryDirectory() as temp_dir: - with Mocketizer(truesocket_recording_dir=temp_dir): - url = "http://httpbin.local/ip" - - urlopen(url) - requests.get(url) - resp = urlopen(url) - self.assertEqual(resp.code, 200) - resp = requests.get(url) - self.assertEqual(resp.status_code, 200) - assert "origin" in resp.json() - - dump_filename = os.path.join( - Mocket.get_truesocket_recording_dir(), - Mocket.get_namespace() + ".json", - ) - with io.open(dump_filename) as f: - responses = json.load(f) + with tempfile.TemporaryDirectory() as temp_dir, Mocketizer( + truesocket_recording_dir=temp_dir + ): + url = "http://httpbin.local/ip" + + urlopen(url) + requests.get(url) + resp = urlopen(url) + self.assertEqual(resp.code, 200) + resp = requests.get(url) + self.assertEqual(resp.status_code, 200) + assert "origin" in resp.json() + + dump_filename = os.path.join( + Mocket.get_truesocket_recording_dir(), + Mocket.get_namespace() + ".json", + ) + with open(dump_filename) as f: + responses = json.load(f) self.assertEqual(len(responses["httpbin.local"]["80"].keys()), 2) def test_truesendall_with_gzip_recording(self): - with tempfile.TemporaryDirectory() as temp_dir: - with Mocketizer(truesocket_recording_dir=temp_dir): - url = "http://httpbin.local/gzip" + with tempfile.TemporaryDirectory() as temp_dir, Mocketizer( + truesocket_recording_dir=temp_dir + ): + url = "http://httpbin.local/gzip" - requests.get(url) - resp = requests.get(url) - self.assertEqual(resp.status_code, 200) + requests.get(url) + resp = requests.get(url) + self.assertEqual(resp.status_code, 200) - dump_filename = os.path.join( - Mocket.get_truesocket_recording_dir(), - Mocket.get_namespace() + ".json", - ) - with io.open(dump_filename) as f: - responses = json.load(f) + dump_filename = os.path.join( + Mocket.get_truesocket_recording_dir(), + Mocket.get_namespace() + ".json", + ) + with open(dump_filename) as f: + responses = json.load(f) assert len(responses["httpbin.local"]["80"].keys()) == 1 def test_truesendall_with_chunk_recording(self): - with tempfile.TemporaryDirectory() as temp_dir: - with Mocketizer(truesocket_recording_dir=temp_dir): - url = "http://httpbin.local/range/70000?chunk_size=65536" + with tempfile.TemporaryDirectory() as temp_dir, Mocketizer( + truesocket_recording_dir=temp_dir + ): + url = "http://httpbin.local/range/70000?chunk_size=65536" - requests.get(url) - resp = requests.get(url) - self.assertEqual(resp.status_code, 200) + requests.get(url) + resp = requests.get(url) + self.assertEqual(resp.status_code, 200) - dump_filename = os.path.join( - Mocket.get_truesocket_recording_dir(), - Mocket.get_namespace() + ".json", - ) - with io.open(dump_filename) as f: - responses = json.load(f) + dump_filename = os.path.join( + Mocket.get_truesocket_recording_dir(), + Mocket.get_namespace() + ".json", + ) + with open(dump_filename) as f: + responses = json.load(f) assert len(responses["httpbin.local"]["80"].keys()) == 1 @@ -246,12 +247,12 @@ def test_multipart(self): def test_file_object(self): url = "http://github.com/fluidicon.png" filename = "tests/fluidicon.png" - file_obj = open(filename, "rb") - Entry.single_register(Entry.GET, url, body=file_obj) + with open(filename, "rb") as file_obj: + Entry.single_register(Entry.GET, url, body=file_obj) r = requests.get(url) remote_content = r.content - local_file_obj = open(filename, "rb") - local_content = local_file_obj.read() + with open(filename, "rb") as local_file_obj: + local_content = local_file_obj.read() self.assertEqual(remote_content, local_content) self.assertEqual(len(remote_content), len(local_content)) self.assertEqual(int(r.headers["Content-Length"]), len(local_content)) @@ -261,12 +262,12 @@ def test_file_object(self): def test_file_object_with_no_lib_magic(self): url = "http://github.com/fluidicon.png" filename = "tests/fluidicon.png" - file_obj = open(filename, "rb") - Entry.register(Entry.GET, url, Response(body=file_obj, lib_magic=None)) + with open(filename, "rb") as file_obj: + Entry.register(Entry.GET, url, Response(body=file_obj, lib_magic=None)) r = requests.get(url) remote_content = r.content - local_file_obj = open(filename, "rb") - local_content = local_file_obj.read() + with open(filename, "rb") as local_file_obj: + local_content = local_file_obj.read() self.assertEqual(remote_content, local_content) self.assertEqual(len(remote_content), len(local_content)) self.assertEqual(int(r.headers["Content-Length"]), len(local_content)) @@ -299,7 +300,7 @@ def test_request_bodies(self): for e in range(5): u = url.format(e) Entry.single_register(Entry.POST, u, body=str(e)) - request_body = urlencode({"key-{0}".format(e): "value={0}".format(e)}) + request_body = urlencode({f"key-{e}": f"value={e}"}) urlopen(u, request_body.encode("utf-8")) last_request = Mocket.last_request() assert last_request.body == request_body @@ -316,7 +317,7 @@ def test_truesendall_with_dump_from_recording(self): dump_filename = os.path.join( Mocket.get_truesocket_recording_dir(), Mocket.get_namespace() + ".json" ) - with io.open(dump_filename) as f: + with open(dump_filename) as f: responses = json.load(f) self.assertEqual(len(responses["httpbin.local"]["80"].keys()), 2) @@ -325,15 +326,15 @@ def test_truesendall_with_dump_from_recording(self): def test_post_file_object(self): url = "http://github.com/fluidicon.png" Entry.single_register(Entry.POST, url, status=201) - file_obj = open("tests/fluidicon.png", "rb") - files = {"content": file_obj} - r = requests.post(url, files=files, data={}) + with open("tests/fluidicon.png", "rb") as file_obj: + files = {"content": file_obj} + r = requests.post(url, files=files, data={}) self.assertEqual(r.status_code, 201) @mocketize def test_raise_exception(self): url = "http://github.com/fluidicon.png" - Entry.single_register(Entry.GET, url, exception=socket.error()) + Entry.single_register(Entry.GET, url, exception=OSError()) with self.assertRaises(requests.exceptions.ConnectionError): requests.get(url) @@ -359,8 +360,8 @@ def test_sockets(self): sock = socket.socket(address[0], address[1], address[2]) sock.connect(address[-1]) - sock.write("%s %s HTTP/1.0\r\n" % (method, path)) - sock.write("Host: %s\r\n" % host) + sock.write(f"{method} {path} HTTP/1.0\r\n") + sock.write(f"Host: {host}\r\n") sock.write("Content-Type: application/json\r\n") sock.write("Content-Length: %d\r\n" % len(data)) sock.write("Connection: close\r\n\r\n") diff --git a/tests/main/test_http_with_xxhash.py b/tests/main/test_http_with_xxhash.py index 4c29505..4600bf3 100644 --- a/tests/main/test_http_with_xxhash.py +++ b/tests/main/test_http_with_xxhash.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - -import io import json import os @@ -25,7 +21,7 @@ def test_truesendall_with_dump_from_recording(self): Mocket.get_truesocket_recording_dir(), Mocket.get_namespace() + ".json", ) - with io.open(dump_filename) as f: + with open(dump_filename) as f: responses = json.load(f) self.assertEqual(len(responses["httpbin.local"]["80"].keys()), 2) diff --git a/tests/main/test_httpretty.py b/tests/main/test_httpretty.py index 474a0dd..2b00a15 100644 --- a/tests/main/test_httpretty.py +++ b/tests/main/test_httpretty.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - # # Copyright (C) <2011-2015> Gabriel Falcão # @@ -24,7 +22,6 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE. -from __future__ import unicode_literals import requests from sure import expect diff --git a/tests/main/test_https.py b/tests/main/test_https.py index f00fe6a..44c6605 100644 --- a/tests/main/test_https.py +++ b/tests/main/test_https.py @@ -1,4 +1,3 @@ -import io import json import os import tempfile @@ -45,18 +44,19 @@ def test_json(response): @pytest.mark.skipif('os.getenv("SKIP_TRUE_HTTP", False)') def test_truesendall_with_recording_https(url_to_mock): - with tempfile.TemporaryDirectory() as temp_dir: - with Mocketizer(truesocket_recording_dir=temp_dir): - requests.get(url_to_mock, headers={"Accept": "application/json"}) - resp = requests.get(url_to_mock, headers={"Accept": "application/json"}) - assert resp.status_code == 200 - - dump_filename = os.path.join( - Mocket.get_truesocket_recording_dir(), - Mocket.get_namespace() + ".json", - ) - with io.open(dump_filename) as f: - responses = json.load(f) + with tempfile.TemporaryDirectory() as temp_dir, Mocketizer( + truesocket_recording_dir=temp_dir + ): + requests.get(url_to_mock, headers={"Accept": "application/json"}) + resp = requests.get(url_to_mock, headers={"Accept": "application/json"}) + assert resp.status_code == 200 + + dump_filename = os.path.join( + Mocket.get_truesocket_recording_dir(), + Mocket.get_namespace() + ".json", + ) + with open(dump_filename) as f: + responses = json.load(f) assert len(responses["httpbin.org"]["443"].keys()) == 1 diff --git a/tests/main/test_mocket.py b/tests/main/test_mocket.py index c6ed535..e6116dd 100644 --- a/tests/main/test_mocket.py +++ b/tests/main/test_mocket.py @@ -1,5 +1,3 @@ -from __future__ import unicode_literals - import io import os import socket @@ -91,7 +89,7 @@ def test_empty_getresponse(self): self.assertEqual(entry.get_response(), encode_to_bytes("")) def test_raise_exception(self): - entry = MocketEntry(("localhost", 8080), [IOError()]) + entry = MocketEntry(("localhost", 8080), [OSError()]) with self.assertRaises(IOError): entry.get_response() @@ -179,13 +177,12 @@ def test_multiple_socket_connections(self): def test_socket_as_context_manager(self): addr = ("localhost", 80) Mocket.register(MocketEntry(addr, ["Show me.\r\n"])) - with Mocketizer(): - with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as _so: - _so.connect(addr) - _so.sendall(encode_to_bytes("Whatever...")) - data = _so.recv(4096) - self.assertEqual(data, encode_to_bytes("Show me.\r\n")) - self.assertEqual(str(_so), "(MocketSocket)(family=2 type=1 protocol=0)") + with Mocketizer(), socket.socket(socket.AF_INET, socket.SOCK_STREAM) as _so: + _so.connect(addr) + _so.sendall(encode_to_bytes("Whatever...")) + data = _so.recv(4096) + self.assertEqual(data, encode_to_bytes("Show me.\r\n")) + self.assertEqual(str(_so), "(MocketSocket)(family=2 type=1 protocol=0)") class MocketizeTestCase(TestCase): @@ -212,7 +209,7 @@ def two(): @mocketize def test_mocketize_with_fixture(two): - assert 2 == two + assert two == 2 @mocketize @@ -233,8 +230,7 @@ async def test_no_dangling_fds(): prev_num_fds = proc.num_fds() - async with Mocketizer(strict_mode=False): - async with httpx.AsyncClient() as client: - await client.get(url) + async with Mocketizer(strict_mode=False), httpx.AsyncClient() as client: + await client.get(url) assert proc.num_fds() == prev_num_fds diff --git a/tests/main/test_mode.py b/tests/main/test_mode.py index 746d0ca..2a76494 100644 --- a/tests/main/test_mode.py +++ b/tests/main/test_mode.py @@ -22,9 +22,8 @@ def test_intermittent_strict_mode(): with Mocketizer(strict_mode=False): requests.get(url) - with Mocketizer(strict_mode=True): - with pytest.raises(StrictMocketException): - requests.get(url) + with Mocketizer(strict_mode=True), pytest.raises(StrictMocketException): + requests.get(url) with Mocketizer(strict_mode=False): requests.get(url) diff --git a/tests/main/test_redis.py b/tests/main/test_redis.py index 507d34d..50b9bea 100644 --- a/tests/main/test_redis.py +++ b/tests/main/test_redis.py @@ -1,8 +1,4 @@ -# -*- coding: utf-8 -*- - -from __future__ import unicode_literals - -import socket +import contextlib from unittest import TestCase import pytest @@ -135,9 +131,9 @@ def test_hm(self): @mocketize def test_lrange(self): - l = [b"one", b"two", b"three"] - self.rclient.rpush("list", *l) - self.assertEqual(self.rclient.lrange("list", 0, -1), l) + list_ = [b"one", b"two", b"three"] + self.rclient.rpush("list", *list_) + self.assertEqual(self.rclient.lrange("list", 0, -1), list_) @mocketize def test_err(self): @@ -146,10 +142,8 @@ def test_err(self): @mocketize def test_shutdown(self): rc = redis.StrictRedis(host="127.1.1.1") - try: + with contextlib.suppress(redis.ConnectionError): rc.get("foo") - except redis.ConnectionError: - pass @mocketize def test_select_db(self): @@ -238,9 +232,9 @@ def test_get_unicode(self): @mocketize def test_lrange(self): - l = [b"one", b"two", b"three"] - Entry.register_response("LRANGE list 0 -1", l) - self.assertEqual(self.rclient.lrange("list", 0, -1), l) + list_ = [b"one", b"two", b"three"] + Entry.register_response("LRANGE list 0 -1", list_) + self.assertEqual(self.rclient.lrange("list", 0, -1), list_) self.assertEqual(len(Mocket.request_list()), 1) self.assertEqual( Mocket.last_request().data, @@ -261,7 +255,7 @@ def test_err(self): @mocketize def test_raise_exception(self): - Entry.register_response("INCRBY counter one", socket.error("Mocket rulez!")) + Entry.register_response("INCRBY counter one", OSError("Mocket rulez!")) self.assertRaises( redis.exceptions.ConnectionError, self.rclient.incr, "counter", "one" ) diff --git a/tests/tests37/test_asyncio.py b/tests/tests37/test_asyncio.py index c90bcb8..a9df4b6 100644 --- a/tests/tests37/test_asyncio.py +++ b/tests/tests37/test_asyncio.py @@ -1,6 +1,5 @@ import asyncio import glob -import io import json import socket import tempfile @@ -34,7 +33,7 @@ async def test_asyncio_connection(): files = glob.glob(f"{temp_dir}/*.json") assert len(files) == 1 - with io.open(files[0]) as f: + with open(files[0]) as f: responses = json.load(f) assert len(responses["google.com"]["80"].keys()) == 1 diff --git a/tests/tests38/test_http_aiohttp.py b/tests/tests38/test_http_aiohttp.py index b2d7249..e5e0f03 100644 --- a/tests/tests38/test_http_aiohttp.py +++ b/tests/tests38/test_http_aiohttp.py @@ -27,9 +27,9 @@ def test_https_session(self): Entry.single_register(Entry.GET, url, body=body, status=404) Entry.single_register(Entry.POST, url, body=body * 2, status=201) - async def main(l): + async def main(loop_): async with aiohttp.ClientSession( - loop=l, timeout=aiohttp.ClientTimeout(total=3) + loop=loop_, timeout=aiohttp.ClientTimeout(total=3) ) as session: async with session.get(url) as get_response: assert get_response.status == 404 @@ -77,10 +77,11 @@ async def test_httprettish_session(self): body=json.dumps(dict(origin="127.0.0.1")), ) - async with aiohttp.ClientSession(timeout=self.timeout) as session: - async with session.get(self.target_url) as get_response: - assert get_response.status == 200 - assert await get_response.text() == '{"origin": "127.0.0.1"}' + async with aiohttp.ClientSession( + timeout=self.timeout + ) as session, session.get(self.target_url) as get_response: + assert get_response.status == 200 + assert await get_response.text() == '{"origin": "127.0.0.1"}' class AioHttpsEntryTestCase(IsolatedAsyncioTestCase): timeout = aiohttp.ClientTimeout(total=3) @@ -111,9 +112,10 @@ async def test_https_session(self): async def test_no_verify(self): Entry.single_register(Entry.GET, self.target_url, status=404) - async with aiohttp.ClientSession(timeout=self.timeout) as session: - async with session.get(self.target_url, ssl=False) as get_response: - assert get_response.status == 404 + async with aiohttp.ClientSession( + timeout=self.timeout + ) as session, session.get(self.target_url, ssl=False) as get_response: + assert get_response.status == 404 @async_httprettified async def test_httprettish_session(self): @@ -123,10 +125,11 @@ async def test_httprettish_session(self): body=json.dumps(dict(origin="127.0.0.1")), ) - async with aiohttp.ClientSession(timeout=self.timeout) as session: - async with session.get(self.target_url) as get_response: - assert get_response.status == 200 - assert await get_response.text() == '{"origin": "127.0.0.1"}' + async with aiohttp.ClientSession( + timeout=self.timeout + ) as session, session.get(self.target_url) as get_response: + assert get_response.status == 200 + assert await get_response.text() == '{"origin": "127.0.0.1"}' @pytest.mark.skipif('os.getenv("SKIP_TRUE_HTTP", False)') async def test_mocked_https_request_after_unmocked_https_request(self):