|
| 1 | +# Copyright 2017 Kristopher Heijari |
| 2 | +# |
| 3 | +# This file is part of vim-liq. |
| 4 | +# |
| 5 | +# vim-liq.is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# vim-liq.is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with vim-liq. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +"""This module tests installation and using the python language serverer "e2e".""" |
| 19 | +import shutil |
| 20 | +import tempfile |
| 21 | + |
| 22 | +from context import * |
| 23 | + |
| 24 | +import vimliq.install.python_lsp |
| 25 | + |
| 26 | +# Hackky workaroudn to access MonkeyPatch class in module scoped fixture |
| 27 | +# Suggestion taken from here: https://github.com/pytest-dev/pytest/issues/363 |
| 28 | +from _pytest.monkeypatch import MonkeyPatch |
| 29 | + |
| 30 | +# Line is "one based" while col is "zero based" since vim seems to behave that way. E.g. call |
| 31 | +# ":py import vim; vim.current.window.cursor = (4, 4)" jumps to line 4 col 5 (if counting 1 based) |
| 32 | +FUNC_LINE = 4 |
| 33 | +FUNC_COL = 4 |
| 34 | +FUNC_CALL_LINE = 8 |
| 35 | +FUNC_CALL_COL = 13 |
| 36 | +VAR_LINE = 8 |
| 37 | +VAR_COL = 0 |
| 38 | +VAR_REF_LINE = 9 |
| 39 | +VAR_REF_COL = 6 |
| 40 | + |
| 41 | +install_dir = os.path.join(os.path.dirname(__file__), ".lsp_install_dir") |
| 42 | +f_type = "python" |
| 43 | +f_path = os.path.join(os.path.dirname(__file__), "python_test.py") |
| 44 | +f_content = "" |
| 45 | +with open(f_path) as f: |
| 46 | + f_content = f.read() |
| 47 | + |
| 48 | +# Override static vim stuff |
| 49 | +@pytest.fixture(scope="module") |
| 50 | +def vim_static(): |
| 51 | + mp = MonkeyPatch() |
| 52 | + mp.setattr("vimliq.vimutils.filetype", mock.Mock(return_value=f_type)) |
| 53 | + mp.setattr("vimliq.vimutils.current_file", mock.Mock(return_value=f_path)) |
| 54 | + mp.setattr("vimliq.vimutils.current_source", mock.Mock(return_value=f_content)) |
| 55 | + |
| 56 | + |
| 57 | +# This is the client manager used by all tests |
| 58 | +@pytest.fixture(scope="module") |
| 59 | +def LSP(request, vim_static): |
| 60 | + if not os.path.exists(install_dir): |
| 61 | + log.debug("Installing python lsp to %s", install_dir) |
| 62 | + os.makedirs(install_dir) |
| 63 | + langserver = vimliq.install.python_lsp.install(install_dir) |
| 64 | + else: |
| 65 | + # Just doing like this to avoid re-downloading everytime. If the dict returned from |
| 66 | + # the python lsp installation this needs to be updated. |
| 67 | + langserver = {'python': {'start_cmd': install_dir + '/python_lsp_server/bin/pyls', |
| 68 | + 'log_arg': '--log-file', 'transport': 'STDIO'}} |
| 69 | + |
| 70 | + log.debug("langserver: %s", langserver) |
| 71 | + client_manager = vimliq.clientmanager.ClientManager(langserver) |
| 72 | + client_manager.add_client() |
| 73 | + |
| 74 | + # Start the newly added server and open our fake file |
| 75 | + client_manager.start_server() |
| 76 | + client_manager.td_did_open() |
| 77 | + |
| 78 | + def fin(): |
| 79 | + # not using the convinience vim_mock since the scope is module |
| 80 | + sys.modules["vim"].eval.return_value = f_path |
| 81 | + client_manager.td_did_close() |
| 82 | + client_manager.shutdown_all() |
| 83 | + |
| 84 | + request.addfinalizer(fin) |
| 85 | + |
| 86 | + return client_manager |
| 87 | + |
| 88 | + |
| 89 | +def test_did_save(LSP): |
| 90 | + LSP.td_did_save() |
| 91 | + |
| 92 | + |
| 93 | +def test_did_change(LSP): |
| 94 | + LSP.td_did_change() |
| 95 | + |
| 96 | + |
| 97 | +def test_definition(LSP, vim_mock): |
| 98 | + vim_mock.current.window.cursor = (FUNC_CALL_LINE, FUNC_CALL_COL) |
| 99 | + LSP.td_definition() |
| 100 | + vim_mock.command.assert_called_with("e {}".format(f_path)) |
| 101 | + assert vim_mock.current.window.cursor == (FUNC_LINE, FUNC_COL) |
| 102 | + |
| 103 | + |
| 104 | +def test_reference(LSP, vim_mock): |
| 105 | + vim_mock.current.window.cursor = (VAR_REF_LINE, VAR_REF_COL) |
| 106 | + LSP.td_references() |
| 107 | + print(vim_mock.eval.mock_calls) |
| 108 | + vim_mock.eval.assert_called_with(Partial('"filename":"{}"'.format(f_path))) |
| 109 | + vim_mock.eval.assert_called_with(Partial('"lnum":{}'.format(VAR_LINE))) |
| 110 | + vim_mock.eval.assert_called_with(Partial('"col":{}'.format(VAR_COL))) |
| 111 | + |
| 112 | +def test_diagnostics(LSP): |
| 113 | + # For now just check the diagnostics list is updated |
| 114 | + LSP.process_diagnostics() |
| 115 | + print(LSP.diagnostics) |
| 116 | + assert LSP.diagnostics[f_path][0].start_line == 9 |
| 117 | + assert LSP.diagnostics[f_path][0].message == "W391 blank line at end of file" |
| 118 | + |
| 119 | + |
| 120 | +def test_symbols(LSP, vim_mock): |
| 121 | + LSP.td_symbols() |
| 122 | + print(vim_mock.eval.mock_calls) |
| 123 | + vim_mock.eval.assert_any_call(Partial('"text":"{}'.format("a_variable"))) |
| 124 | + vim_mock.eval.assert_any_call(Partial('"text":"{}'.format("def a_function():"))) |
| 125 | + |
| 126 | + |
| 127 | +def test_completion(LSP, vim_mock, monkeypatch): |
| 128 | + # Mock omni_add_base directly to avoid problems with omnifunc call |
| 129 | + monkeypatch.setattr("vimliq.client.omni_add_base", |
| 130 | + mock.Mock(return_value=("a_var", f_content))) |
| 131 | + vim_mock.current.window.cursor = (VAR_REF_LINE, VAR_REF_COL + 5) |
| 132 | + LSP.td_completion() |
| 133 | + print(vim_mock.command.mock_calls) |
| 134 | + # Check that a_variable is returned from the omnifunc function |
| 135 | + vim_mock.command.assert_called_with(Partial('"word":"a_variable"')) |
0 commit comments