File: test_command.c

package info (click to toggle)
libnih 1.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 10,384 kB
  • ctags: 4,453
  • sloc: ansic: 185,873; sh: 11,213; makefile: 1,116; yacc: 291; xml: 227; sed: 16
file content (898 lines) | stat: -rw-r--r-- 22,628 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
/* libnih
 *
 * test_command.c - test suite for nih/command.c
 *
 * Copyright © 2009 Scott James Remnant <scott@netsplit.com>.
 * Copyright © 2009 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <nih/test.h>

#include <sys/types.h>
#include <sys/wait.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <nih/macros.h>
#include <nih/alloc.h>
#include <nih/string.h>
#include <nih/main.h>
#include <nih/option.h>
#include <nih/command.h>


static int dry_run = 0;
static int wobble = 0;

static int was_called = 0;
static NihCommand *last_command = NULL;
static char *last_arg0 = NULL;
static char *last_arg1 = NULL;

static int
my_action (NihCommand *command, char * const *args)
{
	was_called++;

	last_command = malloc (sizeof (NihCommand));
	memcpy (last_command, command, sizeof (NihCommand));

	if (args[0]) {
		last_arg0 = strdup (args[0]);
		if (args[1]) {
			last_arg1 = strdup (args[1]);
		} else {
			last_arg1 = NULL;
		}

		if (! strcmp (args[0], "fail"))
			return -1;
	} else {
		last_arg0 = NULL;
		last_arg1 = NULL;
	}

	return 0;
}


static NihOption options[] = {
	{ 'n', "dry-run", N_("simulate and output actions only"),
	  NULL, NULL, &dry_run, NULL },

	NIH_OPTION_LAST
};

static NihOption wibble_options[] = {
	{ 0, "wobble", N_("wobble file while wibbling"),
	  NULL, NULL, &wobble, NULL },

	NIH_OPTION_LAST
};

static NihCommandGroup test_group1 = { "First test group" };
static NihCommandGroup test_group2 = { "Second test group" };
static NihCommand commands[] = {
	{ "foo", NULL,
	  N_("do something fooish"),
	  NULL,
	  &test_group1, NULL,
	  my_action },

	{ "bar", N_("FILE"),
	  N_("do something barish to a file"),
	  NULL,
	  &test_group1, NULL,
	  my_action },

	{ "baz", NULL,
	  N_("do something bazish"),
	  NULL,
	  &test_group2, NULL,
	  my_action },

	{ "wibble", N_("SRC DEST"),
	  N_("wibble a file from one place to another"),
	  N_("Takes the file from SRC, wibbles it until any loose pieces "
	     "fall off, and until it reaches DEST.  SRC and DEST may not "
	     "be the same location."),
	  NULL,
	  wibble_options,
	  my_action },

	NIH_COMMAND_LAST
};

static NihCommand commands2[] = {
	{ "really-overly-long-command-name", NULL,
	  N_("does something irrelevant, and the synopsis is long enough to "
	     "wrap across multiple lines"),
	  NULL,
	  NULL,
	  NULL,
	  NULL },

	{ "hidden", NULL, NULL, NULL, NULL, NULL, NULL },

	NIH_COMMAND_LAST
};

void
test_parser (void)
{
	FILE *output;
	char *argv[16];
	int   argc, ret = 0;

	TEST_FUNCTION ("nih_command_parser");
	program_name = "test";
	output = tmpfile ();


	/* Check that the command parser calls the command function, and
	 * when there are no arguments, just passes in a NULL array.
	 */
	TEST_FEATURE ("with just command");
	TEST_ALLOC_FAIL {
		argc = 0;
		argv[argc++] = "ignored";
		argv[argc++] = "foo";
		argv[argc] = NULL;

		was_called = 0;
		last_command = NULL;
		last_arg0 = NULL;
		last_arg1 = NULL;

		ret = nih_command_parser (NULL, argc, argv, options, commands);

		TEST_EQ (ret, 0);
		TEST_TRUE (was_called);
		TEST_EQ_STR (last_command->command, "foo");
		TEST_EQ_P (last_arg0, NULL);

		free (last_command);
	}


	/* Check that a global option that appears before a command is
	 * honoured.
	 */
	TEST_FEATURE ("with global option followed by command");
	TEST_ALLOC_FAIL {
		argc = 0;
		argv[argc++] = "ignored";
		argv[argc++] = "-n";
		argv[argc++] = "foo";
		argv[argc] = NULL;

		dry_run = 0;
		was_called = 0;
		last_command = NULL;
		last_arg0 = NULL;
		last_arg1 = NULL;

		ret = nih_command_parser (NULL, argc, argv, options, commands);

		TEST_EQ (ret, 0);
		TEST_TRUE (dry_run);
		TEST_TRUE (was_called);
		TEST_EQ_STR (last_command->command, "foo");
		TEST_EQ_P (last_arg0, NULL);

		free (last_command);
	}


	/* Check that a global option that appears after a command is
	 * still honoured, despite not being in the command's own options.
	 */
	TEST_FEATURE ("with command followed by global option");
	TEST_ALLOC_FAIL {
		argc = 0;
		argv[argc++] = "ignored";
		argv[argc++] = "foo";
		argv[argc++] = "-n";
		argv[argc] = NULL;

		dry_run = 0;
		was_called = 0;
		last_command = NULL;
		last_arg0 = NULL;
		last_arg1 = NULL;

		ret = nih_command_parser (NULL, argc, argv, options, commands);

		TEST_EQ (ret, 0);
		TEST_TRUE (dry_run);
		TEST_TRUE (was_called);
		TEST_EQ_STR (last_command->command, "foo");
		TEST_EQ_P (last_arg0, NULL);

		free (last_command);
	}


	/* Check that a command's own options are also honoured. */
	TEST_FEATURE ("with command followed by specific option");
	TEST_ALLOC_FAIL {
		argc = 0;
		argv[argc++] = "ignored";
		argv[argc++] = "wibble";
		argv[argc++] = "--wobble";
		argv[argc] = NULL;

		wobble = 0;
		was_called = 0;
		last_command = NULL;
		last_arg0 = NULL;
		last_arg1 = NULL;

		ret = nih_command_parser (NULL, argc, argv, options, commands);

		TEST_EQ (ret, 0);
		TEST_TRUE (wobble);
		TEST_TRUE (was_called);
		TEST_EQ_STR (last_command->command, "wibble");
		TEST_EQ_P (last_arg0, NULL);

		free (last_command);
	}


	/* Check that global options and command-specific options can be
	 * both given at once.
	 */
	TEST_FEATURE ("with global option, command, then specific option");
	TEST_ALLOC_FAIL {
		argc = 0;
		argv[argc++] = "ignored";
		argv[argc++] = "--dry-run";
		argv[argc++] = "wibble";
		argv[argc++] = "--wobble";
		argv[argc] = NULL;

		wobble = 0;
		dry_run = 0;
		was_called = 0;
		last_command = NULL;
		last_arg0 = NULL;
		last_arg1 = NULL;

		ret = nih_command_parser (NULL, argc, argv, options, commands);

		TEST_EQ (ret, 0);
		TEST_TRUE (wobble);
		TEST_TRUE (dry_run);
		TEST_TRUE (was_called);
		TEST_EQ_STR (last_command->command, "wibble");
		TEST_EQ_P (last_arg0, NULL);

		free (last_command);
	}


	/* Check that a double-dash terminator may appear before a command,
	 * which only terminates the global options, not the command-specific
	 * ones.
	 */
	TEST_FEATURE ("with terminator before command");
	TEST_ALLOC_FAIL {
		argc = 0;
		argv[argc++] = "ignored";
		argv[argc++] = "--dry-run";
		argv[argc++] = "--";
		argv[argc++] = "wibble";
		argv[argc++] = "--wobble";
		argv[argc] = NULL;

		wobble = 0;
		dry_run = 0;
		was_called = 0;
		last_command = NULL;
		last_arg0 = NULL;
		last_arg1 = NULL;

		ret = nih_command_parser (NULL, argc, argv, options, commands);

		TEST_EQ (ret, 0);
		TEST_TRUE (wobble);
		TEST_TRUE (dry_run);
		TEST_TRUE (was_called);
		TEST_EQ_STR (last_command->command, "wibble");
		TEST_EQ_P (last_arg0, NULL);

		free (last_command);
	}


	/* Check that a double-dash terminator may appear after a command,
	 * which terminates the option processing for that command as well.
	 * Any option-like argument is passed to the function as an ordinary
	 * argument in the array.
	 */
	TEST_FEATURE ("with terminator before and after command");
	TEST_ALLOC_FAIL {
		argc = 0;
		argv[argc++] = "ignored";
		argv[argc++] = "--dry-run";
		argv[argc++] = "--";
		argv[argc++] = "wibble";
		argv[argc++] = "--";
		argv[argc++] = "--wobble";
		argv[argc] = NULL;

		wobble = 0;
		dry_run = 0;
		was_called = 0;
		last_command = NULL;
		last_arg0 = NULL;
		last_arg1 = NULL;

		ret = nih_command_parser (NULL, argc, argv, options, commands);

		TEST_EQ (ret, 0);
		TEST_FALSE (wobble);
		TEST_TRUE (dry_run);
		TEST_TRUE (was_called);
		TEST_EQ_STR (last_command->command, "wibble");
		TEST_EQ_STR (last_arg0, "--wobble");
		TEST_EQ_P (last_arg1, NULL);

		free (last_arg0);
		free (last_command);
	}


	/* Check that non-option arguments may follow a command, they're
	 * collected and passed to the function in a NULL-terminated array.
	 */
	TEST_FEATURE ("with command and single argument");
	TEST_ALLOC_FAIL {
		argc = 0;
		argv[argc++] = "ignored";
		argv[argc++] = "bar";
		argv[argc++] = "snarf";
		argv[argc] = NULL;

		was_called = 0;
		last_command = NULL;
		last_arg0 = NULL;
		last_arg1 = NULL;

		ret = nih_command_parser (NULL, argc, argv, options, commands);

		TEST_EQ (ret, 0);
		TEST_TRUE (was_called);
		TEST_EQ_STR (last_command->command, "bar");
		TEST_EQ_STR (last_arg0, "snarf");
		TEST_EQ_P (last_arg1, NULL);

		free (last_arg0);
		free (last_command);
	}


	/* Check that multiple arguments after the command are all passed
	 * in the array.
	 */
	TEST_FEATURE ("with command and multiple arguments");
	TEST_ALLOC_FAIL {
		argc = 0;
		argv[argc++] = "ignored";
		argv[argc++] = "wibble";
		argv[argc++] = "snarf";
		argv[argc++] = "lick";
		argv[argc] = NULL;

		was_called = 0;
		last_command = NULL;
		last_arg0 = NULL;
		last_arg1 = NULL;

		ret = nih_command_parser (NULL, argc, argv, options, commands);

		TEST_EQ (ret, 0);
		TEST_TRUE (was_called);
		TEST_EQ_STR (last_command->command, "wibble");
		TEST_EQ_STR (last_arg0, "snarf");
		TEST_EQ_STR (last_arg1, "lick");

		free (last_arg0);
		free (last_arg1);
		free (last_command);
	}


	/* Check that an invalid global option appearing results in the
	 * parser returning a negative number and outputting an error
	 * message to stderr with a suggestion about help.
	 */
	TEST_FEATURE ("with invalid global option before command");
	TEST_ALLOC_FAIL {
		argc = 0;
		argv[argc++] = "ignored";
		argv[argc++] = "-z";
		argv[argc++] = "foo";
		argv[argc] = NULL;

		was_called = 0;

		TEST_DIVERT_STDERR (output) {
			ret = nih_command_parser (NULL, argc, argv,
						  options, commands);
		}
		rewind (output);

		TEST_LT (ret, 0);
		TEST_FALSE (was_called);

		TEST_FILE_EQ (output, "test: invalid option: -z\n");
		TEST_FILE_EQ (output,
			      "Try `test --help' for more information.\n");
		TEST_FILE_END (output);

		TEST_FILE_RESET (output);
	}


	/* Check that an invalid option appearing after the command also
	 * results in the parser returning an error without running the
	 * command function.
	 */
	TEST_FEATURE ("with invalid option after command");
	TEST_ALLOC_FAIL {
		argc = 0;
		argv[argc++] = "ignored";
		argv[argc++] = "foo";
		argv[argc++] = "-z";
		argv[argc] = NULL;

		was_called = 0;

		TEST_DIVERT_STDERR (output) {
			ret = nih_command_parser (NULL, argc, argv,
						  options, commands);
		}
		rewind (output);

		TEST_LT (ret, 0);
		TEST_FALSE (was_called);

		TEST_FILE_EQ (output, "test: invalid option: -z\n");
		TEST_FILE_EQ (output, "Try `test --help' for more information.\n");
		TEST_FILE_END (output);

		TEST_FILE_RESET (output);
	}


	/* Check that a missing command entirely results in the parser
	 * terminating with an error and outputting a message.
	 */
	TEST_FEATURE ("with missing command");
	TEST_ALLOC_FAIL {
		argc = 0;
		argv[argc++] = "ignored";
		argv[argc] = NULL;

		was_called = 0;

		TEST_DIVERT_STDERR (output) {
			ret = nih_command_parser (NULL, argc, argv,
						  options, commands);
		}
		rewind (output);

		TEST_LT (ret, 0);
		TEST_FALSE (was_called);

		TEST_FILE_EQ (output, "test: missing command\n");
		TEST_FILE_EQ (output,
			      "Try `test --help' for more information.\n");
		TEST_FILE_END (output);

		TEST_FILE_RESET (output);
	}


	/* Check that an invalid command results in the parser returning
	 * an error and outputting a message.
	 */
	TEST_FEATURE ("with invalid command");
	TEST_ALLOC_FAIL {
		argc = 0;
		argv[argc++] = "ignored";
		argv[argc++] = "lick";
		argv[argc] = NULL;

		was_called = 0;

		TEST_DIVERT_STDERR (output) {
			ret = nih_command_parser (NULL, argc, argv,
						  options, commands);
		}
		rewind (output);

		TEST_LT (ret, 0);
		TEST_FALSE (was_called);

		TEST_FILE_EQ (output, "test: invalid command: lick\n");
		TEST_FILE_EQ (output,
			      "Try `test --help' for more information.\n");
		TEST_FILE_END (output);

		TEST_FILE_RESET (output);
	}


	/* Check that the command may appear in the program name instead,
	 * in which case all arguments are used including the first, and
	 * all options considered to be both global and command options.
	 */
	TEST_FEATURE ("with command in program name");
	TEST_ALLOC_FAIL {
		program_name = "wibble";

		argc = 0;
		argv[argc++] = "ignored";
		argv[argc++] = "snarf";
		argv[argc++] = "lick";
		argv[argc++] = "--wobble";
		argv[argc++] = "-n";
		argv[argc] = NULL;

		dry_run = 0;
		wobble = 0;
		was_called = 0;
		last_command = NULL;
		last_arg0 = NULL;
		last_arg1 = NULL;

		ret = nih_command_parser (NULL, argc, argv, options, commands);

		TEST_EQ (ret, 0);
		TEST_TRUE (dry_run);
		TEST_TRUE (wobble);
		TEST_TRUE (was_called);
		TEST_EQ_STR (last_command->command, "wibble");
		TEST_EQ_STR (last_arg0, "snarf");
		TEST_EQ_STR (last_arg1, "lick");

		free (last_arg0);
		free (last_arg1);
		free (last_command);
	}


	fclose (output);
}


