Debian Codegraph @

test/helpers.py

raw symbols SCIP
1 """
2 A helper module for testing, improves compatibility for testing (as
3 ``jedi._compatibility``) as well as introducing helper functions.
4 """
5
6 from contextlib import contextmanager
7
8 import os
9 import pytest
10 from functools import partial, wraps
11 from jedi import Project
12 from pathlib import Path
13
14 test_dir = Path(__file__).absolute().parent
15 test_dir_project = Project(test_dir)
16 root_dir = test_dir.parent
17 example_dir = test_dir.joinpath('examples')
18
19 sample_int = 1 # This is used in completion/imports.py
20
21 skip_if_windows = partial(pytest.param,
22 marks=pytest.mark.skipif("sys.platform=='win32'"))
23 skip_if_not_windows = partial(pytest.param,
24 marks=pytest.mark.skipif("sys.platform!='win32'"))
25
26
27 def get_example_dir(*names):
28 return example_dir.joinpath(*names)
29
30
31 def cwd_at(path):
32 """
33 Decorator to run function at `path`.
34
35 :type path: str
36 :arg path: relative path from repository root (e.g., ``'jedi'``).
37 """
38 def decorator(func):
39 @wraps(func)
40 def wrapper(Script, **kwargs):
41 with set_cwd(path):
42 return func(Script, **kwargs)
43 return wrapper
44 return decorator
45
46
47 @contextmanager
48 def set_cwd(path, absolute_path=False):
49 repo_root = test_dir.parent
50
51 oldcwd = Path.cwd()
52 os.chdir(repo_root.joinpath(path))
53 try:
54 yield
55 finally:
56 os.chdir(oldcwd)