Skip to content

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

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 1, 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 @@ -20,10 +20,10 @@ Most indexer tests follow a pattern:

### Test Projects (Fixtures)

Index test projects should be put in the `Tests/INPUTS` directory, and use the [Tibs](Tibs.md) build system to define their sources and targets. An example test project might look like:
Index test projects should be put in the `ISDBTestSupport/INPUTS` directory, and use the [Tibs](Tibs.md) build system to define their sources and targets. An example test project might look like:

```
Tests/
ISDBTestSupport/
INPUTS/
MyTestProj/
a.swift
Expand Down
2 changes: 1 addition & 1 deletion Documentation/Tibs.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tibs ("Test Index Build System") is a simple and flexible build system designed

Tibs is implemented using [Ninja](https://ninja-build.org), which introduces a new dependency in IndexStoreDB when running tests.

Tibs projects are described by a `project.json` file containing one or more targets. Typically, a test case will use a project fixture located in the `Tests/INPUTS` directory.
Tibs projects are described by a `project.json` file containing one or more targets. Typically, a test case will use a project fixture located in the `INPUTS` directory.

## Project

Expand Down
60 changes: 51 additions & 9 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.2
// swift-tools-version:5.3

import PackageDescription

Expand All @@ -25,7 +25,8 @@ let package = Package(

.target(
name: "IndexStoreDB",
dependencies: ["IndexStoreDB_CIndexStoreDB"]),
dependencies: ["IndexStoreDB_CIndexStoreDB"],
exclude: ["CMakeLists.txt"]),

.testTarget(
name: "IndexStoreDBTests",
Expand All @@ -40,7 +41,7 @@ let package = Package(

.testTarget(
name: "ISDBTibsTests",
dependencies: ["ISDBTibs"]),
dependencies: ["ISDBTibs", "ISDBTestSupport"]),

// Commandline tool for working with tibs projects.
.target(
Expand All @@ -50,27 +51,40 @@ let package = Package(
// Test support library, built on top of tibs.
.target(
name: "ISDBTestSupport",
dependencies: ["IndexStoreDB", "ISDBTibs", "tibs"]),
dependencies: ["IndexStoreDB", "ISDBTibs", "tibs"],
resources: [
.copy("INPUTS")
]),

// MARK: C++ interface

// Primary C++ interface.
.target(
name: "IndexStoreDB_Index",
dependencies: ["IndexStoreDB_Database"],
path: "lib/Index"),
path: "lib/Index",
exclude: [
"CMakeLists.txt",
"indexstore_functions.def",
]),

// C wrapper for IndexStoreDB_Index.
.target(
name: "IndexStoreDB_CIndexStoreDB",
dependencies: ["IndexStoreDB_Index"],
path: "lib/CIndexStoreDB"),
path: "lib/CIndexStoreDB",
exclude: ["CMakeLists.txt"]),

// The lmdb database layer.
.target(
name: "IndexStoreDB_Database",
dependencies: ["IndexStoreDB_Core"],
path: "lib/Database",
exclude: [
"CMakeLists.txt",
"lmdb/LICENSE",
"lmdb/COPYRIGHT",
],
cSettings: [
.define("MDB_USE_POSIX_MUTEX", to: "1",
// Windows does not use POSIX mutex
Expand All @@ -82,19 +96,47 @@ let package = Package(
.target(
name: "IndexStoreDB_Core",
dependencies: ["IndexStoreDB_Support"],
path: "lib/Core"),
path: "lib/Core",
exclude: ["CMakeLists.txt"]),

// Support code that is generally useful to the C++ implementation.
.target(
name: "IndexStoreDB_Support",
dependencies: ["IndexStoreDB_LLVMSupport"],
path: "lib/Support"),
path: "lib/Support",
exclude: ["CMakeLists.txt"]),

// Copy of a subset of llvm's ADT and Support libraries.
.target(
name: "IndexStoreDB_LLVMSupport",
dependencies: [],
path: "lib/LLVMSupport"),
path: "lib/LLVMSupport",
exclude: [
"LICENSE.TXT",
"CMakeLists.txt",
// *.inc, *.def
"include/llvm/Support/AArch64TargetParser.def",
"include/llvm/Support/ARMTargetParser.def",
"include/llvm/Support/X86TargetParser.def",
"Support/Unix/Host.inc",
"Support/Unix/Memory.inc",
"Support/Unix/Mutex.inc",
"Support/Unix/Path.inc",
"Support/Unix/Process.inc",
"Support/Unix/Program.inc",
"Support/Unix/Signals.inc",
"Support/Unix/Threading.inc",
"Support/Unix/Watchdog.inc",
"Support/Windows/Host.inc",
"Support/Windows/Memory.inc",
"Support/Windows/Mutex.inc",
"Support/Windows/Path.inc",
"Support/Windows/Process.inc",
"Support/Windows/Program.inc",
"Support/Windows/Signals.inc",
"Support/Windows/Threading.inc",
"Support/Windows/Watchdog.inc",
]),
],

cxxLanguageStandard: .cxx14
Expand Down
64 changes: 42 additions & 22 deletions Sources/ISDBTestSupport/TibsTestWorkspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,14 @@ extension XCTestCase {
/// support this test.
public func staticTibsTestWorkspace(
name: String,
useExplicitOutputUnits: Bool = false,
testFile: String = #file
useExplicitOutputUnits: Bool = false
) throws -> TibsTestWorkspace? {
let testDirName = testDirectoryName

let toolchain = TibsToolchain.testDefault

let workspace = try TibsTestWorkspace(
immutableProjectDir: inputsDirectory(testFile: testFile)
immutableProjectDir: XCTestCase.isdbInputsDirectory
.appendingPathComponent(name, isDirectory: true),
persistentBuildDir: XCTestCase.productsDirectory
.appendingPathComponent("isdb-tests/\(testDirName)", isDirectory: true),
Expand All @@ -242,16 +241,13 @@ extension XCTestCase {
/// * name: The name of the test, which is its path relative to INPUTS.
/// * returns: An immutable TibsTestWorkspace, or nil and prints a warning if toolchain does not
/// support this test.
public func mutableTibsTestWorkspace(
name: String,
testFile: String = #file
) throws -> TibsTestWorkspace? {
public func mutableTibsTestWorkspace(name: String) throws -> TibsTestWorkspace? {
let testDirName = testDirectoryName

let toolchain = TibsToolchain.testDefault

let workspace = try TibsTestWorkspace(
projectDir: inputsDirectory(testFile: testFile)
projectDir: XCTestCase.isdbInputsDirectory
.appendingPathComponent(name, isDirectory: true),
tmpDir: URL(fileURLWithPath: NSTemporaryDirectory())
.appendingPathComponent("isdb-test-data/\(testDirName)", isDirectory: true),
Expand All @@ -267,25 +263,49 @@ extension XCTestCase {
return workspace
}

/// The path the the test INPUTS directory.
public func inputsDirectory(testFile: String = #file) -> URL {
return URL(fileURLWithPath: testFile)
.deletingLastPathComponent()
.deletingLastPathComponent()
.appendingPathComponent("INPUTS", isDirectory: true)
}
/// The bundle of the currently executing test.
public static var testBundle: Bundle = {
#if os(macOS)
if let bundle = Bundle.allBundles.first(where: { $0.bundlePath.hasSuffix(".xctest") }) {
return bundle
}
fatalError("couldn't find the test bundle")
#else
return Bundle.main
#endif
}()

/// The path to the built products directory.
public static var productsDirectory: URL {
public static var productsDirectory: URL = {
#if os(macOS)
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") {
return bundle.bundleURL.deletingLastPathComponent()
}
fatalError("couldn't find the products directory")
return testBundle.bundleURL.deletingLastPathComponent()
#else
return Bundle.main.bundleURL
return testBundle.bundleURL
#endif
}
}()

/// The path to the INPUTS directory of shared test projects.
public static var isdbInputsDirectory: URL = {
// FIXME: Use Bundle.module.resourceURL once the fix for SR-12912 is released.
#if os(macOS)
var resources = XCTestCase.productsDirectory
.appendingPathComponent("IndexStoreDB_ISDBTestSupport.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("IndexStoreDB_ISDBTestSupport.resources")
#endif
guard FileManager.default.fileExists(atPath: resources.path) else {
fatalError("missing resources \(resources.path)")
}
return resources.appendingPathComponent("INPUTS", isDirectory: true).standardizedFileURL
}()

/// The name of this test, mangled for use as a directory.
public var testDirectoryName: String {
Expand Down
15 changes: 1 addition & 14 deletions Tests/ISDBTibsTests/TibsBuildTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

import ISDBTibs
import ISDBTestSupport
import Foundation
import XCTest

Expand Down Expand Up @@ -117,17 +118,3 @@ final class TibsBuildTests: XCTestCase {
XCTAssertEqual(try builder._buildTest(), [dcpp.path, emm.path])
}
}

extension XCTestCase {

/// The name of this test, mangled for use as a directory.
public var testDirectoryName: String {
guard name.starts(with: "-[") else {
return name
}

let className = name.dropFirst(2).prefix(while: { $0 != " " })
let methodName = name[className.endIndex...].dropFirst().prefix(while: { $0 != "]"})
return "\(className).\(methodName)"
}
}
6 changes: 2 additions & 4 deletions Tests/ISDBTibsTests/TibsResolutionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

import ISDBTibs
import ISDBTestSupport
import Foundation
import XCTest

Expand Down Expand Up @@ -200,8 +201,5 @@ final class TibsResolutionTests: XCTestCase {
}

func projectDir(_ name: String) -> URL {
return URL(fileURLWithPath: #file)
.deletingLastPathComponent()
.deletingLastPathComponent()
.appendingPathComponent("INPUTS/\(name)", isDirectory: true)
XCTestCase.isdbInputsDirectory.appendingPathComponent(name, isDirectory: true)
}
6 changes: 2 additions & 4 deletions Tests/IndexStoreDBTests/LocationScannerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,8 @@ final class LocationScannerTests: XCTestCase {
}

func testDirectory() throws {
let proj1 = URL(fileURLWithPath: #file)
.deletingLastPathComponent()
.deletingLastPathComponent()
.appendingPathComponent("INPUTS/proj1", isDirectory: true)
let proj1 = XCTestCase.isdbInputsDirectory
.appendingPathComponent("proj1", isDirectory: true)
XCTAssertEqual(try scanDir(proj1), [
Loc(url: proj1.appendingPathComponent("a.swift", isDirectory: false), "a:def", 1, 15),
Loc(url: proj1.appendingPathComponent("a.swift", isDirectory: false), "b:call", 2, 13),
Expand Down