File: 109_help_flag.t

package info (click to toggle)
libmoosex-getopt-perl 0.45-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 356 kB
  • sloc: perl: 1,964; makefile: 8
file content (61 lines) | stat: -rw-r--r-- 1,898 bytes parent folder | download
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
#!/usr/bin/env perl

# The documentation claims:
#   If Getopt::Long::Descriptive is installed and any of the following command
#   line params are passed (--help, --usage, --?), the program will exit with
#   usage information...

# This is not actually true (as of 0.29), as:
# 1. the consuming class must set up a attributes named 'help', 'usage' and
#     '?' to contain these command line options, which is not clearly
#     documented as a requirement
# 2.  the code is checking whether an option was parsed into an attribute
#     *called* 'help', 'usage' or '?', not whether the option --help, --usage
#     or --? was passed on the command-line (the mapping could be different,
#     if cmd_flag or cmd_aliases is used),

# This inconsistency is the underlying cause of RT#52474, RT#57683, RT#47865.

# Update: since 0.41, usage info is printed to stdout, not stderr.

use strict; use warnings;
use Test::More tests => 18;
use Test::Trap;

{
    package MyClass;
    use strict; use warnings;
    use Moose;
    with 'MooseX::Getopt';
}

# before fix, prints this on stderr:
#Unknown option: ?
#usage: test1.t

# after fix, prints this on stdout (formerly stderr):
#usage: test1.t [-?] [long options...]
#	-? --usage --help  Prints this usage information.

my $obj = MyClass->new_with_options;
ok($obj->meta->has_attribute('usage'), 'class has usage attribute');
isa_ok($obj->usage, 'Getopt::Long::Descriptive::Usage');
my $usage_text = $obj->usage->text;

foreach my $args ( ['--help'], ['--usage'], ['--?'], ['-?'] )
{
    local @ARGV = @$args;
    note "Setting \@ARGV to @$args";

    trap { MyClass->new_with_options() };

    is($trap->leaveby, 'exit', 'bailed with an exit code');
    is($trap->exit, 0, '...of 0');
    is(
        $trap->stdout,
        $usage_text,
        'Usage information printed to STDOUT',
    );
    is($trap->stderr, '', 'there was no STDERR output');
}