diff --git a/Sources/SKTestSupport/INPUTS/MixedPackage/Package.swift b/Sources/SKTestSupport/INPUTS/MixedPackage/Package.swift deleted file mode 100644 index 0628e14a..00000000 --- a/Sources/SKTestSupport/INPUTS/MixedPackage/Package.swift +++ /dev/null @@ -1,10 +0,0 @@ -// swift-tools-version:5.1 -import PackageDescription - -let package = Package( - name: "pkg", - targets: [ - .target(name: "lib", dependencies: []), - .target(name: "clib", dependencies: []), - ] -) diff --git a/Sources/SKTestSupport/INPUTS/MixedPackage/Sources/clib/clib.c b/Sources/SKTestSupport/INPUTS/MixedPackage/Sources/clib/clib.c deleted file mode 100644 index c5a78d35..00000000 --- a/Sources/SKTestSupport/INPUTS/MixedPackage/Sources/clib/clib.c +++ /dev/null @@ -1,3 +0,0 @@ -#include "clib.h" - -void clib_func(void) {/*clib_func:body*/} diff --git a/Sources/SKTestSupport/INPUTS/MixedPackage/Sources/clib/include/clib.h b/Sources/SKTestSupport/INPUTS/MixedPackage/Sources/clib/include/clib.h deleted file mode 100644 index 249d27b5..00000000 --- a/Sources/SKTestSupport/INPUTS/MixedPackage/Sources/clib/include/clib.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef CLIB_H -#define CLIB_H - -void clib_func(void); -void clib_other(void); - -#endif // CLIB_H diff --git a/Sources/SKTestSupport/INPUTS/MixedPackage/Sources/lib/lib.swift b/Sources/SKTestSupport/INPUTS/MixedPackage/Sources/lib/lib.swift deleted file mode 100644 index 1f218dba..00000000 --- a/Sources/SKTestSupport/INPUTS/MixedPackage/Sources/lib/lib.swift +++ /dev/null @@ -1,5 +0,0 @@ -public struct Lib { - public func foo() {} - public init() {} -} -/*lib.swift:toplevel*/ diff --git a/Sources/SKTestSupport/MultiFileTestWorkspace.swift b/Sources/SKTestSupport/MultiFileTestWorkspace.swift index 3b186ec1..30e10e9c 100644 --- a/Sources/SKTestSupport/MultiFileTestWorkspace.swift +++ b/Sources/SKTestSupport/MultiFileTestWorkspace.swift @@ -17,10 +17,10 @@ import SKCore /// The location of a test file within test workspace. public struct RelativeFileLocation: Hashable, ExpressibleByStringLiteral { /// The subdirectories in which the file is located. - fileprivate let directories: [String] + let directories: [String] /// The file's name. - fileprivate let fileName: String + let fileName: String public init(directories: [String] = [], _ fileName: String) { self.directories = directories diff --git a/Sources/SKTestSupport/SwiftPMTestWorkspace.swift b/Sources/SKTestSupport/SwiftPMTestWorkspace.swift index 710a2f43..40e66ad1 100644 --- a/Sources/SKTestSupport/SwiftPMTestWorkspace.swift +++ b/Sources/SKTestSupport/SwiftPMTestWorkspace.swift @@ -36,14 +36,22 @@ public class SwiftPMTestWorkspace: MultiFileTestWorkspace { /// /// If `index` is `true`, then the package will be built, indexing all modules within the package. public init( - files: [String: String], + files: [RelativeFileLocation: String], manifest: String = SwiftPMTestWorkspace.defaultPackageManifest, build: Bool = false, testName: String = #function ) async throws { var filesByPath: [RelativeFileLocation: String] = [:] - for (fileName, contents) in files { - filesByPath[RelativeFileLocation(directories: ["Sources", "MyLibrary"], fileName)] = contents + for (fileLocation, contents) in files { + let directories = + if fileLocation.directories.isEmpty { + ["Sources", "MyLibrary"] + } else if fileLocation.directories.first != "Sources" { + ["Sources"] + fileLocation.directories + } else { + fileLocation.directories + } + filesByPath[RelativeFileLocation(directories: directories, fileLocation.fileName)] = contents } filesByPath["Package.swift"] = manifest try await super.init( diff --git a/Tests/SourceKitLSPTests/WorkspaceTests.swift b/Tests/SourceKitLSPTests/WorkspaceTests.swift index 4b48cfbc..ebfe9ef2 100644 --- a/Tests/SourceKitLSPTests/WorkspaceTests.swift +++ b/Tests/SourceKitLSPTests/WorkspaceTests.swift @@ -238,18 +238,57 @@ final class WorkspaceTests: XCTestCase { } func testMixedPackage() async throws { - guard let ws = try await staticSourceKitSwiftPMWorkspace(name: "MixedPackage") else { return } - try ws.buildAndIndex() + let ws = try await SwiftPMTestWorkspace( + files: [ + "clib/include/clib.h": """ + #ifndef CLIB_H + #define CLIB_H - let cLoc = ws.testLoc("clib_func:body") - let swiftLoc = ws.testLoc("lib.swift:toplevel") + void clib_func(void); + void clib_other(void); - try ws.openDocument(swiftLoc.url, language: .swift) - try ws.openDocument(cLoc.url, language: .c) + #endif // CLIB_H + """, + "clib/clib.c": """ + #include "clib.h" - await assertNoThrow { - _ = try await ws.testClient.send(CompletionRequest(textDocument: cLoc.docIdentifier, position: cLoc.position)) - } + void clib_func(void) {1️⃣} + """, + "lib/lib.swift": """ + public struct Lib { + public func foo() {} + public init() {} + } + 2️⃣ + """, + ], + manifest: """ + // swift-tools-version: 5.7 + + import PackageDescription + + let package = Package( + name: "MyLibrary", + targets: [ + .target(name: "lib", dependencies: []), + .target(name: "clib", dependencies: []), + ] + ) + """ + ) + + let (swiftUri, swiftPositions) = try ws.openDocument("lib.swift") + let (cUri, cPositions) = try ws.openDocument("clib.c") + + let cCompletions = try await ws.testClient.send( + CompletionRequest(textDocument: TextDocumentIdentifier(cUri), position: cPositions["1️⃣"]) + ) + XCTAssertGreaterThanOrEqual(cCompletions.items.count, 0) + + let swiftCompletions = try await ws.testClient.send( + CompletionRequest(textDocument: TextDocumentIdentifier(swiftUri), position: swiftPositions["2️⃣"]) + ) + XCTAssertGreaterThanOrEqual(swiftCompletions.items.count, 0) } func testChangeWorkspaceFolders() async throws {