This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
patch@32274 t/op/taint.t not cleaning up properly on VMS.
[perl5.git] / t / run / switches.t
CommitLineData
8a73d5dd
RGS
1#!./perl -w
2
feafb1eb 3# Tests for the command-line switches:
fed0ca7f 4# -0, -c, -l, -s, -m, -M, -V, -v, -h, -i, -E and all unknown
48eaf804 5# Some switches have their own tests, see MANIFEST.
8a73d5dd
RGS
6
7BEGIN {
8 chdir 't' if -d 't';
9 @INC = '../lib';
10}
11
768fd157 12BEGIN { require "./test.pl"; }
8a73d5dd 13
f23102e2 14plan(tests => 61);
8a73d5dd 15
a09a0aa2
JH
16use Config;
17
b734d6c9 18# due to a bug in VMS's piping which makes it impossible for runperl()
e54d2dfa
JH
19# to emulate echo -n (ie. stdin always winds up with a newline), these
20# tests almost totally fail.
b734d6c9
MS
21$TODO = "runperl() unable to emulate echo -n due to pipe bug" if $^O eq 'VMS';
22
8a73d5dd
RGS
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
7f9e821f
RGS
71$r = runperl(
72 switches => [ '-066' ],
0fbedf1d 73 prog => 'BEGIN { print qq{($/)} } print qq{[$/]}',
7f9e821f
RGS
74);
75is( $r, "(\066)[\066]", '$/ set at compile-time' );
76
8a73d5dd
RGS
77# Tests for -c
78
79my $filename = 'swctest.tmp';
80SKIP: {
b734d6c9
MS
81 local $TODO = ''; # this one works on VMS
82
8a73d5dd
RGS
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
d1e4d418 91 close $f or die "Could not close: $!";
8a73d5dd
RGS
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);
117is( $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);
126is( $r, '21-', '-s switch parsing' );
127
8a73d5dd
RGS
128$filename = 'swstest.tmp';
129SKIP: {
130 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
131 print $f <<'SWTEST';
59e235cb
GA
132#!perl -s
133BEGIN { print $x,$y; exit }
134SWTEST
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';
146SKIP: {
147 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
148 print $f <<'SWTEST';
f0b2cf55
YST
149#!perl -sn
150BEGIN { print $x; exit }
8a73d5dd 151SWTEST
d1e4d418 152 close $f or die "Could not close: $!";
8a73d5dd 153 $r = runperl(
8a73d5dd
RGS
154 progfile => $filename,
155 args => [ '-x=foo' ],
156 );
59e235cb 157 is( $r, 'foo', '-sn on the shebang line' );
8a73d5dd
RGS
158 push @tmpfiles, $filename;
159}
160
161# Tests for -m and -M
162
163$filename = 'swtest.pm';
164SKIP: {
165 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!",4 );
166 print $f <<'SWTESTPM';
167package swtest;
168sub import { print map "<$_>", @_ }
1691;
170SWTESTPM
d1e4d418 171 close $f or die "Could not close: $!";
8a73d5dd
RGS
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 );
b734d6c9
MS
186
187 {
188 local $TODO = ''; # this one works on VMS
189 is( $r, '', '-m' );
190 }
8a73d5dd
RGS
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}
e54d2dfa
JH
198
199# Tests for -V
200
201{
202 local $TODO = ''; # these ones should work on VMS
203
204 # basic perl -V should generate significant output.
ceda46a1 205 # we don't test actual format too much since it could change
e54d2dfa
JH
206 like( runperl( switches => ['-V'] ), qr/(\n.*){20}/,
207 '-V generates 20+ lines' );
208
ceda46a1
JH
209 like( runperl( switches => ['-V'] ),
210 qr/\ASummary of my perl5 .*configuration:/,
211 '-V looks okay' );
212
e54d2dfa
JH
213 # lookup a known config var
214 chomp( $r=runperl( switches => ['-V:osname'] ) );
215 is( $r, "osname='$^O';", 'perl -V:osname');
216
217 # lookup a nonexistent var
218 chomp( $r=runperl( switches => ['-V:this_var_makes_switches_test_fail'] ) );
219 is( $r, "this_var_makes_switches_test_fail='UNKNOWN';",
220 'perl -V:unknown var');
221
222 # regexp lookup
223 # platforms that don't like this quoting can either skip this test
224 # or fix test.pl _quote_args
225 $r = runperl( switches => ['"-V:i\D+size"'] );
226 # should be unlike( $r, qr/^$|not found|UNKNOWN/ );
227 like( $r, qr/^(?!.*(not found|UNKNOWN))./, 'perl -V:re got a result' );
228
229 # make sure each line we got matches the re
230 ok( !( grep !/^i\D+size=/, split /^/, $r ), '-V:re correct' );
231}
ceda46a1
JH
232
233# Tests for -v
234
235{
236 local $TODO = ''; # these ones should work on VMS
237
ceda46a1 238 my $v = sprintf "%vd", $^V;
ceda46a1 239 like( runperl( switches => ['-v'] ),
7850f74c 240 qr/This is perl, v$v (?:DEVEL\d+ )?built for \Q$Config{archname}\E.+Copyright.+Larry Wall.+Artistic License.+GNU General Public License/s,
ceda46a1
JH
241 '-v looks okay' );
242
243}
b8e3af44
JH
244
245# Tests for -h
246
247{
248 local $TODO = ''; # these ones should work on VMS
249
250 like( runperl( switches => ['-h'] ),
a09a0aa2 251 qr/Usage: .+(?i:perl(?:$Config{_exe})?).+switches.+programfile.+arguments/,
b8e3af44
JH
252 '-h looks okay' );
253
254}
255
fed0ca7f 256# Tests for switches which do not exist
b8e3af44 257
fed0ca7f 258foreach my $switch (split //, "ABbGgHJjKkLNOoQqRrYyZz123456789_")
b8e3af44
JH
259{
260 local $TODO = ''; # these ones should work on VMS
261
fed0ca7f
NC
262 like( runperl( switches => ["-$switch"], stderr => 1,
263 prog => 'die "oops"' ),
264 qr/\QUnrecognized switch: -$switch (-h will show valid options)./,
265 "-$switch correctly unknown" );
b8e3af44
JH
266
267}
feafb1eb
JH
268
269# Tests for -i
270
271{
272 local $TODO = ''; # these ones should work on VMS
273
274 sub do_i_unlink { 1 while unlink("file", "file.bak") }
275
276 open(FILE, ">file") or die "$0: Failed to create 'file': $!";
277 print FILE <<__EOF__;
278foo yada dada
279bada foo bing
280king kong foo
281__EOF__
282 close FILE;
283
284 END { do_i_unlink() }
285
286 runperl( switches => ['-pi.bak'], prog => 's/foo/bar/', args => ['file'] );
287
288 open(FILE, "file") or die "$0: Failed to open 'file': $!";
289 chomp(my @file = <FILE>);
290 close FILE;
291
292 open(BAK, "file.bak") or die "$0: Failed to open 'file': $!";
293 chomp(my @bak = <BAK>);
294 close BAK;
295
296 is(join(":", @file),
297 "bar yada dada:bada bar bing:king kong bar",
298 "-i new file");
299 is(join(":", @bak),
300 "foo yada dada:bada foo bing:king kong foo",
301 "-i backup file");
302}
bc9b29db
RH
303
304# Tests for -E
305
306$r = runperl(
307 switches => [ '-E', '"say q(Hello, world!)"']
308);
309is( $r, "Hello, world!\n", "-E say" );
310
311
312$r = runperl(
bc9b29db
RH
313 switches => [ '-E', '"undef ~~ undef and say q(Hello, world!)"']
314);
315is( $r, "Hello, world!\n", "-E ~~" );
316
317$r = runperl(
318 switches => [ '-E', '"given(undef) {when(undef) { say q(Hello, world!)"}}']
319);
320is( $r, "Hello, world!\n", "-E given" );