-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
fixtures.py
84 lines (64 loc) · 2.09 KB
/
fixtures.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Copyright 2017 Palantir Technologies, Inc.
import os
import sys
from mock import Mock
import pytest
from pyls import uris
from pyls.config.config import Config
from pyls.python_ls import PythonLanguageServer
from pyls.workspace import Workspace, Document
if sys.version_info[0] < 3:
from StringIO import StringIO
else:
from io import StringIO
DOC_URI = uris.from_fs_path(__file__)
DOC = """import sys
def main():
print sys.stdin.read()
"""
@pytest.fixture
def pyls(tmpdir):
""" Return an initialized python LS """
ls = PythonLanguageServer(StringIO, StringIO)
ls.m_initialize(
processId=1,
rootUri=uris.from_fs_path(str(tmpdir)),
initializationOptions={}
)
return ls
@pytest.fixture
def workspace(tmpdir):
"""Return a workspace."""
ws = Workspace(uris.from_fs_path(str(tmpdir)), Mock())
ws._config = Config(ws.root_uri, {}, 0, {})
return ws
@pytest.fixture
def workspace_other_root_path(tmpdir):
"""Return a workspace with a root_path other than tmpdir."""
ws_path = str(tmpdir.mkdir('test123').mkdir('test456'))
ws = Workspace(uris.from_fs_path(ws_path), Mock())
ws._config = Config(ws.root_uri, {}, 0, {})
return ws
@pytest.fixture
def config(workspace): # pylint: disable=redefined-outer-name
"""Return a config object."""
return Config(workspace.root_uri, {}, 0, {})
@pytest.fixture
def doc(workspace): # pylint: disable=redefined-outer-name
return Document(DOC_URI, workspace, DOC)
@pytest.fixture
def temp_workspace_factory(workspace): # pylint: disable=redefined-outer-name
'''
Returns a function that creates a temporary workspace from the files dict.
The dict is in the format {"file_name": "file_contents"}
'''
def fn(files):
def create_file(name, content):
fn = os.path.join(workspace.root_path, name)
with open(fn, 'w') as f:
f.write(content)
workspace.put_document(uris.from_fs_path(fn), content)
for name, content in files.items():
create_file(name, content)
return workspace
return fn