Skip to content

Commit ee640b9

Browse files
authored
Swift SDKs: refactor and extend SwiftSDKBundleTests (#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 022b215 commit ee640b9

File tree

3 files changed

+108
-42
lines changed

3 files changed

+108
-42
lines changed

Sources/PackageModel/SwiftSDKBundle.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public struct SwiftSDKBundle {
131131
/// - observabilityScope: Observability scope for reporting warnings and errors.
132132
public static func install(
133133
bundlePathOrURL: String,
134-
destinationsDirectory: AbsolutePath,
134+
swiftSDKsDirectory: AbsolutePath,
135135
_ fileSystem: some FileSystem,
136136
_ archiver: some Archiver,
137137
_ observabilityScope: ObservabilityScope
@@ -179,7 +179,7 @@ public struct SwiftSDKBundle {
179179

180180
try installIfValid(
181181
bundlePath: bundlePath,
182-
destinationsDirectory: destinationsDirectory,
182+
destinationsDirectory: swiftSDKsDirectory,
183183
temporaryDirectory: temporaryDirectory,
184184
fileSystem,
185185
archiver,

Sources/SwiftSDKTool/InstallSwiftSDK.swift

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

Tests/PackageModelTests/SwiftSDKBundleTests.swift

Lines changed: 105 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,51 +16,90 @@ import SPMTestSupport
1616
import XCTest
1717

1818
import struct TSCBasic.ByteString
19+
import protocol TSCBasic.FileSystem
1920
import class TSCBasic.InMemoryFileSystem
2021

2122
private let testArtifactID = "test-artifact"
2223

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

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

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

5998
let archiver = MockArchiver()
6099

61100
try SwiftSDKBundle.install(
62-
bundlePathOrURL: bundlePath1,
63-
destinationsDirectory: destinationsDirectory,
101+
bundlePathOrURL: bundles[0].path,
102+
swiftSDKsDirectory: swiftSDKsDirectory,
64103
fileSystem,
65104
archiver,
66105
system.topScope
@@ -70,7 +109,7 @@ final class SwiftSDKBundleTests: XCTestCase {
70109
do {
71110
try SwiftSDKBundle.install(
72111
bundlePathOrURL: "foobar",
73-
destinationsDirectory: destinationsDirectory,
112+
swiftSDKsDirectory: swiftSDKsDirectory,
74113
fileSystem,
75114
archiver,
76115
system.topScope
@@ -83,7 +122,6 @@ final class SwiftSDKBundleTests: XCTestCase {
83122
return
84123
}
85124

86-
print(error)
87125
switch error {
88126
case .invalidBundleName(let bundleName):
89127
XCTAssertEqual(bundleName, invalidPath)
@@ -94,8 +132,8 @@ final class SwiftSDKBundleTests: XCTestCase {
94132

95133
do {
96134
try SwiftSDKBundle.install(
97-
bundlePathOrURL: bundlePath1,
98-
destinationsDirectory: destinationsDirectory,
135+
bundlePathOrURL: bundles[0].path,
136+
swiftSDKsDirectory: swiftSDKsDirectory,
99137
fileSystem,
100138
archiver,
101139
system.topScope
@@ -110,16 +148,16 @@ final class SwiftSDKBundleTests: XCTestCase {
110148

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

119157
do {
120158
try SwiftSDKBundle.install(
121-
bundlePathOrURL: bundlePath2,
122-
destinationsDirectory: destinationsDirectory,
159+
bundlePathOrURL: bundles[1].path,
160+
swiftSDKsDirectory: swiftSDKsDirectory,
123161
fileSystem,
124162
archiver,
125163
system.topScope
@@ -134,12 +172,40 @@ final class SwiftSDKBundleTests: XCTestCase {
134172

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

0 commit comments

Comments
 (0)