Skip to content

Commit 3e06a39

Browse files
authored
Merge pull request #115 from benlangmuir/test-resources
[test] Put shared test projects INPUTS into bundle resources
2 parents b7cc40f + 9c1f9c7 commit 3e06a39

File tree

34 files changed

+101
-56
lines changed

34 files changed

+101
-56
lines changed

Documentation/Development.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ Most indexer tests follow a pattern:
2020

2121
### Test Projects (Fixtures)
2222

23-
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:
23+
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:
2424

2525
```
26-
Tests/
26+
ISDBTestSupport/
2727
INPUTS/
2828
MyTestProj/
2929
a.swift

Documentation/Tibs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Tibs ("Test Index Build System") is a simple and flexible build system designed
44

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

7-
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.
7+
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.
88

99
## Project
1010

Package.swift

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.2
1+
// swift-tools-version:5.3
22

33
import PackageDescription
44

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

2626
.target(
2727
name: "IndexStoreDB",
28-
dependencies: ["IndexStoreDB_CIndexStoreDB"]),
28+
dependencies: ["IndexStoreDB_CIndexStoreDB"],
29+
exclude: ["CMakeLists.txt"]),
2930

3031
.testTarget(
3132
name: "IndexStoreDBTests",
@@ -40,7 +41,7 @@ let package = Package(
4041

4142
.testTarget(
4243
name: "ISDBTibsTests",
43-
dependencies: ["ISDBTibs"]),
44+
dependencies: ["ISDBTibs", "ISDBTestSupport"]),
4445

4546
// Commandline tool for working with tibs projects.
4647
.target(
@@ -50,27 +51,40 @@ let package = Package(
5051
// Test support library, built on top of tibs.
5152
.target(
5253
name: "ISDBTestSupport",
53-
dependencies: ["IndexStoreDB", "ISDBTibs", "tibs"]),
54+
dependencies: ["IndexStoreDB", "ISDBTibs", "tibs"],
55+
resources: [
56+
.copy("INPUTS")
57+
]),
5458

5559
// MARK: C++ interface
5660

5761
// Primary C++ interface.
5862
.target(
5963
name: "IndexStoreDB_Index",
6064
dependencies: ["IndexStoreDB_Database"],
61-
path: "lib/Index"),
65+
path: "lib/Index",
66+
exclude: [
67+
"CMakeLists.txt",
68+
"indexstore_functions.def",
69+
]),
6270

6371
// C wrapper for IndexStoreDB_Index.
6472
.target(
6573
name: "IndexStoreDB_CIndexStoreDB",
6674
dependencies: ["IndexStoreDB_Index"],
67-
path: "lib/CIndexStoreDB"),
75+
path: "lib/CIndexStoreDB",
76+
exclude: ["CMakeLists.txt"]),
6877

6978
// The lmdb database layer.
7079
.target(
7180
name: "IndexStoreDB_Database",
7281
dependencies: ["IndexStoreDB_Core"],
7382
path: "lib/Database",
83+
exclude: [
84+
"CMakeLists.txt",
85+
"lmdb/LICENSE",
86+
"lmdb/COPYRIGHT",
87+
],
7488
cSettings: [
7589
.define("MDB_USE_POSIX_MUTEX", to: "1",
7690
// Windows does not use POSIX mutex
@@ -82,19 +96,47 @@ let package = Package(
8296
.target(
8397
name: "IndexStoreDB_Core",
8498
dependencies: ["IndexStoreDB_Support"],
85-
path: "lib/Core"),
99+
path: "lib/Core",
100+
exclude: ["CMakeLists.txt"]),
86101

87102
// Support code that is generally useful to the C++ implementation.
88103
.target(
89104
name: "IndexStoreDB_Support",
90105
dependencies: ["IndexStoreDB_LLVMSupport"],
91-
path: "lib/Support"),
106+
path: "lib/Support",
107+
exclude: ["CMakeLists.txt"]),
92108

93109
// Copy of a subset of llvm's ADT and Support libraries.
94110
.target(
95111
name: "IndexStoreDB_LLVMSupport",
96112
dependencies: [],
97-
path: "lib/LLVMSupport"),
113+
path: "lib/LLVMSupport",
114+
exclude: [
115+
"LICENSE.TXT",
116+
"CMakeLists.txt",
117+
// *.inc, *.def
118+
"include/llvm/Support/AArch64TargetParser.def",
119+
"include/llvm/Support/ARMTargetParser.def",
120+
"include/llvm/Support/X86TargetParser.def",
121+
"Support/Unix/Host.inc",
122+
"Support/Unix/Memory.inc",
123+
"Support/Unix/Mutex.inc",
124+
"Support/Unix/Path.inc",
125+
"Support/Unix/Process.inc",
126+
"Support/Unix/Program.inc",
127+
"Support/Unix/Signals.inc",
128+
"Support/Unix/Threading.inc",
129+
"Support/Unix/Watchdog.inc",
130+
"Support/Windows/Host.inc",
131+
"Support/Windows/Memory.inc",
132+
"Support/Windows/Mutex.inc",
133+
"Support/Windows/Path.inc",
134+
"Support/Windows/Process.inc",
135+
"Support/Windows/Program.inc",
136+
"Support/Windows/Signals.inc",
137+
"Support/Windows/Threading.inc",
138+
"Support/Windows/Watchdog.inc",
139+
]),
98140
],
99141

100142
cxxLanguageStandard: .cxx14

Sources/ISDBTestSupport/TibsTestWorkspace.swift

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,14 @@ extension XCTestCase {
207207
/// support this test.
208208
public func staticTibsTestWorkspace(
209209
name: String,
210-
useExplicitOutputUnits: Bool = false,
211-
testFile: String = #file
210+
useExplicitOutputUnits: Bool = false
212211
) throws -> TibsTestWorkspace? {
213212
let testDirName = testDirectoryName
214213

215214
let toolchain = TibsToolchain.testDefault
216215

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

251247
let toolchain = TibsToolchain.testDefault
252248

253249
let workspace = try TibsTestWorkspace(
254-
projectDir: inputsDirectory(testFile: testFile)
250+
projectDir: XCTestCase.isdbInputsDirectory
255251
.appendingPathComponent(name, isDirectory: true),
256252
tmpDir: URL(fileURLWithPath: NSTemporaryDirectory())
257253
.appendingPathComponent("isdb-test-data/\(testDirName)", isDirectory: true),
@@ -267,25 +263,49 @@ extension XCTestCase {
267263
return workspace
268264
}
269265

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

278278
/// The path to the built products directory.
279-
public static var productsDirectory: URL {
279+
public static var productsDirectory: URL = {
280280
#if os(macOS)
281-
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") {
282-
return bundle.bundleURL.deletingLastPathComponent()
283-
}
284-
fatalError("couldn't find the products directory")
281+
return testBundle.bundleURL.deletingLastPathComponent()
285282
#else
286-
return Bundle.main.bundleURL
283+
return testBundle.bundleURL
287284
#endif
288-
}
285+
}()
286+
287+
/// The path to the INPUTS directory of shared test projects.
288+
public static var isdbInputsDirectory: URL = {
289+
// FIXME: Use Bundle.module.resourceURL once the fix for SR-12912 is released.
290+
#if os(macOS)
291+
var resources = XCTestCase.productsDirectory
292+
.appendingPathComponent("IndexStoreDB_ISDBTestSupport.bundle")
293+
.appendingPathComponent("Contents")
294+
.appendingPathComponent("Resources")
295+
if !FileManager.default.fileExists(atPath: resources.path) {
296+
// Xcode and command-line swiftpm differ about the path.
297+
resources.deleteLastPathComponent()
298+
resources.deleteLastPathComponent()
299+
}
300+
#else
301+
let resources = XCTestCase.productsDirectory
302+
.appendingPathComponent("IndexStoreDB_ISDBTestSupport.resources")
303+
#endif
304+
guard FileManager.default.fileExists(atPath: resources.path) else {
305+
fatalError("missing resources \(resources.path)")
306+
}
307+
return resources.appendingPathComponent("INPUTS", isDirectory: true).standardizedFileURL
308+
}()
289309

290310
/// The name of this test, mangled for use as a directory.
291311
public var testDirectoryName: String {

Tests/ISDBTibsTests/TibsBuildTests.swift

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import ISDBTibs
14+
import ISDBTestSupport
1415
import Foundation
1516
import XCTest
1617

@@ -117,17 +118,3 @@ final class TibsBuildTests: XCTestCase {
117118
XCTAssertEqual(try builder._buildTest(), [dcpp.path, emm.path])
118119
}
119120
}
120-
121-
extension XCTestCase {
122-
123-
/// The name of this test, mangled for use as a directory.
124-
public var testDirectoryName: String {
125-
guard name.starts(with: "-[") else {
126-
return name
127-
}
128-
129-
let className = name.dropFirst(2).prefix(while: { $0 != " " })
130-
let methodName = name[className.endIndex...].dropFirst().prefix(while: { $0 != "]"})
131-
return "\(className).\(methodName)"
132-
}
133-
}

Tests/ISDBTibsTests/TibsResolutionTests.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import ISDBTibs
14+
import ISDBTestSupport
1415
import Foundation
1516
import XCTest
1617

@@ -200,8 +201,5 @@ final class TibsResolutionTests: XCTestCase {
200201
}
201202

202203
func projectDir(_ name: String) -> URL {
203-
return URL(fileURLWithPath: #file)
204-
.deletingLastPathComponent()
205-
.deletingLastPathComponent()
206-
.appendingPathComponent("INPUTS/\(name)", isDirectory: true)
204+
XCTestCase.isdbInputsDirectory.appendingPathComponent(name, isDirectory: true)
207205
}

Tests/IndexStoreDBTests/LocationScannerTests.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,8 @@ final class LocationScannerTests: XCTestCase {
155155
}
156156

157157
func testDirectory() throws {
158-
let proj1 = URL(fileURLWithPath: #file)
159-
.deletingLastPathComponent()
160-
.deletingLastPathComponent()
161-
.appendingPathComponent("INPUTS/proj1", isDirectory: true)
158+
let proj1 = XCTestCase.isdbInputsDirectory
159+
.appendingPathComponent("proj1", isDirectory: true)
162160
XCTAssertEqual(try scanDir(proj1), [
163161
Loc(url: proj1.appendingPathComponent("a.swift", isDirectory: false), "a:def", 1, 15),
164162
Loc(url: proj1.appendingPathComponent("a.swift", isDirectory: false), "b:call", 2, 13),

0 commit comments

Comments
 (0)