From f123d8ebf4614c1f2a7622e2e38c74f4dfd9d2de Mon Sep 17 00:00:00 2001 From: James Garner Date: Fri, 11 Oct 2024 10:20:19 +1300 Subject: [PATCH] ci: revert revert as it didn't help This reverts commit 3b00014428689ff9b4c5ef9cd2c3f99fcb1e5788. --- tests/validate/test_facades.py | 82 +++++++++++++++------------------- 1 file changed, 37 insertions(+), 45 deletions(-) diff --git a/tests/validate/test_facades.py b/tests/validate/test_facades.py index 2b0ada18..c5caef12 100644 --- a/tests/validate/test_facades.py +++ b/tests/validate/test_facades.py @@ -4,7 +4,7 @@ import importlib import re from pathlib import Path -from typing import Dict, List, Set, Tuple, TypedDict, cast +from typing import Dict, List, TypedDict, cast import pytest @@ -23,60 +23,52 @@ def project_root(pytestconfig: pytest.Config) -> Path: return pytestconfig.rootpath -def test_client_facades(project_root: Path) -> None: +def test_facade_version_matches_filename(project_root: Path) -> None: + ignore_names = dir(_definitions) + for file in (project_root / 'juju' / 'client').glob('_client[0-9]*.py'): + match = re.search('_client([0-9]+).py', file.name) + assert match + version = int(match.group(1)) + module = importlib.import_module(f'juju.client.{file.stem}') + for cls_name in dir(module): + if cls_name.startswith('_') or cls_name in ignore_names: + continue + assert getattr(module, cls_name).version == version + + +def test_class_name_matches_facade_name(project_root: Path) -> None: + ignore_names = dir(_definitions) + for file in (project_root / 'juju' / 'client').glob('_client[0-9]*.py'): + module = importlib.import_module(f'juju.client.{file.stem}') + for cls_name in dir(module): + if cls_name.startswith('_') or cls_name in ignore_names: + continue + assert getattr(module, cls_name).name == cls_name.removesuffix('Facade') + + +def test_client_facades_matches_generated_code(project_root: Path) -> None: client_facades = cast(ClientFacades, connection.client_facades) - good_facades = make_client_facades_from_generated_code(project_root) + expected_facades = make_client_facades_from_generated_code(project_root) assert { k: v['versions'] for k, v in client_facades.items() } == { - k: v['versions'] for k, v in good_facades.items() + k: v['versions'] for k, v in expected_facades.items() } def make_client_facades_from_generated_code(project_root: Path) -> ClientFacades: """Return a client_facades dictionary from generated code under project_root. """ - files_by_version: List[Tuple[int, Path]] = [] - # [(facade_version, Path), ...] + ignore_names = dir(_definitions) + excluded_facades = connection.excluded_facades + facades: Dict[str, List[int]] = {} for file in (project_root / 'juju' / 'client').glob('_client[0-9]*.py'): - files_by_version.append((_version_from_filename(file), file)) - files_by_version.sort() - - # _clientN.py files import * from _definitions - # so we will ignore any names from there - ignore = dir(_definitions) - - facades_by_version: Dict[int, Set[str]] = {} - # {facade_version: {facade_name, ...}, ...} - for version, file in files_by_version: module = importlib.import_module(f'juju.client.{file.stem}') - facades = { - name.removesuffix("Facade") - for name in dir(module) - if not (name.startswith('_') or name in ignore) - } - facades_by_version[version] = facades - - # client_facades in connection.py is sorted - # so we sort facade names before constructing it - first, *rest = facades_by_version.values() - sorted_facade_names: list[str] = sorted(first.union(*rest)) - - excluded_facades = connection.excluded_facades - client_facades: ClientFacades = {} - # {facade_name: {'versions': [1, 2, 3, ...]}, ...} - for name in sorted_facade_names: - versions: List[int] = [] - for version, facades in facades_by_version.items(): - if version in excluded_facades.get(name, []): + for cls_name in dir(module): + if cls_name.startswith('_') or cls_name in ignore_names: continue - if name in facades: - versions.append(version) - client_facades[name] = {'versions': versions} - return client_facades - - -def _version_from_filename(path: Path) -> int: - match = re.search('_client([0-9]+).py', path.name) - assert match - return int(match.group(1)) + cls = getattr(module, cls_name) + if cls.version in excluded_facades.get(cls.name, []): + continue + facades.setdefault(cls.name, []).append(cls.version) + return {name: {'versions': sorted(facades[name])} for name in sorted(facades)}