Skip to content

Commit 6f049f3

Browse files
committed
Ruff: Fix PTH118, merge PTH11
1 parent 5678d55 commit 6f049f3

File tree

80 files changed

+359
-392
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+359
-392
lines changed

dojo/forms.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2400,7 +2400,7 @@ def get_jira_issue_template_dir_choices():
24002400

24012401
for dirname in dirnames:
24022402
clean_base_dir = base_dir.removeprefix(settings.TEMPLATE_DIR_PREFIX)
2403-
template_dir_list.append((os.path.join(clean_base_dir, dirname), dirname))
2403+
template_dir_list.append((str(Path(clean_base_dir) / dirname), dirname))
24042404

24052405
logger.debug("templates: %s", template_dir_list)
24062406
return template_dir_list

dojo/jira_link/helper.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import io
22
import json
33
import logging
4-
import os
54
from pathlib import Path
65
from typing import Any
76

@@ -333,8 +332,8 @@ def get_jira_issue_template(obj):
333332
template_dir = "issue-trackers/jira_full/"
334333

335334
if isinstance(obj, Finding_Group):
336-
return os.path.join(template_dir, "jira-finding-group-description.tpl")
337-
return os.path.join(template_dir, "jira-description.tpl")
335+
return Path(template_dir) / "jira-finding-group-description.tpl"
336+
return Path(template_dir) / "jira-description.tpl"
338337

339338

340339
def get_jira_creation(obj):

dojo/models.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import copy
33
import hashlib
44
import logging
5-
import os
65
import re
76
import warnings
87
from datetime import datetime
@@ -149,7 +148,7 @@ def __call__(self, model_instance, filename):
149148
filename += ext
150149
if self.directory is None:
151150
return filename
152-
return os.path.join(now().strftime(self.directory), filename)
151+
return Path(now().strftime(self.directory)) / filename
153152

154153

155154
class Regulation(models.Model):

dojo/settings/settings.dist.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ def generate_url(scheme, double_slashes, user, password, host, port, path, param
442442
# Put strings here, like "/home/html/static" or "C:/www/django/static".
443443
# Always use forward slashes, even on Windows.
444444
# Don't forget to use absolute paths, not relative paths.
445-
os.path.join(Path(DOJO_ROOT).parent, "components", "node_modules"),
445+
Path(DOJO_ROOT).parent / "components" / "node_modules",
446446
)
447447

448448
# List of finder classes that know how to find static files in
@@ -949,7 +949,7 @@ def saml2_attrib_map_format(dict):
949949
"entityid": str(SAML2_ENTITY_ID),
950950

951951
# directory with attribute mapping
952-
"attribute_map_dir": path.join(BASEDIR, "attribute-maps"),
952+
"attribute_map_dir": Path(BASEDIR) / "attribute-maps",
953953
# do now discard attributes not specified in attribute-maps
954954
"allow_unknown_attributes": SAML_ALLOW_UNKNOWN_ATTRIBUTES,
955955
# this block states what services we provide

dojo/tools/factory.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def requires_tool_type(scan_type):
117117
package_dir = str(Path(__file__).resolve().parent)
118118
for module_name in os.listdir(package_dir): # noqa: PTH208
119119
# check if it's dir
120-
if Path(os.path.join(package_dir, module_name)).is_dir():
120+
if (Path(package_dir) / module_name).is_dir():
121121
try:
122122
# check if it's a Python module
123123
if find_spec(f"dojo.tools.{module_name}.parser"):

dojo/views.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import logging
2-
import os
32
from pathlib import Path
43

54
from auditlog.models import LogEntry
@@ -151,7 +150,7 @@ def manage_files(request, oid, obj_type):
151150

152151
for o in files_formset.deleted_objects:
153152
logger.debug("removing file: %s", o.file.name)
154-
Path(os.path.join(settings.MEDIA_ROOT, o.file.name)).unlink()
153+
(Path(settings.MEDIA_ROOT) / o.file.name).unlink()
155154

156155
for o in files_formset.new_objects:
157156
logger.debug("adding file: %s", o.file.name)
@@ -162,7 +161,7 @@ def manage_files(request, oid, obj_type):
162161
finding__isnull=True)
163162
for o in orphan_files:
164163
logger.debug("purging orphan file: %s", o.file.name)
165-
Path(os.path.join(settings.MEDIA_ROOT, o.file.name)).unlink()
164+
(Path(settings.MEDIA_ROOT) / o.file.name).unlink()
166165
o.delete()
167166

