|
12 | 12 |
|
13 | 13 | import CompletionScoring
|
14 | 14 | import Foundation
|
| 15 | +import SwiftExtensions |
15 | 16 |
|
16 | 17 | package struct RepeatableRandomNumberGenerator: RandomNumberGenerator {
|
17 | 18 | private let seed: [UInt64]
|
@@ -102,9 +103,54 @@ package struct RepeatableRandomNumberGenerator: RandomNumberGenerator {
|
102 | 103 | }
|
103 | 104 | }
|
104 | 105 |
|
| 106 | +/// The bundle of the currently executing test. |
| 107 | +private let testBundle: Bundle = { |
| 108 | + #if os(macOS) |
| 109 | + if let bundle = Bundle.allBundles.first(where: { $0.bundlePath.hasSuffix(".xctest") }) { |
| 110 | + return bundle |
| 111 | + } |
| 112 | + fatalError("couldn't find the test bundle") |
| 113 | + #else |
| 114 | + return Bundle.main |
| 115 | + #endif |
| 116 | +}() |
| 117 | + |
| 118 | +/// The path to the built products directory, ie. `.build/debug/arm64-apple-macosx` or the platform-specific equivalent. |
| 119 | +private let productsDirectory: URL = { |
| 120 | + #if os(macOS) |
| 121 | + return testBundle.bundleURL.deletingLastPathComponent() |
| 122 | + #else |
| 123 | + return testBundle.bundleURL |
| 124 | + #endif |
| 125 | +}() |
| 126 | + |
| 127 | +/// The path to the INPUTS directory of shared test projects. |
| 128 | +private let skTestSupportInputsDirectory: URL = { |
| 129 | + #if os(macOS) |
| 130 | + var resources = |
| 131 | + productsDirectory |
| 132 | + .appendingPathComponent("SourceKitLSP_CompletionScoringTestSupport.bundle") |
| 133 | + .appendingPathComponent("Contents") |
| 134 | + .appendingPathComponent("Resources") |
| 135 | + if !FileManager.default.fileExists(at: resources) { |
| 136 | + // Xcode and command-line swiftpm differ about the path. |
| 137 | + resources.deleteLastPathComponent() |
| 138 | + resources.deleteLastPathComponent() |
| 139 | + } |
| 140 | + #else |
| 141 | + let resources = |
| 142 | + productsDirectory |
| 143 | + .appendingPathComponent("SourceKitLSP_CompletionScoringTestSupport.resources") |
| 144 | + #endif |
| 145 | + guard FileManager.default.fileExists(at: resources) else { |
| 146 | + fatalError("missing resources \(resources)") |
| 147 | + } |
| 148 | + return resources.appendingPathComponent("INPUTS", isDirectory: true).standardizedFileURL |
| 149 | +}() |
| 150 | + |
105 | 151 | func loadTestResource(name: String, withExtension ext: String) throws -> Data {
|
106 |
| - let file = URL(fileURLWithPath: #filePath) |
107 |
| - .deletingLastPathComponent() |
| 152 | + let file = |
| 153 | + skTestSupportInputsDirectory |
108 | 154 | .appendingPathComponent("\(name).\(ext)")
|
109 | 155 | return try Data(contentsOf: file)
|
110 | 156 | }
|
|
0 commit comments