Skip to content

Commit 090c83b

Browse files
committed
(GH-187) Add integration tests for STDIO and TCP Language Servers
This commit adds smoke integration tests for both the TCP and STDIO variants of the Puppet Language Server.
1 parent 734531f commit 090c83b

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
require 'spec_helper'
2+
require 'open3'
3+
require 'socket'
4+
5+
SERVER_TCP_PORT = 8081
6+
SERVER_HOST = '127.0.0.1'
7+
8+
def start_tcp_server(start_options = ['--no-preload','--timeout=5'])
9+
cmd = "ruby puppet-languageserver #{start_options.join(' ')} --port=#{SERVER_TCP_PORT} --ip=0.0.0.0"
10+
11+
stdin, stdout, stderr, wait_thr = Open3.popen3(cmd)
12+
# Wait for the Language Server to indicate it started
13+
line = nil
14+
begin
15+
line = stdout.readline
16+
end until line =~ /LANGUAGE SERVER RUNNING/
17+
stdout.close
18+
stdin.close
19+
stderr.close
20+
wait_thr
21+
end
22+
23+
def start_stdio_server(start_options = ['--no-preload','--timeout=5'])
24+
cmd = "ruby puppet-languageserver #{start_options.join(' ')} --stdio"
25+
26+
stdin, stdout, stderr, wait_thr = Open3.popen3(cmd)
27+
stderr.close
28+
return stdin, stdout, wait_thr
29+
end
30+
31+
def send_message(sender,message)
32+
str = "Content-Length: #{message.length}\r\n\r\n" + message
33+
sender.write(str)
34+
sender.flush
35+
end
36+
37+
def get_response(reader)
38+
sleep(1)
39+
reader.readpartial(2048)
40+
end
41+
42+
describe 'puppet-languageserver' do
43+
describe 'TCP Server' do
44+
before(:each) do
45+
@server_thr = start_tcp_server
46+
@client = TCPSocket.open(SERVER_HOST, SERVER_TCP_PORT)
47+
end
48+
49+
after(:each) do
50+
@client.close unless @client.nil?
51+
52+
begin
53+
Process.kill("KILL", @server_thr[:pid])
54+
Process.wait(@server_thr[:pid])
55+
rescue
56+
# The server process may not exist and checking in a cross platform way in ruby is difficult
57+
# Instead just swallow any errors
58+
end
59+
end
60+
61+
it 'responds to initialize request' do
62+
send_message(@client, '{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":1580,"rootPath":"c:\\\\Source\\\\puppet-vscode-files","rootUri":"file:///c%3A/Source/puppet-vscode-files","capabilities":{"workspace":{"applyEdit":true,"workspaceEdit":{"documentChanges":true},"didChangeConfiguration":{"dynamicRegistration":false},"didChangeWatchedFiles":{"dynamicRegistration":false},"symbol":{"dynamicRegistration":true},"executeCommand":{"dynamicRegistration":true}},"textDocument":{"synchronization":{"dynamicRegistration":true,"willSave":true,"willSaveWaitUntil":true,"didSave":true},"completion":{"dynamicRegistration":true,"completionItem":{"snippetSupport":true}},"hover":{"dynamicRegistration":true},"signatureHelp":{"dynamicRegistration":true},"references":{"dynamicRegistration":true},"documentHighlight":{"dynamicRegistration":true},"documentSymbol":{"dynamicRegistration":true},"formatting":{"dynamicRegistration":true},"rangeFormatting":{"dynamicRegistration":true},"onTypeFormatting":{"dynamicRegistration":true},"definition":{"dynamicRegistration":true},"codeAction":{"dynamicRegistration":true},"codeLens":{"dynamicRegistration":true},"documentLink":{"dynamicRegistration":true},"rename":{"dynamicRegistration":true}}},"trace":"off"}}')
63+
response = get_response(@client)
64+
65+
expect(response).to match /{"jsonrpc":"2.0","id":0,"result":{"capabilities":/
66+
end
67+
68+
it 'responds to puppet/getVersion request' do
69+
send_message(@client, '{"jsonrpc":"2.0","id":0,"method":"puppet/getVersion"}')
70+
response = get_response(@client)
71+
72+
# Expect the response to have the required parameters
73+
expect(response).to match /"puppetVersion":/
74+
expect(response).to match /"facterVersion":/
75+
expect(response).to match /"functionsLoaded":/
76+
expect(response).to match /"typesLoaded":/
77+
expect(response).to match /"factsLoaded":/
78+
end
79+
end
80+
81+
describe 'STDIO Server' do
82+
before(:each) do
83+
@stdin, @stdout, @server_thr = start_stdio_server
84+
end
85+
86+
after(:each) do
87+
@stdin.close unless @stdin.nil?
88+
@stdout.close unless @stdout.nil?
89+
90+
unless @server_thr.nil?
91+
begin
92+
Process.kill("KILL", @server_thr[:pid])
93+
Process.wait(@server_thr[:pid])
94+
rescue
95+
# The server process may not exist and checking in a cross platform way in ruby is difficult
96+
# Instead just swallow any errors
97+
end
98+
end
99+
end
100+
101+
it 'responds to initialize request' do
102+
send_message(@stdin, '{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":1580,"rootPath":"c:\\\\Source\\\\puppet-vscode-files","rootUri":"file:///c%3A/Source/puppet-vscode-files","capabilities":{"workspace":{"applyEdit":true,"workspaceEdit":{"documentChanges":true},"didChangeConfiguration":{"dynamicRegistration":false},"didChangeWatchedFiles":{"dynamicRegistration":false},"symbol":{"dynamicRegistration":true},"executeCommand":{"dynamicRegistration":true}},"textDocument":{"synchronization":{"dynamicRegistration":true,"willSave":true,"willSaveWaitUntil":true,"didSave":true},"completion":{"dynamicRegistration":true,"completionItem":{"snippetSupport":true}},"hover":{"dynamicRegistration":true},"signatureHelp":{"dynamicRegistration":true},"references":{"dynamicRegistration":true},"documentHighlight":{"dynamicRegistration":true},"documentSymbol":{"dynamicRegistration":true},"formatting":{"dynamicRegistration":true},"rangeFormatting":{"dynamicRegistration":true},"onTypeFormatting":{"dynamicRegistration":true},"definition":{"dynamicRegistration":true},"codeAction":{"dynamicRegistration":true},"codeLens":{"dynamicRegistration":true},"documentLink":{"dynamicRegistration":true},"rename":{"dynamicRegistration":true}}},"trace":"off"}}')
103+
response = get_response(@stdout)
104+
105+
expect(response).to match /{"jsonrpc":"2.0","id":0,"result":{"capabilities":/
106+
end
107+
108+
it 'responds to puppet/getVersion request' do
109+
send_message(@stdin, '{"jsonrpc":"2.0","id":0,"method":"puppet/getVersion"}')
110+
response = get_response(@stdout)
111+
112+
# Expect the response to have the required parameters
113+
expect(response).to match /"puppetVersion":/
114+
expect(response).to match /"facterVersion":/
115+
expect(response).to match /"functionsLoaded":/
116+
expect(response).to match /"typesLoaded":/
117+
expect(response).to match /"factsLoaded":/
118+
end
119+
end
120+
end

0 commit comments

Comments
 (0)