Skip to content

Commit cb7d758

Browse files
author
Richard Howell
committed
add a file registration test
1 parent 4804c7c commit cb7d758

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "client name",
3+
"version": "10",
4+
"bspVersion": "2.0",
5+
"languages": ["a", "b"],
6+
"argv": ["server.py"]
7+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env python
2+
3+
import json
4+
import os
5+
import sys
6+
7+
8+
def send(data):
9+
dataStr = json.dumps(data)
10+
sys.stdout.write("Content-Length: {}\r\n\r\n{}".format(len(dataStr), dataStr))
11+
sys.stdout.flush()
12+
13+
14+
while True:
15+
line = sys.stdin.readline()
16+
if len(line) == 0:
17+
break
18+
19+
assert line.startswith('Content-Length:')
20+
length = int(line[len('Content-Length:'):])
21+
sys.stdin.readline()
22+
message = json.loads(sys.stdin.read(length))
23+
24+
response = None
25+
notification = None
26+
27+
if message["method"] == "build/initialize":
28+
response = {
29+
"jsonrpc": "2.0",
30+
"id": message["id"],
31+
"result": {
32+
"displayName": "test server",
33+
"version": "0.1",
34+
"bspVersion": "2.0",
35+
"rootUri": "blah",
36+
"capabilities": {"languageIds": ["a", "b"]},
37+
"data": {
38+
"indexStorePath": "some/index/store/path"
39+
}
40+
}
41+
}
42+
elif message["method"] == "build/initialized":
43+
continue
44+
elif message["method"] == "build/shutdown":
45+
response = {
46+
"jsonrpc": "2.0",
47+
"id": message["id"],
48+
"result": None
49+
}
50+
elif message["method"] == "build/exit":
51+
break
52+
elif message["method"] == "textDocument/registerForChanges":
53+
response = {
54+
"jsonrpc": "2.0",
55+
"id": message["id"],
56+
"result": None
57+
}
58+
if message["params"]["action"] == "register":
59+
notification = {
60+
"jsonrpc": "2.0",
61+
"method": "build/sourceKitOptionsChanged",
62+
"params": {
63+
"uri": message["params"]["uri"],
64+
"options": [ "-a", "-b" ],
65+
"workingDirectory": "/some/dir",
66+
}
67+
}
68+
69+
# ignore other notifications
70+
elif "id" in message:
71+
response = {
72+
"jsonrpc": "2.0",
73+
"id": message["id"],
74+
"error": {
75+
"code": -32600,
76+
"message": "unhandled method {}".format(message["method"]),
77+
}
78+
}
79+
80+
if response: send(response)
81+
if notification: send(notification)

Tests/SKCoreTests/BuildServerBuildSystemTests.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,38 @@ final class BuildServerBuildSystemTests: XCTestCase {
4646
XCTAssertNil(buildSystem?.settings(for: missingFileURL, Language.swift))
4747
}
4848

49+
func testFileRegistration() {
50+
let root = AbsolutePath(
51+
inputsDirectory().appendingPathComponent(testDirectoryName, isDirectory: true).path)
52+
let buildFolder = AbsolutePath(NSTemporaryDirectory())
53+
let buildSystem = try? BuildServerBuildSystem(projectRoot: root, buildFolder: buildFolder)
54+
XCTAssertNotNil(buildSystem)
55+
56+
let fileUrl = URL(fileURLWithPath: "/some/file/path")
57+
let expectation = XCTestExpectation(description: "\(fileUrl) settings updated")
58+
let buildSystemDelegate = TestDelegate(expectations: [fileUrl: expectation])
59+
buildSystem?.delegate = buildSystemDelegate
60+
buildSystem?.registerForChangeNotifications(for: fileUrl)
61+
62+
let result = XCTWaiter.wait(for: [expectation], timeout: 1)
63+
if result != .completed {
64+
fatalError("error \(result) waiting for settings notification")
65+
}
66+
}
67+
68+
}
69+
70+
final class TestDelegate: BuildSystemDelegate {
71+
72+
let expectations: [URL:XCTestExpectation]
73+
74+
public init(expectations: [URL:XCTestExpectation]) {
75+
self.expectations = expectations
76+
}
77+
78+
func fileBuildSettingsChanged(_ changedFiles: Set<URL>) {
79+
for url in changedFiles {
80+
expectations[url]?.fulfill()
81+
}
82+
}
4983
}

Tests/SKCoreTests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ extension BuildServerBuildSystemTests {
66
// `swift test --generate-linuxmain`
77
// to regenerate.
88
static let __allTests__BuildServerBuildSystemTests = [
9+
("testFileRegistration", testFileRegistration),
910
("testServerInitialize", testServerInitialize),
1011
("testSettings", testSettings),
1112
]

0 commit comments

Comments
 (0)