Skip to content

Commit

Permalink
Change: to regex in spaces_before_dots plugin, include whitespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasHargarter committed Sep 11, 2024
1 parent 4725ed0 commit d7a70c1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
15 changes: 9 additions & 6 deletions tests/plugins/test_spaces_before_dots.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_ok(self):
nasl_file = Path("/some/fake/directory/test.nasl")
content = """
script_tag(name:"summary", value:"Foo Bar.");
script_tag(name:"solution", value:"meh.");
script_tag(name:"solution", value:"Foo .NET.");
"""
fake_context = self.create_file_plugin_context(
nasl_file=nasl_file, file_content=content
Expand All @@ -24,14 +24,16 @@ def test_ok(self):

def test_fail(self):
nasl_file = Path("/some/fake/directory/test.nasl")
content = """
content = (
"""
script_tag(name:"summary", value:"Foo Bar .");
script_tag(name:"vuldetect", value:"Foo Bar .");
script_tag(name:"insight", value:"Foo Bar .");
script_tag(name:"impact", value:"Foo Bar .");
script_tag(name:"affected", value:"Foo Bar .");
script_tag(name:"solution", value:"meh .");
script_tag(name:"impact", value:"Foo . Bar");
"""
'script_tag(name:"affected", value:"Foo\n.\nBar.");'
'script_tag(name:"solution", value:"Foo Bar\n.");'
)
fake_context = self.create_file_plugin_context(
nasl_file=nasl_file, file_content=content
)
Expand All @@ -40,7 +42,8 @@ def test_fail(self):
self.assertEqual(len(results), 6)
self.assertEqual(
results[0].message,
"value of script_tag summary has a excess space before the dot:\n"
"value of script_tag summary has alteast one occurence of excess"
" whitespace before a dot:\n"
" 'script_tag(name:"
'"summary", value:"Foo Bar .");'
"'",
Expand Down
18 changes: 12 additions & 6 deletions troubadix/plugins/spaces_before_dots.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: 2024 Greenbone AG
import re
from collections.abc import Iterator
from pathlib import Path

Expand All @@ -18,6 +19,11 @@
ScriptTag.SOLUTION,
]

# Regex pattern to match:
# 1. A dot preceded and/or followed by any whitespace character (floating between words)
# 2. A dot preceded by any whitespace character at the end of the string
PATTERN = re.compile(r"\s\.\s|\s\.$")


class CheckSpacesBeforeDots(FileContentPlugin):
name = "check_spaces_before_dots"
Expand All @@ -26,7 +32,7 @@ def check_content(
self, nasl_file: Path, file_content: str
) -> Iterator[LinterResult]:
"""
This plugin checks for excess space before the dot
This plugin checks for excess whitespace before a dot
in script_tags that have full sentence values
"""
if nasl_file.suffix == ".inc":
Expand All @@ -35,13 +41,13 @@ def check_content(
pattern = get_script_tag_pattern(tag)
match = pattern.search(file_content)
if match:
s = match.group("value")
# check if last char is a dot and second last a space
if len(s) >= 2 and s[-1] == "." and s[-2] == " ":
value = match.group("value")
if PATTERN.search(value):
fullmatch = match.group()
yield LinterWarning(
f"value of script_tag {match.group('name')} has"
f" a excess space before the dot:\n '{fullmatch}'",
f"value of script_tag {match.group('name')} has alteast"
" one occurence of excess whitespace before a dot:"
f"\n '{fullmatch}'",
file=nasl_file,
plugin=self.name,
)

0 comments on commit d7a70c1

Please sign in to comment.