2
2
//
3
3
// This source file is part of the Swift open source project
4
4
//
5
- // Copyright (c) 2023-2024 Apple Inc. and the Swift project authors
5
+ // Copyright (c) 2023-2025 Apple Inc. and the Swift project authors
6
6
// Licensed under Apache License v2.0 with Runtime Library Exception
7
7
//
8
8
// See http://swift.org/LICENSE.txt for license information
@@ -27,20 +27,26 @@ private let targetTriple = try! Triple("aarch64-unknown-linux")
27
27
private let jsonEncoder = JSONEncoder ( )
28
28
29
29
private func generateBundleFiles( bundle: MockBundle ) throws -> [ ( String , ByteString ) ] {
30
- try [
30
+ return try [
31
31
(
32
32
" \( bundle. path) /info.json " ,
33
33
ByteString ( json: """
34
34
{
35
35
" artifacts " : {
36
36
\( bundle. artifacts. map {
37
- """
37
+ let path = if let metadataPath = $0. metadataPath {
38
+ metadataPath. pathString
39
+ } else {
40
+ " \( $0. id) / \( targetTriple. triple) "
41
+ }
42
+
43
+ return """
38
44
" \( $0. id) " : {
39
45
" type " : " swiftSDK " ,
40
46
" version " : " 0.0.1 " ,
41
47
" variants " : [
42
48
{
43
- " path " : " \( $0 . id ) / \( targetTriple . triple ) " ,
49
+ " path " : " \( path ) " ,
44
50
" supportedTriples " : \( $0. supportedTriples. map ( \. tripleString) )
45
51
}
46
52
]
@@ -55,14 +61,25 @@ private func generateBundleFiles(bundle: MockBundle) throws -> [(String, ByteStr
55
61
) ,
56
62
57
63
] + bundle. artifacts. map {
58
- (
59
- " \( bundle. path) / \( $0. id) / \( targetTriple. tripleString) /swift-sdk.json " ,
64
+ let path = if let metadataPath = $0. metadataPath {
65
+ " \( bundle. path) / \( metadataPath. pathString) "
66
+ } else {
67
+ " \( bundle. path) / \( $0. id) / \( targetTriple. triple) /swift-sdk.json "
68
+ }
69
+
70
+ return (
71
+ path,
60
72
ByteString ( json: try generateSwiftSDKMetadata ( jsonEncoder, createToolset: $0. toolsetRootPath != nil ) )
61
73
)
62
74
} + bundle. artifacts. compactMap { artifact in
63
- artifact. toolsetRootPath. map { path in
75
+ let toolsetPath = if artifact. metadataPath != nil {
76
+ " \( bundle. path) /toolset.json "
77
+ } else {
78
+ " \( bundle. path) / \( artifact. id) / \( targetTriple. triple) /toolset.json "
79
+ }
80
+ return artifact. toolsetRootPath. map { path in
64
81
(
65
- " \( bundle . path ) / \( artifact . id ) / \( targetTriple . tripleString ) /toolset.json " ,
82
+ " \( toolsetPath ) " ,
66
83
ByteString ( json: """
67
84
{
68
85
" schemaVersion " : " 1.0 " ,
@@ -101,16 +118,18 @@ private struct MockBundle {
101
118
private struct MockArtifact {
102
119
let id : String
103
120
let supportedTriples : [ Triple ]
121
+ var metadataPath : RelativePath ?
104
122
var toolsetRootPath : AbsolutePath ?
105
123
}
106
124
107
- private func generateTestFileSystem( bundleArtifacts: [ MockArtifact ] ) throws -> ( some FileSystem , [ MockBundle ] , AbsolutePath ) {
125
+ private func generateTestFileSystem(
126
+ bundleArtifacts: [ MockArtifact ]
127
+ ) throws -> ( some FileSystem , [ MockBundle ] , AbsolutePath ) {
108
128
let bundles = bundleArtifacts. enumerated ( ) . map { ( i, artifacts) in
109
129
let bundleName = " test \( i) . \( artifactBundleExtension) "
110
130
return MockBundle ( name: " test \( i) . \( artifactBundleExtension) " , path: " / \( bundleName) " , artifacts: [ artifacts] )
111
131
}
112
132
113
-
114
133
let fileSystem = try InMemoryFileSystem (
115
134
files: Dictionary (
116
135
uniqueKeysWithValues: bundles. flatMap {
@@ -475,4 +494,54 @@ final class SwiftSDKBundleTests: XCTestCase {
475
494
)
476
495
}
477
496
}
497
+
498
+ func testMetadataJSONPaths( ) async throws {
499
+ let toolsetRootPath = AbsolutePath ( " /path/to/toolpath " )
500
+ let ( fileSystem, bundles, swiftSDKsDirectory) = try generateTestFileSystem (
501
+ bundleArtifacts: [
502
+ . init(
503
+ id: " \( testArtifactID) 1 " ,
504
+ supportedTriples: [ arm64Triple] ,
505
+ metadataPath: " metadata1.json "
506
+ ) ,
507
+ . init(
508
+ id: " \( testArtifactID) 2 " ,
509
+ supportedTriples: [ i686Triple] ,
510
+ metadataPath: " metadata2.json " ,
511
+ toolsetRootPath: toolsetRootPath
512
+ ) ,
513
+ ]
514
+ )
515
+ let system = ObservabilitySystem . makeForTesting ( )
516
+ let archiver = MockArchiver ( )
517
+
518
+ var output = [ SwiftSDKBundleStore . Output] ( )
519
+ let store = SwiftSDKBundleStore (
520
+ swiftSDKsDirectory: swiftSDKsDirectory,
521
+ fileSystem: fileSystem,
522
+ observabilityScope: system. topScope,
523
+ outputHandler: { output. append ( $0) }
524
+ )
525
+
526
+ for bundle in bundles {
527
+ try await store. install ( bundlePathOrURL: bundle. path, archiver)
528
+ }
529
+
530
+ let validBundles = try store. allValidBundles
531
+
532
+ XCTAssertEqual ( validBundles. count, bundles. count)
533
+
534
+ XCTAssertEqual ( validBundles. sortedArtifactIDs, [ " \( testArtifactID) 1 " , " \( testArtifactID) 2 " ] )
535
+ XCTAssertEqual ( output. count, 2 )
536
+ XCTAssertEqual ( output, [
537
+ . installationSuccessful(
538
+ bundlePathOrURL: bundles [ 0 ] . path,
539
+ bundleName: AbsolutePath ( bundles [ 0 ] . path) . components. last!
540
+ ) ,
541
+ . installationSuccessful(
542
+ bundlePathOrURL: bundles [ 1 ] . path,
543
+ bundleName: AbsolutePath ( bundles [ 1 ] . path) . components. last!
544
+ ) ,
545
+ ] )
546
+ }
478
547
}
0 commit comments