for -M:Foo, extended and revised
[p5sagit/p5-mst-13.2.git] / t / run / switches.t
1 #!./perl -w
2
3 # Tests for the command-line switches:
4 # -0, -c, -l, -s, -m, -M, -V, -v, -h, -i, -E and all unknown
5 # Some switches have their own tests, see MANIFEST.
6
7 BEGIN {
8     chdir 't' if -d 't';
9     @INC = '../lib';
10 }
11
12 BEGIN { require "./test.pl"; }
13
14 plan(tests => 68);
15
16 use Config;
17
18 # due to a bug in VMS's piping which makes it impossible for runperl()
19 # to emulate echo -n (ie. stdin always winds up with a newline), these 
20 # tests almost totally fail.
21 $TODO = "runperl() unable to emulate echo -n due to pipe bug" if $^O eq 'VMS';
22
23 my $r;
24 my @tmpfiles = ();
25 END { unlink @tmpfiles }
26
27 # Tests for -0
28
29 $r = runperl(
30     switches    => [ '-0', ],
31     stdin       => 'foo\0bar\0baz\0',
32     prog        => 'print qq(<$_>) while <>',
33 );
34 is( $r, "<foo\0><bar\0><baz\0>", "-0" );
35
36 $r = runperl(
37     switches    => [ '-l', '-0', '-p' ],
38     stdin       => 'foo\0bar\0baz\0',
39     prog        => '1',
40 );
41 is( $r, "foo\nbar\nbaz\n", "-0 after a -l" );
42
43 $r = runperl(
44     switches    => [ '-0', '-l', '-p' ],
45     stdin       => 'foo\0bar\0baz\0',
46     prog        => '1',
47 );
48 is( $r, "foo\0bar\0baz\0", "-0 before a -l" );
49
50 $r = runperl(
51     switches    => [ sprintf("-0%o", ord 'x') ],
52     stdin       => 'fooxbarxbazx',
53     prog        => 'print qq(<$_>) while <>',
54 );
55 is( $r, "<foox><barx><bazx>", "-0 with octal number" );
56
57 $r = runperl(
58     switches    => [ '-00', '-p' ],
59     stdin       => 'abc\ndef\n\nghi\njkl\nmno\n\npq\n',
60     prog        => 's/\n/-/g;$_.=q(/)',
61 );
62 is( $r, 'abc-def--/ghi-jkl-mno--/pq-/', '-00 (paragraph mode)' );
63
64 $r = runperl(
65     switches    => [ '-0777', '-p' ],
66     stdin       => 'abc\ndef\n\nghi\njkl\nmno\n\npq\n',
67     prog        => 's/\n/-/g;$_.=q(/)',
68 );
69 is( $r, 'abc-def--ghi-jkl-mno--pq-/', '-0777 (slurp mode)' );
70
71 $r = runperl(
72     switches    => [ '-066' ],
73     prog        => 'BEGIN { print qq{($/)} } print qq{[$/]}',
74 );
75 is( $r, "(\066)[\066]", '$/ set at compile-time' );
76
77 # Tests for -c
78
79 my $filename = 'swctest.tmp';
80 SKIP: {
81     local $TODO = '';   # this one works on VMS
82
83     open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
84     print $f <<'SWTEST';
85 BEGIN { print "block 1\n"; }
86 CHECK { print "block 2\n"; }
87 INIT  { print "block 3\n"; }
88         print "block 4\n";
89 END   { print "block 5\n"; }
90 SWTEST
91     close $f or die "Could not close: $!";
92     $r = runperl(
93         switches        => [ '-c' ],
94         progfile        => $filename,
95         stderr          => 1,
96     );
97     # Because of the stderr redirection, we can't tell reliably the order
98     # in which the output is given
99     ok(
100         $r =~ /$filename syntax OK/
101         && $r =~ /\bblock 1\b/
102         && $r =~ /\bblock 2\b/
103         && $r !~ /\bblock 3\b/
104         && $r !~ /\bblock 4\b/
105         && $r !~ /\bblock 5\b/,
106         '-c'
107     );
108     push @tmpfiles, $filename;
109 }
110
111 # Tests for -l
112
113 $r = runperl(
114     switches    => [ sprintf("-l%o", ord 'x') ],
115     prog        => 'print for qw/foo bar/'
116 );
117 is( $r, 'fooxbarx', '-l with octal number' );
118
119 # Tests for -s
120
121 $r = runperl(
122     switches    => [ '-s' ],
123     prog        => 'for (qw/abc def ghi/) {print defined $$_ ? $$_ : q(-)}',
124     args        => [ '--', '-abc=2', '-def', ],
125 );
126 is( $r, '21-', '-s switch parsing' );
127
128 $filename = 'swstest.tmp';
129 SKIP: {
130     open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
131     print $f <<'SWTEST';
132 #!perl -s
133 BEGIN { print $x,$y; exit }
134 SWTEST
135     close $f or die "Could not close: $!";
136     $r = runperl(
137         progfile    => $filename,
138         args        => [ '-x=foo -y' ],
139     );
140     is( $r, 'foo1', '-s on the shebang line' );
141     push @tmpfiles, $filename;
142 }
143
144 # Bug ID 20011106.084
145 $filename = 'swsntest.tmp';
146 SKIP: {
147     open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
148     print $f <<'SWTEST';
149 #!perl -sn
150 BEGIN { print $x; exit }
151 SWTEST
152     close $f or die "Could not close: $!";
153     $r = runperl(
154         progfile    => $filename,
155         args        => [ '-x=foo' ],
156     );
157     is( $r, 'foo', '-sn on the shebang line' );
158     push @tmpfiles, $filename;
159 }
160
161 # Tests for -m and -M
162
163 $filename = 'swtest.pm';
164 SKIP: {
165     open my $f, ">$filename" or skip( "Can't write temp file $filename: $!",4 );
166     print $f <<'SWTESTPM';
167 package swtest;
168 sub import { print map "<$_>", @_ }
169 1;
170 SWTESTPM
171     close $f or die "Could not close: $!";
172     $r = runperl(
173         switches    => [ '-Mswtest' ],
174         prog        => '1',
175     );
176     is( $r, '<swtest>', '-M' );
177     $r = runperl(
178         switches    => [ '-Mswtest=foo' ],
179         prog        => '1',
180     );
181     is( $r, '<swtest><foo>', '-M with import parameter' );
182     $r = runperl(
183         switches    => [ '-mswtest' ],
184         prog        => '1',
185     );
186
187     {
188         local $TODO = '';  # this one works on VMS
189         is( $r, '', '-m' );
190     }
191     $r = runperl(
192         switches    => [ '-mswtest=foo,bar' ],
193         prog        => '1',
194     );
195     is( $r, '<swtest><foo><bar>', '-m with import parameters' );
196     push @tmpfiles, $filename;
197
198     is( runperl( switches => [ '-MTie::Hash' ], stderr => 1, prog => 1 ),
199           '', "-MFoo::Bar allowed" );
200
201     like( runperl( switches => [ '-M:swtest' ], stderr => 1,
202                    prog => 'die "oops"' ),
203           qr/Invalid module name [\w:]+ with -M option\b/,
204           "-M:Foo not allowed" );
205
206     like( runperl( switches => [ '-mA:B:C' ], stderr => 1,
207                    prog => 'die "oops"' ),
208           qr/Invalid module name [\w:]+ with -m option\b/,
209           "-mFoo:Bar not allowed" );
210
211     like( runperl( switches => [ '-m-A:B:C' ], stderr => 1,
212                    prog => 'die "oops"' ),
213           qr/Invalid module name [\w:]+ with -m option\b/,
214           "-m-Foo:Bar not allowed" );
215
216     like( runperl( switches => [ '-m-' ], stderr => 1,
217                    prog => 'die "oops"' ),
218           qr/Module name required with -m option\b/,
219           "-m- not allowed" );
220
221     like( runperl( switches => [ '-M-=' ], stderr => 1,
222                    prog => 'die "oops"' ),
223           qr/Module name required with -M option\b/,
224           "-M- not allowed" );
225 }
226
227 # Tests for -V
228
229 {
230     local $TODO = '';   # these ones should work on VMS
231
232     # basic perl -V should generate significant output.
233     # we don't test actual format too much since it could change
234     like( runperl( switches => ['-V'] ), qr/(\n.*){20}/,
235           '-V generates 20+ lines' );
236
237     like( runperl( switches => ['-V'] ),
238           qr/\ASummary of my perl5 .*configuration:/,
239           '-V looks okay' );
240
241     # lookup a known config var
242     chomp( $r=runperl( switches => ['-V:osname'] ) );
243     is( $r, "osname='$^O';", 'perl -V:osname');
244
245     # lookup a nonexistent var
246     chomp( $r=runperl( switches => ['-V:this_var_makes_switches_test_fail'] ) );
247     is( $r, "this_var_makes_switches_test_fail='UNKNOWN';",
248         'perl -V:unknown var');
249
250     # regexp lookup
251     # platforms that don't like this quoting can either skip this test
252     # or fix test.pl _quote_args
253     $r = runperl( switches => ['"-V:i\D+size"'] );
254     # should be unlike( $r, qr/^$|not found|UNKNOWN/ );
255     like( $r, qr/^(?!.*(not found|UNKNOWN))./, 'perl -V:re got a result' );
256
257     # make sure each line we got matches the re
258     ok( !( grep !/^i\D+size=/, split /^/, $r ), '-V:re correct' );
259 }
260
261 # Tests for -v
262
263 {
264     local $TODO = '';   # these ones should work on VMS
265
266     my $v = sprintf "%vd", $^V;
267     like( runperl( switches => ['-v'] ),
268           qr/This is perl, v$v (?:DEVEL\d+ )?built for \Q$Config{archname}\E.+Copyright.+Larry Wall.+Artistic License.+GNU General Public License/s,
269           '-v looks okay' );
270
271 }
272
273 # Tests for -h
274
275 {
276     local $TODO = '';   # these ones should work on VMS
277
278     like( runperl( switches => ['-h'] ),
279           qr/Usage: .+(?i:perl(?:$Config{_exe})?).+switches.+programfile.+arguments/,
280           '-h looks okay' );
281
282 }
283
284 # Tests for switches which do not exist
285
286 foreach my $switch (split //, "ABbGgHJjKkLNOoPQqRrYyZz123456789_")
287 {
288     local $TODO = '';   # these ones should work on VMS
289
290     like( runperl( switches => ["-$switch"], stderr => 1,
291                    prog => 'die "oops"' ),
292           qr/\QUnrecognized switch: -$switch  (-h will show valid options)./,
293           "-$switch correctly unknown" );
294
295 }
296
297 # Tests for -i
298
299 {
300     local $TODO = '';   # these ones should work on VMS
301
302     sub do_i_unlink { 1 while unlink("file", "file.bak") }
303
304     open(FILE, ">file") or die "$0: Failed to create 'file': $!";
305     print FILE <<__EOF__;
306 foo yada dada
307 bada foo bing
308 king kong foo
309 __EOF__
310     close FILE;
311
312     END { do_i_unlink() }
313
314     runperl( switches => ['-pi.bak'], prog => 's/foo/bar/', args => ['file'] );
315
316     open(FILE, "file") or die "$0: Failed to open 'file': $!";
317     chomp(my @file = <FILE>);
318     close FILE;
319
320     open(BAK, "file.bak") or die "$0: Failed to open 'file': $!";
321     chomp(my @bak = <BAK>);
322     close BAK;
323
324     is(join(":", @file),
325        "bar yada dada:bada bar bing:king kong bar",
326        "-i new file");
327     is(join(":", @bak),
328        "foo yada dada:bada foo bing:king kong foo",
329        "-i backup file");
330 }
331
332 # Tests for -E
333
334 $r = runperl(
335     switches    => [ '-E', '"say q(Hello, world!)"']
336 );
337 is( $r, "Hello, world!\n", "-E say" );
338
339
340 $r = runperl(
341     switches    => [ '-E', '"undef ~~ undef and say q(Hello, world!)"']
342 );
343 is( $r, "Hello, world!\n", "-E ~~" );
344
345 $r = runperl(
346     switches    => [ '-E', '"given(undef) {when(undef) { say q(Hello, world!)"}}']
347 );
348 is( $r, "Hello, world!\n", "-E given" );