@@ -40,7 +40,7 @@ struct PackageToJS: CommandPlugin {
4040
4141 OPTIONS:
4242 --product <product> Product to build (default: executable target if there's only one)
43- --output <path> Path to the output directory (default: .build/plugins/PackageToJS/outputs/Package)
43+ --output <path> Path to the output directory (default: .build/plugins/PackageToJS/outputs/Package)
4444 --package-name <name> Name of the package (default: lowercased Package.swift name)
4545 --explain Whether to explain the build plan
4646
@@ -75,22 +75,26 @@ struct PackageToJS: CommandPlugin {
7575 let testLibrary = extractor. extractOption ( named: " test-library " ) . last
7676 let filter = extractor. extractOption ( named: " filter " )
7777 let options = Options . parse ( from: & extractor)
78- return TestOptions ( buildOnly: buildOnly != 0 , listTests: listTests != 0 , testLibrary: testLibrary, filter: filter, options: options)
78+ return TestOptions (
79+ buildOnly: buildOnly != 0 , listTests: listTests != 0 , testLibrary: testLibrary,
80+ filter: filter, options: options
81+ )
7982 }
8083
8184 static func help( ) -> String {
8285 return """
8386 OVERVIEW: Builds and runs tests
8487
8588 USAGE: swift package --swift-sdk <swift-sdk> [SwiftPM options] PackageToJS test [options]
86-
89+
8790 OPTIONS:
8891 --build-only Whether to build only (default: false)
8992
9093 EXAMPLES:
9194 $ swift package --swift-sdk wasm32-unknown-wasi plugin js test
9295 # Just build tests, don't run them
9396 $ swift package --swift-sdk wasm32-unknown-wasi plugin js test --build-only
97+ $ node .build/plugins/PackageToJS/outputs/PackageTests/bin/test.js
9498 """
9599 }
96100 }
@@ -174,7 +178,8 @@ struct PackageToJS: CommandPlugin {
174178
175179 // Build products
176180 let productName = try buildOptions. product ?? deriveDefaultProduct ( package : context. package )
177- let build = try buildWasm ( productName: productName, context: context)
181+ let build = try buildWasm (
182+ productName: productName, context: context, options: buildOptions. options)
178183 guard build. succeeded else {
179184 Self . reportBuildFailure ( build, arguments)
180185 exit ( 1 )
@@ -214,13 +219,15 @@ struct PackageToJS: CommandPlugin {
214219 let testOptions = TestOptions . parse ( from: & extractor)
215220
216221 if extractor. remainingArguments. count > 0 {
217- printStderr ( " Unexpected arguments: \( extractor. remainingArguments. joined ( separator: " " ) ) " )
222+ printStderr (
223+ " Unexpected arguments: \( extractor. remainingArguments. joined ( separator: " " ) ) " )
218224 printStderr ( TestOptions . help ( ) )
219225 exit ( 1 )
220226 }
221227
222228 let productName = " \( context. package . displayName) PackageTests "
223- let build = try buildWasm ( productName: productName, context: context)
229+ let build = try buildWasm (
230+ productName: productName, context: context, options: testOptions. options)
224231 guard build. succeeded else {
225232 Self . reportBuildFailure ( build, arguments)
226233 exit ( 1 )
@@ -274,9 +281,15 @@ struct PackageToJS: CommandPlugin {
274281 if testOptions. listTests {
275282 extraArguments += [ " --list-tests " ]
276283 }
277- try runTest ( testRunner: testRunner, context: context, extraArguments: extraArguments + testOptions. filter)
278- try runTest ( testRunner: testRunner, context: context,
279- extraArguments: [ " --testing-library " , " swift-testing " ] + extraArguments + testOptions. filter. flatMap { [ " --filter " , $0] } )
284+ try runTest (
285+ testRunner: testRunner, context: context,
286+ extraArguments: extraArguments + testOptions. filter
287+ )
288+ try runTest (
289+ testRunner: testRunner, context: context,
290+ extraArguments: [ " --testing-library " , " swift-testing " ] + extraArguments
291+ + testOptions. filter. flatMap { [ " --filter " , $0] }
292+ )
280293 }
281294 }
282295
@@ -292,12 +305,13 @@ struct PackageToJS: CommandPlugin {
292305 task. currentDirectoryURL = context. pluginWorkDirectoryURL
293306 try task. run ( )
294307 task. waitUntilExit ( )
295- guard task. terminationStatus == 0 else {
308+ // swift-testing returns EX_UNAVAILABLE (which is 69 in wasi-libc) for "no tests found"
309+ guard task. terminationStatus == 0 || task. terminationStatus == 69 else {
296310 throw PackageToJSError ( " Test failed with status \( task. terminationStatus) " )
297311 }
298312 }
299313
300- private func buildWasm( productName: String , context: PluginContext ) throws
314+ private func buildWasm( productName: String , context: PluginContext , options : Options ) throws
301315 -> PackageManager . BuildResult
302316 {
303317 var parameters = PackageManager . BuildParameters (
@@ -385,12 +399,7 @@ private func findPackageInDependencies(package: Package, id: Package.ID) -> Pack
385399 func visit( package : Package ) -> Package ? {
386400 if visited. contains ( package . id) { return nil }
387401 visited. insert ( package . id)
388- for dependency in package . dependencies {
389- let dependencyPackage = dependency. package
390- if dependencyPackage. id == id {
391- return dependencyPackage
392- }
393- }
402+ if package . id == id { return package }
394403 for dependency in package . dependencies {
395404 if let found = visit ( package : dependency. package ) {
396405 return found
0 commit comments