From fb8ed61b874f40fb63ae4ee1af471b6808b25bc2 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Tue, 4 Apr 2017 21:03:45 +0200 Subject: [PATCH] Add a way to cwd into a tmpdir. --- test/conftest.py | 6 ++++++ test/helpers.py | 21 +++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/test/conftest.py b/test/conftest.py index 56193a7d..074ef7b2 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -131,3 +131,9 @@ def isolated_jedi_cache(monkeypatch, tmpdir): each test case (scope='function'). """ monkeypatch.setattr(settings, 'cache_directory', str(tmpdir)) + + +@pytest.fixture() +def cwd_tmpdir(monkeypatch, tmpdir): + with helpers.set_cwd(tmpdir.dirpath): + yield tmpdir diff --git a/test/helpers.py b/test/helpers.py index 111b859b..b29dd297 100644 --- a/test/helpers.py +++ b/test/helpers.py @@ -4,6 +4,8 @@ A helper module for testing, improves compatibility for testing (as """ import sys +from contextlib import contextmanager + if sys.hexversion < 0x02070000: import unittest2 as unittest else: @@ -29,12 +31,19 @@ def cwd_at(path): def decorator(func): @functools.wraps(func) def wrapper(*args, **kwds): - try: - oldcwd = os.getcwd() - repo_root = os.path.dirname(test_dir) - os.chdir(os.path.join(repo_root, path)) + with set_cwd(path): return func(*args, **kwds) - finally: - os.chdir(oldcwd) return wrapper return decorator + + +@contextmanager +def set_cwd(path, absolute_path=False): + repo_root = os.path.dirname(test_dir) + + oldcwd = os.getcwd() + os.chdir(os.path.join(repo_root, path)) + try: + yield + finally: + os.chdir(oldcwd)