Skip to content

Commit 32b413e

Browse files
authored
Merge pull request #1630 from ahoppen/full-diag-report
Add convenience accessor in tests to get a full diagnostic report
2 parents 6ab1681 + d4ebf59 commit 32b413e

File tree

7 files changed

+58
-82
lines changed

7 files changed

+58
-82
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 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+
15+
extension DocumentDiagnosticReport {
16+
/// If this is a full diagnostic report, return it. Otherwise return `nil`.
17+
package var fullReport: RelatedFullDocumentDiagnosticReport? {
18+
guard case .full(let report) = self else {
19+
return nil
20+
}
21+
return report
22+
}
23+
}

Tests/SourceKitLSPTests/BackgroundIndexingTests.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -573,11 +573,7 @@ final class BackgroundIndexingTests: XCTestCase {
573573
let initialDiagnostics = try await project.testClient.send(
574574
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
575575
)
576-
guard case .full(let initialDiagnostics) = initialDiagnostics else {
577-
XCTFail("Expected full diagnostics")
578-
return
579-
}
580-
XCTAssertNotEqual(initialDiagnostics.items, [])
576+
XCTAssertNotEqual(initialDiagnostics.fullReport?.items, [])
581577

582578
try "public func foo() {}".write(
583579
to: try XCTUnwrap(project.uri(for: "MyFile.swift").fileURL),
@@ -1004,12 +1000,8 @@ final class BackgroundIndexingTests: XCTestCase {
10041000
let diagnostics = try await project.testClient.send(
10051001
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
10061002
)
1007-
guard case .full(let diagnostics) = diagnostics else {
1008-
XCTFail("Expected full diagnostics report")
1009-
return
1010-
}
10111003
XCTAssert(
1012-
diagnostics.items.contains(where: {
1004+
(diagnostics.fullReport?.items ?? []).contains(where: {
10131005
$0.message == "Cannot convert return expression of type 'Int' to return type 'String'"
10141006
}),
10151007
"Did not get expected diagnostic: \(diagnostics)"

Tests/SourceKitLSPTests/CodeActionTests.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -548,14 +548,9 @@ final class CodeActionTests: XCTestCase {
548548
let report = try await project.testClient.send(
549549
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
550550
)
551-
guard case .full(let fullReport) = report else {
552-
XCTFail("Expected full diagnostics report")
553-
return
554-
}
555551

556-
XCTAssertEqual(fullReport.items.count, 1)
557-
let diagnostic = try XCTUnwrap(fullReport.items.first)
558-
let codeActions = try XCTUnwrap(diagnostic.codeActions)
552+
XCTAssertEqual(report.fullReport?.items.count, 1)
553+
let codeActions = try XCTUnwrap(report.fullReport?.items.first?.codeActions)
559554

560555
let expectedCodeActions = [
561556
CodeAction(

Tests/SourceKitLSPTests/LocalSwiftTests.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,7 +1447,7 @@ final class LocalSwiftTests: XCTestCase {
14471447
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
14481448
)
14491449
/// The diagnostic request times out, which causes us to return empty diagnostics.
1450-
XCTAssertEqual(responseBeforeEdit, .full(RelatedFullDocumentDiagnosticReport(items: [])))
1450+
XCTAssertEqual(responseBeforeEdit.fullReport?.items, [])
14511451

14521452
// Now check that sourcekitd is not blocked.
14531453
// Replacing the file and sending another diagnostic request should return proper diagnostics.
@@ -1462,12 +1462,8 @@ final class LocalSwiftTests: XCTestCase {
14621462
let responseAfterEdit = try await testClient.send(
14631463
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
14641464
)
1465-
guard case .full(let responseAfterEdit) = responseAfterEdit else {
1466-
XCTFail("Expected full diagnostics")
1467-
return
1468-
}
14691465
XCTAssertEqual(
1470-
responseAfterEdit.items.map(\.message),
1466+
responseAfterEdit.fullReport?.items.map(\.message),
14711467
["Cannot convert value of type 'Int' to specified type 'String'"]
14721468
)
14731469
}

Tests/SourceKitLSPTests/PullDiagnosticsTests.swift

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,9 @@ final class PullDiagnosticsTests: XCTestCase {
3131
)
3232

3333
let report = try await testClient.send(DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri)))
34-
guard case .full(let fullReport) = report else {
35-
XCTFail("Expected full diagnostics report")
36-
return
37-
}
3834

39-
XCTAssertEqual(fullReport.items.count, 1)
40-
let diagnostic = try XCTUnwrap(fullReport.items.first)
35+
XCTAssertEqual(report.fullReport?.items.count, 1)
36+
let diagnostic = try XCTUnwrap(report.fullReport?.items.first)
4137
XCTAssertEqual(diagnostic.range, Position(line: 1, utf16index: 2)..<Position(line: 1, utf16index: 9))
4238
}
4339

@@ -64,11 +60,7 @@ final class PullDiagnosticsTests: XCTestCase {
6460
uri: uri
6561
)
6662
let report = try await testClient.send(DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri)))
67-
guard case .full(let fullReport) = report else {
68-
XCTFail("Expected full diagnostics report")
69-
return
70-
}
71-
let diagnostics = fullReport.items
63+
let diagnostics = try XCTUnwrap(report.fullReport?.items)
7264

7365
XCTAssertEqual(diagnostics.count, 1)
7466
let diagnostic = try XCTUnwrap(diagnostics.first)
@@ -128,17 +120,11 @@ final class PullDiagnosticsTests: XCTestCase {
128120
let report = try await project.testClient.send(
129121
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
130122
)
131-
guard case .full(let fullReport) = report else {
132-
XCTFail("Expected full diagnostics report")
133-
return
134-
}
135-
XCTAssertEqual(fullReport.items.count, 1)
136-
let diagnostic = try XCTUnwrap(fullReport.items.first)
123+
let diagnostic = try XCTUnwrap(report.fullReport?.items.only)
137124
XCTAssertEqual(diagnostic.message, "expected '}' to end function")
138125
XCTAssertEqual(diagnostic.range, Range(positions["2️⃣"]))
139126

140-
XCTAssertEqual(diagnostic.relatedInformation?.count, 1)
141-
let note = try XCTUnwrap(diagnostic.relatedInformation?.first)
127+
let note = try XCTUnwrap(diagnostic.relatedInformation?.only)
142128
XCTAssertEqual(note.message, "to match this opening '{'")
143129
XCTAssertEqual(note.location.range, positions["1️⃣"]..<positions["2️⃣"])
144130
}
@@ -161,11 +147,9 @@ final class PullDiagnosticsTests: XCTestCase {
161147
let beforeChangingFileA = try await project.testClient.send(
162148
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(bUri))
163149
)
164-
guard case .full(let fullReportBeforeChangingFileA) = beforeChangingFileA else {
165-
XCTFail("Expected full diagnostics report")
166-
return
167-
}
168-
XCTAssert(fullReportBeforeChangingFileA.items.contains(where: { $0.message == "Cannot find 'sayHello' in scope" }))
150+
XCTAssert(
151+
(beforeChangingFileA.fullReport?.items ?? []).contains(where: { $0.message == "Cannot find 'sayHello' in scope" })
152+
)
169153

170154
let diagnosticsRefreshRequestReceived = self.expectation(description: "DiagnosticsRefreshRequest received")
171155
project.testClient.handleSingleRequest { (request: DiagnosticsRefreshRequest) in
@@ -185,7 +169,7 @@ final class PullDiagnosticsTests: XCTestCase {
185169
let afterChangingFileA = try await project.testClient.send(
186170
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(bUri))
187171
)
188-
XCTAssertEqual(afterChangingFileA, .full(RelatedFullDocumentDiagnosticReport(items: [])))
172+
XCTAssertEqual(afterChangingFileA.fullReport?.items, [])
189173
}
190174

191175
func testDiagnosticUpdatedAfterDependentModuleIsBuilt() async throws {
@@ -219,17 +203,14 @@ final class PullDiagnosticsTests: XCTestCase {
219203
let beforeBuilding = try await project.testClient.send(
220204
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(bUri))
221205
)
222-
guard case .full(let fullReportBeforeBuilding) = beforeBuilding else {
223-
XCTFail("Expected full diagnostics report")
224-
return
225-
}
226206
XCTAssert(
227-
fullReportBeforeBuilding.items.contains(where: {
207+
(beforeBuilding.fullReport?.items ?? []).contains(where: {
228208
#if compiler(>=6.1)
229209
#warning("When we drop support for Swift 5.10 we no longer need to check for the Objective-C error message")
230210
#endif
231211
return $0.message == "No such module 'LibA'" || $0.message == "Could not build Objective-C module 'LibA'"
232-
})
212+
}
213+
)
233214
)
234215

235216
let diagnosticsRefreshRequestReceived = self.expectation(description: "DiagnosticsRefreshRequest received")
@@ -254,7 +235,7 @@ final class PullDiagnosticsTests: XCTestCase {
254235
let afterChangingFileA = try await project.testClient.send(
255236
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(bUri))
256237
)
257-
XCTAssertEqual(afterChangingFileA, .full(RelatedFullDocumentDiagnosticReport(items: [])))
238+
XCTAssertEqual(afterChangingFileA.fullReport?.items, [])
258239
}
259240

260241
func testDiagnosticsWaitForDocumentToBePrepared() async throws {
@@ -307,7 +288,7 @@ final class PullDiagnosticsTests: XCTestCase {
307288
// but before receiving a reply. The async variant doesn't allow this distinction.
308289
let receivedDiagnostics = self.expectation(description: "Received diagnostics")
309290
project.testClient.send(DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))) { diagnostics in
310-
XCTAssertEqual(diagnostics.success, .full(RelatedFullDocumentDiagnosticReport(items: [])))
291+
XCTAssertEqual(diagnostics.success?.fullReport?.items, [])
311292
receivedDiagnostics.fulfill()
312293
}
313294
diagnosticRequestSent.value = true
@@ -366,11 +347,7 @@ final class PullDiagnosticsTests: XCTestCase {
366347
let diagnostics = try await project.testClient.send(
367348
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
368349
)
369-
guard case .full(let diagnostics) = diagnostics else {
370-
XCTFail("Expected full diagnostics report")
371-
return
372-
}
373-
let diagnostic = try XCTUnwrap(diagnostics.items.only)
350+
let diagnostic = try XCTUnwrap(diagnostics.fullReport?.items.only)
374351
let note = try XCTUnwrap(diagnostic.relatedInformation?.only)
375352
XCTAssertEqual(note.location, try project.location(from: "1️⃣", to: "1️⃣", in: "FileA.swift"))
376353
}

Tests/SourceKitLSPTests/SwiftPMIntegrationTests.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,12 +283,8 @@ final class SwiftPMIntegrationTests: XCTestCase {
283283
let diagnostics = try await project.testClient.send(
284284
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
285285
)
286-
guard case .full(let diagnostics) = diagnostics else {
287-
XCTFail("Expected full diagnostics report")
288-
return
289-
}
290286
XCTAssertEqual(
291-
diagnostics.items.map(\.message),
287+
diagnostics.fullReport?.items.map(\.message),
292288
["Cannot convert value of type 'Int' to specified type 'UnsafeRawPointer'"]
293289
)
294290
}

Tests/SourceKitLSPTests/WorkspaceTests.swift

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ final class WorkspaceTests: XCTestCase {
188188
let diags = try await project.testClient.send(
189189
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(bPackageManifestUri))
190190
)
191-
XCTAssertEqual(diags, .full(RelatedFullDocumentDiagnosticReport(items: [])))
191+
XCTAssertEqual(diags.fullReport?.items, [])
192192
}
193193

194194
func testCorrectWorkspaceForPackageSwiftInMultiSwiftPMWorkspaceSetup() async throws {
@@ -898,11 +898,10 @@ final class WorkspaceTests: XCTestCase {
898898
let diagnostics = try await project.testClient.send(
899899
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
900900
)
901-
guard case .full(let diagnostics) = diagnostics else {
902-
XCTFail("Expected full diagnostics")
903-
return
904-
}
905-
XCTAssertEqual(diagnostics.items.map(\.message), ["Cannot convert value of type 'Int' to specified type 'String'"])
901+
XCTAssertEqual(
902+
diagnostics.fullReport?.items.map(\.message),
903+
["Cannot convert value of type 'Int' to specified type 'String'"]
904+
)
906905
}
907906

908907
func testOptionsInInitializeRequest() async throws {
@@ -925,11 +924,10 @@ final class WorkspaceTests: XCTestCase {
925924
let diagnostics = try await project.testClient.send(
926925
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
927926
)
928-
guard case .full(let diagnostics) = diagnostics else {
929-
XCTFail("Expected full diagnostics")
930-
return
931-
}
932-
XCTAssertEqual(diagnostics.items.map(\.message), ["Cannot convert value of type 'Int' to specified type 'String'"])
927+
XCTAssertEqual(
928+
diagnostics.fullReport?.items.map(\.message),
929+
["Cannot convert value of type 'Int' to specified type 'String'"]
930+
)
933931
}
934932

935933
func testWorkspaceOptionsOverrideGlobalOptions() async throws {
@@ -962,10 +960,9 @@ final class WorkspaceTests: XCTestCase {
962960
let diagnostics = try await project.testClient.send(
963961
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
964962
)
965-
guard case .full(let diagnostics) = diagnostics else {
966-
XCTFail("Expected full diagnostics")
967-
return
968-
}
969-
XCTAssertEqual(diagnostics.items.map(\.message), ["Cannot convert value of type 'Int' to specified type 'String'"])
963+
XCTAssertEqual(
964+
diagnostics.fullReport?.items.map(\.message),
965+
["Cannot convert value of type 'Int' to specified type 'String'"]
966+
)
970967
}
971968
}

0 commit comments

Comments
 (0)