Skip to content
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ ruff = ">=0.5.6,<0.15.0"
autohooks-plugin-ruff = ">=24.1,<26.0"

[tool.black]
line-length = 80
line-length = 100
target-version = ['py310', 'py311', 'py312']
exclude = '''
/(
Expand Down
20 changes: 5 additions & 15 deletions tests/helper/test_if_block_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,14 @@ def test_nested_if_statements(self):
self.assertIn('display("nested block")', result.statements[1].outcome)

self.assertEqual("inner2", result.statements[2].condition)
self.assertEqual(
'display("nested inline")', result.statements[2].outcome
)
self.assertEqual('display("nested inline")', result.statements[2].outcome)

def test_complex_condition(self):
content = (
'if (a == 1 && b > 2 || c != "string") { display("complex"); }'
)
content = 'if (a == 1 && b > 2 || c != "string") { display("complex"); }'
result = find_if_statements(content)

self.assertEqual(1, len(result.statements))
self.assertEqual(
'a == 1 && b > 2 || c != "string"', result.statements[0].condition
)
self.assertEqual('a == 1 && b > 2 || c != "string"', result.statements[0].condition)
self.assertEqual('display("complex");', result.statements[0].outcome)

def test_if_with_problematic_stuff(self):
Expand Down Expand Up @@ -151,9 +145,7 @@ def test_useless_semicolon(self):
result = find_if_statements(content)
self.assertEqual(len(result.statements), 0)
self.assertEqual(len(result.errors), 1)
self.assertEqual(
result.errors[0].error_type.name, "TERMINATED_AFTER_CONDITION"
)
self.assertEqual(result.errors[0].error_type.name, "TERMINATED_AFTER_CONDITION")

def test_unclosed_block_brace(self):
content = "if(condition) {\ndisplay();\n# Missing closing brace"
Expand Down Expand Up @@ -226,9 +218,7 @@ def test_mixed_valid_and_invalid_if_statements(self):
IfErrorType.TERMINATED_AFTER_CONDITION,
result.errors[0].error_type,
)
self.assertEqual(
IfErrorType.UNCLOSED_CONDITION, result.errors[1].error_type
)
self.assertEqual(IfErrorType.UNCLOSED_CONDITION, result.errors[1].error_type)

def test_multiple_nested_inline_if_statements(self):
content = """
Expand Down
24 changes: 6 additions & 18 deletions tests/helper/test_linguistic_exception_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ def test_patterns_in_file_check(self):
self.assertEqual(check.execute("test", "hello3hello"), False)

def test_patterns_tuple_in_file_check(self):
check = PatternsInFileCheck(
"test", [(r"test\d", re.IGNORECASE), (r"hello\dtest", 0)]
)
check = PatternsInFileCheck("test", [(r"test\d", re.IGNORECASE), (r"hello\dtest", 0)])

self.assertEqual(check.execute("test", "test1"), True)
self.assertEqual(check.execute("test", "TEST2"), True)
Expand All @@ -158,9 +156,7 @@ def test_pattern_in_file_pattern_check(self):
self.assertEqual(check.execute("foo1", "test1"), False)

def test_pattern_in_file_pattern_flags_check(self):
check = PatternInFilePatternCheck(
r"test|hello", r"foo|bar", re.IGNORECASE, re.IGNORECASE
)
check = PatternInFilePatternCheck(r"test|hello", r"foo|bar", re.IGNORECASE, re.IGNORECASE)

self.assertEqual(check.execute("test1", "foo1"), True)
self.assertEqual(check.execute("test1", "FOO2"), True)
Expand Down Expand Up @@ -200,15 +196,7 @@ def test_patterns_in_file_pattern_flags_check(self):
def test_linguistic_exception_handler(self):
checks = [FileCheck("test"), TextCheck("foo")]

self.assertEqual(
handle_linguistic_checks("test1", "foo1", checks), True
)
self.assertEqual(
handle_linguistic_checks("test1", "bar1", checks), True
)
self.assertEqual(
handle_linguistic_checks("hello1", "foo1", checks), True
)
self.assertEqual(
handle_linguistic_checks("hello1", "bar1", checks), False
)
self.assertEqual(handle_linguistic_checks("test1", "foo1", checks), True)
self.assertEqual(handle_linguistic_checks("test1", "bar1", checks), True)
self.assertEqual(handle_linguistic_checks("hello1", "foo1", checks), True)
self.assertEqual(handle_linguistic_checks("hello1", "bar1", checks), False)
8 changes: 2 additions & 6 deletions tests/helper/test_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ def test_same_patterns_instance(self):
self.assertIs(patterns1, patterns2)

def test_same_pattern_instance(self):
pattern1 = get_special_script_tag_pattern(
SpecialScriptTag.ADD_PREFERENCE
)
pattern2 = get_special_script_tag_pattern(
SpecialScriptTag.ADD_PREFERENCE
)
pattern1 = get_special_script_tag_pattern(SpecialScriptTag.ADD_PREFERENCE)
pattern2 = get_special_script_tag_pattern(SpecialScriptTag.ADD_PREFERENCE)

self.assertIs(pattern1, pattern2)
20 changes: 7 additions & 13 deletions tests/helper/test_remove_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ def test_empty_string(self):
self.assertEqual(remove_comments(input_content), expected_output)

def test_no_comments(self):
input_content = (
"function detect_archlinux(sock, port, SCRIPT_DESC, is_pfsense) {"
)
input_content = "function detect_archlinux(sock, port, SCRIPT_DESC, is_pfsense) {"
expected_output = input_content
self.assertEqual(remove_comments(input_content), expected_output)

Expand All @@ -25,14 +23,14 @@ def test_full_line_comments(self):
self.assertEqual(remove_comments(input_content), expected_output)

def test_inline_comments(self):
input_content = (
"function hello(){ # A function\n return 42; # The answer"
)
input_content = "function hello(){ # A function\n return 42; # The answer"
expected_output = "function hello(){ \n return 42; "
self.assertEqual(remove_comments(input_content), expected_output)

def test_hash_in_strings(self):
input_content = "message = 'This # is not a comment';\nurl = \"http://example.com/#anchor\";"
input_content = (
"message = 'This # is not a comment';\nurl = \"http://example.com/#anchor\";"
)
expected_output = input_content
self.assertEqual(remove_comments(input_content), expected_output)

Expand All @@ -41,9 +39,7 @@ def test_mixed_quotes_and_comments(self):
"display('Hash: #'); # Real comment\n"
's = "My string with # character"; # Another comment'
)
expected_output = (
"display('Hash: #'); \ns = \"My string with # character\"; "
)
expected_output = "display('Hash: #'); \ns = \"My string with # character\"; "
self.assertEqual(remove_comments(input_content), expected_output)

def test_complex_scenario(self):
Expand All @@ -69,9 +65,7 @@ def test_complex_scenario(self):
self.assertEqual(remove_comments(input_content), expected_output)

def test_indented_comments(self):
input_content = (
"function func() {\n # Indented comment\n return 0;\n}"
)
input_content = "function func() {\n # Indented comment\n return 0;\n}"
expected_output = "function func() {\n\n return 0;\n}"
self.assertEqual(remove_comments(input_content), expected_output)

Expand Down
4 changes: 1 addition & 3 deletions tests/helper/test_text_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ def test_position_after_string(self):
def test_escaped_quote_in_single_quotes(self):
text = "x = 'don\\'t';"
self.assertTrue(is_position_in_string(text, 8)) # inside the string
self.assertTrue(
is_position_in_string(text, 10)
) # after the escaped quote
self.assertTrue(is_position_in_string(text, 10)) # after the escaped quote

def test_mixed_quotes(self):
text = 'a = "it\'s ok"; b = \'say "hi"\';'
Expand Down
8 changes: 2 additions & 6 deletions tests/plugins/test_badwords.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ def test_combined(self):
path = Path("some/find_service3.nasl")
content = " OpenVAS-8 and probably prior\nOpenVAS-9"

fake_context = self.create_file_plugin_context(
nasl_file=path, lines=content.splitlines()
)
fake_context = self.create_file_plugin_context(nasl_file=path, lines=content.splitlines())

plugin = CheckBadwords(fake_context)
results = list(plugin.run())
Expand All @@ -72,9 +70,7 @@ def test_exception_ok(self):
"https://www.invt.com/software-download\n"
)

fake_context = self.create_file_plugin_context(
nasl_file=path, lines=content.splitlines()
)
fake_context = self.create_file_plugin_context(nasl_file=path, lines=content.splitlines())

plugin = CheckBadwords(fake_context)
results = list(plugin.run())
Expand Down
19 changes: 5 additions & 14 deletions tests/plugins/test_copyright_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ def test_ok(self):
"right holder(s).\n"
' script_copyright("Copyright (C) 1234");\n'
)
fake_context = self.create_file_plugin_context(
nasl_file=path, file_content=content
)
fake_context = self.create_file_plugin_context(nasl_file=path, file_content=content)

plugin = CheckCopyrightText(fake_context)

Expand All @@ -75,9 +73,7 @@ def test_missing_statement(self):
"# advisory, and are Copyright (C) the respective author(s)\n"
' script_copyright("Copyright (C) 134");\n'
)
fake_context = self.create_file_plugin_context(
nasl_file=path, file_content=content
)
fake_context = self.create_file_plugin_context(nasl_file=path, file_content=content)

plugin = CheckCopyrightText(fake_context)

Expand All @@ -99,9 +95,7 @@ def test_wrong_copyright_text(self):
f"{wrong_text}"
' script_copyright("Copyright (C) 1234");\n'
)
fake_context = self.create_file_plugin_context(
nasl_file=path, file_content=content
)
fake_context = self.create_file_plugin_context(nasl_file=path, file_content=content)

plugin = CheckCopyrightText(fake_context)

Expand All @@ -125,9 +119,7 @@ def test_fix_wrong_copyright_text(self):
)
path.write_text(content, encoding=CURRENT_ENCODING)

fake_context = self.create_file_plugin_context(
nasl_file=path, file_content=content
)
fake_context = self.create_file_plugin_context(nasl_file=path, file_content=content)

plugin = CheckCopyrightText(fake_context)

Expand All @@ -138,7 +130,6 @@ def test_fix_wrong_copyright_text(self):

self.assertIsInstance(results[0], LinterFix)
self.assertEqual(
"The copyright statement has been updated to "
f"{CORRECT_COPYRIGHT_PHRASE}",
"The copyright statement has been updated to " f"{CORRECT_COPYRIGHT_PHRASE}",
results[0].message,
)
47 changes: 12 additions & 35 deletions tests/plugins/test_copyright_year.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ def test_ok_new_header(self):
' script_copyright("Copyright (C) 2022 Greenbone AG");\n'
)

fake_context = self.create_file_plugin_context(
nasl_file=path, file_content=content
)
fake_context = self.create_file_plugin_context(nasl_file=path, file_content=content)
plugin = CheckCopyrightYear(fake_context)

results = list(plugin.run())
Expand All @@ -50,9 +48,7 @@ def test_ok_old_header(self):
'+0200 (Tue, 14 May 2022)");\n'
' script_copyright("Copyright (C) 2022 Greenbone AG");\n'
)
fake_context = self.create_file_plugin_context(
nasl_file=path, file_content=content
)
fake_context = self.create_file_plugin_context(nasl_file=path, file_content=content)
plugin = CheckCopyrightYear(fake_context)

results = list(plugin.run())
Expand All @@ -70,9 +66,7 @@ def test_pre2008_ok_new_header(self):
'+0200 (Tue, 14 May 2022)");\n'
' script_copyright("Copyright (C) 2020 Greenbone AG");\n'
)
fake_context = self.create_file_plugin_context(
nasl_file=path, file_content=content
)
fake_context = self.create_file_plugin_context(nasl_file=path, file_content=content)
plugin = CheckCopyrightYear(fake_context)

results = list(plugin.run())
Expand All @@ -86,9 +80,7 @@ def test_pre2008_ok_old_header(self):
'+0200 (Tue, 14 May 2022)");\n'
' script_copyright("Copyright (C) 2020 Greenbone AG");\n'
)
fake_context = self.create_file_plugin_context(
nasl_file=path, file_content=content
)
fake_context = self.create_file_plugin_context(nasl_file=path, file_content=content)
plugin = CheckCopyrightYear(fake_context)

results = list(plugin.run())
Expand All @@ -110,16 +102,12 @@ def test_missing_creation_date(self):
' script_copyright("Copyright (C) 2022 Greenbone AG");\n'
)

fake_context = self.create_file_plugin_context(
nasl_file=path, file_content=content
)
fake_context = self.create_file_plugin_context(nasl_file=path, file_content=content)
plugin = CheckCopyrightYear(fake_context)

results = list(plugin.run())
self.assertEqual(len(results), 1)
self.assertEqual(
"Missing creation_date statement in VT", results[0].message
)
self.assertEqual("Missing creation_date statement in VT", results[0].message)

def test_missing_copyright_tag(self):
path = Path("some/file.nasl")
Expand All @@ -129,9 +117,7 @@ def test_missing_copyright_tag(self):
'+0200 (Tue, 14 May 2022)");\n'
)

fake_context = self.create_file_plugin_context(
nasl_file=path, file_content=content
)
fake_context = self.create_file_plugin_context(nasl_file=path, file_content=content)
plugin = CheckCopyrightYear(fake_context)

results = list(plugin.run())
Expand All @@ -147,9 +133,7 @@ def test_regex_fail_copyright_tag(self):
' script_copyright("Copyright (C) Greenbone AG");\n'
)

fake_context = self.create_file_plugin_context(
nasl_file=path, file_content=content
)
fake_context = self.create_file_plugin_context(nasl_file=path, file_content=content)
plugin = CheckCopyrightYear(fake_context)

results = list(plugin.run())
Expand All @@ -167,9 +151,7 @@ def test_missing_header_copyright(self):
' script_copyright("Copyright (C) 2022 Greenbone AG");\n'
)

fake_context = self.create_file_plugin_context(
nasl_file=path, file_content=content
)
fake_context = self.create_file_plugin_context(nasl_file=path, file_content=content)
plugin = CheckCopyrightYear(fake_context)

results = list(plugin.run())
Expand All @@ -188,9 +170,7 @@ def test_pre2008_fail(self):
'+0200 (Tue, 14 May 2020)");\n'
' script_copyright("Copyright (C) 2021 Greenbone AG");\n'
)
fake_context = self.create_file_plugin_context(
nasl_file=path, file_content=content
)
fake_context = self.create_file_plugin_context(nasl_file=path, file_content=content)
plugin = CheckCopyrightYear(fake_context)

results = list(plugin.run())
Expand All @@ -200,8 +180,7 @@ def test_pre2008_fail(self):
results[0].message,
)
self.assertEqual(
"a pre2008 vt has a copyright value in the fileheader"
" newer than the creation_year",
"a pre2008 vt has a copyright value in the fileheader" " newer than the creation_year",
results[1].message,
)

Expand All @@ -214,9 +193,7 @@ def test_fail(self):
' script_copyright("Copyright (C) 1000 Greenbone AG");\n'
)

fake_context = self.create_file_plugin_context(
nasl_file=path, file_content=content
)
fake_context = self.create_file_plugin_context(nasl_file=path, file_content=content)
plugin = CheckCopyrightYear(fake_context)

results = list(plugin.run())
Expand Down
Loading