Skip to content

[test] Put shared test projects INPUTS into bundle resources #323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Documentation/Development.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ As much as is practical, all code should be covered by tests. New tests can be a

### Test Projects (Fixtures)

SourceKit test projects should be put in the `Tests/INPUTS` directory. Generally, they should use the [Tibs](#tibs) build system to define their sources and targets. An exception is for tests that need to specifically test the interaction with the Swift Package Manager. An example Tibs test project might look like:
SourceKit test projects should be put in the `SKTestSupport/INPUTS` directory. Generally, they should use the [Tibs](#tibs) build system to define their sources and targets. An exception is for tests that need to specifically test the interaction with the Swift Package Manager. An example Tibs test project might look like:

```
Tests/
SKTestSupport/
INPUTS/
MyTestProj/
a.swift
Expand Down
85 changes: 47 additions & 38 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.1
// swift-tools-version:5.3

import PackageDescription

Expand Down Expand Up @@ -30,12 +30,12 @@ let package = Package(
.target(
name: "sourcekit-lsp",
dependencies: [
"ArgumentParser",
"LanguageServerProtocolJSONRPC",
"SourceKitLSP",
"SwiftToolsSupport-auto",
]
),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
],
exclude: ["CMakeLists.txt"]),

.target(
name: "SourceKitLSP",
Expand All @@ -47,9 +47,9 @@ let package = Package(
"SKCore",
"SourceKitD",
"SKSwiftPMWorkspace",
"SwiftToolsSupport-auto",
]
),
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
],
exclude: ["CMakeLists.txt"]),

.target(
name: "CSKTestSupport",
Expand All @@ -58,11 +58,14 @@ let package = Package(
name: "SKTestSupport",
dependencies: [
"CSKTestSupport",
"ISDBTestSupport",
"LSPTestSupport",
"SourceKitLSP",
"tibs", // Never imported, needed at runtime
"SwiftToolsSupport-auto",
.product(name: "ISDBTestSupport", package: "IndexStoreDB"),
.product(name: "tibs", package: "IndexStoreDB"), // Never imported, needed at runtime
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
],
resources: [
.copy("INPUTS"),
]
),
.testTarget(
Expand All @@ -79,15 +82,16 @@ let package = Package(
"BuildServerProtocol",
"LanguageServerProtocol",
"SKCore",
"SwiftPM-auto",
]
),
.product(name: "SwiftPM-auto", package: "SwiftPM")
],
exclude: ["CMakeLists.txt"]),

.testTarget(
name: "SKSwiftPMWorkspaceTests",
dependencies: [
"SKSwiftPMWorkspace",
"SKTestSupport",
"SwiftToolsSupport-auto",
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
]
),

Expand All @@ -101,9 +105,10 @@ let package = Package(
"LanguageServerProtocol",
"LanguageServerProtocolJSONRPC",
"SKSupport",
"SwiftToolsSupport-auto",
]
),
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
],
exclude: ["CMakeLists.txt"]),

.testTarget(
name: "SKCoreTests",
dependencies: [
Expand All @@ -119,9 +124,10 @@ let package = Package(
"Csourcekitd",
"LSPLogging",
"SKSupport",
"SwiftToolsSupport-auto",
]
),
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
],
exclude: ["CMakeLists.txt"]),

.testTarget(
name: "SourceKitDTests",
dependencies: [
Expand All @@ -134,14 +140,14 @@ let package = Package(
// Csourcekitd: C modules wrapper for sourcekitd.
.target(
name: "Csourcekitd",
dependencies: []
),
dependencies: [],
exclude: ["CMakeLists.txt"]),

// Logging support used in LSP modules.
.target(
name: "LSPLogging",
dependencies: []
),
dependencies: [],
exclude: ["CMakeLists.txt"]),

.testTarget(
name: "LSPLoggingTests",
Expand All @@ -164,8 +170,9 @@ let package = Package(
dependencies: [
"LanguageServerProtocol",
"LSPLogging",
]
),
],
exclude: ["CMakeLists.txt"]),

.testTarget(
name: "LanguageServerProtocolJSONRPCTests",
dependencies: [
Expand All @@ -177,8 +184,9 @@ let package = Package(
// LanguageServerProtocol: The core LSP types, suitable for any LSP implementation.
.target(
name: "LanguageServerProtocol",
dependencies: []
),
dependencies: [],
exclude: ["CMakeLists.txt"]),

.testTarget(
name: "LanguageServerProtocolTests",
dependencies: [
Expand All @@ -192,17 +200,18 @@ let package = Package(
name: "BuildServerProtocol",
dependencies: [
"LanguageServerProtocol"
]
),
],
exclude: ["CMakeLists.txt"]),

// SKSupport: Data structures, algorithms and platform-abstraction code that might be generally
// useful to any Swift package. Similar in spirit to SwiftPM's Basic module.
.target(
name: "SKSupport",
dependencies: [
"SwiftToolsSupport-auto"
]
),
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
],
exclude: ["CMakeLists.txt"]),

.testTarget(
name: "SKSupportTests",
dependencies: [
Expand All @@ -225,15 +234,15 @@ import Foundation
if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
// Building standalone.
package.dependencies += [
.package(url: "https://github.com/apple/indexstore-db.git", .branch("main")),
.package(url: "https://github.com/apple/swift-package-manager.git", .branch("main")),
.package(name: "IndexStoreDB", url: "https://github.com/apple/indexstore-db.git", .branch("main")),
.package(name: "SwiftPM", url: "https://github.com/apple/swift-package-manager.git", .branch("main")),
.package(url: "https://github.com/apple/swift-tools-support-core.git", .branch("main")),
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMinor(from: "0.3.0")),
]
} else {
package.dependencies += [
.package(path: "../indexstore-db"),
.package(path: "../swiftpm"),
.package(name: "IndexStoreDB", path: "../indexstore-db"),
.package(name: "SwiftPM", path: "../swiftpm"),
.package(path: "../swiftpm/swift-tools-support-core"),
.package(path: "../swift-argument-parser")
]
Expand Down
5 changes: 2 additions & 3 deletions Sources/SKTestSupport/SKSwiftPMTestWorkspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,11 @@ extension SKSwiftPMTestWorkspace {

extension XCTestCase {

public func staticSourceKitSwiftPMWorkspace(name: String, testFile: String = #file) throws -> SKSwiftPMTestWorkspace? {
public func staticSourceKitSwiftPMWorkspace(name: String) throws -> SKSwiftPMTestWorkspace? {
let testDirName = testDirectoryName
let toolchain = ToolchainRegistry.shared.default!
let workspace = try SKSwiftPMTestWorkspace(
projectDir: inputsDirectory(testFile: testFile)
.appendingPathComponent(name, isDirectory: true),
projectDir: XCTestCase.sklspInputsDirectory.appendingPathComponent(name, isDirectory: true),
tmpDir: URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
.appendingPathComponent("sk-test-data/\(testDirName)", isDirectory: true),
toolchain: toolchain)
Expand Down
42 changes: 27 additions & 15 deletions Sources/SKTestSupport/SKTibsTestWorkspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,24 +109,15 @@ extension SKTibsTestWorkspace {

extension XCTestCase {

/// The path the the test INPUTS directory.
public func inputsDirectory(testFile: String = #file) -> URL {
return URL(fileURLWithPath: testFile)
.deletingLastPathComponent()
.deletingLastPathComponent()
.appendingPathComponent("INPUTS", isDirectory: true)
}

public func staticSourceKitTibsWorkspace(
name: String,
clientCapabilities: ClientCapabilities = .init(),
tmpDir: URL? = nil,
removeTmpDir: Bool = true,
testFile: String = #file
removeTmpDir: Bool = true
) throws -> SKTibsTestWorkspace? {
let testDirName = testDirectoryName
let workspace = try SKTibsTestWorkspace(
immutableProjectDir: inputsDirectory(testFile: testFile)
immutableProjectDir: XCTestCase.sklspInputsDirectory
.appendingPathComponent(name, isDirectory: true),
persistentBuildDir: XCTestCase.productsDirectory
.appendingPathComponent("sk-tests/\(testDirName)", isDirectory: true),
Expand All @@ -152,13 +143,11 @@ extension XCTestCase {
public func mutableSourceKitTibsTestWorkspace(
name: String,
clientCapabilities: ClientCapabilities = .init(),
tmpDir: URL? = nil,
testFile: String = #file
tmpDir: URL? = nil
) throws -> SKTibsTestWorkspace? {
let testDirName = testDirectoryName
let workspace = try SKTibsTestWorkspace(
projectDir: inputsDirectory(testFile: testFile)
.appendingPathComponent(name, isDirectory: true),
projectDir: XCTestCase.sklspInputsDirectory.appendingPathComponent(name, isDirectory: true),
tmpDir: tmpDir ?? URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
.appendingPathComponent("sk-test-data/\(testDirName)", isDirectory: true),
toolchain: ToolchainRegistry.shared.default!,
Expand All @@ -173,6 +162,29 @@ extension XCTestCase {

return workspace
}

/// The path to the INPUTS directory of shared test projects.
public static var sklspInputsDirectory: URL = {
#if os(macOS)
// FIXME: Use Bundle.module.resourceURL once the fix for SR-12912 is released.
var resources = XCTestCase.productsDirectory
.appendingPathComponent("SourceKitLSP_SKTestSupport.bundle")
.appendingPathComponent("Contents")
.appendingPathComponent("Resources")
if !FileManager.default.fileExists(atPath: resources.path) {
// Xcode and command-line swiftpm differ about the path.
resources.deleteLastPathComponent()
resources.deleteLastPathComponent()
}
#else
let resources = XCTestCase.productsDirectory
.appendingPathComponent("SourceKitLSP_SKTestSupport.resources")
#endif
guard FileManager.default.fileExists(atPath: resources.path) else {
fatalError("missing resources \(resources.path)")
}
return resources.appendingPathComponent("INPUTS", isDirectory: true).standardizedFileURL
}()
}

extension TestLocation {
Expand Down
28 changes: 6 additions & 22 deletions Tests/SKCoreTests/BuildServerBuildSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@ import XCTest

final class BuildServerBuildSystemTests: XCTestCase {

func testServerInitialize() throws {
let root = AbsolutePath(
inputsDirectory().appendingPathComponent(testDirectoryName, isDirectory: true).path)
let buildFolder = AbsolutePath(NSTemporaryDirectory())
var root: AbsolutePath {
AbsolutePath(XCTestCase.sklspInputsDirectory
.appendingPathComponent(testDirectoryName, isDirectory: true).path)
}
let buildFolder = AbsolutePath(NSTemporaryDirectory())

func testServerInitialize() throws {
let buildSystem = try BuildServerBuildSystem(projectRoot: root, buildFolder: buildFolder)

XCTAssertEqual(buildSystem.indexDatabasePath, AbsolutePath("some/index/db/path", relativeTo: root))
XCTAssertEqual(buildSystem.indexStorePath, AbsolutePath("some/index/store/path", relativeTo: root))
}

func testSettings() throws {
let root = AbsolutePath(
inputsDirectory().appendingPathComponent(testDirectoryName, isDirectory: true).path)
let buildFolder = AbsolutePath(NSTemporaryDirectory())
let buildSystem = try BuildServerBuildSystem(projectRoot: root, buildFolder: buildFolder)

// test settings with a response
Expand All @@ -50,9 +49,6 @@ final class BuildServerBuildSystemTests: XCTestCase {
}

func testFileRegistration() throws {
let root = AbsolutePath(
inputsDirectory().appendingPathComponent(testDirectoryName, isDirectory: true).path)
let buildFolder = AbsolutePath(NSTemporaryDirectory())
let buildSystem = try BuildServerBuildSystem(projectRoot: root, buildFolder: buildFolder)

let fileUrl = URL(fileURLWithPath: "/some/file/path")
Expand All @@ -65,9 +61,6 @@ final class BuildServerBuildSystemTests: XCTestCase {
}

func testBuildTargets() throws {
let root = AbsolutePath(
inputsDirectory().appendingPathComponent(testDirectoryName, isDirectory: true).path)
let buildFolder = AbsolutePath(NSTemporaryDirectory())
let buildSystem = try BuildServerBuildSystem(projectRoot: root, buildFolder: buildFolder)

let expectation = XCTestExpectation(description: "build target expectation")
Expand Down Expand Up @@ -100,9 +93,6 @@ final class BuildServerBuildSystemTests: XCTestCase {
}

func testBuildTargetSources() throws {
let root = AbsolutePath(
inputsDirectory().appendingPathComponent(testDirectoryName, isDirectory: true).path)
let buildFolder = AbsolutePath(NSTemporaryDirectory())
let buildSystem = try BuildServerBuildSystem(projectRoot: root, buildFolder: buildFolder)

let expectation = XCTestExpectation(description: "build target sources expectation")
Expand Down Expand Up @@ -133,9 +123,6 @@ final class BuildServerBuildSystemTests: XCTestCase {
}

func testBuildTargetOutputs() throws {
let root = AbsolutePath(
inputsDirectory().appendingPathComponent(testDirectoryName, isDirectory: true).path)
let buildFolder = AbsolutePath(NSTemporaryDirectory())
let buildSystem = try BuildServerBuildSystem(projectRoot: root, buildFolder: buildFolder)

let expectation = XCTestExpectation(description: "build target output expectation")
Expand All @@ -160,9 +147,6 @@ final class BuildServerBuildSystemTests: XCTestCase {
}

func testBuildTargetsChanged() throws {
let root = AbsolutePath(
inputsDirectory().appendingPathComponent(testDirectoryName, isDirectory: true).path)
let buildFolder = AbsolutePath(NSTemporaryDirectory())
let buildSystem = try BuildServerBuildSystem(projectRoot: root, buildFolder: buildFolder)

let fileUrl = URL(fileURLWithPath: "/some/file/path")
Expand Down