Discussion:
[Fab-user] Isolating env for unittests?
Chris Spencer
2017-03-10 02:37:28 UTC
Permalink
What's the best way to save and restore env?

I'm trying to unittest some custom Fabric tasks, and I'm having a real
problem not-polluting the global env variable. I've tried things like:

tmp = env.copy()
...run test
env.clear()
env.update(tmp)

but I still get weird errors caused by left-over env keys from other tests.
Carlos García
2017-03-10 09:24:09 UTC
Permalink
Hi Chris,

I think the cleanest way is to use the context manager settings()
<http://docs.fabfile.org/en/1.13/usage/env.html#the-settings-context-manager>

From the docs:

from fabric.api import settings, run
def exists(path):
with settings(warn_only=True):
return run('test -e %s' % path)

Regards

2017-03-10 3:37 GMT+01:00 Chris Spencer <***@gmail.com>:

What's the best way to save and restore env?
Post by Chris Spencer
I'm trying to unittest some custom Fabric tasks, and I'm having a real
tmp = env.copy()
...run test
env.clear()
env.update(tmp)
but I still get weird errors caused by left-over env keys from other tests.
_______________________________________________
Fab-user mailing list
https://lists.nongnu.org/mailman/listinfo/fab-user
​
Azul
2017-03-10 10:22:06 UTC
Permalink
Globals are always a pain,

I use a decorator:

https://github.com/pyBookshelf/bookshelf/blob/master/bookshelf/tests/api_v2/docker_based_tests.py

and then:

https://github.com/pyBookshelf/bookshelf/blob/master/bookshelf/tests/api_v2/test_pkg.py



On 10 Mar 2017 9:24 am, "Carlos García" <
Post by Carlos García
Hi Chris,
I think the cleanest way is to use the context manager settings()
<http://docs.fabfile.org/en/1.13/usage/env.html#the-settings-context-manager>
from fabric.api import settings, run
return run('test -e %s' % path)
Regards
What's the best way to save and restore env?
Post by Chris Spencer
I'm trying to unittest some custom Fabric tasks, and I'm having a real
tmp = env.copy()
...run test
env.clear()
env.update(tmp)
but I still get weird errors caused by left-over env keys from other tests.
_______________________________________________
Fab-user mailing list
https://lists.nongnu.org/mailman/listinfo/fab-user
​
_______________________________________________
Fab-user mailing list
https://lists.nongnu.org/mailman/listinfo/fab-user
Loading...