Skip to content

Commit b1e1812

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

File tree

1 file changed

+77
-7
lines changed

1 file changed

+77
-7
lines changed

Plugins/PackageToJS/Tests/ExampleTests.swift

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,78 @@ 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 requireEmbeddedSwiftInToolchain(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"
3651
)
3752
}
53+
54+
static func requireEmbeddedSwiftInSwiftSDK() -> ConditionTrait {
55+
// Check if ${SWIFT_SDK_ID}-embedded is available
56+
return .enabled(
57+
if: {
58+
/// Check if the Swift SDK with the given ID is available.
59+
func isSwiftSDKAvailable(_ id: String, swiftPath: String) -> Bool {
60+
let swiftExecutable = URL(
61+
fileURLWithPath: "swift",
62+
relativeTo: URL(fileURLWithPath: swiftPath)
63+
)
64+
let process = Process()
65+
process.executableURL = swiftExecutable
66+
let arguments = ["sdk", "configure", "--show-configuration", id]
67+
process.arguments = arguments
68+
process.standardOutput = FileHandle.nullDevice
69+
process.standardError = FileHandle.nullDevice
70+
do {
71+
try process.run()
72+
process.waitUntilExit()
73+
return process.terminationStatus == 0
74+
} catch {
75+
return false
76+
}
77+
}
78+
guard let swiftPath = ProcessInfo.processInfo.environment["SWIFT_PATH"],
79+
let swiftSDKID = ProcessInfo.processInfo.environment["SWIFT_SDK_ID"]
80+
else {
81+
return false
82+
}
83+
let embeddedSDKID = "\(swiftSDKID)-embedded"
84+
return isSwiftSDKAvailable(embeddedSDKID, swiftPath: swiftPath)
85+
}(),
86+
"Requires SWIFT_SDK_ID to contain 'embedded'"
87+
)
88+
}
3889
}
3990

4091
@Suite struct ExampleTests {
@@ -46,6 +97,11 @@ extension Trait where Self == ConditionTrait {
4697
ProcessInfo.processInfo.environment["SWIFT_PATH"]
4798
}
4899

100+
static func getEmbeddedSwiftSDKID() -> String? {
101+
guard let swiftSDKID = getSwiftSDKID() else { return nil }
102+
return "\(swiftSDKID)-embedded"
103+
}
104+
49105
static let repoPath = URL(fileURLWithPath: #filePath)
50106
.deletingLastPathComponent()
51107
.deletingLastPathComponent()
@@ -220,7 +276,7 @@ extension Trait where Self == ConditionTrait {
220276
let swiftPath = try #require(Self.getSwiftPath())
221277
try withPackage(at: "Examples/Testing") { packageDir, runProcess, runSwift in
222278
try runSwift(
223-
["package", "--swift-sdk", swiftSDKID, "js", "test", "--enable-code-coverage"],
279+
["package", "--disable-sandbox", "--swift-sdk", swiftSDKID, "js", "test", "--enable-code-coverage"],
224280
[
225281
"LLVM_PROFDATA_PATH": URL(fileURLWithPath: swiftPath).appending(path: "llvm-profdata").path
226282
]
@@ -267,7 +323,8 @@ extension Trait where Self == ConditionTrait {
267323
}
268324
}
269325

270-
@Test(.requireEmbeddedSwift) func embedded() throws {
326+
@Test(.requireEmbeddedSwiftInToolchain(triple: "wasm32-unknown-none-wasm"))
327+
func embeddedWasmUnknownNone() throws {
271328
try withPackage(at: "Examples/Embedded") { packageDir, _, runSwift in
272329
try runSwift(
273330
["package", "--triple", "wasm32-unknown-none-wasm", "js", "-c", "release"],
@@ -278,6 +335,19 @@ extension Trait where Self == ConditionTrait {
278335
}
279336
}
280337

338+
@Test(.requireEmbeddedSwiftInSwiftSDK())
339+
func embeddedWasmUnknownWasi() throws {
340+
let swiftSDKID = try #require(Self.getEmbeddedSwiftSDKID())
341+
try withPackage(at: "Examples/Embedded") { packageDir, _, runSwift in
342+
try runSwift(
343+
["package", "--swift-sdk", swiftSDKID, "js", "-c", "release"],
344+
[
345+
"JAVASCRIPTKIT_EXPERIMENTAL_EMBEDDED_WASM": "true"
346+
]
347+
)
348+
}
349+
}
350+
281351
@Test(.requireSwiftSDK)
282352
func continuationLeakInTest_XCTest() throws {
283353
let swiftSDKID = try #require(Self.getSwiftSDKID())

0 commit comments

Comments
 (0)