void
test_help (void)
{
	FILE  *output;
	char  *argv[4];
	pid_t  pid;
	int    argc, status;

	TEST_FUNCTION ("nih_command_help");
	output = tmpfile ();

	/* Check that we can obtain a list of command using the "help"
	 * command; which terminates the process with exit code 0.  The
	 * output should be grouped according to the command group, and
	 * each command indented with the text alongside and wrapped.
	 */
	TEST_FEATURE ("with multiple groups");
	nih_main_init_full ("test", "wibble", "1.0",
			    "foo@bar.com", "Copyright Message");
	TEST_ALLOC_FAIL {
		argc = 0;
		argv[argc++] = "ignored";
		argv[argc++] = "help";
		argv[argc] = NULL;

		TEST_CHILD (pid) {
			unsetenv ("COLUMNS");

			TEST_DIVERT_STDOUT (output) {
				nih_command_parser (NULL, argc, argv,
						    options, commands);
				exit (1);
			}
		}

		waitpid (pid, &status, 0);
		rewind (output);

		TEST_TRUE (WIFEXITED (status));
		TEST_EQ (WEXITSTATUS (status), 0);

		TEST_FILE_EQ (output, "First test group commands:\n");
		TEST_FILE_EQ (output, ("  foo                         "
				       "do something fooish\n"));
		TEST_FILE_EQ (output, ("  bar                         "
				       "do something barish to a file\n"));
		TEST_FILE_EQ (output, "\n");
		TEST_FILE_EQ (output, "Second test group commands:\n");
		TEST_FILE_EQ (output, ("  baz                         "
				       "do something bazish\n"));
		TEST_FILE_EQ (output, "\n");
		TEST_FILE_EQ (output, "Other commands:\n");
		TEST_FILE_EQ (output, ("  wibble                      "
				       "wibble a file from one place to "
				       "another\n"));
		TEST_FILE_EQ (output, ("  help                        "
				       "display list of commands\n"));
		TEST_FILE_EQ (output, "\n");
		TEST_FILE_EQ (output, ("For more information on a command, "
				       "try `test COMMAND --help'.\n"));
		TEST_FILE_END (output);

		TEST_FILE_RESET (output);
	}


	/* Check that if there's only a single group, the title is different;
	 * also check that an overly long command name is wrapped properly,
	 * synopsis is wrapped to multiple lines and a command without a
	 * synopsis is not output at all.
	 */
	TEST_FEATURE ("with single group and long name");
	nih_main_init_full ("test", "wibble", "1.0",
			    "foo@bar.com", "Copyright Message");
	TEST_ALLOC_FAIL {
		argc = 0;
		argv[argc++] = "ignored";
		argv[argc++] = "help";
		argv[argc] = NULL;

		TEST_CHILD (pid) {
			unsetenv ("COLUMNS");

			TEST_DIVERT_STDOUT (output) {
				nih_command_parser (NULL, argc, argv,
						    options, commands2);
				exit (1);
			}
		}

		waitpid (pid, &status, 0);
		rewind (output);

		TEST_TRUE (WIFEXITED (status));
		TEST_EQ (WEXITSTATUS (status), 0);

		TEST_FILE_EQ (output, "Commands:\n");
		TEST_FILE_EQ (output, "  really-overly-long-command-name\n");
		TEST_FILE_EQ (output, ("                              "
				       "does something irrelevant, and the "
				       "synopsis is\n"));
		TEST_FILE_EQ (output, ("                                "
				       "long enough to wrap across multiple "
				       "lines\n"));
		TEST_FILE_EQ (output, ("  help                        "
				       "display list of commands\n"));
		TEST_FILE_EQ (output, "\n");
		TEST_FILE_EQ (output, ("For more information on a command, "
				       "try `test COMMAND --help'.\n"));
		TEST_FILE_END (output);

		TEST_FILE_RESET (output);
	}


	/* Check that the command functions sufficiently wrap the
	 * nih_option_help function such that we can obtain help for the
	 * program as a whole and get a message saying how to see the
	 * commands list.
	 */
	TEST_FUNCTION ("nih_option_help");

	TEST_FEATURE ("with no command");
	TEST_ALLOC_FAIL {
		argc = 0;
		argv[argc++] = "ignored";
		argv[argc++] = "--help";
		argv[argc] = NULL;

		nih_option_set_synopsis ("This is my program");
		nih_option_set_help ("Some help text");

		TEST_CHILD (pid) {
			unsetenv ("COLUMNS");

			TEST_DIVERT_STDOUT (output) {
				nih_command_parser (NULL, argc, argv,
						    options, commands);
				exit (1);
			}
		}

		waitpid (pid, &status, 0);
		rewind (output);

		TEST_TRUE (WIFEXITED (status));
		TEST_EQ (WEXITSTATUS (status), 0);

		TEST_FILE_EQ (output, ("Usage: test [OPTION]... "
				       "COMMAND [OPTION]... [ARG]...\n"));
		TEST_FILE_EQ (output, "This is my program\n");
		TEST_FILE_EQ (output, "\n");
		TEST_FILE_EQ (output, "Options:\n");
		TEST_FILE_EQ (output, ("  -n, --dry-run               "
				       "simulate and output actions only\n"));
		TEST_FILE_EQ (output, ("  -q, --quiet                 "
				       "reduce output to errors only\n"));
		TEST_FILE_EQ (output, ("  -v, --verbose               "
				       "increase output to include "
				       "informational messages\n"));
		TEST_FILE_EQ (output, ("      --help                  "
				       "display this help and exit\n"));
		TEST_FILE_EQ (output, ("      --version               "
				       "output version information and "
				       "exit\n"));
		TEST_FILE_EQ (output, "\n");
		TEST_FILE_EQ (output, "Some help text\n");
		TEST_FILE_EQ (output, "\n");
		TEST_FILE_EQ (output, ("For a list of commands, "
				       "try `test help'.\n"));
		TEST_FILE_EQ (output, "\n");
		TEST_FILE_EQ (output, "Report bugs to <foo@bar.com>\n");
		TEST_FILE_END (output);

		TEST_FILE_RESET (output);
	}


	/* Check that the wrapping is sufficient that following a command
	 * with the --help option outputs help for that option, including
	 * the global options in the list.
	 */
	TEST_FEATURE ("with a command");
	TEST_ALLOC_FAIL {
		argc = 0;
		argv[argc++] = "ignored";
		argv[argc++] = "wibble";
		argv[argc++] = "--help";
		argv[argc] = NULL;

		TEST_CHILD (pid) {
			unsetenv ("COLUMNS");

			TEST_DIVERT_STDOUT (output) {
				nih_command_parser (NULL, argc, argv,
						    options, commands);
				exit (1);
			}
		}

		waitpid (pid, &status, 0);
		rewind (output);

		TEST_TRUE (WIFEXITED (status));
		TEST_EQ (WEXITSTATUS (status), 0);

		TEST_FILE_EQ (output, ("Usage: test wibble [OPTION]... "
				       "SRC DEST\n"));
		TEST_FILE_EQ (output, ("wibble a file from one place "
				       "to another\n"));
		TEST_FILE_EQ (output, "\n");
		TEST_FILE_EQ (output, "Options:\n");
		TEST_FILE_EQ (output, ("      --wobble                "
				       "wobble file while wibbling\n"));
		TEST_FILE_EQ (output, ("  -n, --dry-run               "
				       "simulate and output actions only\n"));
		TEST_FILE_EQ (output, ("  -q, --quiet                 "
				       "reduce output to errors only\n"));
		TEST_FILE_EQ (output, ("  -v, --verbose               "
				       "increase output to include "
				       "informational messages\n"));
		TEST_FILE_EQ (output, ("      --help                  "
				       "display this help and exit\n"));
		TEST_FILE_EQ (output, ("      --version               "
				       "output version information "
				       "and exit\n"));
		TEST_FILE_EQ (output, "\n");
		TEST_FILE_EQ (output, ("Takes the file from SRC, wibbles it "
				       "until any loose pieces fall off, and "
				       "until\n"));
		TEST_FILE_EQ (output, ("it reaches DEST.  SRC and DEST may "
				       "not be the same location.\n"));
		TEST_FILE_EQ (output, "\n");
		TEST_FILE_EQ (output, "Report bugs to <foo@bar.com>\n");
		TEST_FILE_END (output);

		TEST_FILE_RESET (output);
	}


	/* Check that --help works if the program name itself is the name
	 * of the command, it should behave as if the real binary were
	 * called with the command as the first argument, except all of the
	 * usage strings, etc. should make sense.
	 */
	TEST_FEATURE ("with command in program_name");
	TEST_ALLOC_FAIL {
		program_name = "wibble";
		argc = 0;
		argv[argc++] = "ignored";
		argv[argc++] = "--help";
		argv[argc] = NULL;

		TEST_CHILD (pid) {
			unsetenv ("COLUMNS");

			TEST_DIVERT_STDOUT (output) {
				nih_command_parser (NULL, argc, argv,
						    options, commands);
				exit (1);
			}
		}

		waitpid (pid, &status, 0);
		rewind (output);

		TEST_TRUE (WIFEXITED (status));
		TEST_EQ (WEXITSTATUS (status), 0);

		TEST_FILE_EQ (output, "Usage: wibble [OPTION]... SRC DEST\n");
		TEST_FILE_EQ (output, ("wibble a file from one place to "
				       "another\n"));
		TEST_FILE_EQ (output, "\n");
		TEST_FILE_EQ (output, "Options:\n");
		TEST_FILE_EQ (output, ("      --wobble                "
				       "wobble file while wibbling\n"));
		TEST_FILE_EQ (output, ("  -n, --dry-run               "
				       "simulate and output actions only\n"));
		TEST_FILE_EQ (output, ("  -q, --quiet                 "
				       "reduce output to errors only\n"));
		TEST_FILE_EQ (output, ("  -v, --verbose               "
				       "increase output to include "
				       "informational messages\n"));
		TEST_FILE_EQ (output, ("      --help                  "
				       "display this help and exit\n"));
		TEST_FILE_EQ (output, ("      --version               "
				       "output version information and "
				       "exit\n"));
		TEST_FILE_EQ (output, "\n");
		TEST_FILE_EQ (output, ("Takes the file from SRC, wibbles it "
				       "until any loose pieces fall off, and "
				       "until\n"));
		TEST_FILE_EQ (output, ("it reaches DEST.  SRC and DEST may "
				       "not be the same location.\n"));
		TEST_FILE_EQ (output, "\n");
		TEST_FILE_EQ (output, "Report bugs to <foo@bar.com>\n");
		TEST_FILE_END (output);
	}

	fclose (output);
}


int
main (int   argc,
      char *argv[])
{
	test_parser ();
	test_help ();

	return 0;
}