Set up ruff so that test files have at least some linting (#10399)

Alternative implementation for #7908.

In this PR:
- set up ruff in CI (add to `tox.ini`, mark dep in `certbot/setup.py`)
- add a `ruff.toml` that ignores particularly annoying errors. I think
line length isn't actually necessary to set with this workflow since
we're not checking it but putting it there for future usage.
- either fix or ignore the rest of the errors that come with the default
linting configuration. fixed errors are mostly unused variables. ignored
are usually where we're doing weird import things for a specific reason.
This commit is contained in:
ohemorange
2025-08-08 08:48:43 -07:00
committed by GitHub
parent 5859e50e44
commit dea3e5f1c4
26 changed files with 102 additions and 81 deletions
+1
View File
@@ -71,6 +71,7 @@ test_extras = [
'pytest',
'pytest-cov',
'pytest-xdist',
'ruff',
'setuptools',
'tox',
'types-httplib2',
@@ -1,5 +1,6 @@
"""Certbot command line argument & config processing."""
# pylint: disable=too-many-lines
# ruff: noqa: F401
import argparse
import logging
import logging.handlers
@@ -51,7 +51,7 @@ class ChallengeFactoryTest(unittest.TestCase):
[messages.STATUS_PENDING])
achalls = self.handler._challenge_factory(authzr, [0])
assert type(achalls[0]) == achallenges.Other
assert type(achalls[0]) is achallenges.Other
class HandleAuthorizationsTest(unittest.TestCase):
@@ -226,7 +226,8 @@ class CertificatesTest(BaseCertManagerTest):
# pylint: disable=protected-access
# pylint: disable=protected-access
get_report = lambda: cert_manager._report_human_readable(mock_config, parsed_certs)
def get_report():
return cert_manager._report_human_readable(mock_config, parsed_certs)
out = get_report()
assert "INVALID: EXPIRED" in out
@@ -117,11 +117,10 @@ class ParseTest(unittest.TestCase):
with tempfile.NamedTemporaryFile() as tmp_config:
tmp_config.close() # close now because of compatibility issues on Windows
# use a shim to get ConfigArgParse to pick up tmp_config
shim = (
lambda v: copy.deepcopy(constants.CLI_DEFAULTS[v])
if v != "config_files"
else [tmp_config.name]
)
def shim(v):
return (copy.deepcopy(constants.CLI_DEFAULTS[v])
if v != "config_files"
else [tmp_config.name])
mock_flag_default.side_effect = shim
namespace = self.parse(["certonly"])
@@ -1061,7 +1061,7 @@ class EnhanceConfigTest(ClientTestCommon):
def _test_error(self, enhance_error=False, restart_error=False):
self.config.redirect = True
with mock.patch('certbot._internal.client.logger') as mock_logger, \
test_util.patch_display_util() as mock_gu:
test_util.patch_display_util():
with pytest.raises(errors.PluginError):
self._test_with_all_supported()
@@ -565,7 +565,7 @@ class RealpathTest(test_util.TempDirTestCase):
os.symlink(link2_path, link3_path)
os.symlink(link3_path, link1_path)
with pytest.raises(RuntimeError, match='link1 is a loop!') as error:
with pytest.raises(RuntimeError, match='link1 is a loop!'):
filesystem.realpath(link1_path)
@@ -1,5 +1,6 @@
"""Tests for certbot._internal.lock."""
import functools
import importlib
import multiprocessing
import sys
from unittest import mock
@@ -10,13 +11,10 @@ from certbot import errors
from certbot.compat import os
from certbot.tests import util as test_util
try:
import fcntl # pylint: disable=import-error,unused-import
except ImportError:
POSIX_MODE = False
else:
if importlib.util.find_spec('fcntl'):
POSIX_MODE = True
else:
POSIX_MODE = False
@@ -198,7 +198,7 @@ class RenewalTest(test_util.ConfigTestCase):
from certbot._internal import renewal
lineage_config = copy.deepcopy(self.config)
renewal_candidate = renewal.reconstitute(lineage_config, rc_path)
renewal.reconstitute(lineage_config, rc_path)
# This means that manual_public_ip_logging_ok was not modified in the config based on its
# value in the renewal conf file
assert isinstance(lineage_config.manual_public_ip_logging_ok, mock.MagicMock)
@@ -233,7 +233,7 @@ class RenewalTest(test_util.ConfigTestCase):
test_util.make_lineage(self.config.config_dir, 'sample-renewal.conf', ec=False)
config = configuration.NamespaceConfig(self.config)
with mock.patch('time.sleep') as sleep:
with mock.patch('time.sleep'):
renewal.handle_renewal_request(config)
mock_renew_cert.assert_called_once()
@@ -254,7 +254,7 @@ class RenewalTest(test_util.ConfigTestCase):
test_util.make_lineage(self.config.config_dir, 'sample-renewal.conf', ec=False)
with mock.patch('time.sleep') as sleep:
with mock.patch('time.sleep'):
renewal.handle_renewal_request(self.config)
assert mock_client_network_get.call_count == 0
@@ -265,7 +265,7 @@ class RenewalTest(test_util.ConfigTestCase):
self.config.dry_run = True
ari_client_pool = mock.MagicMock()
ari_client_pool.get.return_value = mock_acme
with mock.patch('time.sleep') as sleep:
with mock.patch('time.sleep'):
renewal.should_renew(self.config, mock.Mock(), ari_client_pool)
assert mock_acme.renewal_time.call_count == 0
+1 -1
View File
@@ -13,7 +13,7 @@ from __future__ import absolute_import
# First round of wrapping: we import statically all public attributes exposed by the os.path
# module. This allows in particular to have pylint, mypy, IDEs be aware that most of os.path
# members are available in certbot.compat.path.
from os.path import * # pylint: disable=wildcard-import,unused-wildcard-import,os-module-forbidden
from os.path import * # pylint: disable=wildcard-import,unused-wildcard-import,os-module-forbidden # noqa: F403
# Second round of wrapping: we import dynamically all attributes from the os.path module that have
# not yet been imported by the first round (static star import).
+2 -2
View File
@@ -21,7 +21,7 @@ from __future__ import absolute_import
# First round of wrapping: we import statically all public attributes exposed by the os module
# This allows in particular to have pylint, mypy, IDEs be aware that most of os members are
# available in certbot.compat.os.
from os import * # pylint: disable=wildcard-import,unused-wildcard-import,redefined-builtin,os-module-forbidden
from os import * # pylint: disable=wildcard-import,unused-wildcard-import,redefined-builtin,os-module-forbidden # noqa: F403
# Second round of wrapping: we import dynamically all attributes from the os module that have not
# yet been imported by the first round (static import). This covers in particular the case of
@@ -43,7 +43,7 @@ if not std_os.environ.get("CERTBOT_DOCS") == "1":
# Import our internal path module, then allow certbot.compat.os.path
# to behave as a module (similarly to os.path).
from certbot.compat import _path as path # type: ignore # pylint: disable=wrong-import-position
from certbot.compat import _path as path # type: ignore # pylint: disable=wrong-import-position # noqa: E402
std_sys.modules[__name__ + '.path'] = path
# Clean all remaining importables that are not from the core os module.