Skip to content

Commit

Permalink
ci: revert revert as it didn't help
Browse files Browse the repository at this point in the history
This reverts commit 3b00014.
  • Loading branch information
james-garner-canonical committed Oct 10, 2024
1 parent 3b00014 commit f123d8e
Showing 1 changed file with 37 additions and 45 deletions.
82 changes: 37 additions & 45 deletions tests/validate/test_facades.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)}

0 comments on commit f123d8e

Please sign in to comment.