@@ -266,30 +266,68 @@ def _swift_linking_rule_impl(
266
266
267
267
return out_bin , providers
268
268
269
- def _create_xctest_runner (name , actions , binary , xctest_runner_template ):
270
- """Creates a script that will bundle a test binary and launch `xctest` .
269
+ def _create_xctest_bundle (name , actions , binary ):
270
+ """Creates an `.xctest` bundle that contains the given binary .
271
271
272
272
Args:
273
273
name: The name of the target being built, which will be used as the
274
- basename of the bundle (followed by the ` .xctest` bundle extension).
274
+ basename of the bundle (followed by the .xctest bundle extension).
275
275
actions: The context's actions object.
276
- binary: The `File` representing the test binary that should be bundled
277
- and executed.
276
+ binary: The binary that will be copied into the test bundle.
277
+
278
+ Returns:
279
+ A `File` (tree artifact) representing the `.xctest` bundle.
280
+ """
281
+ xctest_bundle = derived_files .xctest_bundle (
282
+ actions = actions ,
283
+ target_name = name ,
284
+ )
285
+
286
+ args = actions .args ()
287
+ args .add (xctest_bundle .path )
288
+ args .add (binary )
289
+
290
+ actions .run_shell (
291
+ arguments = [args ],
292
+ command = (
293
+ 'mkdir -p "$1/Contents/MacOS" && ' +
294
+ 'cp "$2" "$1/Contents/MacOS"'
295
+ ),
296
+ inputs = [binary ],
297
+ mnemonic = "SwiftCreateTestBundle" ,
298
+ outputs = [xctest_bundle ],
299
+ progress_message = "Creating test bundle for {}" .format (name ),
300
+ )
301
+
302
+ return xctest_bundle
303
+
304
+ def _create_xctest_runner (name , actions , bundle , xctest_runner_template ):
305
+ """Creates a script that will launch `xctest` with the given test bundle.
306
+
307
+ Args:
308
+ name: The name of the target being built, which will be used as the
309
+ basename of the test runner script.
310
+ actions: The context's actions object.
311
+ bundle: The `File` representing the `.xctest` bundle that should be
312
+ executed.
278
313
xctest_runner_template: The `File` that will be used as a template to
279
314
generate the test runner shell script.
280
315
281
316
Returns:
282
317
A `File` representing the shell script that will launch the test bundle
283
318
with the `xctest` tool.
284
319
"""
285
- xctest_runner = derived_files .xctest_runner_script (actions , name )
320
+ xctest_runner = derived_files .xctest_runner_script (
321
+ actions = actions ,
322
+ target_name = name ,
323
+ )
286
324
287
325
actions .expand_template (
288
326
is_executable = True ,
289
327
output = xctest_runner ,
290
328
template = xctest_runner_template ,
291
329
substitutions = {
292
- "%binary %" : binary .short_path ,
330
+ "%bundle %" : bundle .short_path ,
293
331
},
294
332
)
295
333
@@ -335,9 +373,6 @@ def _swift_test_impl(ctx):
335
373
336
374
# If we need to run the test in an .xctest bundle, the binary must have
337
375
# Mach-O type `MH_BUNDLE` instead of `MH_EXECUTE`.
338
- # TODO(allevato): This should really be done in the toolchain's
339
- # linker_opts_producer partial, but it doesn't take the
340
- # feature_configuration as an argument. We should update it to do so.
341
376
linkopts = ["-Wl,-bundle" ] if is_bundled else []
342
377
343
378
binary , providers = _swift_linking_rule_impl (
@@ -348,20 +383,22 @@ def _swift_test_impl(ctx):
348
383
swift_toolchain = swift_toolchain ,
349
384
)
350
385
351
- # If the tests are to be bundled, create the test runner script as the
352
- # rule's executable and place the binary in runfiles so that it can be
353
- # copied into place. Otherwise, just use the binary itself as the executable
354
- # to launch.
355
- # TODO(b/65413470): Make the output of the rule _itself_ an `.xctest` bundle
356
- # once some limitations of directory artifacts are resolved.
386
+ # If the tests are to be bundled, create the bundle and the test runner
387
+ # script that launches it via `xctest`. Otherwise, just use the binary
388
+ # itself as the executable to launch.
357
389
if is_bundled :
358
- xctest_runner = _create_xctest_runner (
390
+ xctest_bundle = _create_xctest_bundle (
359
391
name = ctx .label .name ,
360
392
actions = ctx .actions ,
361
393
binary = binary ,
394
+ )
395
+ xctest_runner = _create_xctest_runner (
396
+ name = ctx .label .name ,
397
+ actions = ctx .actions ,
398
+ bundle = xctest_bundle ,
362
399
xctest_runner_template = ctx .file ._xctest_runner_template ,
363
400
)
364
- additional_test_outputs = [binary ]
401
+ additional_test_outputs = [xctest_bundle ]
365
402
executable = xctest_runner
366
403
else :
367
404
additional_test_outputs = []
0 commit comments