Add a way to cwd into a tmpdir.

This commit is contained in:
Dave Halter
2017-04-04 21:03:45 +02:00
parent 0117f83809
commit fb8ed61b87
2 changed files with 21 additions and 6 deletions

View File

@@ -131,3 +131,9 @@ def isolated_jedi_cache(monkeypatch, tmpdir):
each test case (scope='function'). each test case (scope='function').
""" """
monkeypatch.setattr(settings, 'cache_directory', str(tmpdir)) monkeypatch.setattr(settings, 'cache_directory', str(tmpdir))
@pytest.fixture()
def cwd_tmpdir(monkeypatch, tmpdir):
with helpers.set_cwd(tmpdir.dirpath):
yield tmpdir

View File

@@ -4,6 +4,8 @@ A helper module for testing, improves compatibility for testing (as
""" """
import sys import sys
from contextlib import contextmanager
if sys.hexversion < 0x02070000: if sys.hexversion < 0x02070000:
import unittest2 as unittest import unittest2 as unittest
else: else:
@@ -29,12 +31,19 @@ def cwd_at(path):
def decorator(func): def decorator(func):
@functools.wraps(func) @functools.wraps(func)
def wrapper(*args, **kwds): def wrapper(*args, **kwds):
try: with set_cwd(path):
oldcwd = os.getcwd()
repo_root = os.path.dirname(test_dir)
os.chdir(os.path.join(repo_root, path))
return func(*args, **kwds) return func(*args, **kwds)
finally:
os.chdir(oldcwd)
return wrapper return wrapper
return decorator 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)