getopt-gnu: add another test
authorEric Blake <ebb9@byu.net>
Tue, 6 Oct 2009 20:29:13 +0000 (14:29 -0600)
committerEric Blake <ebb9@byu.net>
Wed, 7 Oct 2009 03:37:36 +0000 (21:37 -0600)
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>
ChangeLog
modules/getopt-posix-tests
tests/test-getopt.c
tests/test-getopt_long.h

index 78b3386..1ce9fc9 100644 (file)
--- 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.
index ac4b965..3499b5a 100644 (file)
@@ -4,6 +4,7 @@ tests/test-getopt.h
 tests/test-getopt_long.h
 
 Depends-on:
+setenv
 unistd
 unsetenv
 
index b3dd60b..12d8d92 100644 (file)
@@ -60,6 +60,9 @@ main ()
   test_getopt ();
 #if GNULIB_GETOPT_GNU
   test_getopt_long ();
+
+  setenv ("POSIXLY_CORRECT", "1", 0);
+  test_getopt_long_posix ();
 #endif
 
   return 0;
index 0017f19..fb505b2 100644 (file)
@@ -935,3 +935,24 @@ test_getopt_long (void)
       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);
+}