1"""
2A helper module for testing, improves compatibility for testing (as
3``jedi._compatibility``) as well as introducing helper functions.
4"""
5
6from contextlib import contextmanager
7
8import os
9import pytest
10from functools import partial, wraps
11from jedi import Project
12from pathlib import Path
13
14test_dir = Path(__file__).absolute().parent
15test_dir_project = Project(test_dir)
16root_dir = test_dir.parent
17example_dir = test_dir.joinpath('examples')
18
19sample_int = 1  # This is used in completion/imports.py
20
21skip_if_windows = partial(pytest.param,
22                          marks=pytest.mark.skipif("sys.platform=='win32'"))
23skip_if_not_windows = partial(pytest.param,
24                              marks=pytest.mark.skipif("sys.platform!='win32'"))
25
26
27def get_example_dir(*names):
28    return example_dir.joinpath(*names)
29
30
31def 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
48def 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)
57