Skip to content

Commit ccc8e89

Browse files
committed
[test] Put shared test projects INPUTS into bundle resources
This avoids depending on the absolute path to the INPUTS and allows looking it up at run time even if the tests have moved.
1 parent b7cc40f commit ccc8e89

File tree

32 files changed

+48
-47
lines changed

32 files changed

+48
-47
lines changed

Package.swift

Lines changed: 6 additions & 3 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

@@ -40,7 +40,7 @@ let package = Package(
4040

4141
.testTarget(
4242
name: "ISDBTibsTests",
43-
dependencies: ["ISDBTibs"]),
43+
dependencies: ["ISDBTibs", "ISDBTestSupport"]),
4444

4545
// Commandline tool for working with tibs projects.
4646
.target(
@@ -50,7 +50,10 @@ let package = Package(
5050
// Test support library, built on top of tibs.
5151
.target(
5252
name: "ISDBTestSupport",
53-
dependencies: ["IndexStoreDB", "ISDBTibs", "tibs"]),
53+
dependencies: ["IndexStoreDB", "ISDBTibs", "tibs"],
54+
resources: [
55+
.copy("INPUTS")
56+
]),
5457

5558
// MARK: C++ interface
5659

Sources/ISDBTestSupport/TibsTestWorkspace.swift

Lines changed: 37 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,44 @@ 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+
var resources = XCTestCase.productsDirectory
291+
.appendingPathComponent("IndexStoreDB_ISDBTestSupport.bundle")
292+
.appendingPathComponent("Contents")
293+
.appendingPathComponent("Resources")
294+
if !FileManager.default.fileExists(atPath: resources.path) {
295+
// Xcode and command-line swiftpm differ about the path.
296+
resources.deleteLastPathComponent()
297+
resources.deleteLastPathComponent()
298+
}
299+
guard FileManager.default.fileExists(atPath: resources.path) else {
300+
fatalError("missing resources \(resources.path)")
301+
}
302+
return resources.appendingPathComponent("INPUTS", isDirectory: true).standardizedFileURL
303+
}()
289304

290305
/// The name of this test, mangled for use as a directory.
291306
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)