Add server tests

This commit is contained in:
Regen
2020-01-03 23:23:34 +09:00
parent 9e6fc1a277
commit c870e3d512
4 changed files with 173 additions and 7 deletions

View File

@@ -1,9 +1,19 @@
import asyncio
import logging
import os
import pprint
from subprocess import PIPE, run
from threading import Thread
import pytest
from pygls import features
from pygls.server import LanguageServer
from cmake_language_server.server import CMakeLanguageServer
@pytest.fixture()
def cmake_build(shared_datadir):
from subprocess import run, PIPE
source = shared_datadir / 'cmake'
build = source / 'build'
build.mkdir()
@@ -13,11 +23,37 @@ def cmake_build(shared_datadir):
stderr=PIPE,
universal_newlines=True)
if p.returncode != 0:
import logging
import os
import pprint
logging.error('env:\n' + pprint.pformat(os.environ))
logging.error('stdout:\n' + p.stdout)
logging.error('stderr:\n' + p.stderr)
raise RuntimeError("CMake failed")
yield build
@pytest.fixture()
def client_server():
c2s_r, c2s_w = os.pipe()
s2c_r, s2c_w = os.pipe()
def start(ls: LanguageServer, fdr, fdw):
# TODO: better patch is needed
# disable `close()` to avoid error messages
close = ls.loop.close
ls.loop.close = lambda: None
ls.start_io(os.fdopen(fdr, 'rb'), os.fdopen(fdw, 'wb'))
ls.loop.close = close
server = CMakeLanguageServer(asyncio.new_event_loop())
server_thread = Thread(target=start, args=(server, c2s_r, s2c_w))
server_thread.start()
client = LanguageServer(asyncio.new_event_loop())
client_thread = Thread(target=start, args=(client, s2c_r, c2s_w))
client_thread.start()
yield client, server
client.send_notification(features.EXIT)
server.send_notification(features.EXIT)
server_thread.join()
client_thread.join()