Skip to content

Commit 79e8f78

Browse files
authored
Ruff: Fix PTH123 in unittests (exclude unittests/tools) (#12112)
1 parent db2ddd8 commit 79e8f78

10 files changed

Lines changed: 55 additions & 48 deletions

ruff.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ preview = true
110110
"unittests/**" = [
111111
"S105", # hardcoded passwords in tests are fine
112112
"S108", # tmp paths mentioned in tests are fine
113-
"PTH123", # Fix is needed in unittests as well but there are too many problematic files, let's do it per partes
113+
]
114+
"unittests/tools/**" = [
115+
"PTH123", # Fix is needed in unittests/tools as well but there are too many problematic files, let's do it per partes
114116
]
115117
".github/pr-reminder.py" = [
116118
"INP001", # https://docs.astral.sh/ruff/rules/implicit-namespace-package/

unittests/dojo_test_case.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ def import_scan_with_params(self, filename, scan_type="ZAP Scan", engagement=1,
510510
product_name=None, product_type_name=None, auto_create_context=None, expected_http_status_code=201, test_title=None,
511511
scan_date=None, service=None, force_active=True, force_verified=True):
512512

513-
with open(get_unit_tests_path() / filename, encoding="utf-8") as testfile:
513+
with (get_unit_tests_path() / filename).open(encoding="utf-8") as testfile:
514514
payload = {
515515
"minimum_severity": minimum_severity,
516516
"active": active,
@@ -562,7 +562,7 @@ def import_scan_with_params(self, filename, scan_type="ZAP Scan", engagement=1,
562562
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,
563563
tags=None, close_old_findings=True, group_by=None, engagement_name=None, scan_date=None,
564564
product_name=None, product_type_name=None, auto_create_context=None, expected_http_status_code=201, test_title=None):
565-
with open(filename, encoding="utf-8") as testfile:
565+
with Path(filename).open(encoding="utf-8") as testfile:
566566
payload = {
567567
"minimum_severity": minimum_severity,
568568
"active": active,
@@ -611,7 +611,7 @@ def reimport_scan_with_params(self, test_id, filename, scan_type="ZAP Scan", eng
611611
def endpoint_meta_import_scan_with_params(self, filename, product=1, product_name=None, *,
612612
create_endpoints=True, create_tags=True, create_dojo_meta=True,
613613
expected_http_status_code=201):
614-
with open(filename, encoding="utf-8") as testfile:
614+
with Path(filename).open(encoding="utf-8") as testfile:
615615
payload = {
616616
"create_endpoints": create_endpoints,
617617
"create_tags": create_tags,

unittests/test_apiv2_scan_import_options.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from pathlib import Path
2+
13
from django.core.files.uploadedfile import SimpleUploadedFile
24
from django.urls import reverse
35
from rest_framework.authtoken.models import Token
@@ -32,7 +34,7 @@ def setUp(self):
3234
test.save()
3335

3436
def import_zap_scan(self, *, upload_empty_scan=False):
35-
with open("tests/zap_sample.xml", encoding="utf-8") as file:
37+
with Path("tests/zap_sample.xml").open(encoding="utf-8") as file:
3638
if upload_empty_scan:
3739
tested_file = SimpleUploadedFile("zap_sample.xml", self.EMPTY_ZAP_SCAN.encode("utf-8"))
3840
else:

unittests/test_endpoint_meta_import.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from pathlib import Path
23

34
from django.test.client import Client
45
from django.urls import reverse
@@ -206,7 +207,7 @@ def endpoint_meta_import_ui(self, product, payload):
206207

207208
def endpoint_meta_import_scan_with_params_ui(self, filename, product=1, *, create_endpoints=True,
208209
create_tags=True, create_dojo_meta=True, expected_http_status_code=201):
209-
with open(filename, encoding="utf-8") as testfile:
210+
with Path(filename).open(encoding="utf-8") as testfile:
210211
payload = {
211212
"create_endpoints": create_endpoints,
212213
"create_tags": create_tags,

unittests/test_factory.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,25 @@ class TestFactory(DojoTestCase):
1515
def test_get_parser(self):
1616
with self.subTest(scan_type="Acunetix Scan"):
1717
scan_type = "Acunetix Scan"
18-
testfile = open(get_unit_tests_path() / "scans" / "acunetix" / "one_finding.xml", encoding="utf-8")
18+
testfile = (get_unit_tests_path() / "scans" / "acunetix" / "one_finding.xml").open(encoding="utf-8")
1919
parser = get_parser(scan_type)
2020
parser.get_findings(testfile, Test())
2121
testfile.close()
2222
with self.subTest(scan_type="Anchore Engine Scan"):
2323
scan_type = "Anchore Engine Scan"
24-
testfile = open(get_unit_tests_path() / "scans" / "anchore_engine" / "one_vuln.json", encoding="utf-8")
24+
testfile = (get_unit_tests_path() / "scans" / "anchore_engine" / "one_vuln.json").open(encoding="utf-8")
2525
parser = get_parser(scan_type)
2626
parser.get_findings(testfile, Test())
2727
testfile.close()
2828
with self.subTest(scan_type="Tenable Scan"):
2929
scan_type = "Tenable Scan"
30-
testfile = open(get_unit_tests_path() / "scans" / "tenable/nessus" / "nessus_v_unknown.xml", encoding="utf-8")
30+
testfile = (get_unit_tests_path() / "scans" / "tenable/nessus" / "nessus_v_unknown.xml").open(encoding="utf-8")
3131
parser = get_parser(scan_type)
3232
parser.get_findings(testfile, Test())
3333
testfile.close()
3434
with self.subTest(scan_type="ZAP Scan"):
3535
scan_type = "ZAP Scan"
36-
testfile = open(get_unit_tests_path() / "scans" / "zap" / "some_2.9.0.xml", encoding="utf-8")
36+
testfile = (get_unit_tests_path() / "scans" / "zap" / "some_2.9.0.xml").open(encoding="utf-8")
3737
parser = get_parser(scan_type)
3838
parser.get_findings(testfile, Test())
3939
testfile.close()

unittests/test_import_reimport.py

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

33
# from unittest import skip
44
import logging
5+
from pathlib import Path
56

67
from django.test import override_settings
78
from django.test.client import Client
@@ -1794,7 +1795,7 @@ def import_scan_ui(self, engagement, payload):
17941795
response = self.client_ui.post(reverse("import_scan_results", args=(engagement, )), payload)
17951796

17961797
test = Test.objects.get(id=response.url.split("/")[-1])
1797-
# f = open('response.html', 'w+')
1798+
# f = Path('response.html').open('w+')
17981799
# f.write(str(response.content, 'utf-8'))
17991800
# f.close()
18001801
self.assertEqual(302, response.status_code, response.content[:1000])
@@ -1822,7 +1823,7 @@ def import_scan_with_params_ui(self, filename, scan_type="ZAP Scan", engagement=
18221823
elif not verified:
18231824
verifiedPayload = "force_to_false"
18241825

1825-
with open(filename, encoding="utf-8") as testfile:
1826+
with Path(filename).open(encoding="utf-8") as testfile:
18261827
payload = {
18271828
"minimum_severity": minimum_severity,
18281829
"active": activePayload,
@@ -1860,7 +1861,7 @@ def reimport_scan_with_params_ui(self, test_id, filename, scan_type="ZAP Scan",
18601861
if not verified:
18611862
verifiedPayload = "force_to_false"
18621863

1863-
with open(filename, encoding="utf-8") as testfile:
1864+
with Path(filename).open(encoding="utf-8") as testfile:
18641865
payload = {
18651866
"minimum_severity": minimum_severity,
18661867
"active": activePayload,

unittests/test_importers_closeold.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ def test_close_old_same_engagement(self):
3737
"scan_type": scan_type,
3838
}
3939
# Import first test
40-
with open(get_unit_tests_scans_path("acunetix") / "many_findings.xml", "r+", encoding="utf-8") as many_findings_scan:
40+
with (get_unit_tests_scans_path("acunetix") / "many_findings.xml").open("r+", encoding="utf-8") as many_findings_scan:
4141
importer = DefaultImporter(close_old_findings=False, **import_options)
4242
_, _, len_new_findings, len_closed_findings, _, _, _ = importer.process_scan(many_findings_scan)
4343
self.assertEqual(4, len_new_findings)
4444
self.assertEqual(0, len_closed_findings)
4545
# Import same test, should close no findings
46-
with open(get_unit_tests_scans_path("acunetix") / "many_findings.xml", "r+", encoding="utf-8") as many_findings_scan:
46+
with (get_unit_tests_scans_path("acunetix") / "many_findings.xml").open("r+", encoding="utf-8") as many_findings_scan:
4747
importer = DefaultImporter(close_old_findings=True, **import_options)
4848
_, _, len_new_findings, len_closed_findings, _, _, _ = importer.process_scan(many_findings_scan)
4949
self.assertEqual(4, len_new_findings)
5050
self.assertEqual(0, len_closed_findings)
5151
# Import test with only one finding. Remaining findings should close
52-
with open(get_unit_tests_scans_path("acunetix") / "one_finding.xml", "r+", encoding="utf-8") as single_finding_scan:
52+
with (get_unit_tests_scans_path("acunetix") / "one_finding.xml").open("r+", encoding="utf-8") as single_finding_scan:
5353
importer = DefaultImporter(close_old_findings=True, **import_options)
5454
_, _, len_new_findings, len_closed_findings, _, _, _ = importer.process_scan(single_finding_scan)
5555
self.assertEqual(1, len_new_findings)
@@ -95,19 +95,19 @@ def test_close_old_same_product_scan(self):
9595
"scan_type": scan_type,
9696
}
9797
# Import first test
98-
with open(get_unit_tests_scans_path("acunetix") / "many_findings.xml", "r+", encoding="utf-8") as many_findings_scan:
98+
with (get_unit_tests_scans_path("acunetix") / "many_findings.xml").open("r+", encoding="utf-8") as many_findings_scan:
9999
importer = DefaultImporter(engagement=engagement1, close_old_findings=False, **import_options)
100100
_, _, len_new_findings, len_closed_findings, _, _, _ = importer.process_scan(many_findings_scan)
101101
self.assertEqual(4, len_new_findings)
102102
self.assertEqual(0, len_closed_findings)
103103
# Import same test, should close no findings
104-
with open(get_unit_tests_scans_path("acunetix") / "many_findings.xml", "r+", encoding="utf-8") as many_findings_scan:
104+
with (get_unit_tests_scans_path("acunetix") / "many_findings.xml").open("r+", encoding="utf-8") as many_findings_scan:
105105
importer = DefaultImporter(engagement=engagement2, close_old_findings=True, **import_options)
106106
_, _, len_new_findings, len_closed_findings, _, _, _ = importer.process_scan(many_findings_scan)
107107
self.assertEqual(4, len_new_findings)
108108
self.assertEqual(0, len_closed_findings)
109109
# Import test with only one finding. Remaining findings should close
110-
with open(get_unit_tests_scans_path("acunetix") / "one_finding.xml", "r+", encoding="utf-8") as single_finding_scan:
110+
with (get_unit_tests_scans_path("acunetix") / "one_finding.xml").open("r+", encoding="utf-8") as single_finding_scan:
111111
importer = DefaultImporter(engagement=engagement3, close_old_findings=True, **import_options)
112112
_, _, len_new_findings, len_closed_findings, _, _, _ = importer.process_scan(single_finding_scan)
113113
self.assertEqual(1, len_new_findings)
@@ -153,20 +153,20 @@ def test_close_old_same_product_scan_matching_with_unique_id_from_tool(self):
153153
"scan_type": scan_type,
154154
}
155155
# Import first test
156-
with open(get_unit_tests_scans_path("semgrep") / "close_old_findings_report_line31.json", "r+", encoding="utf-8") as many_findings_scan:
156+
with (get_unit_tests_scans_path("semgrep") / "close_old_findings_report_line31.json").open("r+", encoding="utf-8") as many_findings_scan:
157157
importer = DefaultImporter(engagement=engagement1, close_old_findings=False, **import_options)
158158
_, _, len_new_findings, len_closed_findings, _, _, _ = importer.process_scan(many_findings_scan)
159159
self.assertEqual(1, len_new_findings)
160160
self.assertEqual(0, len_closed_findings)
161161
# Import separate report with different line number. Before this change, the legacy dedupe algorithm would calculate a different
162162
# hash code and close of the findings. Now that we are matching on unique ID from tool, we should no close anything, and create one
163-
with open(get_unit_tests_scans_path("semgrep") / "close_old_findings_report_second_run_line24.json", "r+", encoding="utf-8") as many_findings_scan:
163+
with (get_unit_tests_scans_path("semgrep") / "close_old_findings_report_second_run_line24.json").open("r+", encoding="utf-8") as many_findings_scan:
164164
importer = DefaultImporter(engagement=engagement2, close_old_findings=True, **import_options)
165165
_, _, len_new_findings, len_closed_findings, _, _, _ = importer.process_scan(many_findings_scan)
166166
self.assertEqual(1, len_new_findings)
167167
self.assertEqual(0, len_closed_findings)
168168
# This scan has a different unique ID from tool, so we should have one new finding, and one closed finding
169-
with open(get_unit_tests_scans_path("semgrep") / "close_old_findings_report_third_run_different_unique_id.json", "r+", encoding="utf-8") as many_findings_scan:
169+
with (get_unit_tests_scans_path("semgrep") / "close_old_findings_report_third_run_different_unique_id.json").open("r+", encoding="utf-8") as many_findings_scan:
170170
importer = DefaultImporter(engagement=engagement3, close_old_findings=True, **import_options)
171171
_, _, len_new_findings, len_closed_findings, _, _, _ = importer.process_scan(many_findings_scan)
172172
self.assertEqual(1, len_new_findings)

unittests/test_importers_importer.py

Lines changed: 3 additions & 3 deletions
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 (get_unit_tests_path() / "scans" / "acunetix" / "one_finding.xml").open(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 (get_unit_tests_path() / "scans" / "sarif" / "spotbugs.sarif").open(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")
@@ -114,7 +114,7 @@ def test_import_scan(self):
114114
self.assertEqual(0, len_closed_findings)
115115

116116
def test_import_scan_without_test_scan_type(self):
117-
with open(get_unit_tests_scans_path("gitlab_sast") / "gl-sast-report-1-vuln_v15.json", encoding="utf-8") as scan:
117+
with (get_unit_tests_scans_path("gitlab_sast") / "gl-sast-report-1-vuln_v15.json").open(encoding="utf-8") as scan:
118118
# GitLabSastParser implements get_tests but report has no scanner name
119119
scan_type = GitlabSastParser().get_scan_types()[0]
120120
user, _ = User.objects.get_or_create(username="admin")

unittests/test_parsers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def test_file_existence(self):
8585
if file.is_file() and file.name != "__pycache__" and file.name != "__init__.py":
8686
f_path = Path(basedir) / "dojo" / "tools" / parser_dir.name / file.name
8787
read_true = False
88-
with open(f_path, encoding="utf-8") as f:
88+
with f_path.open(encoding="utf-8") as f:
8989
i = 0
9090
for line in f:
9191
if read_true is True:

0 commit comments

Comments
 (0)