From 497ec25b4164fcf4b8a68a41f28e27cb1030c441 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Tue, 6 Oct 2009 14:29:13 -0600 Subject: [PATCH] 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 --- ChangeLog | 7 +++++++ modules/getopt-posix-tests | 1 + tests/test-getopt.c | 3 +++ tests/test-getopt_long.h | 21 +++++++++++++++++++++ 4 files changed, 32 insertions(+) diff --git a/ChangeLog b/ChangeLog index 78b33867c..1ce9fc945 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2009-10-06 Eric Blake + 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. diff --git a/modules/getopt-posix-tests b/modules/getopt-posix-tests index ac4b96561..3499b5a3e 100644 --- a/modules/getopt-posix-tests +++ b/modules/getopt-posix-tests @@ -4,6 +4,7 @@ tests/test-getopt.h tests/test-getopt_long.h Depends-on: +setenv unistd unsetenv diff --git a/tests/test-getopt.c b/tests/test-getopt.c index b3dd60b26..12d8d92fe 100644 --- a/tests/test-getopt.c +++ b/tests/test-getopt.c @@ -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; diff --git a/tests/test-getopt_long.h b/tests/test-getopt_long.h index 0017f1981..fb505b2c1 100644 --- a/tests/test-getopt_long.h +++ b/tests/test-getopt_long.h @@ -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); +} -- 2.11.0