Skip to content

Commit

Permalink
Experiment with a tracking system (super ugly and not usable in pract…
Browse files Browse the repository at this point in the history
…ice right now, but good enough to run some tests).
  • Loading branch information
tonyandrewmeyer committed Mar 28, 2024
1 parent 4d846b2 commit d6e8d0a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ops/framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,10 @@ def _reemit(self, single_event_path: Optional[str] = None):
# scratch in the next path.
self.framework._forget(event) # type: ignore

# XXX Super hacky just for a PoC.
if isinstance(event, charm.HookEvent) and hasattr(observer, "_reset_attributes"):
observer._reset_attributes()

if not deferred and last_event_path is not None:
self._storage.drop_snapshot(last_event_path)

Expand Down
30 changes: 30 additions & 0 deletions ops/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,36 @@ class TestEvents(self._charm_cls.on.__class__):

class TestCharm(self._charm_cls): # type: ignore
on = TestEvents()
def __init__(self, *args: Any, **kwargs: Any):
super().__setattr__("_attributes", set(dir(self)))
super().__setattr__("_disabled", True)
super().__init__(*args, **kwargs)
super().__setattr__("_fresh_attributes", set(super().__getattribute__("_attributes")))
super().__setattr__("_disabled", False)

def __getattribute__(self, name: str) -> Any:
try:
if super().__getattribute__("_disabled"):
disabled = True
attrs = {}
else:
attrs = super().__getattribute__("_attributes")
disabled = False
except AttributeError:
# Still setting up.
pass
else:
if not disabled and name not in attrs:
raise AttributeError(f"You didn't set {name} in this event")
return super().__getattribute__(name)

def __setattr__(self, name: str, value: Any) -> None:
if name != "_fresh_attributes":
super().__getattribute__("_attributes").add(name)
return super().__setattr__(name, value)

def _reset_attributes(self):
super().__setattr__("_attributes", set(super().__getattribute__("_fresh_attributes")))

# Note: jam 2020-03-01 This is so that errors in testing say MyCharm has no attribute foo,
# rather than TestCharm has no attribute foo.
Expand Down

0 comments on commit d6e8d0a

Please sign in to comment.