Skip to content

Commit af990de

Browse files
authored
Merge pull request #1948 from ahoppen/load-scoring-resources
Load `RandomSeed.plist` and `CommonFunctionTerms.json` from test resources
2 parents fe5644b + 994a6b5 commit af990de

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ var targets: [Target] = [
149149
.target(
150150
name: "CompletionScoringTestSupport",
151151
dependencies: ["CompletionScoring", "SwiftExtensions"],
152+
resources: [.copy("INPUTS")],
152153
swiftSettings: globalSwiftSettings
153154
),
154155

Sources/CompletionScoringTestSupport/RepeatableRandomNumberGenerator.swift

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import CompletionScoring
1414
import Foundation
15+
import SwiftExtensions
1516

1617
package struct RepeatableRandomNumberGenerator: RandomNumberGenerator {
1718
private let seed: [UInt64]
@@ -102,9 +103,54 @@ package struct RepeatableRandomNumberGenerator: RandomNumberGenerator {
102103
}
103104
}
104105

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+
105151
func loadTestResource(name: String, withExtension ext: String) throws -> Data {
106-
let file = URL(fileURLWithPath: #filePath)
107-
.deletingLastPathComponent()
152+
let file =
153+
skTestSupportInputsDirectory
108154
.appendingPathComponent("\(name).\(ext)")
109155
return try Data(contentsOf: file)
110156
}

0 commit comments

Comments
 (0)