Skip to content

Commit 4b6abeb

Browse files
authored
[5.9] Swift SDKs: refactor and extend SwiftSDKBundleTests (#6589)
Cherry-pick of #6588 `SwiftSDKBundleTests` should cover the `list` subcommand and verify that all installed Swift SDK bundles are listed. New `testList` test function was added, while `testInstall` was refactored to share mock Swift SDK generation code with it. rdar://107882144
1 parent ff80c0a commit 4b6abeb

File tree

3 files changed

+109
-43
lines changed

3 files changed

+109
-43
lines changed

Sources/PackageModel/SwiftSDKBundle.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public struct SwiftSDKBundle {
134134
/// - observabilityScope: Observability scope for reporting warnings and errors.
135135
public static func install(
136136
bundlePathOrURL: String,
137-
destinationsDirectory: AbsolutePath,
137+
swiftSDKsDirectory: AbsolutePath,
138138
_ fileSystem: some FileSystem,
139139
_ archiver: some Archiver,
140140
_ observabilityScope: ObservabilityScope
@@ -182,7 +182,7 @@ public struct SwiftSDKBundle {
182182

183183
try installIfValid(
184184
bundlePath: bundlePath,
185-
destinationsDirectory: destinationsDirectory,
185+
destinationsDirectory: swiftSDKsDirectory,
186186
temporaryDirectory: temporaryDirectory,
187187
fileSystem,
188188
archiver,

Sources/SwiftSDKTool/InstallSwiftSDK.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public struct InstallSwiftSDK: SwiftSDKSubcommand {
4747
cancellator.installSignalHandlers()
4848
try SwiftSDKBundle.install(
4949
bundlePathOrURL: bundlePathOrURL,
50-
destinationsDirectory: destinationsDirectory,
50+
swiftSDKsDirectory: destinationsDirectory,
5151
self.fileSystem,
5252
UniversalArchiver(self.fileSystem, cancellator),
5353
observabilityScope

Tests/PackageModelTests/SwiftSDKBundleTests.swift

Lines changed: 106 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,51 +17,90 @@ import XCTest
1717

1818
import struct TSCBasic.AbsolutePath
1919
import struct TSCBasic.ByteString
20+
import protocol TSCBasic.FileSystem
2021
import class TSCBasic.InMemoryFileSystem
2122

2223
private let testArtifactID = "test-artifact"
2324

24-
private let infoJSON = ByteString(stringLiteral: """
25-
{
26-
"artifacts" : {
27-
"\(testArtifactID)" : {
28-
"type" : "swiftSDK",
29-
"version" : "0.0.1",
30-
"variants" : [
31-
{
32-
"path" : "\(testArtifactID)/aarch64-unknown-linux",
33-
"supportedTriples" : [
34-
"arm64-apple-macosx13.0"
35-
]
36-
}
37-
]
25+
private func generateInfoJSON(artifacts: [MockArtifact]) -> String {
26+
"""
27+
{
28+
"artifacts" : {
29+
\(artifacts.map {
30+
"""
31+
"\($0.id)" : {
32+
"type" : "swiftSDK",
33+
"version" : "0.0.1",
34+
"variants" : [
35+
{
36+
"path" : "\($0.id)/aarch64-unknown-linux",
37+
"supportedTriples" : \($0.supportedTriples.map(\.tripleString))
38+
}
39+
]
40+
}
41+
"""
42+
}.joined(separator: ",\n")
43+
)
44+
},
45+
"schemaVersion" : "1.0"
46+
}
47+
"""
48+
}
49+
50+
private struct MockBundle {
51+
let name: String
52+
let path: String
53+
let artifacts: [MockArtifact]
54+
}
55+
56+
private struct MockArtifact {
57+
let id: String
58+
let supportedTriples: [Triple]
59+
}
60+
61+
private func generateTestFileSystem(bundleArtifacts: [MockArtifact]) throws -> (some FileSystem, [MockBundle], AbsolutePath) {
62+
let bundles = bundleArtifacts.enumerated().map { (i, artifacts) in
63+
let bundleName = "test\(i).artifactbundle"
64+
return MockBundle(name: "test\(i).artifactbundle", path: "/\(bundleName)", artifacts: [artifacts])
3865
}
39-
},
40-
"schemaVersion" : "1.0"
66+
67+
let fileSystem = InMemoryFileSystem(
68+
files: Dictionary(uniqueKeysWithValues: bundles.map {
69+
(
70+
"\($0.path)/info.json",
71+
ByteString(
72+
encodingAsUTF8: generateInfoJSON(artifacts: $0.artifacts)
73+
)
74+
)
75+
})
76+
)
77+
78+
let swiftSDKsDirectory = try AbsolutePath(validating: "/sdks")
79+
try fileSystem.createDirectory(fileSystem.tempDirectory)
80+
try fileSystem.createDirectory(swiftSDKsDirectory)
81+
82+
return (fileSystem, bundles, swiftSDKsDirectory)
4183
}
42-
""")
84+
85+
private let arm64Triple = try! Triple("arm64-apple-macosx13.0")
86+
let i686Triple = try! Triple("i686-apple-macosx13.0")
4387

4488
final class SwiftSDKBundleTests: XCTestCase {
4589
func testInstall() async throws {
4690
let system = ObservabilitySystem.makeForTesting()
4791

48-
let bundleName1 = "test1.artifactbundle"
49-
let bundleName2 = "test2.artifactbundle"
50-
let bundlePath1 = "/\(bundleName1)"
51-
let bundlePath2 = "/\(bundleName2)"
52-
let destinationsDirectory = try AbsolutePath(validating: "/destinations")
53-
let fileSystem = InMemoryFileSystem(files: [
54-
"\(bundlePath1)/info.json": infoJSON,
55-
"\(bundlePath2)/info.json": infoJSON,
56-
])
57-
try fileSystem.createDirectory(fileSystem.tempDirectory)
58-
try fileSystem.createDirectory(destinationsDirectory)
92+
let (fileSystem, bundles, swiftSDKsDirectory) = try generateTestFileSystem(
93+
bundleArtifacts: [
94+
.init(id: testArtifactID, supportedTriples: [arm64Triple]),
95+
.init(id: testArtifactID, supportedTriples: [arm64Triple])
96+
]
97+
)
5998

6099
let archiver = MockArchiver()
61100

62101
try SwiftSDKBundle.install(
63-
bundlePathOrURL: bundlePath1,
64-
destinationsDirectory: destinationsDirectory,
102+
bundlePathOrURL: bundles[0].path,
103+
swiftSDKsDirectory: swiftSDKsDirectory,
65104
fileSystem,
66105
archiver,
67106
system.topScope
@@ -70,8 +109,8 @@ final class SwiftSDKBundleTests: XCTestCase {
70109
let invalidPath = "foobar"
71110
do {
72111
try SwiftSDKBundle.install(
73-
bundlePathOrURL: invalidPath,
74-
destinationsDirectory: destinationsDirectory,
112+
bundlePathOrURL: "foobar",
113+
swiftSDKsDirectory: swiftSDKsDirectory,
75114
fileSystem,
76115
archiver,
77116
system.topScope
@@ -84,7 +123,6 @@ final class SwiftSDKBundleTests: XCTestCase {
84123
return
85124
}
86125

87-
print(error)
88126
switch error {
89127
case .invalidBundleName(let bundleName):
90128
XCTAssertEqual(bundleName, invalidPath)
@@ -95,8 +133,8 @@ final class SwiftSDKBundleTests: XCTestCase {
95133

96134
do {
97135
try SwiftSDKBundle.install(
98-
bundlePathOrURL: bundlePath1,
99-
destinationsDirectory: destinationsDirectory,
136+
bundlePathOrURL: bundles[0].path,
137+
swiftSDKsDirectory: swiftSDKsDirectory,
100138
fileSystem,
101139
archiver,
102140
system.topScope
@@ -111,16 +149,16 @@ final class SwiftSDKBundleTests: XCTestCase {
111149

112150
switch error {
113151
case .destinationBundleAlreadyInstalled(let installedBundleName):
114-
XCTAssertEqual(bundleName1, installedBundleName)
152+
XCTAssertEqual(bundles[0].name, installedBundleName)
115153
default:
116154
XCTFail("Unexpected error value")
117155
}
118156
}
119157

120158
do {
121159
try SwiftSDKBundle.install(
122-
bundlePathOrURL: bundlePath2,
123-
destinationsDirectory: destinationsDirectory,
160+
bundlePathOrURL: bundles[1].path,
161+
swiftSDKsDirectory: swiftSDKsDirectory,
124162
fileSystem,
125163
archiver,
126164
system.topScope
@@ -135,12 +173,40 @@ final class SwiftSDKBundleTests: XCTestCase {
135173

136174
switch error {
137175
case .destinationArtifactAlreadyInstalled(let installedBundleName, let newBundleName, let artifactID):
138-
XCTAssertEqual(bundleName1, installedBundleName)
139-
XCTAssertEqual(bundleName2, newBundleName)
176+
XCTAssertEqual(bundles[0].name, installedBundleName)
177+
XCTAssertEqual(bundles[1].name, newBundleName)
140178
XCTAssertEqual(artifactID, testArtifactID)
141179
default:
142180
XCTFail("Unexpected error value")
143181
}
144182
}
145183
}
184+
185+
func testList() async throws {
186+
let (fileSystem, bundles, swiftSDKsDirectory) = try generateTestFileSystem(
187+
bundleArtifacts: [
188+
.init(id: "\(testArtifactID)1", supportedTriples: [arm64Triple]),
189+
.init(id: "\(testArtifactID)2", supportedTriples: [i686Triple])
190+
]
191+
)
192+
let system = ObservabilitySystem.makeForTesting()
193+
194+
for bundle in bundles {
195+
try SwiftSDKBundle.install(
196+
bundlePathOrURL: bundle.path,
197+
swiftSDKsDirectory: swiftSDKsDirectory,
198+
fileSystem,
199+
MockArchiver(),
200+
system.topScope
201+
)
202+
}
203+
204+
let validBundles = try SwiftSDKBundle.getAllValidBundles(
205+
swiftSDKsDirectory: swiftSDKsDirectory,
206+
fileSystem: fileSystem,
207+
observabilityScope: system.topScope
208+
)
209+
210+
XCTAssertEqual(validBundles.count, bundles.count)
211+
}
146212
}

0 commit comments

Comments
 (0)