Skip to content

Commit 6b5d3f7

Browse files
committed
Improve test scratch directory creation
Resolves #929 rdar://117547610
1 parent 9cd33a6 commit 6b5d3f7

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

Sources/SKTestSupport/IndexedSingleSwiftFileWorkspace.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public struct IndexedSingleSwiftFileWorkspace {
3838
_ markedText: String,
3939
indexSystemModules: Bool = false,
4040
workspaceDirectory: URL? = nil,
41-
cleanUp: Bool = true,
41+
cleanUp: Bool = cleanScratchDirectories,
4242
testName: String = #function
4343
) async throws {
4444
let testWorkspaceDirectory = try workspaceDirectory ?? testScratchDir(testName: testName)

Sources/SKTestSupport/MultiFileTestWorkspace.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ public class MultiFileTestWorkspace {
105105
self.testClient = try await TestSourceKitLSPClient(
106106
workspaceFolders: workspaces(scratchDirectory),
107107
cleanUp: { [scratchDirectory] in
108-
try? FileManager.default.removeItem(at: scratchDirectory)
108+
if cleanScratchDirectories {
109+
try? FileManager.default.removeItem(at: scratchDirectory)
110+
}
109111
}
110112
)
111113
}

Sources/SKTestSupport/Utils.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,20 @@ extension DocumentURI {
4444
}
4545
}
4646

47+
public let cleanScratchDirectories = (ProcessInfo.processInfo.environment["SOURCEKITLSP_KEEP_TEST_SCRATCH_DIR"] == nil)
48+
4749
/// An empty directory in which a test with `#function` name `testName` can store temporary data.
4850
public func testScratchDir(testName: String = #function) throws -> URL {
4951
let testBaseName = testName.prefix(while: \.isLetter)
5052

53+
var uuid = UUID().uuidString[...]
54+
if let firstDash = uuid.firstIndex(of: "-") {
55+
uuid = uuid[..<firstDash]
56+
}
5157
let url = FileManager.default.temporaryDirectory
5258
.realpath
5359
.appendingPathComponent("sourcekit-lsp-test-scratch")
54-
.appendingPathComponent(UUID().uuidString)
55-
.appendingPathComponent(String(testBaseName))
60+
.appendingPathComponent("\(testBaseName)-\(uuid)")
5661
try? FileManager.default.removeItem(at: url)
5762
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
5863
return url

0 commit comments

Comments
 (0)