diff --git a/tests/definitions_test.py b/tests/definitions_test.py index dce19f84a..ab5f61fab 100644 --- a/tests/definitions_test.py +++ b/tests/definitions_test.py @@ -1,4 +1,4 @@ -from test_configuration import COMPONENT_TYPES, IMAGE_FILETYPES, SCHEMAS, KNOWN_SLUGS, ROOT_DIR, USE_LOCAL_KNOWN_SLUGS, NETBOX_DT_LIBRARY_URL +from test_configuration import COMPONENT_TYPES, IMAGE_FILETYPES, SCHEMAS, KNOWN_SLUGS, ROOT_DIR, USE_LOCAL_KNOWN_SLUGS, NETBOX_DT_LIBRARY_URL, KNOWN_MODULES import pickle_operations from yaml_loader import DecimalSafeLoader from device_types import DeviceType, ModuleType, verify_filename, validate_components @@ -110,10 +110,12 @@ image_files = _get_image_files() if USE_LOCAL_KNOWN_SLUGS: KNOWN_SLUGS = pickle_operations.read_pickle_data(f'{ROOT_DIR}/tests/known-slugs.pickle') + KNOWN_MODULES = pickle_operations.read_pickle_data(f'{ROOT_DIR}/tests/known-modules.pickle') else: temp_dir = tempfile.TemporaryDirectory() repo = Repo.clone_from(url=NETBOX_DT_LIBRARY_URL, to_path=temp_dir.name) KNOWN_SLUGS = pickle_operations.read_pickle_data(f'{temp_dir.name}/tests/known-slugs.pickle') + KNOWN_MODULES = pickle_operations.read_pickle_data(f'{temp_dir.name}/tests/known-modules.pickle') @pytest.mark.parametrize(('file_path', 'schema'), definition_files) def test_definitions(file_path, schema): @@ -159,7 +161,7 @@ def test_definitions(file_path, schema): assert this_device.verify_slug(KNOWN_SLUGS), pytest.fail(this_device.failureMessage, False) # Verify the filename is valid. Must either be the model or part_number. - assert verify_filename(this_device), pytest.fail(this_device.failureMessage, False) + assert verify_filename(this_device, (KNOWN_MODULES if not this_device.isDevice else None)), pytest.fail(this_device.failureMessage, False) # Check for duplicate components within the definition assert validate_components(COMPONENT_TYPES, this_device), pytest.fail(this_device.failureMessage, False) diff --git a/tests/device_types.py b/tests/device_types.py index 7b58cd0e7..8b6b9321f 100644 --- a/tests/device_types.py +++ b/tests/device_types.py @@ -151,7 +151,7 @@ class ModuleType: slugified = slugified[:-1] return slugified -def verify_filename(device: (DeviceType or ModuleType)): +def verify_filename(device: (DeviceType or ModuleType), KNOWN_MODULES: (set or None)): head, tail = os.path.split(device.get_filepath()) filename = tail.rsplit(".", 1)[0].casefold() @@ -159,6 +159,12 @@ def verify_filename(device: (DeviceType or ModuleType)): device.failureMessage = f'{device.file_path} file name is invalid. Must be either the model "{device._slug_model}" or part_number "{device.part_number} / {device._slug_part_number}"' return False + if not device.isDevice: + matches = [file_name for file_name, file_path in KNOWN_MODULES if file_name.casefold() == filename.casefold()] + if len(matches) > 1: + device.failureMessage = f'{device.file_path} appears to be duplicated. Found {len(matches)} matches: {", ".join(matches)}' + return False + return True def validate_components(component_types, device_or_module): diff --git a/tests/generate-slug-list.py b/tests/generate-slug-list.py index 7cf3f1d0e..cb901c559 100644 --- a/tests/generate-slug-list.py +++ b/tests/generate-slug-list.py @@ -6,18 +6,18 @@ import decimal from yaml_loader import DecimalSafeLoader from jsonschema import Draft4Validator, RefResolver from jsonschema.exceptions import ValidationError -from test_configuration import SCHEMAS, KNOWN_SLUGS, ROOT_DIR +from test_configuration import SCHEMAS, KNOWN_SLUGS, ROOT_DIR, KNOWN_MODULES from urllib.request import urlopen import pickle_operations -def _get_device_type_files(): +def _get_type_files(device_or_module): """ Return a list of all definition files within the specified path. """ file_list = [] for path, schema in SCHEMAS: - if path == 'device-types': + if path == f'{device_or_module}-types': # Initialize the schema with open(f"{ROOT_DIR}/schema/{schema}") as schema_file: schema = json.loads(schema_file.read(), @@ -75,8 +75,8 @@ def load_file(file_path, schema): return (True, definition) -def _generate_known_slugs(): - all_files = _get_device_type_files() +def _generate_knowns(device_or_module): + all_files = _get_type_files(device_or_module) for file_path, schema in all_files: definition_status, definition = load_file(file_path, schema) @@ -84,7 +84,13 @@ def _generate_known_slugs(): print(definition) exit(1) - KNOWN_SLUGS.add((definition.get('slug'), file_path)) + if device_or_module == 'device': + KNOWN_SLUGS.add((definition.get('slug'), file_path)) + else: + KNOWN_MODULES.add((os.path.splitext(os.path.basename(file_path))[0], os.path.dirname(file_path))) -_generate_known_slugs() -pickle_operations.write_pickle_data(KNOWN_SLUGS, f'{ROOT_DIR}/tests/known-slugs.pickle') \ No newline at end of file +_generate_knowns('device') +pickle_operations.write_pickle_data(KNOWN_SLUGS, f'{ROOT_DIR}/tests/known-slugs.pickle') + +_generate_knowns('module') +pickle_operations.write_pickle_data(KNOWN_MODULES, f'{ROOT_DIR}/tests/known-modules.pickle') \ No newline at end of file diff --git a/tests/known-modules.pickle b/tests/known-modules.pickle new file mode 100644 index 000000000..4fad73ab6 Binary files /dev/null and b/tests/known-modules.pickle differ diff --git a/tests/test_configuration.py b/tests/test_configuration.py index 5f90a0f44..b2835790a 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -24,6 +24,7 @@ COMPONENT_TYPES = ( ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..')) KNOWN_SLUGS = set() +KNOWN_MODULES = set() USE_LOCAL_KNOWN_SLUGS = False