diff --git a/shlax/targets/base.py b/shlax/targets/base.py index 28dbb45..f1ab1ab 100644 --- a/shlax/targets/base.py +++ b/shlax/targets/base.py @@ -47,10 +47,18 @@ class Target: # the calling target self.parent = target + result = Result(self, self) + result.status = 'success' + for action in actions or self.actions: if await self.action(action, reraise=bool(actions)): + result.status = 'failure' break + if getattr(self, 'clean', None): + self.output.clean(self) + await self.clean(self, result) + async def action(self, action, reraise=False): result = Result(self, action) self.output.start(action) diff --git a/tests/test_target.py b/tests/test_target.py index 7827167..f294503 100644 --- a/tests/test_target.py +++ b/tests/test_target.py @@ -72,6 +72,37 @@ async def test_function(): await Stub()(hello) +@pytest.mark.asyncio +async def test_action_clean(): + class Example: + def __init__(self): + self.was_called = False + async def clean(self, target, result): + self.was_called = True + async def __call__(self, target): + raise Exception('lol') + + action = Example() + target = Stub() + with pytest.raises(Exception): + await target(action) + assert action.was_called + + +@pytest.mark.asyncio +async def test_target_clean(): + class Example(Stub): + def __init__(self, action): + self.was_called = False + super().__init__(action) + async def clean(self, target, result): + self.was_called = True + + target = Example(Error()) + await target() + assert target.was_called + + @pytest.mark.asyncio async def test_method(): class Example: