]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Merge pull request #2669 from benoit-pierre/pr/minor_test_related_tweaks
authorMartin Matuška <martin@matuska.de>
Thu, 12 Jun 2025 08:57:17 +0000 (10:57 +0200)
committerMartin Matuska <martin@matuska.de>
Wed, 10 Sep 2025 08:15:07 +0000 (10:15 +0200)
minor test related tweaks

(cherry picked from commit 251f7e839c17cbfffc08439ca332e13082ef6875)

libarchive/test/test_read_set_format.c
test_utils/test_common.h
test_utils/test_main.c

index c760de0056d361682033fe5bed38c94e67a82de2..ad212b5cb71ef33b74ca71fa29e9a65896650d7d 100644 (file)
@@ -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;
   }
index 064f0a6ec241771db803cb85cdb94659f45f5efc..a9df300e3be8505e26db5d4987335d1449731daf 100644 (file)
@@ -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);
index fe330e5a052e99300aebf05493f64d0b19cd9e24..c4244e9c6bcaa8ab4cf9641f8b3bb2c7188d6d5c 100644 (file)
@@ -2523,167 +2523,77 @@ 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 bzip2 program?
+ * Can this platform run the specified command?
  */
 int
-canBzip2(void)
+canRunCommand(const char *cmd, int *tested)
 {
-       static int tested = 0, value = 0;
-       if (!tested) {
-               tested = 1;
-               if (systemf("bzip2 --help %s", redirectArgs) == 0)
-                       value = 1;
-       }
-       return (value);
+  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?
+ */
+CAN_RUN_FUNC(Bzip2, "bzip2 --help");
+
 /*
  * Can this platform run the grzip program?
  */
-int
-canGrzip(void)
-{
-       static int tested = 0, value = 0;
-       if (!tested) {
-               tested = 1;
-               if (systemf("grzip -V %s", redirectArgs) == 0)
-                       value = 1;
-       }
-       return (value);
-}
+CAN_RUN_FUNC(Grzip, "grzip -V");
 
 /*
  * Can this platform run the gzip program?
  */
-int
-canGzip(void)
-{
-       static int tested = 0, value = 0;
-       if (!tested) {
-               tested = 1;
-               if (systemf("gzip --help %s", redirectArgs) == 0)
-                       value = 1;
-       }
-       return (value);
-}
+CAN_RUN_FUNC(Gzip, "gzip --help");
 
 /*
  * 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)
-{
-       static int tested = 0, value = 0;
-       if (!tested) {
-               tested = 1;
-               if (systemf("lrzip -V %s", redirectArgs) == 0)
-                       value = 1;
-       }
-       return (value);
-}
+CAN_RUN_FUNC(Lrzip, "lrzip -V");
 
 /*
  * Can this platform run the lz4 program?
  */
-int
-canLz4(void)
-{
-       static int tested = 0, value = 0;
-       if (!tested) {
-               tested = 1;
-               if (systemf("lz4 --help %s", redirectArgs) == 0)
-                       value = 1;
-       }
-       return (value);
-}
+CAN_RUN_FUNC(Lz4, "lz4 --help");
 
 /*
  * Can this platform run the zstd program?
  */
-int
-canZstd(void)
-{
-       static int tested = 0, value = 0;
-       if (!tested) {
-               tested = 1;
-               if (systemf("zstd --help %s", redirectArgs) == 0)
-                       value = 1;
-       }
-       return (value);
-}
+CAN_RUN_FUNC(Zstd, "zstd --help");
 
 /*
  * Can this platform run the lzip program?
  */
-int
-canLzip(void)
-{
-       static int tested = 0, value = 0;
-       if (!tested) {
-               tested = 1;
-               if (systemf("lzip --help %s", redirectArgs) == 0)
-                       value = 1;
-       }
-       return (value);
-}
+CAN_RUN_FUNC(Lzip, "lzip --help");
 
 /*
  * Can this platform run the lzma program?
  */
-int
-canLzma(void)
-{
-       static int tested = 0, value = 0;
-       if (!tested) {
-               tested = 1;
-               if (systemf("lzma --help %s", redirectArgs) == 0)
-                       value = 1;
-       }
-       return (value);
-}
+CAN_RUN_FUNC(Lzma, "lzma --help");
 
 /*
  * Can this platform run the lzop program?
  */
-int
-canLzop(void)
-{
-       static int tested = 0, value = 0;
-       if (!tested) {
-               tested = 1;
-               if (systemf("lzop --help %s", redirectArgs) == 0)
-                       value = 1;
-       }
-       return (value);
-}
+CAN_RUN_FUNC(Lzop, "lzop --help");
 
 /*
  * Can this platform run the xz program?
  */
-int
-canXz(void)
-{
-       static int tested = 0, value = 0;
-       if (!tested) {
-               tested = 1;
-               if (systemf("xz --help %s", redirectArgs) == 0)
-                       value = 1;
-       }
-       return (value);
-}
+CAN_RUN_FUNC(Xz, "xz --help");
 
 /*
  * Can this filesystem handle nodump flags.