11
11
//===----------------------------------------------------------------------===//
12
12
13
13
import Basics
14
- import PackageModel
14
+ @ testable import PackageModel
15
15
import SPMTestSupport
16
16
import XCTest
17
17
@@ -21,27 +21,58 @@ import class TSCBasic.InMemoryFileSystem
21
21
22
22
private let testArtifactID = " test-artifact "
23
23
24
- private func generateInfoJSON( artifacts: [ MockArtifact ] ) -> SerializedJSON {
25
- """
26
- {
27
- " artifacts " : {
28
- \( artifacts. map {
29
- """
30
- " \( $0. id) " : {
31
- " type " : " swiftSDK " ,
32
- " version " : " 0.0.1 " ,
33
- " variants " : [
34
- {
35
- " path " : " \( $0. id) /aarch64-unknown-linux " ,
36
- " supportedTriples " : \( $0. supportedTriples. map ( \. tripleString) )
24
+ private let targetTriple = try ! Triple ( " aarch64-unknown-linux " )
25
+
26
+ private let jsonEncoder = JSONEncoder ( )
27
+
28
+ private func generateBundleFiles( bundle: MockBundle ) throws -> [ ( String , ByteString ) ] {
29
+ try [
30
+ (
31
+ " \( bundle. path) /info.json " ,
32
+ ByteString ( json: """
33
+ {
34
+ " artifacts " : {
35
+ \( bundle. artifacts. map {
36
+ """
37
+ " \( $0. id) " : {
38
+ " type " : " swiftSDK " ,
39
+ " version " : " 0.0.1 " ,
40
+ " variants " : [
41
+ {
42
+ " path " : " \( $0. id) / \( targetTriple. triple) " ,
43
+ " supportedTriples " : \( $0. supportedTriples. map ( \. tripleString) )
44
+ }
45
+ ]
37
46
}
38
- ]
39
- }
40
- """
41
- } . joined ( separator: " , \n " )
42
- )
43
- },
44
- " schemaVersion " : " 1.0 "
47
+ """
48
+ } . joined ( separator: " , \n " )
49
+ )
50
+ },
51
+ " schemaVersion " : " 1.0 "
52
+ }
53
+ """ )
54
+ ) ,
55
+
56
+ ] + bundle. artifacts. map {
57
+ (
58
+ " \( bundle. path) / \( $0. id) / \( targetTriple. tripleString) /swift-sdk.json " ,
59
+ ByteString ( json: try generateSwiftSDKMetadata ( jsonEncoder) )
60
+ )
61
+ }
62
+ }
63
+
64
+ private func generateSwiftSDKMetadata( _ encoder: JSONEncoder ) throws -> SerializedJSON {
65
+ try """
66
+ {
67
+ " schemaVersion " : " 4.0 " ,
68
+ " targetTriples " : \(
69
+ String (
70
+ bytes: encoder. encode ( [
71
+ targetTriple. tripleString: SwiftSDKMetadataV4 . TripleProperties ( sdkRootPath: " sdk " )
72
+ ] ) ,
73
+ encoding: . utf8
74
+ ) !
75
+ )
45
76
}
46
77
"""
47
78
}
@@ -63,15 +94,13 @@ private func generateTestFileSystem(bundleArtifacts: [MockArtifact]) throws -> (
63
94
return MockBundle ( name: " test \( i) .artifactbundle " , path: " / \( bundleName) " , artifacts: [ artifacts] )
64
95
}
65
96
66
- let fileSystem = InMemoryFileSystem (
67
- files: Dictionary ( uniqueKeysWithValues: bundles. map {
68
- (
69
- " \( $0. path) /info.json " ,
70
- ByteString (
71
- json: generateInfoJSON ( artifacts: $0. artifacts)
72
- )
73
- )
74
- } )
97
+
98
+ let fileSystem = try InMemoryFileSystem (
99
+ files: Dictionary (
100
+ uniqueKeysWithValues: bundles. flatMap {
101
+ try generateBundleFiles ( bundle: $0)
102
+ }
103
+ )
75
104
)
76
105
77
106
let swiftSDKsDirectory = try AbsolutePath ( validating: " /sdks " )
@@ -208,4 +237,34 @@ final class SwiftSDKBundleTests: XCTestCase {
208
237
209
238
XCTAssertEqual ( validBundles. count, bundles. count)
210
239
}
240
+
241
+ func testBundleSelection( ) async throws {
242
+ let ( fileSystem, bundles, swiftSDKsDirectory) = try generateTestFileSystem (
243
+ bundleArtifacts: [
244
+ . init( id: " \( testArtifactID) 1 " , supportedTriples: [ arm64Triple] ) ,
245
+ . init( id: " \( testArtifactID) 2 " , supportedTriples: [ i686Triple] )
246
+ ]
247
+ )
248
+ let system = ObservabilitySystem . makeForTesting ( )
249
+
250
+ for bundle in bundles {
251
+ try await SwiftSDKBundle . install (
252
+ bundlePathOrURL: bundle. path,
253
+ swiftSDKsDirectory: swiftSDKsDirectory,
254
+ fileSystem,
255
+ MockArchiver ( ) ,
256
+ system. topScope
257
+ )
258
+ }
259
+
260
+ let sdk = try SwiftSDKBundle . selectBundle (
261
+ fromBundlesAt: swiftSDKsDirectory,
262
+ fileSystem: fileSystem,
263
+ matching: " \( testArtifactID) 1 " ,
264
+ hostTriple: Triple ( " arm64-apple-macosx14.0 " ) ,
265
+ observabilityScope: system. topScope
266
+ )
267
+
268
+ XCTAssertEqual ( sdk. targetTriple, targetTriple)
269
+ }
211
270
}
0 commit comments