xref: /openbsd/gnu/usr.bin/perl/t/run/switches.t (revision 404b540a)
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
7BEGIN {
8    chdir 't' if -d 't';
9    @INC = '../lib';
10}
11
12BEGIN { require "./test.pl"; }
13
14plan(tests => 69);
15
16use 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
23my $r;
24my @tmpfiles = ();
25END { 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);
34is( $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);
41is( $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);
48is( $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);
55is( $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);
62is( $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);
69is( $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);
75is( $r, "(\066)[\066]", '$/ set at compile-time' );
76
77# Tests for -c
78
79my $filename = tempfile();
80SKIP: {
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';
85BEGIN { print "block 1\n"; }
86CHECK { print "block 2\n"; }
87INIT  { print "block 3\n"; }
88	print "block 4\n";
89END   { print "block 5\n"; }
90SWTEST
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}
109
110# Tests for -l
111
112$r = runperl(
113    switches	=> [ sprintf("-l%o", ord 'x') ],
114    prog	=> 'print for qw/foo bar/'
115);
116is( $r, 'fooxbarx', '-l with octal number' );
117
118# Tests for -s
119
120$r = runperl(
121    switches	=> [ '-s' ],
122    prog	=> 'for (qw/abc def ghi/) {print defined $$_ ? $$_ : q(-)}',
123    args	=> [ '--', '-abc=2', '-def', ],
124);
125is( $r, '21-', '-s switch parsing' );
126
127$filename = tempfile();
128SKIP: {
129    open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
130    print $f <<'SWTEST';
131#!perl -s
132BEGIN { print $x,$y; exit }
133SWTEST
134    close $f or die "Could not close: $!";
135    $r = runperl(
136	progfile    => $filename,
137	args	    => [ '-x=foo -y' ],
138    );
139    is( $r, 'foo1', '-s on the shebang line' );
140}
141
142# Bug ID 20011106.084
143$filename = tempfile();
144SKIP: {
145    open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
146    print $f <<'SWTEST';
147#!perl -sn
148BEGIN { print $x; exit }
149SWTEST
150    close $f or die "Could not close: $!";
151    $r = runperl(
152	progfile    => $filename,
153	args	    => [ '-x=foo' ],
154    );
155    is( $r, 'foo', '-sn on the shebang line' );
156}
157
158# Tests for -m and -M
159
160my $package = tempfile();
161$filename = "$package.pm";
162SKIP: {
163    open my $f, ">$filename" or skip( "Can't write temp file $filename: $!",4 );
164    print $f <<"SWTESTPM";
165package $package;
166sub import { print map "<\$_>", \@_ }
1671;
168SWTESTPM
169    close $f or die "Could not close: $!";
170    $r = runperl(
171	switches    => [ "-M$package" ],
172	prog	    => '1',
173    );
174    is( $r, "<$package>", '-M' );
175    $r = runperl(
176	switches    => [ "-M$package=foo" ],
177	prog	    => '1',
178    );
179    is( $r, "<$package><foo>", '-M with import parameter' );
180    $r = runperl(
181	switches    => [ "-m$package" ],
182	prog	    => '1',
183    );
184
185    {
186        local $TODO = '';  # this one works on VMS
187        is( $r, '', '-m' );
188    }
189    $r = runperl(
190	switches    => [ "-m$package=foo,bar" ],
191	prog	    => '1',
192    );
193    is( $r, "<$package><foo><bar>", '-m with import parameters' );
194    push @tmpfiles, $filename;
195
196  {
197    local $TODO = '';  # these work on VMS
198
199    is( runperl( switches => [ '-MTie::Hash' ], stderr => 1, prog => 1 ),
200	  '', "-MFoo::Bar allowed" );
201
202    like( runperl( switches => [ "-M:$package" ], stderr => 1,
203		   prog => 'die "oops"' ),
204	  qr/Invalid module name [\w:]+ with -M option\b/,
205          "-M:Foo not allowed" );
206
207    like( runperl( switches => [ '-mA:B:C' ], stderr => 1,
208		   prog => 'die "oops"' ),
209	  qr/Invalid module name [\w:]+ with -m option\b/,
210          "-mFoo:Bar not allowed" );
211
212    like( runperl( switches => [ '-m-A:B:C' ], stderr => 1,
213		   prog => 'die "oops"' ),
214	  qr/Invalid module name [\w:]+ with -m option\b/,
215          "-m-Foo:Bar not allowed" );
216
217    like( runperl( switches => [ '-m-' ], stderr => 1,
218		   prog => 'die "oops"' ),
219	  qr/Module name required with -m option\b/,
220  	  "-m- not allowed" );
221
222    like( runperl( switches => [ '-M-=' ], stderr => 1,
223		   prog => 'die "oops"' ),
224	  qr/Module name required with -M option\b/,
225  	  "-M- not allowed" );
226  }  # disable TODO on VMS
227}
228
229# Tests for -V
230
231{
232    local $TODO = '';   # these ones should work on VMS
233
234    # basic perl -V should generate significant output.
235    # we don't test actual format too much since it could change
236    like( runperl( switches => ['-V'] ), qr/(\n.*){20}/,
237          '-V generates 20+ lines' );
238
239    like( runperl( switches => ['-V'] ),
240	  qr/\ASummary of my perl5 .*configuration:/,
241          '-V looks okay' );
242
243    # lookup a known config var
244    chomp( $r=runperl( switches => ['-V:osname'] ) );
245    is( $r, "osname='$^O';", 'perl -V:osname');
246
247    # lookup a nonexistent var
248    chomp( $r=runperl( switches => ['-V:this_var_makes_switches_test_fail'] ) );
249    is( $r, "this_var_makes_switches_test_fail='UNKNOWN';",
250        'perl -V:unknown var');
251
252    # regexp lookup
253    # platforms that don't like this quoting can either skip this test
254    # or fix test.pl _quote_args
255    $r = runperl( switches => ['"-V:i\D+size"'] );
256    # should be unlike( $r, qr/^$|not found|UNKNOWN/ );
257    like( $r, qr/^(?!.*(not found|UNKNOWN))./, 'perl -V:re got a result' );
258
259    # make sure each line we got matches the re
260    ok( !( grep !/^i\D+size=/, split /^/, $r ), '-V:re correct' );
261}
262
263# Tests for -v
264
265{
266    local $TODO = '';   # these ones should work on VMS
267    # there are definitely known build configs where this test will fail
268    # DG/UX comes to mind. Maybe we should remove these special cases?
269    my $v = sprintf "%vd", $^V;
270    like( runperl( switches => ['-v'] ),
271	  qr/This is perl, v$v(?:[-\w]+| \([^)]+\))? built for \Q$Config{archname}\E.+Copyright.+Larry Wall.+Artistic License.+GNU General Public License/s,
272          '-v looks okay' );
273
274}
275
276# Tests for -h
277
278{
279    local $TODO = '';   # these ones should work on VMS
280
281    like( runperl( switches => ['-h'] ),
282	  qr/Usage: .+(?i:perl(?:$Config{_exe})?).+switches.+programfile.+arguments/,
283          '-h looks okay' );
284
285}
286
287# Tests for switches which do not exist
288
289foreach my $switch (split //, "ABbGgHJjKkLNOoQqRrYyZz123456789_")
290{
291    local $TODO = '';   # these ones should work on VMS
292
293    like( runperl( switches => ["-$switch"], stderr => 1,
294		   prog => 'die "oops"' ),
295	  qr/\QUnrecognized switch: -$switch  (-h will show valid options)./,
296          "-$switch correctly unknown" );
297
298}
299
300# Tests for -i
301
302{
303    local $TODO = '';   # these ones should work on VMS
304
305    sub do_i_unlink { 1 while unlink("file", "file.bak") }
306
307    open(FILE, ">file") or die "$0: Failed to create 'file': $!";
308    print FILE <<__EOF__;
309foo yada dada
310bada foo bing
311king kong foo
312__EOF__
313    close FILE;
314
315    END { do_i_unlink() }
316
317    runperl( switches => ['-pi.bak'], prog => 's/foo/bar/', args => ['file'] );
318
319    open(FILE, "file") or die "$0: Failed to open 'file': $!";
320    chomp(my @file = <FILE>);
321    close FILE;
322
323    open(BAK, "file.bak") or die "$0: Failed to open 'file': $!";
324    chomp(my @bak = <BAK>);
325    close BAK;
326
327    is(join(":", @file),
328       "bar yada dada:bada bar bing:king kong bar",
329       "-i new file");
330    is(join(":", @bak),
331       "foo yada dada:bada foo bing:king kong foo",
332       "-i backup file");
333}
334
335# Tests for -E
336
337$TODO = '';  # the -E tests work on VMS
338
339$r = runperl(
340    switches	=> [ '-E', '"say q(Hello, world!)"']
341);
342is( $r, "Hello, world!\n", "-E say" );
343
344
345$r = runperl(
346    switches	=> [ '-E', '"undef ~~ undef and say q(Hello, world!)"']
347);
348is( $r, "Hello, world!\n", "-E ~~" );
349
350$r = runperl(
351    switches	=> [ '-E', '"given(undef) {when(undef) { say q(Hello, world!)"}}']
352);
353is( $r, "Hello, world!\n", "-E given" );
354
355$r = runperl(
356    switches    => [ '-nE', q("} END { say q/affe/") ],
357    stdin       => 'zomtek',
358);
359is( $r, "affe\n", '-E works outside of the block created by -n' );
360
361# RT #30660
362
363$filename = tempfile();
364SKIP: {
365    open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
366    print $f <<'SWTEST';
367#!perl -w    -iok
368print "$^I\n";
369SWTEST
370    close $f or die "Could not close: $!";
371    $r = runperl(
372	progfile    => $filename,
373    );
374    like( $r, qr/ok/, 'Spaces on the #! line (#30660)' );
375}
376