diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2016-06-01 13:47:32 -0700 |
---|---|---|
committer | Dylan Baker <dylan@pnwbakers.com> | 2016-06-02 11:12:28 -0700 |
commit | 0cdf9a3e8147b87705dd4035c988523ba62df77e (patch) | |
tree | e2f74c51f9b1bbcbfc5e7ca2dbe2cac59359af40 /unittests/utils/nose.py | |
parent | 2c5b008fce555a3ef47739c67133332a46fd4923 (diff) |
unittests: add utility context manager for changing directories
Using this context manager ensures that you change back to the original
directory when leaving the context manager
Signed-off-by: Dylan Baker <dylanx.c.baker@intel.com>
Diffstat (limited to 'unittests/utils/nose.py')
-rw-r--r-- | unittests/utils/nose.py | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/unittests/utils/nose.py b/unittests/utils/nose.py index 42982960a..701b567c9 100644 --- a/unittests/utils/nose.py +++ b/unittests/utils/nose.py @@ -404,3 +404,19 @@ def capture_stderr(func): sys.stderr = restore return _inner + + +@contextmanager +def chdir(goto): + """chdir to a directory and back, guaranteed. + + This contextmanager ensures that after changing directory you cahgne back, + using a try/finally block. + + """ + returnto = getcwd() + os.chdir(goto) + try: + yield + finally: + os.chdir(returnto) |