Define sources for mixed package tests inline with the tests

This commit is contained in:
Alex Hoppen
2023-10-24 13:47:22 -07:00
parent 9c424d8740
commit 036b1db8a8
7 changed files with 61 additions and 39 deletions

View File

@@ -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: []),
]
)

View File

@@ -1,3 +0,0 @@
#include "clib.h"
void clib_func(void) {/*clib_func:body*/}

View File

@@ -1,7 +0,0 @@
#ifndef CLIB_H
#define CLIB_H
void clib_func(void);
void clib_other(void);
#endif // CLIB_H

View File

@@ -1,5 +0,0 @@
public struct Lib {
public func foo() {}
public init() {}
}
/*lib.swift:toplevel*/

View File

@@ -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

View File

@@ -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(

View File

@@ -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 {