mirror of
https://github.com/ansible-collections/community.docker.git
synced 2026-07-29 11:55:04 +00:00
Python code modernization, 4/n (#1162)
* Address attribute-defined-outside-init. * Address broad-exception-raised. * Address broad-exception-caught. * Address consider-iterating-dictionary. * Address consider-using-dict-comprehension. * Address consider-using-f-string. * Address consider-using-in. * Address consider-using-max-builtin. * Address some consider-using-with. * Address invalid-name. * Address keyword-arg-before-vararg. * Address line-too-long. * Address no-else-continue. * Address no-else-raise. * Address no-else-return. * Remove broken dead code. * Make consider-using-f-string changes compatible with older Python versions. * Python 3.11 and earlier apparently do not like multi-line f-strings.
This commit is contained in:
@@ -72,7 +72,9 @@ def response(
|
||||
return res
|
||||
|
||||
|
||||
def fake_resolve_authconfig(authconfig, registry=None, *args, **kwargs):
|
||||
def fake_resolve_authconfig(
|
||||
authconfig, registry=None, *args, **kwargs
|
||||
): # pylint: disable=keyword-arg-before-vararg
|
||||
return None
|
||||
|
||||
|
||||
@@ -87,7 +89,7 @@ def fake_resp(method, url, *args, **kwargs):
|
||||
elif (url, method) in fake_api.fake_responses:
|
||||
key = (url, method)
|
||||
if not key:
|
||||
raise Exception(f"{method} {url}")
|
||||
raise NotImplementedError(f"{method} {url}")
|
||||
status_code, content = fake_api.fake_responses[key]()
|
||||
return response(status_code=status_code, content=content)
|
||||
|
||||
@@ -115,8 +117,8 @@ def fake_read_from_socket(self, response, stream, tty=False, demux=False):
|
||||
return b""
|
||||
|
||||
|
||||
url_base = f"{fake_api.prefix}/"
|
||||
url_prefix = f"{url_base}v{DEFAULT_DOCKER_API_VERSION}/"
|
||||
url_base = f"{fake_api.prefix}/" # pylint: disable=invalid-name
|
||||
url_prefix = f"{url_base}v{DEFAULT_DOCKER_API_VERSION}/" # pylint: disable=invalid-name
|
||||
|
||||
|
||||
class BaseAPIClientTest(unittest.TestCase):
|
||||
@@ -482,7 +484,7 @@ class TCPSocketStreamTest(unittest.TestCase):
|
||||
stderr_data = cls.stderr_data
|
||||
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
def do_POST(self):
|
||||
def do_POST(self): # pylint: disable=invalid-name
|
||||
resp_data = self.get_resp_data()
|
||||
self.send_response(101)
|
||||
self.send_header("Content-Type", "application/vnd.docker.raw-stream")
|
||||
@@ -498,15 +500,14 @@ class TCPSocketStreamTest(unittest.TestCase):
|
||||
path = self.path.split("/")[-1]
|
||||
if path == "tty":
|
||||
return stdout_data + stderr_data
|
||||
elif path == "no-tty":
|
||||
if path == "no-tty":
|
||||
data = b""
|
||||
data += self.frame_header(1, stdout_data)
|
||||
data += stdout_data
|
||||
data += self.frame_header(2, stderr_data)
|
||||
data += stderr_data
|
||||
return data
|
||||
else:
|
||||
raise Exception(f"Unknown path {path}")
|
||||
raise NotImplementedError(f"Unknown path {path}")
|
||||
|
||||
@staticmethod
|
||||
def frame_header(stream, data):
|
||||
@@ -604,6 +605,7 @@ class DisableSocketTest(unittest.TestCase):
|
||||
class DummySocket:
|
||||
def __init__(self, timeout=60):
|
||||
self.timeout = timeout
|
||||
self._sock = None
|
||||
|
||||
def settimeout(self, timeout):
|
||||
self.timeout = timeout
|
||||
|
||||
@@ -537,9 +537,9 @@ def post_fake_secret():
|
||||
|
||||
|
||||
# Maps real api url to fake response callback
|
||||
prefix = "http+docker://localhost"
|
||||
prefix = "http+docker://localhost" # pylint: disable=invalid-name
|
||||
if constants.IS_WINDOWS_PLATFORM:
|
||||
prefix = "http+docker://localnpipe"
|
||||
prefix = "http+docker://localnpipe" # pylint: disable=invalid-name
|
||||
|
||||
fake_responses = {
|
||||
f"{prefix}/version": get_fake_version,
|
||||
|
||||
@@ -417,8 +417,8 @@ class TarTest(unittest.TestCase):
|
||||
self.addCleanup(shutil.rmtree, base)
|
||||
|
||||
with tar(base, exclude=exclude) as archive:
|
||||
tar_data = tarfile.open(fileobj=archive)
|
||||
assert sorted(tar_data.getnames()) == sorted(expected_names)
|
||||
with tarfile.open(fileobj=archive) as tar_data:
|
||||
assert sorted(tar_data.getnames()) == sorted(expected_names)
|
||||
|
||||
def test_tar_with_empty_directory(self):
|
||||
base = tempfile.mkdtemp()
|
||||
@@ -426,8 +426,8 @@ class TarTest(unittest.TestCase):
|
||||
for d in ["foo", "bar"]:
|
||||
os.makedirs(os.path.join(base, d))
|
||||
with tar(base) as archive:
|
||||
tar_data = tarfile.open(fileobj=archive)
|
||||
assert sorted(tar_data.getnames()) == ["bar", "foo"]
|
||||
with tarfile.open(fileobj=archive) as tar_data:
|
||||
assert sorted(tar_data.getnames()) == ["bar", "foo"]
|
||||
|
||||
@pytest.mark.skipif(
|
||||
IS_WINDOWS_PLATFORM or os.geteuid() == 0,
|
||||
@@ -454,8 +454,8 @@ class TarTest(unittest.TestCase):
|
||||
os.makedirs(os.path.join(base, "bar"))
|
||||
os.symlink("../foo", os.path.join(base, "bar/foo"))
|
||||
with tar(base) as archive:
|
||||
tar_data = tarfile.open(fileobj=archive)
|
||||
assert sorted(tar_data.getnames()) == ["bar", "bar/foo", "foo"]
|
||||
with tarfile.open(fileobj=archive) as tar_data:
|
||||
assert sorted(tar_data.getnames()) == ["bar", "bar/foo", "foo"]
|
||||
|
||||
@pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason="No symlinks on Windows")
|
||||
def test_tar_with_directory_symlinks(self):
|
||||
@@ -465,8 +465,8 @@ class TarTest(unittest.TestCase):
|
||||
os.makedirs(os.path.join(base, d))
|
||||
os.symlink("../foo", os.path.join(base, "bar/foo"))
|
||||
with tar(base) as archive:
|
||||
tar_data = tarfile.open(fileobj=archive)
|
||||
assert sorted(tar_data.getnames()) == ["bar", "bar/foo", "foo"]
|
||||
with tarfile.open(fileobj=archive) as tar_data:
|
||||
assert sorted(tar_data.getnames()) == ["bar", "bar/foo", "foo"]
|
||||
|
||||
@pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason="No symlinks on Windows")
|
||||
def test_tar_with_broken_symlinks(self):
|
||||
@@ -477,8 +477,8 @@ class TarTest(unittest.TestCase):
|
||||
|
||||
os.symlink("../baz", os.path.join(base, "bar/foo"))
|
||||
with tar(base) as archive:
|
||||
tar_data = tarfile.open(fileobj=archive)
|
||||
assert sorted(tar_data.getnames()) == ["bar", "bar/foo", "foo"]
|
||||
with tarfile.open(fileobj=archive) as tar_data:
|
||||
assert sorted(tar_data.getnames()) == ["bar", "bar/foo", "foo"]
|
||||
|
||||
@pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason="No UNIX sockets on Win32")
|
||||
def test_tar_socket_file(self):
|
||||
@@ -490,8 +490,8 @@ class TarTest(unittest.TestCase):
|
||||
self.addCleanup(sock.close)
|
||||
sock.bind(os.path.join(base, "test.sock"))
|
||||
with tar(base) as archive:
|
||||
tar_data = tarfile.open(fileobj=archive)
|
||||
assert sorted(tar_data.getnames()) == ["bar", "foo"]
|
||||
with tarfile.open(fileobj=archive) as tar_data:
|
||||
assert sorted(tar_data.getnames()) == ["bar", "foo"]
|
||||
|
||||
def tar_test_negative_mtime_bug(self):
|
||||
base = tempfile.mkdtemp()
|
||||
@@ -501,9 +501,9 @@ class TarTest(unittest.TestCase):
|
||||
f.write("Invisible Full Moon")
|
||||
os.utime(filename, (12345, -3600.0))
|
||||
with tar(base) as archive:
|
||||
tar_data = tarfile.open(fileobj=archive)
|
||||
assert tar_data.getnames() == ["th.txt"]
|
||||
assert tar_data.getmember("th.txt").mtime == -3600
|
||||
with tarfile.open(fileobj=archive) as tar_data:
|
||||
assert tar_data.getnames() == ["th.txt"]
|
||||
assert tar_data.getmember("th.txt").mtime == -3600
|
||||
|
||||
@pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason="No symlinks on Windows")
|
||||
def test_tar_directory_link(self):
|
||||
@@ -513,8 +513,8 @@ class TarTest(unittest.TestCase):
|
||||
self.addCleanup(shutil.rmtree, base)
|
||||
os.symlink(os.path.join(base, "b"), os.path.join(base, "a/c/b"))
|
||||
with tar(base) as archive:
|
||||
tar_data = tarfile.open(fileobj=archive)
|
||||
names = tar_data.getnames()
|
||||
with tarfile.open(fileobj=archive) as tar_data:
|
||||
names = tar_data.getnames()
|
||||
for member in dirs + files:
|
||||
assert member in names
|
||||
assert "a/c/b" in names
|
||||
|
||||
@@ -12,6 +12,7 @@ import json
|
||||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
import typing as t
|
||||
import unittest
|
||||
|
||||
from pytest import fixture, mark
|
||||
@@ -26,6 +27,7 @@ except ImportError:
|
||||
|
||||
|
||||
class FindConfigFileTest(unittest.TestCase):
|
||||
mkdir: t.Callable[[str], os.PathLike[str]]
|
||||
|
||||
@fixture(autouse=True)
|
||||
def tmpdir(self, tmpdir):
|
||||
|
||||
@@ -205,9 +205,8 @@ class ParseEnvFileTest(unittest.TestCase):
|
||||
of 'file_content' and returns the filename.
|
||||
Don't forget to unlink the file with os.unlink() after.
|
||||
"""
|
||||
local_tempfile = tempfile.NamedTemporaryFile(delete=False)
|
||||
local_tempfile.write(file_content.encode("UTF-8"))
|
||||
local_tempfile.close()
|
||||
with tempfile.NamedTemporaryFile(delete=False) as local_tempfile:
|
||||
local_tempfile.write(file_content.encode("UTF-8"))
|
||||
return local_tempfile.name
|
||||
|
||||
def test_parse_env_file_proper(self):
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
# pylint: disable=line-too-long
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from ansible_collections.community.docker.plugins.module_utils._compose_v2 import (
|
||||
|
||||
Reference in New Issue
Block a user