Skip to content

Commit fb8c57a

Browse files
Test: Cover wasm32-unknown-wasip1 with Embedded Swift mode
1 parent a2c2963 commit fb8c57a

File tree

1 file changed

+59
-7
lines changed

1 file changed

+59
-7
lines changed

Plugins/PackageToJS/Tests/ExampleTests.swift

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,36 @@ extension Trait where Self == ConditionTrait {
1414

1515
static func requireSwiftSDK(triple: String) -> ConditionTrait {
1616
.enabled(
17-
if: ProcessInfo.processInfo.environment["SWIFT_SDK_ID"] != nil
18-
&& ProcessInfo.processInfo.environment["SWIFT_PATH"] != nil
19-
&& ProcessInfo.processInfo.environment["SWIFT_SDK_ID"]!.hasSuffix(triple),
17+
if: {
18+
guard let swiftSDKID = ProcessInfo.processInfo.environment["SWIFT_SDK_ID"],
19+
ProcessInfo.processInfo.environment["SWIFT_PATH"] != nil else {
20+
return false
21+
}
22+
func sanityCheckCompatibility(triple: String) -> Bool {
23+
return swiftSDKID.hasSuffix(triple)
24+
}
25+
// For compatibility with old SDKs, we check wasm32-unknown-wasi as well when
26+
// wasm32-unknown-wasip1 is requested.
27+
if triple == "wasm32-unknown-wasip1" {
28+
if sanityCheckCompatibility(triple: "wasm32-unknown-wasi") {
29+
return true
30+
}
31+
}
32+
return sanityCheckCompatibility(triple: triple)
33+
}(),
2034
"Requires SWIFT_SDK_ID and SWIFT_PATH environment variables"
2135
)
2236
}
2337

24-
static var requireEmbeddedSwift: ConditionTrait {
38+
static func requireEmbeddedSwift(triple: String) -> ConditionTrait {
2539
// Check if $SWIFT_PATH/../lib/swift/embedded/wasm32-unknown-none-wasm/ exists
2640
return .enabled(
2741
if: {
2842
guard let swiftPath = ProcessInfo.processInfo.environment["SWIFT_PATH"] else {
2943
return false
3044
}
3145
let embeddedPath = URL(fileURLWithPath: swiftPath).deletingLastPathComponent()
32-
.appending(path: "lib/swift/embedded/wasm32-unknown-none-wasm")
46+
.appending(path: "lib/swift/embedded/\(triple)")
3347
return FileManager.default.fileExists(atPath: embeddedPath.path)
3448
}(),
3549
"Requires embedded Swift SDK under $SWIFT_PATH/../lib/swift/embedded"
@@ -46,6 +60,28 @@ extension Trait where Self == ConditionTrait {
4660
ProcessInfo.processInfo.environment["SWIFT_PATH"]
4761
}
4862

63+
/// Check if the Swift SDK with the given ID is available.
64+
static func isSwiftSDKAvailable(_ id: String) throws -> Bool {
65+
let swiftExecutable = URL(
66+
fileURLWithPath: "swift",
67+
relativeTo: URL(fileURLWithPath: try #require(Self.getSwiftPath()))
68+
)
69+
let process = Process()
70+
process.executableURL = swiftExecutable
71+
let arguments = ["sdk", "configure", "--show-configuration", id]
72+
process.arguments = arguments
73+
process.standardOutput = FileHandle.nullDevice
74+
process.standardError = FileHandle.nullDevice
75+
do {
76+
try process.run()
77+
process.waitUntilExit()
78+
return process.terminationStatus == 0
79+
} catch {
80+
try #require(Bool(false), "Failed to run swift \(arguments.joined(separator: " ")) with error: \(error)")
81+
return false
82+
}
83+
}
84+
4985
static let repoPath = URL(fileURLWithPath: #filePath)
5086
.deletingLastPathComponent()
5187
.deletingLastPathComponent()
@@ -220,7 +256,7 @@ extension Trait where Self == ConditionTrait {
220256
let swiftPath = try #require(Self.getSwiftPath())
221257
try withPackage(at: "Examples/Testing") { packageDir, runProcess, runSwift in
222258
try runSwift(
223-
["package", "--swift-sdk", swiftSDKID, "js", "test", "--enable-code-coverage"],
259+
["package", "--disable-sandbox", "--swift-sdk", swiftSDKID, "js", "test", "--enable-code-coverage"],
224260
[
225261
"LLVM_PROFDATA_PATH": URL(fileURLWithPath: swiftPath).appending(path: "llvm-profdata").path
226262
]
@@ -267,7 +303,8 @@ extension Trait where Self == ConditionTrait {
267303
}
268304
}
269305

270-
@Test(.requireEmbeddedSwift) func embedded() throws {
306+
@Test(.requireEmbeddedSwift(triple: "wasm32-unknown-none-wasm"))
307+
func embeddedWasmUnknownNone() throws {
271308
try withPackage(at: "Examples/Embedded") { packageDir, _, runSwift in
272309
try runSwift(
273310
["package", "--triple", "wasm32-unknown-none-wasm", "js", "-c", "release"],
@@ -278,6 +315,21 @@ extension Trait where Self == ConditionTrait {
278315
}
279316
}
280317

318+
@Test(.requireSwiftSDK(triple: "wasm32-unknown-wasip1"))
319+
func embeddedWasmUnknownWasi() throws {
320+
let baseSwiftSDKID = try #require(Self.getSwiftSDKID())
321+
let swiftSDKID = "\(baseSwiftSDKID)-embedded"
322+
try #require(try Self.isSwiftSDKAvailable(swiftSDKID), "Embedded Swift SDK with ID \(swiftSDKID) is not available")
323+
try withPackage(at: "Examples/Embedded") { packageDir, _, runSwift in
324+
try runSwift(
325+
["package", "--swift-sdk", swiftSDKID, "js", "-c", "release"],
326+
[
327+
"JAVASCRIPTKIT_EXPERIMENTAL_EMBEDDED_WASM": "true"
328+
]
329+
)
330+
}
331+
}
332+
281333
@Test(.requireSwiftSDK)
282334
func continuationLeakInTest_XCTest() throws {
283335
let swiftSDKID = try #require(Self.getSwiftSDKID())

0 commit comments

Comments
 (0)