Discussion:
[Fab-user] Clearing runs_once?
Chris Spencer
2017-03-10 16:36:31 UTC
Permalink
I'm trying to write unittests to check some Fabric-based functionality, and
I'm running into some roadblocks with the @runs_once decorator. Some tests
are failing when other tests call tasks decorated by @runs_once, since the
decorator caches the very first return value.

From inspecting the code, I see I can simply clear this by doing `del
mytask.return_value`. However, to properly fix this in all tests, I'll have
to track down every task that uses @runs_once and delete this, which will
be tedious.

Is there an easier to to reset @runs_once globally?
Brandon Whaley
2017-03-10 17:01:46 UTC
Permalink
Looks like fabric keeps a dictionary of found tasks in
fabric.state.commands:
https://github.com/fabric/fabric/blob/1.13.1/fabric/state.py#L405

You could give this a try:

```
from fabric.state import commands

for task in commands.values():
if hasattr(task, 'return_value'):
del task.return_value
```
Post by Chris Spencer
I'm trying to write unittests to check some Fabric-based functionality,
since the decorator caches the very first return value.
From inspecting the code, I see I can simply clear this by doing `del
mytask.return_value`. However, to properly fix this in all tests, I'll have
be tedious.
_______________________________________________
Fab-user mailing list
https://lists.nongnu.org/mailman/listinfo/fab-user
Chris Spencer
2017-03-11 17:43:14 UTC
Permalink
Thanks, I ended up doing something very similar. I wrapped Fabric's
runs_once with my own decorator, and in that created a global registry that
tracked all the Fabric runs_once wrappings. Then, in my unittest's setUp(),
I iterate over the registry and delete any return_value attributes I find.
Post by Brandon Whaley
Looks like fabric keeps a dictionary of found tasks in
fabric.state.commands: https://github.com/fabric/fabric/
blob/1.13.1/fabric/state.py#L405
```
from fabric.state import commands
del task.return_value
```
Post by Chris Spencer
I'm trying to write unittests to check some Fabric-based functionality,
since the decorator caches the very first return value.
From inspecting the code, I see I can simply clear this by doing `del
mytask.return_value`. However, to properly fix this in all tests, I'll have
be tedious.
_______________________________________________
Fab-user mailing list
https://lists.nongnu.org/mailman/listinfo/fab-user
Loading...