@@ -93,7 +93,9 @@ struct PackageToJSPlugin: CommandPlugin {
93
93
// Build products
94
94
let productName = try buildOptions. product ?? deriveDefaultProduct ( package : context. package )
95
95
let build = try buildWasm (
96
- productName: productName, context: context)
96
+ productName: productName, context: context,
97
+ enableCodeCoverage: buildOptions. packageOptions. enableCodeCoverage
98
+ )
97
99
guard build. succeeded else {
98
100
reportBuildFailure ( build, arguments)
99
101
exit ( 1 )
@@ -145,7 +147,9 @@ struct PackageToJSPlugin: CommandPlugin {
145
147
146
148
let productName = " \( context. package . displayName) PackageTests "
147
149
let build = try buildWasm (
148
- productName: productName, context: context)
150
+ productName: productName, context: context,
151
+ enableCodeCoverage: testOptions. packageOptions. enableCodeCoverage
152
+ )
149
153
guard build. succeeded else {
150
154
reportBuildFailure ( build, arguments)
151
155
exit ( 1 )
@@ -198,36 +202,18 @@ struct PackageToJSPlugin: CommandPlugin {
198
202
try make. build ( output: rootTask, scope: scope)
199
203
print ( " Packaging tests finished " )
200
204
201
- let testRunner = scope. resolve ( path: binDir. appending ( path: " test.js " ) )
202
205
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 " ) )
222
207
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
226
212
)
227
213
}
228
214
}
229
215
230
- private func buildWasm( productName: String , context: PluginContext ) throws
216
+ private func buildWasm( productName: String , context: PluginContext , enableCodeCoverage : Bool ) throws
231
217
-> PackageManager . BuildResult
232
218
{
233
219
var parameters = PackageManager . BuildParameters (
@@ -248,6 +234,12 @@ struct PackageToJSPlugin: CommandPlugin {
248
234
parameters. otherLinkerFlags = [
249
235
" --export-if-defined=__main_argc_argv "
250
236
]
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
+ }
251
243
}
252
244
return try self . packageManager. build ( . product( productName) , parameters: parameters)
253
245
}
@@ -292,8 +284,9 @@ extension PackageToJS.PackageOptions {
292
284
let packageName = extractor. extractOption ( named: " package-name " ) . last
293
285
let explain = extractor. extractFlag ( named: " explain " )
294
286
let useCDN = extractor. extractFlag ( named: " use-cdn " )
287
+ let enableCodeCoverage = extractor. extractFlag ( named: " enable-code-coverage " )
295
288
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
297
290
)
298
291
}
299
292
}
@@ -314,12 +307,14 @@ extension PackageToJS.BuildOptions {
314
307
USAGE: swift package --swift-sdk <swift-sdk> [SwiftPM options] PackageToJS [options] [subcommand]
315
308
316
309
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)
323
318
324
319
SUBCOMMANDS:
325
320
test Builds and runs tests
@@ -365,10 +360,12 @@ extension PackageToJS.TestOptions {
365
360
USAGE: swift package --swift-sdk <swift-sdk> [SwiftPM options] PackageToJS test [options]
366
361
367
362
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)
372
369
373
370
EXAMPLES:
374
371
$ swift package --swift-sdk wasm32-unknown-wasi plugin js test
0 commit comments