1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import LanguageServerProtocol
import SKTestSupport
import XCTest
final class IndexTests: XCTestCase {
func testIndexSwiftModules() async throws {
let project = try await SwiftPMTestProject(
files: [
"LibA/LibA.swift": """
public func 1️⃣aaa() {}
""",
"LibB/LibB.swift": """
import LibA
public func bbb() {
2️⃣aaa()
}
""",
"LibC/LibC.swift": """
import LibA
public func ccc() {
3️⃣aaa()
}
""",
],
manifest: """
let package = Package(
name: "MyLibrary",
targets: [
.target(name: "LibA"),
.target(name: "LibB", dependencies: ["LibA"]),
.target(name: "LibC", dependencies: ["LibA", "LibB"]),
]
)
""",
enableBackgroundIndexing: true
)
let (libAUri, libAPositions) = try project.openDocument("LibA.swift")
let libBUri = try project.uri(for: "LibB.swift")
let (libCUri, libCPositions) = try project.openDocument("LibC.swift")
let definitionPos = libAPositions["1️⃣"]
let referencePos = try project.position(of: "2️⃣", in: "LibB.swift")
let callPos = libCPositions["3️⃣"]
// MARK: Jump to definition
let response = try await project.testClient.send(
DefinitionRequest(
textDocument: TextDocumentIdentifier(libCUri),
position: libCPositions["3️⃣"]
)
)
guard case .locations(let jump) = response else {
XCTFail("Response is not locations")
return
}
XCTAssertEqual(jump.count, 1)
XCTAssertEqual(jump.first?.uri, libAUri)
XCTAssertEqual(jump.first?.range.lowerBound, definitionPos)
// MARK: Find references
let refs = try await project.testClient.send(
ReferencesRequest(
textDocument: TextDocumentIdentifier(libAUri),
position: definitionPos,
context: ReferencesContext(includeDeclaration: true)
)
)
XCTAssertEqual(
Set(refs),
[
Location(
uri: libAUri,
range: Range(definitionPos)
),
Location(
uri: libBUri,
range: Range(referencePos)
),
Location(
uri: libCUri,
range: Range(callPos)
),
]
)
}
func testIndexShutdown() async throws {
func listdir(_ url: URL) throws -> [URL] {
try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil)
}
func checkRunningIndex(cleanUp: Bool, workspaceDirectory: URL) async throws -> URL? {
let project = try await IndexedSingleSwiftFileTestProject(
"""
func 1️⃣foo() {}
func bar() {
2️⃣foo()
}
""",
workspaceDirectory: workspaceDirectory,
cleanUp: cleanUp
)
let response = try await project.testClient.send(
DefinitionRequest(
textDocument: TextDocumentIdentifier(project.fileURI),
position: project.positions["2️⃣"]
)
)
guard case .locations(let jump) = response else {
XCTFail("Response is not locations")
return nil
}
XCTAssertEqual(jump.count, 1)
XCTAssertEqual(jump.first?.uri, project.fileURI)
XCTAssertEqual(jump.first?.range.lowerBound, project.positions["1️⃣"])
let tmpContents = try listdir(project.indexDBURL)
guard let versionedPath = tmpContents.filter({ $0.lastPathComponent.starts(with: "v") }).spm_only else {
XCTFail("expected one version path 'v[0-9]*', found \(tmpContents)")
return nil
}
let versionContentsBefore = try listdir(versionedPath)
XCTAssertEqual(versionContentsBefore.count, 1)
XCTAssert(versionContentsBefore.first?.lastPathComponent.starts(with: "p") ?? false)
_ = try await project.testClient.send(ShutdownRequest())
return versionedPath
}
let workspaceDirectory = try testScratchDir()
guard let versionedPath = try await checkRunningIndex(cleanUp: false, workspaceDirectory: workspaceDirectory) else {
return
}
let versionContentsAfter = try listdir(versionedPath)
XCTAssertEqual(versionContentsAfter.count, 1)
XCTAssertEqual(versionContentsAfter.first?.lastPathComponent, "saved")
_ = try await checkRunningIndex(cleanUp: true, workspaceDirectory: workspaceDirectory)
}
}
|