Skip to content

Commit

Permalink
Add: Add plugin for spaces in filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasHargarter committed Apr 29, 2024
1 parent 33c4021 commit b12552b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tests/plugins/test_spaces_in_filename.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: 2024 Greenbone AG

from pathlib import Path

from tests.plugins import PluginTestCase
from troubadix.plugin import LinterError
from troubadix.plugins.spaces_in_filename import CheckSpacesInFilename


class TestSpacesInFilename(PluginTestCase):
def test_ok(self):
nasl_file = Path(__file__).parent / "foo.nasl"
fake_context = self.create_file_plugin_context(nasl_file=nasl_file)
plugin = CheckSpacesInFilename(fake_context)
results = list(plugin.run())
self.assertEqual(len(results), 0)

def test_fail(self):
nasl_file = Path(__file__).parent / "foo bar.nasl"
fake_context = self.create_file_plugin_context(nasl_file=nasl_file)
plugin = CheckSpacesInFilename(fake_context)
results = list(plugin.run())
self.assertEqual(len(results), 1)
self.assertIsInstance(results[0], LinterError)
self.assertEqual(
results[0].message,
f"The VT {nasl_file} contains spaces in the filename",
)
2 changes: 2 additions & 0 deletions troubadix/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from typing import Iterable, List

from troubadix.plugin import FilePlugin, FilesPlugin, Plugin
from troubadix.plugins.spaces_in_filename import CheckSpacesInFilename

from .badwords import CheckBadwords
from .copyright_text import CheckCopyrightText
Expand Down Expand Up @@ -134,6 +135,7 @@
CheckVTPlacement,
CheckWrongSetGetKBCalls,
CheckOverlongDescriptionLines,
CheckSpacesInFilename,
]

# plugins checking all files
Expand Down
19 changes: 19 additions & 0 deletions troubadix/plugins/spaces_in_filename.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: 2024 Greenbone AG

from typing import Iterator

from troubadix.plugin import FilePlugin, LinterError, LinterResult


class CheckSpacesInFilename(FilePlugin):
name = "check_spaces_in_filename"

def run(self) -> Iterator[LinterResult]:
if " " in self.context.nasl_file.name:
yield LinterError(
f"The VT {self.context.nasl_file}"
" contains spaces in the filename",
file=self.context.nasl_file,
plugin=self.name,
)

0 comments on commit b12552b

Please sign in to comment.