|
5 | 5 | import pytest
|
6 | 6 |
|
7 | 7 | from pyls import uris
|
| 8 | +from pyls.python_ls import PythonLanguageServer |
8 | 9 |
|
9 | 10 | PY2 = sys.version_info.major == 2
|
10 | 11 |
|
11 | 12 | if PY2:
|
12 | 13 | import pathlib2 as pathlib
|
| 14 | + from StringIO import StringIO |
13 | 15 | else:
|
14 | 16 | import pathlib
|
| 17 | + from io import StringIO |
15 | 18 |
|
16 | 19 |
|
17 | 20 | DOC_URI = uris.from_fs_path(__file__)
|
@@ -119,3 +122,50 @@ def test_multiple_workspaces(tmpdir, pyls):
|
119 | 122 | pyls.m_workspace__did_change_workspace_folders(
|
120 | 123 | added=[], removed=[added_workspaces[0]])
|
121 | 124 | assert workspace1_uri not in pyls.workspaces
|
| 125 | + |
| 126 | + |
| 127 | +@pytest.mark.parametrize('metafile', ['setup.cfg', 'tox.ini', None]) |
| 128 | +def test_source_roots_config(tmpdir, metafile): |
| 129 | + root_path = str(tmpdir) |
| 130 | + |
| 131 | + invalid_roots = ['/invalid/root', '../baz'] |
| 132 | + source_roots = ['service/foo', 'service/bar'] + invalid_roots |
| 133 | + |
| 134 | + if metafile: |
| 135 | + # configured by metafile at pyls startup |
| 136 | + with open(os.path.join(root_path, metafile), 'w+') as f: |
| 137 | + f.write('[pyls]\nsource-roots=\n %s\n' % |
| 138 | + ',\n '.join(source_roots)) |
| 139 | + |
| 140 | + pyls = PythonLanguageServer(StringIO, StringIO) |
| 141 | + pyls.m_initialize( |
| 142 | + processId=1, |
| 143 | + rootUri=uris.from_fs_path(root_path), |
| 144 | + initializationOptions={} |
| 145 | + ) |
| 146 | + |
| 147 | + if not metafile: |
| 148 | + # configured by client via LSP after pyls startup |
| 149 | + pyls.m_workspace__did_change_configuration({ |
| 150 | + 'pyls': {'sourceRoots': source_roots}, |
| 151 | + }) |
| 152 | + |
| 153 | + # put new document under ROOT/service/foo |
| 154 | + prefix = os.path.join(root_path, source_roots[0]) |
| 155 | + test_uri = uris.from_fs_path(os.path.join(prefix, 'hello/test.py')) |
| 156 | + pyls.workspace.put_document(test_uri, 'assert True') |
| 157 | + test_doc = pyls.workspace.get_document(test_uri) |
| 158 | + |
| 159 | + # apply os.path.normcase() on paths below, because case-sensitive |
| 160 | + # comparison on Windows causes unintentional failure for case |
| 161 | + # instability around drive letter |
| 162 | + |
| 163 | + sys_path = [os.path.normcase(p) for p in test_doc.sys_path()] |
| 164 | + for raw_path in source_roots: |
| 165 | + full_path = os.path.normcase(os.path.join(root_path, raw_path)) |
| 166 | + norm_path = os.path.normpath(full_path) |
| 167 | + if raw_path in invalid_roots: |
| 168 | + assert norm_path not in sys_path |
| 169 | + assert full_path not in sys_path # check for safety |
| 170 | + else: |
| 171 | + assert norm_path in sys_path |
0 commit comments