Skip to content

Commit

Permalink
wd_test: Move template string constants to top level (#3476)
Browse files Browse the repository at this point in the history
Having these unindented lines inside of a conditional block makes
the flow control hard to read
  • Loading branch information
hoodmane authored Feb 7, 2025
1 parent dd66ee1 commit 0c9f19d
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions build/wd_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,7 @@ def wd_test(
**kwargs
)

def _wd_test_impl(ctx):
is_windows = ctx.target_platform_has_constraint(ctx.attr._platforms_os_windows[platform_common.ConstraintValueInfo])

# Bazel insists that the rule must actually create the executable that it intends to run; it
# can't just specify some other executable with some args. OK, fine, we'll use a script that
# just execs its args.
if is_windows:
# Batch script executables must end with ".bat"
executable = ctx.actions.declare_file("%s_wd_test.bat" % ctx.label.name)
content = """
WINDOWS_TEMPLATE = """
@echo off
setlocal EnableDelayedExpansion
Expand All @@ -87,10 +78,9 @@ if defined SIDECAR_PID (
)
exit /b !TEST_EXIT!
""".replace("$(SIDECAR)", ctx.file.sidecar.path if ctx.file.sidecar else "")
else:
executable = ctx.outputs.executable
content = """#!/bin/sh
"""

SH_TEMPLATE = """#!/bin/sh
set -e
cleanup() {
Expand All @@ -110,7 +100,21 @@ fi
# Run the actual test
"$@" -dTEST_TMPDIR=$TEST_TMPDIR
""".replace("$(SIDECAR)", ctx.file.sidecar.short_path if ctx.file.sidecar else "")
"""

def _wd_test_impl(ctx):
is_windows = ctx.target_platform_has_constraint(ctx.attr._platforms_os_windows[platform_common.ConstraintValueInfo])

# Bazel insists that the rule must actually create the executable that it intends to run; it
# can't just specify some other executable with some args. OK, fine, we'll use a script that
# just execs its args.
if is_windows:
# Batch script executables must end with ".bat"
executable = ctx.actions.declare_file("%s_wd_test.bat" % ctx.label.name)
content = WINDOWS_TEMPLATE.replace("$(SIDECAR)", ctx.file.sidecar.path if ctx.file.sidecar else "")
else:
executable = ctx.outputs.executable
content = SH_TEMPLATE.replace("$(SIDECAR)", ctx.file.sidecar.short_path if ctx.file.sidecar else "")

ctx.actions.write(
output = executable,
Expand Down

0 comments on commit 0c9f19d

Please sign in to comment.