Skip to content

Commit

Permalink
Adding test to check for invalid parser names
Browse files Browse the repository at this point in the history
  • Loading branch information
cneill committed Jul 30, 2024
1 parent a38541b commit 7a9fa52
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions unittests/test_factory.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import os
from importlib import import_module
from importlib.util import find_spec
from inspect import isclass

from dojo.models import Test, Test_Type
from dojo.tools.factory import get_parser

Expand Down Expand Up @@ -53,3 +58,23 @@ def test_get_parser_test_active_in_db(self):
)
parser = get_parser(scan_type)
self.assertIsNotNone(parser)

def test_parser_name_matches_module(self):
"""Test to ensure that parsers' class names match their module names"""
package_dir = "dojo/tools"
module_names = os.listdir(package_dir)
missing_parsers = []
for module_name in module_names:
if os.path.isdir(os.path.join(package_dir, module_name)):
found = False
if find_spec(f"dojo.tools.{module_name}.parser"):
module = import_module(f"dojo.tools.{module_name}.parser")
for attribute_name in dir(module):
attribute = getattr(module, attribute_name)
if isclass(attribute) and attribute_name.lower() == module_name.replace("_", "") + "parser":
found = True
if not found and module_name != "__pycache__":
missing_parsers.append(module_name)
if len(missing_parsers) > 0:
print(f"Parsers with invalid names: {missing_parsers}")
self.assertEqual(0, len(missing_parsers))

0 comments on commit 7a9fa52

Please sign in to comment.