Skip to content

Commit fa9b5f9

Browse files
committed
Add a test case checking that documentDependenciesUpdated works for clang
We weren’t sure if it works because it always sends 0 as the document’s version number, but everythig appears to be fine.
1 parent 93a8f91 commit fa9b5f9

File tree

4 files changed

+61
-16
lines changed

4 files changed

+61
-16
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import LanguageServerProtocol
14+
import SourceKitLSP
15+
16+
public extension SourceKitServer {
17+
func workspaceForDocumentOnQueue(uri: DocumentURI) -> Workspace? {
18+
self.queue.sync {
19+
return self.workspaceForDocument(uri: uri)
20+
}
21+
}
22+
}

Tests/SourceKitDTests/CrashRecoveryTests.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,6 @@ fileprivate extension HoverResponse {
4040
}
4141
}
4242

43-
fileprivate extension SourceKitServer {
44-
func workspaceForDocumentOnQueue(uri: DocumentURI) -> Workspace? {
45-
self.queue.sync {
46-
return self.workspaceForDocument(uri: uri)
47-
}
48-
}
49-
}
50-
5143
final class CrashRecoveryTests: XCTestCase {
5244
func testSourcekitdCrashRecovery() throws {
5345
try XCTSkipUnless(isDarwinHost, "Linux and Windows use in-process sourcekitd")

Tests/SourceKitLSPTests/LocalClangTests.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,4 +322,43 @@ final class LocalClangTests: XCTestCase {
322322
throw e
323323
}
324324
}
325+
326+
func testDocumentDependenciesUpdated() throws {
327+
let ws = try! mutableSourceKitTibsTestWorkspace(name: "BasicCXX")!
328+
329+
let cFileLoc = ws.testLoc("Object:ref:main")
330+
331+
// Initially the workspace should build fine.
332+
let documentOpened = self.expectation(description: "documentOpened")
333+
ws.sk.handleNextNotification({ (note: LanguageServerProtocol.Notification<PublishDiagnosticsNotification>) in
334+
XCTAssert(note.params.diagnostics.isEmpty)
335+
documentOpened.fulfill()
336+
})
337+
338+
try! ws.openDocument(cFileLoc.url, language: .cpp)
339+
340+
self.wait(for: [documentOpened], timeout: 5)
341+
342+
// We rename Object to MyObject in the header.
343+
_ = try ws.sources.edit { builder in
344+
let headerFilePath = ws.sources.rootDirectory.appendingPathComponent("Object.h")
345+
var headerFile = try! String(contentsOf: headerFilePath, encoding: .utf8)
346+
let targetMarkerRange = headerFile.range(of: "/*Object*/")!
347+
headerFile.replaceSubrange(targetMarkerRange, with: "My")
348+
builder.write(headerFile, to: headerFilePath)
349+
}
350+
351+
// Now we should get a diagnostic in main.c file because `Object` is no longer defined.
352+
let updatedNotificationsReceived = self.expectation(description: "updatedNotificationsReceived")
353+
ws.sk.handleNextNotification({ (note: LanguageServerProtocol.Notification<PublishDiagnosticsNotification>) in
354+
XCTAssertFalse(note.params.diagnostics.isEmpty)
355+
updatedNotificationsReceived.fulfill()
356+
})
357+
358+
let clangdServer = ws.testServer.server!._languageService(for: cFileLoc.docUri, .cpp, in: ws.testServer.server!.workspaceForDocumentOnQueue(uri: cFileLoc.docUri)!)!
359+
360+
clangdServer.documentDependenciesUpdated(cFileLoc.docUri)
361+
362+
self.wait(for: [updatedNotificationsReceived], timeout: 5)
363+
}
325364
}

Tests/SourceKitLSPTests/WorkspaceTests.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ import SKTestSupport
1919
import TSCBasic
2020
import XCTest
2121

22-
fileprivate extension SourceKitServer {
23-
func workspaceForDocumentOnQueue(uri: DocumentURI) -> Workspace? {
24-
self.queue.sync {
25-
return self.workspaceForDocument(uri: uri)
26-
}
27-
}
28-
}
29-
3022
final class WorkspaceTests: XCTestCase {
3123

3224
func testMultipleSwiftPMWorkspaces() throws {

0 commit comments

Comments
 (0)