changeset 12124:ddffc79e6aa1

getopt-gnu: add another test Ensure that POSIXLY_CORRECT does not interfere with optional argument behavior; older BSD implementations botched this. * tests/test-getopt_long.h (test_getopt_long_posix): New test, to guarantee behavior relied on by m4. * tests/test-getopt.c (main): Use it. * modules/getopt-posix-tests (Depends-on): Add setenv. See http://lists.gnu.org/archive/html/bug-m4/2006-09/msg00028.html. Signed-off-by: Eric Blake <ebb9@byu.net>
author Eric Blake <ebb9@byu.net>
date Tue, 06 Oct 2009 14:29:13 -0600
parents 4ace69c0279d
children 6ade22f26c05
files ChangeLog modules/getopt-posix-tests tests/test-getopt.c tests/test-getopt_long.h
diffstat 4 files changed, 32 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2009-10-06  Eric Blake  <ebb9@byu.net>
 
+	getopt-gnu: add another test
+	* tests/test-getopt_long.h (test_getopt_long_posix): New test, to
+	guarantee behavior relied on by m4.
+	* tests/test-getopt.c (main): Use it.
+	* modules/getopt-posix-tests (Depends-on): Add setenv.
+	See http://lists.gnu.org/archive/html/bug-m4/2006-09/msg00028.html.
+
 	getopt: fix compilation on darwin
 	* lib/getopt.in.h (includes): Leave breadcrumbs during system
 	include.
--- a/modules/getopt-posix-tests
+++ b/modules/getopt-posix-tests
@@ -4,6 +4,7 @@
 tests/test-getopt_long.h
 
 Depends-on:
+setenv
 unistd
 unsetenv
 
--- a/tests/test-getopt.c
+++ b/tests/test-getopt.c
@@ -60,6 +60,9 @@
   test_getopt ();
 #if GNULIB_GETOPT_GNU
   test_getopt_long ();
+
+  setenv ("POSIXLY_CORRECT", "1", 0);
+  test_getopt_long_posix ();
 #endif
 
   return 0;
--- a/tests/test-getopt_long.h
+++ b/tests/test-getopt_long.h
@@ -935,3 +935,24 @@
       ASSERT (optind == 4);
     }
 }
+
+/* Test behavior of getopt_long when POSIXLY_CORRECT is set in the
+   environment.  Options with optional arguments should not change
+   behavior just because of an environment variable.
+   http://lists.gnu.org/archive/html/bug-m4/2006-09/msg00028.html  */
+static void
+test_getopt_long_posix (void)
+{
+  int c = 3;
+  char *v[4] = {"test", "-r", "foo", NULL};
+  struct option l[] = {{NULL}};
+  int start;
+  int result;
+  for (start = OPTIND_MIN; start <= 1; start++)
+    {
+      optind = start;
+      result = getopt_long (c, v, "r::", l, NULL);
+    }
+  ASSERT (result == 'r');
+  ASSERT (optarg == NULL);
+}