From dcf5475661623ef2bc100767822db979456cd1fb Mon Sep 17 00:00:00 2001 From: Benoit Pierre Date: Sun, 8 Jun 2025 00:04:23 +0200 Subject: [PATCH] test_utils: fix `canRunCommand` implementation Do not unconditionally cache the result of the first call (and reused it for subsequent calls). Thankfully, the function is only called once. --- libarchive/test/test_read_set_format.c | 2 +- test_utils/test_common.h | 10 ++++++-- test_utils/test_main.c | 34 +++++++++++++++++--------- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/libarchive/test/test_read_set_format.c b/libarchive/test/test_read_set_format.c index c760de005..ad212b5cb 100644 --- a/libarchive/test/test_read_set_format.c +++ b/libarchive/test/test_read_set_format.c @@ -210,7 +210,7 @@ DEFINE_TEST(test_read_append_filter_wrong_program) /* * If we have "bunzip2 -q", try using that. */ - if (!canRunCommand("bunzip2 -h")) { + if (!canRunCommand("bunzip2 -h", NULL)) { skipping("Can't run bunzip2 program on this platform"); return; } diff --git a/test_utils/test_common.h b/test_utils/test_common.h index 064f0a6ec..a9df300e3 100644 --- a/test_utils/test_common.h +++ b/test_utils/test_common.h @@ -340,8 +340,14 @@ int canGrzip(void); /* Return true if this platform can run the "gzip" program. */ int canGzip(void); -/* Return true if this platform can run the specified command. */ -int canRunCommand(const char *); +/* Return true if this platform can run the specified command. + * + * Result can be optionally cached with `*tested`: + * - 0 if not tested yet + * - <0 if already tested negative + * - >0 if already tested positive + */ +int canRunCommand(const char *cmd, int *tested); /* Return true if this platform can run the "lrzip" program. */ int canLrzip(void); diff --git a/test_utils/test_main.c b/test_utils/test_main.c index fe330e5a0..1cadd2bfa 100644 --- a/test_utils/test_main.c +++ b/test_utils/test_main.c @@ -2523,6 +2523,28 @@ static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */ #else static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */ #endif + +/* + * Can this platform run the specified command? + */ +int +canRunCommand(const char *cmd, int *tested) +{ + int value = tested ? *tested : 0; + if (!value) { + value = systemf("%s %s", cmd, redirectArgs) ? -1 : +1; + if (tested) + *tested = value; + } + return (value > 0); +} + +#define CAN_RUN_FUNC(Program, Command) \ + int can##Program(void) { \ + static int tested = 0; \ + return canRunCommand((Command), &tested); \ + } + /* * Can this platform run the bzip2 program? */ @@ -2571,18 +2593,6 @@ canGzip(void) /* * Can this platform run the lrzip program? */ -int -canRunCommand(const char *cmd) -{ - static int tested = 0, value = 0; - if (!tested) { - tested = 1; - if (systemf("%s %s", cmd, redirectArgs) == 0) - value = 1; - } - return (value); -} - int canLrzip(void) { -- 2.47.2