Skip to content

Commit 251f7e8

Browse files
authored
Merge pull request libarchive#2669 from benoit-pierre/pr/minor_test_related_tweaks
minor test related tweaks
2 parents 47bdf82 + 864c904 commit 251f7e8

3 files changed

Lines changed: 39 additions & 123 deletions

File tree

libarchive/test/test_read_set_format.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ DEFINE_TEST(test_read_append_filter_wrong_program)
210210
/*
211211
* If we have "bunzip2 -q", try using that.
212212
*/
213-
if (!canRunCommand("bunzip2 -h")) {
213+
if (!canRunCommand("bunzip2 -h", NULL)) {
214214
skipping("Can't run bunzip2 program on this platform");
215215
return;
216216
}

test_utils/test_common.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,14 @@ int canGrzip(void);
340340
/* Return true if this platform can run the "gzip" program. */
341341
int canGzip(void);
342342

343-
/* Return true if this platform can run the specified command. */
344-
int canRunCommand(const char *);
343+
/* Return true if this platform can run the specified command.
344+
*
345+
* Result can be optionally cached with `*tested`:
346+
* - 0 if not tested yet
347+
* - <0 if already tested negative
348+
* - >0 if already tested positive
349+
*/
350+
int canRunCommand(const char *cmd, int *tested);
345351

346352
/* Return true if this platform can run the "lrzip" program. */
347353
int canLrzip(void);

test_utils/test_main.c

Lines changed: 30 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,167 +2523,77 @@ static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
25232523
#else
25242524
static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
25252525
#endif
2526+
25262527
/*
2527-
* Can this platform run the bzip2 program?
2528+
* Can this platform run the specified command?
25282529
*/
25292530
int
2530-
canBzip2(void)
2531+
canRunCommand(const char *cmd, int *tested)
25312532
{
2532-
static int tested = 0, value = 0;
2533-
if (!tested) {
2534-
tested = 1;
2535-
if (systemf("bzip2 --help %s", redirectArgs) == 0)
2536-
value = 1;
2537-
}
2538-
return (value);
2533+
int value = tested ? *tested : 0;
2534+
if (!value) {
2535+
value = systemf("%s %s", cmd, redirectArgs) ? -1 : +1;
2536+
if (tested)
2537+
*tested = value;
2538+
}
2539+
return (value > 0);
25392540
}
25402541

2542+
#define CAN_RUN_FUNC(Program, Command) \
2543+
int can##Program(void) { \
2544+
static int tested = 0; \
2545+
return canRunCommand((Command), &tested); \
2546+
}
2547+
2548+
/*
2549+
* Can this platform run the bzip2 program?
2550+
*/
2551+
CAN_RUN_FUNC(Bzip2, "bzip2 --help");
2552+
25412553
/*
25422554
* Can this platform run the grzip program?
25432555
*/
2544-
int
2545-
canGrzip(void)
2546-
{
2547-
static int tested = 0, value = 0;
2548-
if (!tested) {
2549-
tested = 1;
2550-
if (systemf("grzip -V %s", redirectArgs) == 0)
2551-
value = 1;
2552-
}
2553-
return (value);
2554-
}
2556+
CAN_RUN_FUNC(Grzip, "grzip -V");
25552557

25562558
/*
25572559
* Can this platform run the gzip program?
25582560
*/
2559-
int
2560-
canGzip(void)
2561-
{
2562-
static int tested = 0, value = 0;
2563-
if (!tested) {
2564-
tested = 1;
2565-
if (systemf("gzip --help %s", redirectArgs) == 0)
2566-
value = 1;
2567-
}
2568-
return (value);
2569-
}
2561+
CAN_RUN_FUNC(Gzip, "gzip --help");
25702562

25712563
/*
25722564
* Can this platform run the lrzip program?
25732565
*/
2574-
int
2575-
canRunCommand(const char *cmd)
2576-
{
2577-
static int tested = 0, value = 0;
2578-
if (!tested) {
2579-
tested = 1;
2580-
if (systemf("%s %s", cmd, redirectArgs) == 0)
2581-
value = 1;
2582-
}
2583-
return (value);
2584-
}
2585-
2586-
int
2587-
canLrzip(void)
2588-
{
2589-
static int tested = 0, value = 0;
2590-
if (!tested) {
2591-
tested = 1;
2592-
if (systemf("lrzip -V %s", redirectArgs) == 0)
2593-
value = 1;
2594-
}
2595-
return (value);
2596-
}
2566+
CAN_RUN_FUNC(Lrzip, "lrzip -V");
25972567

25982568
/*
25992569
* Can this platform run the lz4 program?
26002570
*/
2601-
int
2602-
canLz4(void)
2603-
{
2604-
static int tested = 0, value = 0;
2605-
if (!tested) {
2606-
tested = 1;
2607-
if (systemf("lz4 --help %s", redirectArgs) == 0)
2608-
value = 1;
2609-
}
2610-
return (value);
2611-
}
2571+
CAN_RUN_FUNC(Lz4, "lz4 --help");
26122572

26132573
/*
26142574
* Can this platform run the zstd program?
26152575
*/
2616-
int
2617-
canZstd(void)
2618-
{
2619-
static int tested = 0, value = 0;
2620-
if (!tested) {
2621-
tested = 1;
2622-
if (systemf("zstd --help %s", redirectArgs) == 0)
2623-
value = 1;
2624-
}
2625-
return (value);
2626-
}
2576+
CAN_RUN_FUNC(Zstd, "zstd --help");
26272577

26282578
/*
26292579
* Can this platform run the lzip program?
26302580
*/
2631-
int
2632-
canLzip(void)
2633-
{
2634-
static int tested = 0, value = 0;
2635-
if (!tested) {
2636-
tested = 1;
2637-
if (systemf("lzip --help %s", redirectArgs) == 0)
2638-
value = 1;
2639-
}
2640-
return (value);
2641-
}
2581+
CAN_RUN_FUNC(Lzip, "lzip --help");
26422582

26432583
/*
26442584
* Can this platform run the lzma program?
26452585
*/
2646-
int
2647-
canLzma(void)
2648-
{
2649-
static int tested = 0, value = 0;
2650-
if (!tested) {
2651-
tested = 1;
2652-
if (systemf("lzma --help %s", redirectArgs) == 0)
2653-
value = 1;
2654-
}
2655-
return (value);
2656-
}
2586+
CAN_RUN_FUNC(Lzma, "lzma --help");
26572587

26582588
/*
26592589
* Can this platform run the lzop program?
26602590
*/
2661-
int
2662-
canLzop(void)
2663-
{
2664-
static int tested = 0, value = 0;
2665-
if (!tested) {
2666-
tested = 1;
2667-
if (systemf("lzop --help %s", redirectArgs) == 0)
2668-
value = 1;
2669-
}
2670-
return (value);
2671-
}
2591+
CAN_RUN_FUNC(Lzop, "lzop --help");
26722592

26732593
/*
26742594
* Can this platform run the xz program?
26752595
*/
2676-
int
2677-
canXz(void)
2678-
{
2679-
static int tested = 0, value = 0;
2680-
if (!tested) {
2681-
tested = 1;
2682-
if (systemf("xz --help %s", redirectArgs) == 0)
2683-
value = 1;
2684-
}
2685-
return (value);
2686-
}
2596+
CAN_RUN_FUNC(Xz, "xz --help");
26872597

26882598
/*
26892599
* Can this filesystem handle nodump flags.

0 commit comments

Comments
 (0)