@@ -15,7 +15,7 @@ import SKCore
15
15
import LanguageServerProtocol
16
16
import LanguageServerProtocolJSONRPC
17
17
import SourceKitLSP
18
- import class Foundation. Pipe
18
+ import Foundation
19
19
import LSPTestSupport
20
20
21
21
public final class TestSourceKitServer {
@@ -36,22 +36,40 @@ public final class TestSourceKitServer {
36
36
37
37
public static let serverOptions : SourceKitServer . Options = SourceKitServer . Options ( )
38
38
39
+ /// If the server is not using the global module cache, the path of the local
40
+ /// module cache.
41
+ ///
42
+ /// This module cache will be deleted when the test server is destroyed.
43
+ private let moduleCache : URL ?
44
+
39
45
public let client : TestClient
40
46
let connImpl : ConnectionImpl
41
47
42
48
public var hasShutdown : Bool = false
43
49
44
50
/// The server, if it is in the same process.
45
51
public let server : SourceKitServer ?
46
-
47
- public init ( connectionKind: ConnectionKind = . local) {
52
+
53
+ /// - Parameters:
54
+ /// - useGlobalModuleCache: If `false`, the server will use its own module
55
+ /// cache in an empty temporary directory instead of the global module cache.
56
+ public init ( connectionKind: ConnectionKind = . local, useGlobalModuleCache: Bool = true ) {
57
+ if !useGlobalModuleCache {
58
+ moduleCache = URL ( fileURLWithPath: NSTemporaryDirectory ( ) ) . appendingPathComponent ( UUID ( ) . uuidString)
59
+ } else {
60
+ moduleCache = nil
61
+ }
62
+ var serverOptions = Self . serverOptions
63
+ if let moduleCache {
64
+ serverOptions. buildSetup. flags. swiftCompilerFlags += [ " -module-cache-path " , moduleCache. path]
65
+ }
48
66
49
67
switch connectionKind {
50
68
case . local:
51
69
let clientConnection = LocalConnection ( )
52
70
let serverConnection = LocalConnection ( )
53
71
client = TestClient ( server: serverConnection)
54
- server = SourceKitServer ( client: clientConnection, options: Self . serverOptions, onExit: {
72
+ server = SourceKitServer ( client: clientConnection, options: serverOptions, onExit: {
55
73
clientConnection. close ( )
56
74
} )
57
75
@@ -76,7 +94,7 @@ public final class TestSourceKitServer {
76
94
)
77
95
78
96
client = TestClient ( server: clientConnection)
79
- server = SourceKitServer ( client: serverConnection, options: Self . serverOptions, onExit: {
97
+ server = SourceKitServer ( client: serverConnection, options: serverOptions, onExit: {
80
98
serverConnection. close ( )
81
99
} )
82
100
@@ -101,6 +119,10 @@ public final class TestSourceKitServer {
101
119
102
120
deinit {
103
121
close ( )
122
+
123
+ if let moduleCache {
124
+ try ? FileManager . default. removeItem ( at: moduleCache)
125
+ }
104
126
}
105
127
106
128
func close( ) {
0 commit comments