Skip to content

Commit 99a4b90

Browse files
Test: Cover wasm32-unknown-wasip1 with Embedded Swift mode
1 parent bb99be7 commit 99a4b90

File tree

1 file changed

+63
-7
lines changed

1 file changed

+63
-7
lines changed

Plugins/PackageToJS/Tests/ExampleTests.swift

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,37 @@ 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
20+
else {
21+
return false
22+
}
23+
func sanityCheckCompatibility(triple: String) -> Bool {
24+
return swiftSDKID.hasSuffix(triple)
25+
}
26+
// For compatibility with old SDKs, we check wasm32-unknown-wasi as well when
27+
// wasm32-unknown-wasip1 is requested.
28+
if triple == "wasm32-unknown-wasip1" {
29+
if sanityCheckCompatibility(triple: "wasm32-unknown-wasi") {
30+
return true
31+
}
32+
}
33+
return sanityCheckCompatibility(triple: triple)
34+
}(),
2035
"Requires SWIFT_SDK_ID and SWIFT_PATH environment variables"
2136
)
2237
}
2338

24-
static var requireEmbeddedSwift: ConditionTrait {
39+
static func requireEmbeddedSwift(triple: String) -> ConditionTrait {
2540
// Check if $SWIFT_PATH/../lib/swift/embedded/wasm32-unknown-none-wasm/ exists
2641
return .enabled(
2742
if: {
2843
guard let swiftPath = ProcessInfo.processInfo.environment["SWIFT_PATH"] else {
2944
return false
3045
}
3146
let embeddedPath = URL(fileURLWithPath: swiftPath).deletingLastPathComponent()
32-
.appending(path: "lib/swift/embedded/wasm32-unknown-none-wasm")
47+
.appending(path: "lib/swift/embedded/\(triple)")
3348
return FileManager.default.fileExists(atPath: embeddedPath.path)
3449
}(),
3550
"Requires embedded Swift SDK under $SWIFT_PATH/../lib/swift/embedded"
@@ -46,6 +61,28 @@ extension Trait where Self == ConditionTrait {
4661
ProcessInfo.processInfo.environment["SWIFT_PATH"]
4762
}
4863

64+
/// Check if the Swift SDK with the given ID is available.
65+
static func isSwiftSDKAvailable(_ id: String) throws -> Bool {
66+
let swiftExecutable = URL(
67+
fileURLWithPath: "swift",
68+
relativeTo: URL(fileURLWithPath: try #require(Self.getSwiftPath()))
69+
)
70+
let process = Process()
71+
process.executableURL = swiftExecutable
72+
let arguments = ["sdk", "configure", "--show-configuration", id]
73+
process.arguments = arguments
74+
process.standardOutput = FileHandle.nullDevice
75+
process.standardError = FileHandle.nullDevice
76+
do {
77+
try process.run()
78+
process.waitUntilExit()
79+
return process.terminationStatus == 0
80+
} catch {
81+
try #require(Bool(false), "Failed to run swift \(arguments.joined(separator: " ")) with error: \(error)")
82+
return false
83+
}
84+
}
85+
4986
static let repoPath = URL(fileURLWithPath: #filePath)
5087
.deletingLastPathComponent()
5188
.deletingLastPathComponent()
@@ -220,7 +257,7 @@ extension Trait where Self == ConditionTrait {
220257
let swiftPath = try #require(Self.getSwiftPath())
221258
try withPackage(at: "Examples/Testing") { packageDir, runProcess, runSwift in
222259
try runSwift(
223-
["package", "--swift-sdk", swiftSDKID, "js", "test", "--enable-code-coverage"],
260+
["package", "--disable-sandbox", "--swift-sdk", swiftSDKID, "js", "test", "--enable-code-coverage"],
224261
[
225262
"LLVM_PROFDATA_PATH": URL(fileURLWithPath: swiftPath).appending(path: "llvm-profdata").path
226263
]
@@ -267,7 +304,8 @@ extension Trait where Self == ConditionTrait {
267304
}
268305
}
269306

270-
@Test(.requireEmbeddedSwift) func embedded() throws {
307+
@Test(.requireEmbeddedSwift(triple: "wasm32-unknown-none-wasm"))
308+
func embeddedWasmUnknownNone() throws {
271309
try withPackage(at: "Examples/Embedded") { packageDir, _, runSwift in
272310
try runSwift(
273311
["package", "--triple", "wasm32-unknown-none-wasm", "js", "-c", "release"],
@@ -278,6 +316,24 @@ extension Trait where Self == ConditionTrait {
278316
}
279317
}
280318

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

0 commit comments

Comments
 (0)