From 0c9f19d4ca7118d81da82b01c359863c6bc45531 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Fri, 7 Feb 2025 13:51:10 +0100 Subject: [PATCH] wd_test: Move template string constants to top level (#3476) Having these unindented lines inside of a conditional block makes the flow control hard to read --- build/wd_test.bzl | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/build/wd_test.bzl b/build/wd_test.bzl index c1f042122c3..ff1f8929161 100644 --- a/build/wd_test.bzl +++ b/build/wd_test.bzl @@ -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 @@ -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() { @@ -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,