Repository navigation
Expand file tree
/
Copy pathfixtures.py
More file actions
112 lines (86 loc) · 2.9 KB
/
Copy pathfixtures.py
File metadata and controls
112 lines (86 loc) · 2.9 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Copyright 2017-2020 Palantir Technologies, Inc.
# Copyright 2021- Python Language Server Contributors.
import os
from io import StringIO
from unittest.mock import Mock
import pytest
from pylsp import uris
from pylsp.config.config import Config
from pylsp.python_lsp import PythonLSPServer
from pylsp.workspace import Workspace, Document
DOC_URI = uris.from_fs_path(__file__)
DOC = """import sys
def main():
print sys.stdin.read()
"""
@pytest.fixture
def pylsp(tmpdir):
""" Return an initialized python LS """
ls = PythonLSPServer(StringIO, StringIO)
ls.m_initialize(
processId=1,
rootUri=uris.from_fs_path(str(tmpdir)),
initializationOptions={}
)
return ls
@pytest.fixture
def pylsp_w_workspace_folders(tmpdir):
""" Return an initialized python LS """
ls = PythonLSPServer(StringIO, StringIO)
folder1 = tmpdir.mkdir('folder1')
folder2 = tmpdir.mkdir('folder2')
ls.m_initialize(
processId=1,
rootUri=uris.from_fs_path(str(folder1)),
initializationOptions={},
workspaceFolders=[
{
'uri': uris.from_fs_path(str(folder1)),
'name': 'folder1'
},
{
'uri': uris.from_fs_path(str(folder2)),
'name': 'folder2'
}
]
)
workspace_folders = [folder1, folder2]
return (ls, workspace_folders)
@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."""
cfg = Config(workspace.root_uri, {}, 0, {})
cfg._plugin_settings = {'plugins': {'pylint': {'enabled': False, 'args': [], 'executable': None}}}
return cfg
@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', encoding='utf-8') 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