@@ -93,7 +93,9 @@ struct PackageToJSPlugin: CommandPlugin {
9393 // Build products
9494 let productName = try buildOptions. product ?? deriveDefaultProduct ( package : context. package )
9595 let build = try buildWasm (
96- productName: productName, context: context)
96+ productName: productName, context: context,
97+ enableCodeCoverage: buildOptions. packageOptions. enableCodeCoverage
98+ )
9799 guard build. succeeded else {
98100 reportBuildFailure ( build, arguments)
99101 exit ( 1 )
@@ -145,7 +147,9 @@ struct PackageToJSPlugin: CommandPlugin {
145147
146148 let productName = " \( context. package . displayName) PackageTests "
147149 let build = try buildWasm (
148- productName: productName, context: context)
150+ productName: productName, context: context,
151+ enableCodeCoverage: testOptions. packageOptions. enableCodeCoverage
152+ )
149153 guard build. succeeded else {
150154 reportBuildFailure ( build, arguments)
151155 exit ( 1 )
@@ -198,36 +202,18 @@ struct PackageToJSPlugin: CommandPlugin {
198202 try make. build ( output: rootTask, scope: scope)
199203 print ( " Packaging tests finished " )
200204
201- let testRunner = scope. resolve ( path: binDir. appending ( path: " test.js " ) )
202205 if !testOptions. buildOnly {
203- var testJsArguments : [ String ] = [ ]
204- var testFrameworkArguments : [ String ] = [ ]
205- if testOptions. listTests {
206- testFrameworkArguments += [ " --list-tests " ]
207- }
208- if let prelude = testOptions. prelude {
209- let preludeURL = URL ( fileURLWithPath: prelude, relativeTo: URL ( fileURLWithPath: FileManager . default. currentDirectoryPath) )
210- testJsArguments += [ " --prelude " , preludeURL. path]
211- }
212- if let environment = testOptions. environment {
213- testJsArguments += [ " --environment " , environment]
214- }
215- if testOptions. inspect {
216- testJsArguments += [ " --inspect " ]
217- }
218- try PackageToJS . runTest (
219- testRunner: testRunner, currentDirectoryURL: context. pluginWorkDirectoryURL,
220- extraArguments: testJsArguments + [ " -- " ] + testFrameworkArguments + testOptions. filter
221- )
206+ let testRunner = scope. resolve ( path: binDir. appending ( path: " test.js " ) )
222207 try PackageToJS . runTest (
223- testRunner: testRunner, currentDirectoryURL: context. pluginWorkDirectoryURL,
224- extraArguments: testJsArguments + [ " -- " , " --testing-library " , " swift-testing " ] + testFrameworkArguments
225- + testOptions. filter. flatMap { [ " --filter " , $0] }
208+ testRunner: testRunner,
209+ currentDirectoryURL: context. pluginWorkDirectoryURL,
210+ outputDir: outputDir,
211+ testOptions: testOptions
226212 )
227213 }
228214 }
229215
230- private func buildWasm( productName: String , context: PluginContext ) throws
216+ private func buildWasm( productName: String , context: PluginContext , enableCodeCoverage : Bool ) throws
231217 -> PackageManager . BuildResult
232218 {
233219 var parameters = PackageManager . BuildParameters (
@@ -248,6 +234,12 @@ struct PackageToJSPlugin: CommandPlugin {
248234 parameters. otherLinkerFlags = [
249235 " --export-if-defined=__main_argc_argv "
250236 ]
237+
238+ // Enable code coverage options if requested
239+ if enableCodeCoverage {
240+ parameters. otherSwiftcFlags += [ " -profile-coverage-mapping " , " -profile-generate " ]
241+ parameters. otherCFlags += [ " -fprofile-instr-generate " , " -fcoverage-mapping " ]
242+ }
251243 }
252244 return try self . packageManager. build ( . product( productName) , parameters: parameters)
253245 }
@@ -292,8 +284,9 @@ extension PackageToJS.PackageOptions {
292284 let packageName = extractor. extractOption ( named: " package-name " ) . last
293285 let explain = extractor. extractFlag ( named: " explain " )
294286 let useCDN = extractor. extractFlag ( named: " use-cdn " )
287+ let enableCodeCoverage = extractor. extractFlag ( named: " enable-code-coverage " )
295288 return PackageToJS . PackageOptions (
296- outputPath: outputPath, packageName: packageName, explain: explain != 0 , useCDN: useCDN != 0
289+ outputPath: outputPath, packageName: packageName, explain: explain != 0 , useCDN: useCDN != 0 , enableCodeCoverage : enableCodeCoverage != 0
297290 )
298291 }
299292}
@@ -314,12 +307,14 @@ extension PackageToJS.BuildOptions {
314307 USAGE: swift package --swift-sdk <swift-sdk> [SwiftPM options] PackageToJS [options] [subcommand]
315308
316309 OPTIONS:
317- --product <product> Product to build (default: executable target if there's only one)
318- --output <path> Path to the output directory (default: .build/plugins/PackageToJS/outputs/Package)
319- --package-name <name> Name of the package (default: lowercased Package.swift name)
320- --explain Whether to explain the build plan
321- --split-debug Whether to split debug information into a separate .wasm.debug file (default: false)
322- --no-optimize Whether to disable wasm-opt optimization (default: false)
310+ --product <product> Product to build (default: executable target if there's only one)
311+ --output <path> Path to the output directory (default: .build/plugins/PackageToJS/outputs/Package)
312+ --package-name <name> Name of the package (default: lowercased Package.swift name)
313+ --explain Whether to explain the build plan (default: false)
314+ --split-debug Whether to split debug information into a separate .wasm.debug file (default: false)
315+ --no-optimize Whether to disable wasm-opt optimization (default: false)
316+ --use-cdn Whether to use CDN for dependency packages (default: false)
317+ --enable-code-coverage Whether to enable code coverage collection (default: false)
323318
324319 SUBCOMMANDS:
325320 test Builds and runs tests
@@ -365,10 +360,12 @@ extension PackageToJS.TestOptions {
365360 USAGE: swift package --swift-sdk <swift-sdk> [SwiftPM options] PackageToJS test [options]
366361
367362 OPTIONS:
368- --build-only Whether to build only (default: false)
369- --prelude <path> Path to the prelude script
370- --environment <name> The environment to use for the tests
371- --inspect Whether to run tests in the browser with inspector enabled
363+ --build-only Whether to build only (default: false)
364+ --prelude <path> Path to the prelude script
365+ --environment <name> The environment to use for the tests
366+ --inspect Whether to run tests in the browser with inspector enabled
367+ --use-cdn Whether to use CDN for dependency packages (default: false)
368+ --enable-code-coverage Whether to enable code coverage collection (default: false)
372369
373370 EXAMPLES:
374371 $ swift package --swift-sdk wasm32-unknown-wasi plugin js test
0 commit comments