From da53a028b4937ebfeb562b0c30572735e4b907de Mon Sep 17 00:00:00 2001 From: Jordan Bonser Date: Wed, 28 May 2025 10:58:09 +0100 Subject: [PATCH] fix(//labgrid/run): handle quoted `program_prefix` `labgrid_test` will provide `''` for `program_prefix` if the target is in the root of the Bazel module. --- e2e/run/BUILD.bazel | 20 ++++++++++++++++++++ labgrid/run/__main__.py | 17 ++++++++++------- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/e2e/run/BUILD.bazel b/e2e/run/BUILD.bazel index 13941537..34cf2f09 100644 --- a/e2e/run/BUILD.bazel +++ b/e2e/run/BUILD.bazel @@ -1,6 +1,7 @@ load("@bazel_skylib//rules:write_file.bzl", "write_file") load("@rules_diff//diff/file/test:defs.bzl", "diff_file_test") load("@rules_labgrid//labgrid/genrule:defs.bzl", "labgrid_genrule") +load("@rules_labgrid//labgrid/run/binary:defs.bzl", "labgrid_run_binary") load("@rules_python//python:defs.bzl", "py_binary") labgrid_genrule( @@ -105,6 +106,24 @@ labgrid_genrule( tools = ["@rules_labgrid//labgrid/run"], ) +# Regression test for: https://gitlab.arm.com/bazel/rules_labgrid/-/issues/73 +labgrid_run_binary( + name = "hello-with-program-prefix", + srcs = ["@ape//ape:bash"], + outs = ["program-prefix.actual"], + args = [ + "--program-prefix", + "''", + "--get", + "output.txt:$(location :program-prefix.actual)", + "$(location @ape//ape:bash)", + "-c", + "echo world > output.txt", + ], + platform = "@rules_labgrid//labgrid/platform:localhost", + tool = "@rules_labgrid//labgrid/run", +) + write_file( name = "expected", out = ":hello.txt", @@ -131,5 +150,6 @@ write_file( "put-file-with-remote", "put-file-without-remote", "env", + "program-prefix", ) ] diff --git a/labgrid/run/__main__.py b/labgrid/run/__main__.py index ae8eab7b..b9021c52 100644 --- a/labgrid/run/__main__.py +++ b/labgrid/run/__main__.py @@ -26,8 +26,8 @@ def arguments(prsr: ArgumentParser) -> None: ) prsr.add_argument( "--program-prefix", - type=PurePath, - default=PurePath(""), + type=unquoted, + default="", help="Prefix of the path to the program on the device.", ) prsr.add_argument( @@ -70,6 +70,11 @@ def resolve(value: str) -> Path: return Path(value) +def unquoted(value: str) -> str: + # FIXME: Extract `BazelArgumentParser` to handle quoting + return value.removeprefix("'").removesuffix("'") + + def env(arg: str) -> tuple[str, str]: key, sep, value = arg.partition("=") if not sep: @@ -80,8 +85,7 @@ def env(arg: str) -> tuple[str, str]: def get(value: str) -> FileTransfer: - # FIXME: Extract `BazelArgumentParser` to handle quoting - remote, found, local = value.removeprefix("'").removesuffix("'").partition(":") + remote, found, local = unquoted(value).partition(":") optional = remote.endswith("?") if optional: remote = remote.removesuffix("?") @@ -93,8 +97,7 @@ def get(value: str) -> FileTransfer: def put(value: str) -> FileTransfer: - # FIXME: Extract `BazelArgumentParser` to handle quoting - local, found, remote = value.removeprefix("'").removesuffix("'").partition(":") + local, found, remote = unquoted(value).partition(":") if found: local = Template(local).substitute(environ) else: @@ -114,7 +117,7 @@ def main(exe: Path, *args: str) -> int: return run( parsed.program, *parsed.arguments, - program_prefix=parsed.program_prefix, + program_prefix=PurePath(parsed.program_prefix), downloads=parsed.downloads, uploads=parsed.uploads, env=dict(parsed.env), -- GitLab