Skip to content

Commit 1728224

Browse files
committed
ruff check
1 parent 3769ef4 commit 1728224

File tree

8 files changed

+95
-87
lines changed

8 files changed

+95
-87
lines changed

test/pyproject.toml

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -27,74 +27,74 @@ dev = [
2727

2828
line-length = 120
2929

30-
#[tool.ruff.lint]
31-
#select = [
32-
# "ALL"
33-
#]
34-
#
35-
#ignore = [
36-
# "ANN", # Missing type annotations
30+
[tool.ruff.lint]
31+
select = [
32+
"ALL"
33+
]
34+
35+
ignore = [
36+
"ANN", # Missing type annotations
3737
# "A002", # Function argument `id` is shadowing a Python builtin
38-
# "ARG001", # Unused function argument: `...`
39-
# "COM812", # Trailing comma missing
40-
# "D100", # Missing docstring in public module
38+
"ARG001", # Unused function argument: `...`
39+
"COM812", # Trailing comma missing
40+
"D100", # Missing docstring in public module
4141
# "D101", # Missing docstring in public class
4242
# "D102", # Missing docstring in public method
43-
# "D103", # Missing docstring in public function
44-
# "D104", # Missing docstring in public package
43+
"D103", # Missing docstring in public function
44+
"D104", # Missing docstring in public package
4545
# "D107", # Missing docstring in __init__
46-
# "D203", # incorrect-blank-line-before-class
47-
# "D212", # Multi-line docstring summary should start at the first line
48-
# "D212", # Multi-line docstring summary should start at the first line
49-
# "D400", # First line should end with a period
50-
# "D415", # First line should end with a period, question mark, or exclamation point
46+
"D200", # One-line docstring should fit on one line
47+
"D203", # incorrect-blank-line-before-class
48+
"D212", # Multi-line docstring summary should start at the first line
49+
"D400", # First line should end with a period
50+
"D415", # First line should end with a period, question mark, or exclamation point
5151
# "DTZ005", # `datetime.datetime.now()` called without a `tz` argument
5252
# "EM102", # Exception must not use an f-string literal, assign to variable first
53-
# "ERA001", # Found commented-out code
53+
"ERA001", # Found commented-out code
5454
# "FBT002", # Boolean default positional argument in function definition
55-
# "FIX002", # Line contains TODO, consider resolving the issue
56-
# "FIX003", # Line contains XXX, consider resolving the issue
55+
"FIX002", # Line contains TODO, consider resolving the issue
56+
"FIX003", # Line contains XXX, consider resolving the issue
5757
# "N802", # Function name `testLogging` should be lowercase
58-
# "PLW1510", # `subprocess.run` without explicit `check` argument
59-
# "S101", # Use of 'assert' detected
58+
"PLW1510", # `subprocess.run` without explicit `check` argument
59+
"S101", # Use of 'assert' detected
6060
# "S104", # Possible binding to all interfaces
6161
# "S314", # Using `xml` to parse untrusted data is known to be vulnerable to XML attacks; use `defusedxml` equivalents
62-
# "S603", # `subprocess` call: check for execution of untrusted input
62+
"S603", # `subprocess` call: check for execution of untrusted input
6363
# "S604", # Function call with `shell=True` parameter identified, security issue
64-
# "S607", # Starting a process with a partial executable path
64+
"S607", # Starting a process with a partial executable path
6565
# "SIM108", # Use ternary operator `...` instead of `if`-`else`-block
66-
# "TD001", # Invalid TODO tag: `XXX`
67-
# "TD002", # Missing author in TODO
68-
# "TD003", # Missing issue link for this TODO
66+
"TD",
6967
# "TRY003", # Avoid specifying long messages outside the exception class
70-
# "PLR2004", # Magic value used in comparison, consider replacing `...` with a constant variable
71-
# "PLR0913", # Too many arguments in function definition (6 > 5)
72-
# "PTH107", # `os.remove()` should be replaced by `Path.unlink()`
73-
# "PTH108", # `os.unlink()` should be replaced by `Path.unlink()`
74-
# "PTH110", # `os.path.exists()` should be replaced by `Path.exists()`
75-
# "PTH116", # `os.stat()` should be replaced by `Path.stat()`, `Path.owner()`, or `Path.group()`
68+
"PLR2004", # Magic value used in comparison, consider replacing `...` with a constant variable
69+
"PLR0913", # Too many arguments in function definition (6 > 5)
70+
"PTH107", # `os.remove()` should be replaced by `Path.unlink()`
71+
"PTH108", # `os.unlink()` should be replaced by `Path.unlink()`
72+
"PTH110", # `os.path.exists()` should be replaced by `Path.exists()`
73+
"PTH116", # `os.stat()` should be replaced by `Path.stat()`, `Path.owner()`, or `Path.group()`
7674
# "PTH120", # `os.path.dirname()` should be replaced by `Path.parent`
7775
# "PTH123", # `open()` should be replaced by `Path.open()`
7876
# "PT009", # Use a regular `assert` instead of unittest-style `assertEqual`
79-
# "PT022", # No teardown in fixture `fw_cfg_factory`, use `return` instead of `yield`
77+
"PT022", # No teardown in fixture `fw_cfg_factory`, use `return` instead of `yield`
8078
# "TID252", # Prefer absolute imports over relative imports from parent modules
81-
# "UP022", # Prefer `capture_output` over sending `stdout` and `stderr` to `PIPE`
82-
#]
83-
#
84-
#[tool.basedpyright]
85-
#reportAny = "none"
86-
#reportArgumentType = "none"
87-
#reportAttributeAccessIssue = "none"
79+
"UP022", # Prefer `capture_output` over sending `stdout` and `stderr` to `PIPE`
80+
]
81+
82+
[tool.basedpyright]
83+
reportAny = "none"
84+
reportArgumentType = "none"
85+
reportAttributeAccessIssue = "none"
8886
#reportImplicitOverride = "none"
8987
#reportImplicitStringConcatenation = "none"
90-
#reportMissingParameterType = "none"
91-
#reportMissingTypeStubs = "none"
92-
#reportOptionalMemberAccess = "none"
88+
reportMissingParameterType = "none"
89+
reportMissingTypeStubs = "none"
90+
reportOptionalMemberAccess = "none"
9391
#reportUnannotatedClassAttribute = "none"
9492
#reportUninitializedInstanceVariable = "none"
95-
#reportUnknownArgumentType = "none"
96-
#reportUnknownMemberType = "none"
97-
#reportUnknownParameterType = "none"
98-
#reportUnknownVariableType = "none"
99-
#reportUnusedCallResult = "none"
100-
#reportUnusedParameter = "none"
93+
reportUnknownArgumentType = "none"
94+
reportUnknownMemberType = "none"
95+
reportUnknownParameterType = "none"
96+
reportUnknownVariableType = "none"
97+
reportUnusedCallResult = "none"
98+
reportUnusedParameter = "none"
99+
reportUnusedVariable = "none"
100+
reportOperatorIssue = "none"

test/tests/bouncer/test_custom_bouncer.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import time
3+
from pathlib import Path
34

45

56
def test_no_custom_binary(crowdsec, bouncer, cb_cfg_factory):
@@ -212,15 +213,15 @@ def test_add_decisions(bouncer_with_lapi):
212213

213214
time.sleep(1)
214215

215-
with open(data) as f:
216+
with Path(data).open() as f:
216217
lines = f.readlines()
217218
assert len(lines) == 5
218219

219220
for i, line in enumerate(lines, start=1):
220-
line = json.loads(line)
221-
line.pop("duration", None)
222-
line.pop("uuid", None)
223-
assert line == {
221+
j = json.loads(line)
222+
j.pop("duration", None)
223+
j.pop("uuid", None)
224+
assert j == {
224225
"action": "add",
225226
"id": i,
226227
"origin": "cscli",
@@ -252,7 +253,7 @@ def test_bin_args(bouncer_with_lapi, tmp_path_factory):
252253

253254
time.sleep(2)
254255

255-
with open(data) as f:
256+
with Path(data).open() as f:
256257
lines = f.readlines()
257258
assert len(lines) == 5
258259

@@ -274,7 +275,7 @@ def test_cache_retention(bouncer_with_lapi):
274275
res = lapi.cont.exec_run(f"cscli decisions add -i 1.2.3.{i}")
275276
assert res.exit_code == 0
276277
time.sleep(1)
277-
with open(data) as f:
278+
with Path(data).open() as f:
278279
lines = f.readlines()
279280
assert len(lines) == 2
280281

@@ -297,14 +298,14 @@ def test_delete_decisions(bouncer_with_lapi):
297298
res = lapi.cont.exec_run(f"cscli decisions delete --ip 1.2.3.{i}")
298299
assert res.exit_code == 0
299300
time.sleep(1)
300-
with open(data) as f:
301+
with Path(data).open() as f:
301302
lines = f.readlines()
302303
assert len(lines) == 10
303304
current_decisions = set()
304305
for line in lines:
305-
line = json.loads(line)
306-
if line["action"] == "add":
307-
current_decisions.add(line["id"])
308-
elif line["action"] == "del":
309-
current_decisions.remove(line["id"])
306+
j = json.loads(line)
307+
if j["action"] == "add":
308+
current_decisions.add(j["id"])
309+
elif j["action"] == "del":
310+
current_decisions.remove(j["id"])
310311
assert len(current_decisions) == 0

test/tests/bouncer/test_tls.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
def test_tls_server(crowdsec, certs_dir, api_key_factory, bouncer, cb_stream_cfg_factory):
55
"""TLS with server-only certificate"""
6-
76
api_key = api_key_factory()
87

98
lapi_env = {
@@ -56,7 +55,6 @@ def test_tls_server(crowdsec, certs_dir, api_key_factory, bouncer, cb_stream_cfg
5655

5756
def test_tls_mutual(crowdsec, certs_dir, api_key_factory, bouncer, cb_stream_cfg_factory, bouncer_under_test):
5857
"""TLS with two-way bouncer/lapi authentication"""
59-
6058
lapi_env = {
6159
"CACERT_FILE": "/etc/ssl/crowdsec/ca.crt",
6260
"LAPI_CERT_FILE": "/etc/ssl/crowdsec/lapi.crt",

test/tests/conftest.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ def bouncer_under_test():
1717
# Create a lapi container, register a bouncer and run it with the updated config.
1818
# - Return context manager that yields a tuple of (bouncer, lapi)
1919
@pytest.fixture(scope="session")
20-
def bouncer_with_lapi(bouncer, crowdsec, cb_stream_cfg_factory, api_key_factory, tmp_path_factory, bouncer_binary):
20+
def bouncer_with_lapi(
21+
bouncer,
22+
crowdsec,
23+
cb_stream_cfg_factory,
24+
api_key_factory: plugin.ApiKeyFactoryType,
25+
tmp_path_factory,
26+
bouncer_binary,
27+
):
2128
@contextlib.contextmanager
2229
def closure(config_lapi=None, config_bouncer=None, api_key=None):
2330
if config_bouncer is None:

test/tests/install/no_crowdsec/test_no_crowdsec_deb.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import subprocess
3+
from pathlib import Path
34

45
import pytest
56

@@ -16,10 +17,10 @@ def test_deb_install_purge(deb_package_path, bouncer_under_test, must_be_root):
1617
assert deb_package_path.exists(), f"This test requires {deb_package_path}"
1718

1819
bouncer_exe = f"/usr/bin/{bouncer_under_test}"
19-
assert not os.path.exists(bouncer_exe)
20+
assert not Path(bouncer_exe).exists()
2021

2122
config = f"/etc/crowdsec/bouncers/{bouncer_under_test}.yaml"
22-
assert not os.path.exists(config)
23+
assert not Path(config).exists()
2324

2425
# install the package
2526
p = subprocess.run(
@@ -30,7 +31,7 @@ def test_deb_install_purge(deb_package_path, bouncer_under_test, must_be_root):
3031
)
3132
assert p.returncode == 0, f"Failed to install {deb_package_path}"
3233

33-
assert os.path.exists(bouncer_exe)
34+
assert Path(bouncer_exe).exists()
3435
assert os.stat(bouncer_exe).st_mode & 0o777 == 0o755
3536

3637
assert os.path.exists(config)

test/tests/install/no_crowdsec/test_no_crowdsec_scripts.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import os
2-
import pexpect
3-
import yaml
2+
from pathlib import Path
43

4+
import pexpect
55
import pytest
6+
import yaml
67

78
BOUNCER = "crowdsec-custom-bouncer"
89
CONFIG = f"/etc/crowdsec/bouncers/{BOUNCER}.yaml"
910

1011

11-
@pytest.mark.dependency()
12+
@pytest.mark.dependency
1213
def test_install_no_crowdsec(project_repo, bouncer_binary, must_be_root):
1314
c = pexpect.spawn("/usr/bin/sh", ["scripts/install.sh"], cwd=project_repo)
1415

1516
c.expect("Path to your custom binary:")
16-
c.sendline("/tmp/foo")
17+
c.sendline("/path/to/binary")
1718
c.expect(f"Installing {BOUNCER}")
1819
c.expect("WARN.* cscli not found, you will need to generate an api key.")
1920
c.expect(f"WARN.* service not started. You need to get an API key and configure it in {CONFIG}")
@@ -22,10 +23,10 @@ def test_install_no_crowdsec(project_repo, bouncer_binary, must_be_root):
2223
assert c.terminated
2324
assert c.exitstatus == 0
2425

25-
with open(CONFIG) as f:
26+
with Path(CONFIG).open() as f:
2627
y = yaml.safe_load(f)
2728
assert y["api_key"] == "<API_KEY>"
28-
assert y["bin_path"] == "/tmp/foo"
29+
assert y["bin_path"] == "/path/to/binary"
2930

3031
assert os.path.exists(CONFIG)
3132
assert os.stat(CONFIG).st_mode & 0o777 == 0o600

test/tests/install/with_crowdsec/test_crowdsec_deb.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import os
22
import subprocess
3-
import yaml
43
from pathlib import Path
54

65
import pytest
6+
import yaml
77
from zxcvbn import zxcvbn
88

99
pytestmark = pytest.mark.deb
@@ -47,13 +47,13 @@ def test_deb_install_purge(deb_package_path, bouncer_under_test, must_be_root):
4747
assert os.path.exists(config)
4848
assert os.stat(config).st_mode & 0o777 == 0o600
4949

50-
with open(config) as f:
50+
with Path(config).open() as f:
5151
cfg = yaml.safe_load(f)
5252
api_key = cfg["api_key"]
5353
# the api key has been set to a random value
5454
assert zxcvbn(api_key)["score"] == 4, f"weak api_key: '{api_key}'"
5555

56-
with open(config + ".id") as f:
56+
with Path(config + ".id").open() as f:
5757
bouncer_name = f.read().strip()
5858

5959
p = subprocess.check_output(["cscli", "bouncers", "list", "-o", "json"])
@@ -78,7 +78,6 @@ def test_deb_install_purge_yaml_local(deb_package_path, bouncer_under_test, must
7878
7979
=> the configuration files are not touched (no new api key)
8080
"""
81-
8281
assert deb_package_path.exists(), f"This test requires {deb_package_path}"
8382

8483
p = subprocess.check_output(["dpkg-deb", "-f", deb_package_path.as_posix(), "Package"], encoding="utf-8")
@@ -93,7 +92,7 @@ def test_deb_install_purge_yaml_local(deb_package_path, bouncer_under_test, must
9392

9493
subprocess.check_call(["cscli", "bouncers", "add", "testbouncer", "-k", "123456"])
9594

96-
with open(config.with_suffix(".yaml.local"), "w") as f:
95+
with config.with_suffix(".yaml.local").open("w") as f:
9796
f.write('api_key: "123456"')
9897

9998
p = subprocess.run(
@@ -107,7 +106,7 @@ def test_deb_install_purge_yaml_local(deb_package_path, bouncer_under_test, must
107106
assert os.path.exists(bouncer_exe)
108107
assert os.path.exists(config)
109108

110-
with open(config) as f:
109+
with Path(config).open() as f:
111110
cfg = yaml.safe_load(f)
112111
api_key = cfg["api_key"]
113112
# the api key has not been set

test/tests/install/with_crowdsec/test_crowdsec_scripts.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import os
2-
import pexpect
3-
import yaml
2+
from pathlib import Path
43

4+
import pexpect
55
import pytest
6+
import yaml
67
from pytest_cs.lib import cscli, text
78

89
BOUNCER = "crowdsec-custom-bouncer"
910
CONFIG = f"/etc/crowdsec/bouncers/{BOUNCER}.yaml"
1011

1112

1213
@pytest.mark.systemd_debug(BOUNCER)
13-
@pytest.mark.dependency()
14+
@pytest.mark.dependency
1415
def test_install_crowdsec(project_repo, bouncer_binary, tmp_path, must_be_root):
1516
c = pexpect.spawn("/usr/bin/sh", ["scripts/install.sh"], encoding="utf-8", cwd=project_repo)
1617

@@ -36,13 +37,13 @@ def test_install_crowdsec(project_repo, bouncer_binary, tmp_path, must_be_root):
3637
assert os.stat(f"/usr/local/bin/{BOUNCER}").st_mode & 0o777 == 0o755
3738

3839
# configuration check
39-
with open(CONFIG) as f:
40+
with Path(CONFIG).open() as f:
4041
y = yaml.safe_load(f)
4142
assert y["api_key"] == api_key
4243
assert y["bin_path"] == foo.as_posix()
4344

4445
# the bouncer is registered
45-
with open(f"{CONFIG}.id") as f:
46+
with Path(f"{CONFIG}.id").open() as f:
4647
bouncer_name = f.read().strip()
4748

4849
assert len(list(cscli.get_bouncers(name=bouncer_name))) == 1
@@ -70,7 +71,7 @@ def test_upgrade_crowdsec(project_repo, must_be_root):
7071
@pytest.mark.dependency(depends=["test_upgrade_crowdsec"])
7172
def test_uninstall_crowdsec(project_repo, must_be_root):
7273
# the bouncer is registered
73-
with open(f"{CONFIG}.id") as f:
74+
with Path(f"{CONFIG}.id").open() as f:
7475
bouncer_name = f.read().strip()
7576

7677
c = pexpect.spawn("/usr/bin/sh", ["scripts/uninstall.sh"], encoding="utf-8", cwd=project_repo)

0 commit comments

Comments
 (0)