168167
messages.add_message(

ruff.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ select = [
6666
"TCH",
6767
"INT",
6868
"ARG003", "ARG004", "ARG005",
69-
"PTH2", "PTH101", "PTH102", "PTH103", "PTH104", "PTH105", "PTH106", "PTH107", "PTH108", "PTH109", "PTH110", "PTH111", "PTH112", "PTH113", "PTH114", "PTH115", "PTH116", "PTH117", "PTH119", "PTH120", "PTH121", "PTH122", "PTH124",
69+
"PTH2", "PTH101", "PTH102", "PTH103", "PTH104", "PTH105", "PTH106", "PTH107", "PTH108", "PTH109", "PTH11", "PTH120", "PTH121", "PTH122", "PTH124",
7070
"TD001", "TD004", "TD005",
7171
"PD",
7272
"PGH",

tests/file_test.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_add_file_finding_level(self):
3535
driver.find_element(By.LINK_TEXT, "Manage Files").click()
3636
# select first file input field: form-0-image
3737
# Set full image path for image file 'strange.png
38-
image_path = os.path.join(dir_path, "finding_image.png")
38+
image_path = Path(dir_path) / "finding_image.png"
3939
driver.find_element(By.ID, "id_form-0-title").send_keys("Finding Title")
4040
driver.find_element(By.ID, "id_form-0-file").send_keys(image_path)
4141
# Save uploaded image
@@ -76,7 +76,7 @@ def test_add_file_test_level(self):
7676
driver.find_element(By.NAME, "Manage Files").click()
7777
# select first file input field: form-0-image
7878
# Set full image path for image file 'strange.png
79-
image_path = os.path.join(dir_path, "finding_image.png")
79+
image_path = Path(dir_path) / "finding_image.png"
8080
driver.find_element(By.ID, "id_form-0-title").send_keys("Test Title")
8181
driver.find_element(By.ID, "id_form-0-file").send_keys(image_path)
8282
# Save uploaded image
@@ -116,7 +116,7 @@ def test_add_file_engagement_level(self):
116116
driver.find_element(By.NAME, "Manage Files").click()
117117
# select first file input field: form-0-image
118118
# Set full image path for image file 'strange.png
119-
image_path = os.path.join(dir_path, "finding_image.png")
119+
image_path = Path(dir_path) / "finding_image.png"
120120
driver.find_element(By.ID, "id_form-0-title").send_keys("Engagement Title")
121121
driver.find_element(By.ID, "id_form-0-file").send_keys(image_path)
122122
# Save uploaded image

tests/finding_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def test_add_image(self):
146146
driver.find_element(By.LINK_TEXT, "Manage Files").click()
147147
# select first file input field: form-0-image
148148
# Set full image path for image file 'strange.png
149-
image_path = os.path.join(dir_path, "finding_image.png")
149+
image_path = Path(dir_path) / "finding_image.png"
150150
driver.find_element(By.ID, "id_form-0-file").send_keys(image_path)
151151
driver.find_element(By.ID, "id_form-0-title").send_keys("Image Title")
152152
# Save uploaded image
@@ -466,7 +466,7 @@ def test_import_scan_result(self):
466466
# Select `Default` as the Environment
467467
Select(driver.find_element(By.ID, "id_environment")).select_by_visible_text("Development")
468468
# upload scan file
469-
file_path = os.path.join(dir_path, "zap_sample.xml")
469+
file_path = Path(dir_path) / "zap_sample.xml"
470470
driver.find_element(By.NAME, "file").send_keys(file_path)
471471
# Click Submit button
472472
with WaitForPageLoad(driver, timeout=50):

tests/ibm_appscan_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_import_ibm_app_scan_result(self):
3131
# Select `Default` as the Environment
3232
Select(driver.find_element(By.ID, "id_environment")).select_by_visible_text("Development")
3333
# Upload Scan result file
34-
scanner_file = os.path.join(dir_path, "ibm_appscan_xml_file.xml")
34+
scanner_file = Path(dir_path) / "ibm_appscan_xml_file.xml"
3535
driver.find_element(By.NAME, "file").send_keys(scanner_file)
3636
# click on upload button
3737
driver.find_elements(By.CSS_SELECTOR, "button.btn.btn-primary")[1].click()

unittests/dojo_test_case.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import copy
22
import json
33
import logging
4-
import os
54
from functools import wraps
65
from itertools import chain
76
from pathlib import Path
@@ -40,7 +39,7 @@
4039

4140

4241
def get_unit_tests_path():
43-
return str(Path(os.path.realpath(__file__)).parent)
42+
return Path(__file__).parent
4443

4544

4645
def toggle_system_setting_boolean(flag_name, value):
@@ -504,7 +503,7 @@ def import_scan_with_params(self, filename, scan_type="ZAP Scan", engagement=1,
504503
product_name=None, product_type_name=None, auto_create_context=None, expected_http_status_code=201, test_title=None,
505504
scan_date=None, service=None, forceActive=True, forceVerified=True):
506505

507-
with open(get_unit_tests_path() + "/" + filename, encoding="utf-8") as testfile:
506+
with open(get_unit_tests_path() / filename, encoding="utf-8") as testfile:
508507
payload = {
509508
"minimum_severity": minimum_severity,
510509
"active": active,
@@ -556,7 +555,7 @@ def import_scan_with_params(self, filename, scan_type="ZAP Scan", engagement=1,
556555
def reimport_scan_with_params(self, test_id, filename, scan_type="ZAP Scan", engagement=1, minimum_severity="Low", active=True, verified=False, push_to_jira=None,
557556
tags=None, close_old_findings=True, group_by=None, engagement_name=None, scan_date=None,
558557
product_name=None, product_type_name=None, auto_create_context=None, expected_http_status_code=201, test_title=None):
559-
with open(get_unit_tests_path() + "/" + filename, encoding="utf-8") as testfile:
558+
with open(get_unit_tests_path() / filename, encoding="utf-8") as testfile:
560559
payload = {
561560
"minimum_severity": minimum_severity,
562561
"active": active,
@@ -605,7 +604,7 @@ def reimport_scan_with_params(self, test_id, filename, scan_type="ZAP Scan", eng
605604
def endpoint_meta_import_scan_with_params(self, filename, product=1, product_name=None,
606605
create_endpoints=True, create_tags=True, create_dojo_meta=True,
607606
expected_http_status_code=201):
608-
with open(get_unit_tests_path() + "/" + filename, encoding="utf-8") as testfile:
607+
with open(get_unit_tests_path() / filename, encoding="utf-8") as testfile:
609608
payload = {
610609
"create_endpoints": create_endpoints,
611610
"create_tags": create_tags,

unittests/test_endpoint_meta_import.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def endpoint_meta_import_ui(self, product, payload):
206206

207207
def endpoint_meta_import_scan_with_params_ui(self, filename, product=1, create_endpoints=True,
208208
create_tags=True, create_dojo_meta=True, expected_http_status_code=201):
209-
with open(get_unit_tests_path() + "/" + filename, encoding="utf-8") as testfile:
209+
with open(get_unit_tests_path() / filename, encoding="utf-8") as testfile:
210210
payload = {
211211
"create_endpoints": create_endpoints,
212212
"create_tags": create_tags,

unittests/test_factory.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import logging
2-
import os
32
from importlib import import_module
43
from importlib.util import find_spec
54
from inspect import isclass
@@ -16,25 +15,25 @@ class TestFactory(DojoTestCase):
1615
def test_get_parser(self):
1716
with self.subTest(scan_type="Acunetix Scan"):
1817
scan_type = "Acunetix Scan"
19-
testfile = open(get_unit_tests_path() + "/scans/acunetix/one_finding.xml", encoding="utf-8")
18+
testfile = open(get_unit_tests_path() / "scans" / "acunetix" / "one_finding.xml", encoding="utf-8")
2019
parser = get_parser(scan_type)
2120
parser.get_findings(testfile, Test())
2221
testfile.close()
2322
with self.subTest(scan_type="Anchore Engine Scan"):
2423
scan_type = "Anchore Engine Scan"
25-
testfile = open(get_unit_tests_path() + "/scans/anchore_engine/one_vuln.json", encoding="utf-8")
24+
testfile = open(get_unit_tests_path() / "scans" / "anchore_engine" / "one_vuln.json", encoding="utf-8")
2625
parser = get_parser(scan_type)
2726
parser.get_findings(testfile, Test())
2827
testfile.close()
2928
with self.subTest(scan_type="Tenable Scan"):
3029
scan_type = "Tenable Scan"
31-
testfile = open(get_unit_tests_path() + "/scans/tenable/nessus/nessus_v_unknown.xml", encoding="utf-8")
30+
testfile = open(get_unit_tests_path() / "scans" / "tenable/nessus" / "nessus_v_unknown.xml", encoding="utf-8")
3231
parser = get_parser(scan_type)
3332
parser.get_findings(testfile, Test())
3433
testfile.close()
3534
with self.subTest(scan_type="ZAP Scan"):
3635
scan_type = "ZAP Scan"
37-
testfile = open(get_unit_tests_path() + "/scans/zap/some_2.9.0.xml", encoding="utf-8")
36+
testfile = open(get_unit_tests_path() / "scans" / "zap" / "some_2.9.0.xml", encoding="utf-8")
3837
parser = get_parser(scan_type)
3938
parser.get_findings(testfile, Test())
4039
testfile.close()
@@ -73,7 +72,7 @@ def test_parser_name_matches_module(self):
7372
for module_name in module_names:
7473
if module_name in excluded_parsers:
7574
continue
76-
if Path(os.path.join(package_dir, module_name)).is_dir():
75+
if (Path(package_dir) / module_name).is_dir():
7776
found = False
7877
if find_spec(f"dojo.tools.{module_name}.parser"):
7978
module = import_module(f"dojo.tools.{module_name}.parser")

unittests/test_import_reimport.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1823,7 +1823,7 @@ def import_scan_with_params_ui(self, filename, scan_type="ZAP Scan", engagement=
18231823
elif not verified:
18241824
verifiedPayload = "force_to_false"
18251825

1826-
with open(get_unit_tests_path() + filename, encoding="utf-8") as testfile:
1826+
with open(get_unit_tests_path() / filename, encoding="utf-8") as testfile:
18271827
payload = {
18281828
"minimum_severity": minimum_severity,
18291829
"active": activePayload,
@@ -1861,7 +1861,7 @@ def reimport_scan_with_params_ui(self, test_id, filename, scan_type="ZAP Scan",
18611861
if not verified:
18621862
verifiedPayload = "force_to_false"
18631863

1864-
with open(get_unit_tests_path() + filename, encoding="utf-8") as testfile:
1864+
with open(get_unit_tests_path() / filename, encoding="utf-8") as testfile:
18651865
payload = {
18661866
"minimum_severity": minimum_severity,
18671867
"active": activePayload,

unittests/test_importers_importer.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
class TestDojoDefaultImporter(DojoTestCase):
4141
def test_parse_findings(self):
42-
with open(get_unit_tests_path() + "/scans/acunetix/one_finding.xml", encoding="utf-8") as scan:
42+
with open(get_unit_tests_path() / "scans" / "acunetix" / "one_finding.xml", encoding="utf-8") as scan:
4343
scan_type = "Acunetix Scan"
4444
user, _created = User.objects.get_or_create(username="admin")
4545
product_type, _created = Product_Type.objects.get_or_create(name="test")
@@ -80,7 +80,7 @@ def test_parse_findings(self):
8080
self.assertIn(finding.numerical_severity, ["S0", "S1", "S2", "S3", "S4"])
8181

8282
def test_import_scan(self):
83-
with open(get_unit_tests_path() + "/scans/sarif/spotbugs.sarif", encoding="utf-8") as scan:
83+
with open(get_unit_tests_path() / "scans" / "sarif" / "spotbugs.sarif", encoding="utf-8") as scan:
8484
scan_type = SarifParser().get_scan_types()[0] # SARIF format implement the new method
8585
user, _ = User.objects.get_or_create(username="admin")
8686
product_type, _ = Product_Type.objects.get_or_create(name="test2")

unittests/test_jira_config_engagement_epic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def _get_vcr(self, **kwargs):
2727
my_vcr.record_mode = "once"
2828
my_vcr.path_transformer = VCR.ensure_suffix(".yaml")
2929
my_vcr.filter_headers = ["Authorization", "X-Atlassian-Token"]
30-
my_vcr.cassette_library_dir = get_unit_tests_path() + "/vcr/jira/"
30+
my_vcr.cassette_library_dir = get_unit_tests_path() / "vcr" / "jira" # TODO: check this
3131
# filters headers doesn't seem to work for cookies, so use callbacks to filter cookies from being recorded
3232
my_vcr.before_record_request = self.before_record_request
3333
my_vcr.before_record_response = self.before_record_response

unittests/test_jira_import_and_pushing_api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def _get_vcr(self, **kwargs):
5050
my_vcr.record_mode = "once"
5151
my_vcr.path_transformer = VCR.ensure_suffix(".yaml")
5252
my_vcr.filter_headers = ["Authorization", "X-Atlassian-Token"]
53-
my_vcr.cassette_library_dir = get_unit_tests_path() + "/vcr/jira/"
53+
my_vcr.cassette_library_dir = get_unit_tests_path() / "vcr" / "jira" # TODO: check this
5454
# filters headers doesn't seem to work for cookies, so use callbacks to filter cookies from being recorded
5555
my_vcr.before_record_request = self.before_record_request
5656
my_vcr.before_record_response = self.before_record_response

0 commit comments

Comments
 (0)