This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit bdc11eb942a05ba97714881c50c9295ffc2cdb84
Author: Marco Casaroli <[email protected]>
AuthorDate: Sun Oct 18 15:40:49 2026 +0200

    testing/libc/popen: Check the status that pclose() returns.
    
    Add a test that starts "popen_test --exit 3" and checks that pclose()
    returns the status of a command that exited with a failure.  The shell
    gives its own status, so the test only checks that it is not 0.  The
    status of a command that already exited is only kept with
    CONFIG_SCHED_CHILD_STATUS, so the test runs only with that option.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 testing/libc/popen/popen_test.c | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/testing/libc/popen/popen_test.c b/testing/libc/popen/popen_test.c
index 7a0a66194..00fe161e0 100644
--- a/testing/libc/popen/popen_test.c
+++ b/testing/libc/popen/popen_test.c
@@ -26,9 +26,11 @@
 
 #include <nuttx/config.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <sys/wait.h>
 
 /****************************************************************************
  * Private Functions
@@ -109,6 +111,34 @@ static int test_pclose_complete(void)
   return 0;
 }
 
+#ifdef CONFIG_SCHED_CHILD_STATUS
+static int test_pclose_status(void)
+{
+  FILE *fp;
+  char buf[256];
+  int status;
+
+  printf("[TEST 8] pclose returns a failure status...\n");
+  fp = popen("popen_test --exit 3", "r");
+  if (fp == NULL)
+    {
+      printf("[FAIL] popen returned NULL\n");
+      return 1;
+    }
+
+  while (fgets(buf, sizeof(buf), fp) != NULL);
+  status = pclose(fp);
+  if (status == -1 || !WIFEXITED(status) || WEXITSTATUS(status) == 0)
+    {
+      printf("[FAIL] pclose returned %d\n", status);
+      return 1;
+    }
+
+  printf("[PASS]\n");
+  return 0;
+}
+#endif
+
 static int test_popen_invalid_mode(void)
 {
   FILE *fp;
@@ -231,6 +261,13 @@ int main(int argc, FAR char *argv[])
       return 0;
     }
 
+  /* Sub-command: exit with a status, used to check pclose() */
+
+  if (argc > 2 && strcmp(argv[1], "--exit") == 0)
+    {
+      return atoi(argv[2]);
+    }
+
   printf("=== popen/dpopen test ===\n\n");
 
   /* popen tests */
@@ -239,6 +276,9 @@ int main(int argc, FAR char *argv[])
   fail += test_popen_multiarg();
   fail += test_pclose_complete();
   fail += test_popen_invalid_mode();
+#ifdef CONFIG_SCHED_CHILD_STATUS
+  fail += test_pclose_status();
+#endif
 
   /* dpopen tests */
 

Reply via email to