Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruff: Add and fix SIM117 #11653

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ ignore = [
"SIM102",
"SIM113",
"SIM115",
"SIM117",
"RUF012",
"RUF015",
"D205",
Expand Down
8 changes: 4 additions & 4 deletions unittests/tools/test_anchore_enterprise_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ def test_anchore_policy_check_parser_has_multiple_findings(self):
self.assertEqual("CVE-2015-2992", finding.unsaved_vulnerability_ids[0])

def test_anchore_policy_check_parser_invalid_format(self):
with open(get_unit_tests_scans_path("anchore_enterprise") / "invalid_checks_format.json", encoding="utf-8") as testfile:
with self.assertRaises(Exception):
parser = AnchoreEnterpriseParser()
parser.get_findings(testfile, Test())
with open(get_unit_tests_scans_path("anchore_enterprise") / "invalid_checks_format.json", encoding="utf-8") as testfile, \
self.assertRaises(Exception):
parser = AnchoreEnterpriseParser()
parser.get_findings(testfile, Test())

def test_anchore_policy_check_extract_vulnerability_id(self):
vulnerability_id = extract_vulnerability_id("CVE-2019-14540+openapi-generator-cli-4.0.0.jar:jackson-databind")
Expand Down
8 changes: 4 additions & 4 deletions unittests/tools/test_auditjs_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ def test_auditjs_parser_with_many_vuln_has_many_findings(self):
self.assertEqual(400, findings[4].cwe)

def test_auditjs_parser_empty_with_error(self):
with self.assertRaises(ValueError) as context:
with open(get_unit_tests_scans_path("auditjs") / "empty_with_error.json", encoding="utf-8") as testfile:
parser = AuditJSParser()
parser.get_findings(testfile, Test())
with self.assertRaises(ValueError) as context, \
open(get_unit_tests_scans_path("auditjs") / "empty_with_error.json", encoding="utf-8") as testfile:
parser = AuditJSParser()
parser.get_findings(testfile, Test())

self.assertIn(
"Invalid JSON format. Are you sure you used --json option ?", str(context.exception),
Expand Down
16 changes: 8 additions & 8 deletions unittests/tools/test_aws_inspector2_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ def test_aws_inspector2_parser_with_many_vuln_has_many_findings(self):
self.assertEqual(8, len(findings))

def test_aws_inspector2_parser_empty_with_error(self):
with self.assertRaises(TypeError) as context:
with open(get_unit_tests_scans_path("aws_inspector2") / "empty_with_error.json", encoding="utf-8") as testfile:
parser = AWSInspector2Parser()
parser.get_findings(testfile, Test())
testfile.close()
self.assertTrue(
"Incorrect Inspector2 report format" in str(context.exception),
)
with self.assertRaises(TypeError) as context, \
open(get_unit_tests_scans_path("aws_inspector2") / "empty_with_error.json", encoding="utf-8") as testfile:
parser = AWSInspector2Parser()
parser.get_findings(testfile, Test())
testfile.close()
self.assertTrue(
"Incorrect Inspector2 report format" in str(context.exception),
)
9 changes: 4 additions & 5 deletions unittests/tools/test_burp_graphql_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,10 @@ def test_burp_no_findings(self):
self.assertEqual(0, len(findings))

def test_burp_null_title(self):
with open(get_unit_tests_scans_path("burp_graphql") / "null_title.json", encoding="utf-8") as test_file:

with self.assertRaises(ValueError):
parser = BurpGraphQLParser()
parser.get_findings(test_file, Test())
with open(get_unit_tests_scans_path("burp_graphql") / "null_title.json", encoding="utf-8") as test_file, \
self.assertRaises(ValueError):
parser = BurpGraphQLParser()
parser.get_findings(test_file, Test())

def test_burp_null_request_segments(self):
with open(get_unit_tests_scans_path("burp_graphql") / "null_request_segments.json", encoding="utf-8") as test_file:
Expand Down
8 changes: 4 additions & 4 deletions unittests/tools/test_coverity_api_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

class TestZapParser(DojoTestCase):
def test_parse_wrong_file(self):
with self.assertRaises(ValueError):
with open(get_unit_tests_scans_path("coverity_api") / "wrong.json", encoding="utf-8") as testfile:
parser = CoverityApiParser()
parser.get_findings(testfile, Test())
with self.assertRaises(ValueError), \
open(get_unit_tests_scans_path("coverity_api") / "wrong.json", encoding="utf-8") as testfile:
parser = CoverityApiParser()
parser.get_findings(testfile, Test())

def test_parse_no_findings(self):
with open(get_unit_tests_scans_path("coverity_api") / "empty.json", encoding="utf-8") as testfile:
Expand Down
8 changes: 4 additions & 4 deletions unittests/tools/test_gitlab_api_fuzzing_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def test_gitlab_api_fuzzing_parser_with_one_criticle_vuln_has_one_findings_v15(s
)

def test_gitlab_api_fuzzing_parser_with_invalid_json(self):
with open(get_unit_tests_scans_path("gitlab_api_fuzzing") / "gitlab_api_fuzzing_invalid.json", encoding="utf-8") as testfile:
with open(get_unit_tests_scans_path("gitlab_api_fuzzing") / "gitlab_api_fuzzing_invalid.json", encoding="utf-8") as testfile, \
self.assertRaises((KeyError, ValueError)):
# Something is wrong with JSON file
with self.assertRaises((KeyError, ValueError)):
parser = GitlabAPIFuzzingParser()
parser.get_findings(testfile, Test())
parser = GitlabAPIFuzzingParser()
parser.get_findings(testfile, Test())
8 changes: 4 additions & 4 deletions unittests/tools/test_govulncheck_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
class TestGovulncheckParser(DojoTestCase):

def test_parse_empty(self):
with self.assertRaises(ValueError) as exp:
with open(get_unit_tests_scans_path("govulncheck") / "empty.json", encoding="utf-8") as testfile:
parser = GovulncheckParser()
parser.get_findings(testfile, Test())
with self.assertRaises(ValueError) as exp, \
open(get_unit_tests_scans_path("govulncheck") / "empty.json", encoding="utf-8") as testfile:
parser = GovulncheckParser()
parser.get_findings(testfile, Test())
self.assertIn(
"Invalid JSON format", str(exp.exception),
)
Expand Down
8 changes: 4 additions & 4 deletions unittests/tools/test_intsights_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ def test_intsights_parser_with_many_vuln_has_many_findings_csv(self):
self.assertEqual(9, len(findings))

def test_intsights_parser_invalid_text_with_error_csv(self):
with self.assertRaises(ValueError):
with open(get_unit_tests_scans_path("intsights") / "intsights_invalid_file.txt", encoding="utf-8") as testfile:
parser = IntSightsParser()
parser.get_findings(testfile, Test())
with self.assertRaises(ValueError), \
open(get_unit_tests_scans_path("intsights") / "intsights_invalid_file.txt", encoding="utf-8") as testfile:
parser = IntSightsParser()
parser.get_findings(testfile, Test())

def test_intsights_parser_with_no_alerts_json(self):
with open(get_unit_tests_scans_path("intsights") / "intsights_zero_vuln.json", encoding="utf-8") as testfile:
Expand Down
8 changes: 4 additions & 4 deletions unittests/tools/test_kubehunter_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ def test_kubehunter_parser_with_many_vuln_has_many_findings(self):
self.assertEqual(8, len(findings))

def test_kubehunter_parser_empty_with_error(self):
with self.assertRaises(ValueError) as context:
with open(get_unit_tests_scans_path("kubehunter") / "empty.json", encoding="utf-8") as testfile:
parser = KubeHunterParser()
parser.get_findings(testfile, Test())
with self.assertRaises(ValueError) as context, \
open(get_unit_tests_scans_path("kubehunter") / "empty.json", encoding="utf-8") as testfile:
parser = KubeHunterParser()
parser.get_findings(testfile, Test())

self.assertEqual(
"Expecting value: line 1 column 1 (char 0)", str(context.exception),
Expand Down
8 changes: 4 additions & 4 deletions unittests/tools/test_meterian_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
class TestMeterianParser(DojoTestCase):

def test_meterianParser_invalid_security_report_raise_ValueError_exception(self):
with self.assertRaises(ValueError):
with open(get_unit_tests_scans_path("meterian") / "report_invalid.json", encoding="utf-8") as testfile:
parser = MeterianParser()
parser.get_findings(testfile, Test())
with self.assertRaises(ValueError), \
open(get_unit_tests_scans_path("meterian") / "report_invalid.json", encoding="utf-8") as testfile:
parser = MeterianParser()
parser.get_findings(testfile, Test())

def test_meterianParser_report_has_no_finding(self):
with open(get_unit_tests_scans_path("meterian") / "report_no_vulns.json", encoding="utf-8") as testfile:
Expand Down
20 changes: 10 additions & 10 deletions unittests/tools/test_noseyparker_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ def test_noseyparker_parser_many_vulns(self):
self.assertEqual(3, len(findings))

def test_noseyparker_parser_error(self):
with self.assertRaises(ValueError) as context:
with open(get_unit_tests_scans_path("noseyparker") / "empty_with_error.json", encoding="utf-8") as testfile:
parser = NoseyParkerParser()
findings = parser.get_findings(testfile, Test())
testfile.close()
self.assertEqual(0, len(findings))
self.assertIn(
"Invalid Nosey Parker data, make sure to use Nosey Parker v0.16.0", str(context.exception),
)
self.assertIn("ECONNREFUSED", str(context.exception))
with self.assertRaises(ValueError) as context, \
open(get_unit_tests_scans_path("noseyparker") / "empty_with_error.json", encoding="utf-8") as testfile:
parser = NoseyParkerParser()
findings = parser.get_findings(testfile, Test())
testfile.close()
self.assertEqual(0, len(findings))
self.assertIn(
"Invalid Nosey Parker data, make sure to use Nosey Parker v0.16.0", str(context.exception),
)
self.assertIn("ECONNREFUSED", str(context.exception))

def test_noseyparker_version_0_22_0(self):
with open("unittests/scans/noseyparker/noseyparker_0_22_0.jsonl", encoding="utf-8") as testfile:
Expand Down
16 changes: 8 additions & 8 deletions unittests/tools/test_npm_audit_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,19 @@ def test_npm_audit_parser_with_one_criticle_vuln_has_null_as_cwe(self):
self.assertEqual("1.9.2", findings[0].component_version)

def test_npm_audit_parser_empty_with_error(self):
with self.assertRaises(ValueError) as context:
with open(get_unit_tests_scans_path("npm_audit") / "empty_with_error.json", encoding="utf-8") as testfile:
parser = NpmAuditParser()
parser.get_findings(testfile, Test())
with self.assertRaises(ValueError) as context, \
open(get_unit_tests_scans_path("npm_audit") / "empty_with_error.json", encoding="utf-8") as testfile:
parser = NpmAuditParser()
parser.get_findings(testfile, Test())

self.assertIn("npm audit report contains errors:", str(context.exception))
self.assertIn("ENOAUDIT", str(context.exception))

def test_npm_audit_parser_many_vuln_npm7(self):
with self.assertRaises(ValueError) as context:
with open(get_unit_tests_scans_path("npm_audit") / "many_vuln_npm7.json", encoding="utf-8") as testfile:
parser = NpmAuditParser()
parser.get_findings(testfile, Test())
with self.assertRaises(ValueError) as context, \
open(get_unit_tests_scans_path("npm_audit") / "many_vuln_npm7.json", encoding="utf-8") as testfile:
parser = NpmAuditParser()
parser.get_findings(testfile, Test())

self.assertIn("npm7 with auditReportVersion 2 or higher not yet supported", str(context.exception))

Expand Down
16 changes: 8 additions & 8 deletions unittests/tools/test_risk_recon_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
class TestRiskReconAPIParser(DojoTestCase):

def test_api_with_bad_url(self):
with open(get_unit_tests_scans_path("risk_recon") / "bad_url.json", encoding="utf-8") as testfile:
with self.assertRaises(Exception):
parser = RiskReconParser()
parser.get_findings(testfile, Test())
with open(get_unit_tests_scans_path("risk_recon") / "bad_url.json", encoding="utf-8") as testfile, \
self.assertRaises(Exception):
parser = RiskReconParser()
parser.get_findings(testfile, Test())

def test_api_with_bad_key(self):
with open(get_unit_tests_scans_path("risk_recon") / "bad_key.json", encoding="utf-8") as testfile:
with self.assertRaises(Exception):
parser = RiskReconParser()
parser.get_findings(testfile, Test())
with open(get_unit_tests_scans_path("risk_recon") / "bad_key.json", encoding="utf-8") as testfile, \
self.assertRaises(Exception):
parser = RiskReconParser()
parser.get_findings(testfile, Test())

def test_parser_without_api(self):
with open(get_unit_tests_scans_path("risk_recon") / "findings.json", encoding="utf-8") as testfile:
Expand Down
28 changes: 14 additions & 14 deletions unittests/tools/test_sysdig_reports_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,25 @@ def test_sysdig_parser_with_many_vuln_has_many_findings(self):
self.assertEqual(50, len(findings))

def test_sysdig_parser_missing_cve_field_id_from_csv_file(self):
with self.assertRaises(ValueError) as context:
with open(get_unit_tests_scans_path("sysdig_reports") / "sysdig_reports_missing_cve_field.csv", encoding="utf-8") as testfile:
parser = SysdigReportsParser()
findings = parser.get_findings(testfile, Test())
for finding in findings:
for endpoint in finding.unsaved_endpoints:
endpoint.clean()
with self.assertRaises(ValueError) as context, \
open(get_unit_tests_scans_path("sysdig_reports") / "sysdig_reports_missing_cve_field.csv", encoding="utf-8") as testfile:
parser = SysdigReportsParser()
findings = parser.get_findings(testfile, Test())
for finding in findings:
for endpoint in finding.unsaved_endpoints:
endpoint.clean()
self.assertEqual(
"Number of fields in row (22) does not match number of headers (21)", str(context.exception),
)

def test_sysdig_parser_missing_cve_field_not_starting_with_cve(self):
with self.assertRaises(ValueError) as context:
with open(get_unit_tests_scans_path("sysdig_reports") / "sysdig_reports_not_starting_with_cve.csv", encoding="utf-8") as testfile:
parser = SysdigReportsParser()
findings = parser.get_findings(testfile, Test())
for finding in findings:
for endpoint in finding.unsaved_endpoints:
endpoint.clean()
with self.assertRaises(ValueError) as context, \
open(get_unit_tests_scans_path("sysdig_reports") / "sysdig_reports_not_starting_with_cve.csv", encoding="utf-8") as testfile:
parser = SysdigReportsParser()
findings = parser.get_findings(testfile, Test())
for finding in findings:
for endpoint in finding.unsaved_endpoints:
endpoint.clean()
self.assertEqual(
"Number of fields in row (22) does not match number of headers (21)", str(context.exception),
)
Expand Down
8 changes: 4 additions & 4 deletions unittests/tools/test_threat_composer_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ def test_threat_composer_parser_with_many_threats_has_many_findings(self):
self.assertEqual(21, len(findings))

def test_threat_composer_parser_empty_with_error(self):
with self.assertRaises(ValueError) as context:
with open(sample_path("threat_composer_no_threats_with_error.json"), encoding="utf-8") as testfile:
parser = ThreatComposerParser()
parser.get_findings(testfile, Test())
with self.assertRaises(ValueError) as context, \
open(sample_path("threat_composer_no_threats_with_error.json"), encoding="utf-8") as testfile:
parser = ThreatComposerParser()
parser.get_findings(testfile, Test())

self.assertNotIn("No threats found in the JSON file", str(context.exception))

Expand Down
16 changes: 8 additions & 8 deletions unittests/tools/test_whitehat_sentinel_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
class TestWhiteHatSentinelParser(DojoTestCase):

def test_parse_file_with_no_vuln_has_no_findings(self):
with self.assertRaises(ValueError):
with open(get_unit_tests_scans_path("whitehat_sentinel") / "empty_file.json", encoding="utf-8") as testfile:
parser = WhiteHatSentinelParser()
parser.get_findings(testfile, Test())
with self.assertRaises(ValueError), \
open(get_unit_tests_scans_path("whitehat_sentinel") / "empty_file.json", encoding="utf-8") as testfile:
parser = WhiteHatSentinelParser()
parser.get_findings(testfile, Test())

def test_parse_file_with_one_vuln_has_one_findings(self):
with open(get_unit_tests_scans_path("whitehat_sentinel") / "one_vuln.json", encoding="utf-8") as testfile:
Expand All @@ -24,7 +24,7 @@ def test_parse_file_with_multiple_vuln_has_multiple_finding(self):
self.assertEqual(3, len(findings))

def test_parse_file_with_invalid_data(self):
with self.assertRaises(ValueError):
with open(get_unit_tests_scans_path("whitehat_sentinel") / "invalid_data.txt", encoding="utf-8") as testfile:
parser = WhiteHatSentinelParser()
parser.get_findings(testfile, Test())
with self.assertRaises(ValueError), \
open(get_unit_tests_scans_path("whitehat_sentinel") / "invalid_data.txt", encoding="utf-8") as testfile:
parser = WhiteHatSentinelParser()
parser.get_findings(testfile, Test())
16 changes: 8 additions & 8 deletions unittests/tools/test_yarn_audit_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ def test_yarn_audit_parser_with_multiple_cwes_per_finding_list(self):
self.assertEqual(findings[1].unsaved_vulnerability_ids[0], "CVE-2021-3807")

def test_yarn_audit_parser_empty_with_error(self):
with self.assertRaises(ValueError) as context:
with open(get_unit_tests_scans_path("yarn_audit") / "empty_with_error.json", encoding="utf-8") as testfile:
parser = YarnAuditParser()
parser.get_findings(testfile, self.get_test())
self.assertIn(
"yarn audit report contains errors:", str(context.exception),
)
self.assertIn("ECONNREFUSED", str(context.exception))
with self.assertRaises(ValueError) as context, \
open(get_unit_tests_scans_path("yarn_audit") / "empty_with_error.json", encoding="utf-8") as testfile:
parser = YarnAuditParser()
parser.get_findings(testfile, self.get_test())
self.assertIn(
"yarn audit report contains errors:", str(context.exception),
)
self.assertIn("ECONNREFUSED", str(context.exception))

def test_yarn_audit_parser_issue_6495(self):
with open(get_unit_tests_scans_path("yarn_audit") / "issue_6495.json", encoding="utf-8") as testfile:
Expand Down