File: run.t

package info (click to toggle)
libtest-cmd-perl 1.09-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 356 kB
  • sloc: perl: 720; makefile: 2
file content (230 lines) | stat: -rw-r--r-- 6,862 bytes parent folder | download | duplicates (7)
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
# Copyright 1999-2001 Steven Knight.  All rights reserved.  This program
# is free software; you can redistribute it and/or modify it under the
# same terms as Perl itself.

######################### We start with some black magic to print on failure.

use Config;
use Test;
my $iswin32;
BEGIN {
    $| = 1;
    if ($] <  5.003) {
	eval("require Win32");
	$iswin32 = ! $@;
    } else {
	$iswin32 = $^O eq "MSWin32";
    }
    plan tests => 53, onfail => sub { $? = 1 if $ENV{AEGIS_TEST} }
}
END {print "not ok 1\n" unless $loaded;}
use Test::Cmd;
$loaded = 1;
ok(1);

######################### End of black magic.

my($run_env, $ret, $testx, $test, $subdir);

#
# The following complicated dance attempts to ensure we can create
# an executable Perl script named "scriptx" on both UNIX and Win32
# systems.  We want it to be Perl since it's about the only thing
# that we can rely on in common between the systems.
#
# The UNIX side is easy; we just put our desired Perl script in
# the file name with $Config{startperl} at the top, chmod it
# executable, and away we go.
#
# For Win32, we go the route of creating a "scriptx.bat" file with
# the magic header that reads as both an NT and a Perl script.
# The hassle is that we want this .bat file to be executable
# regardless of where we are at the moment, and the only way I
# could figure out how to do this was to put the absolute path
# name to the file in the .bat file as the first argument to
# the perl.exe invocation.  This means that we have to create our
# initial running environments up front, so we know where the
# "scriptx.bat" file will end up and can put its path name in
# itself.
#
# If anyone cares to suggest an easier way to do this, I'd be
# thrilled to hear about it.
#
$My_Config{_bat} = $iswin32 ? '.bat' : '';

$run_env = Test::Cmd->new(workdir => '');
ok($run_env);
$wdir = $run_env->workdir;
ok($wdir);
$ret = chdir($wdir);
ok($ret);

my $script = "script";
my $scriptx = "scriptx$My_Config{_bat}";

if ($iswin32) {
    my $workpath_scriptx = $run_env->workpath($scriptx);
    $My_Config{startperl} = <<EOF;
\@rem = '--*-PERL-*--';
\@rem = '
\@echo off
rem setlocal
set ARGS=
:loop
if .%1==. goto endloop
set ARGS=%ARGS% %1
shift
goto loop
:endloop
rem ***** This assumes PERL is in the PATH *****
perl.exe $workpath_scriptx %ARGS%
goto endofperl
\@rem ';
EOF
    $My_Config{endperl} = <<'EOF';
#:endofperl
EOF
    $My_Config{cwd_pkg} = 'Win32';
    $My_Config{cwd_sub} = 'Win32::GetCwd';
} else {
    $My_Config{startperl} = $Config{startperl};
    $My_Config{endperl} = '';
    $My_Config{cwd_pkg} = 'Cwd';
    $My_Config{cwd_sub} = 'cwd';
}

#
$ret = $run_env->write($script, <<EOF);
use $My_Config{cwd_pkg};
my \$cwd = $My_Config{cwd_sub}();
print STDOUT "$script:  \$string:  STDOUT:  \$cwd:  '\@ARGV'\\n";
print STDERR "$script:  \$string:  STDERR:  \$cwd:  '\@ARGV'\\n";
exit 0;
EOF
ok($ret);

$ret = $run_env->write('xxx.pm', <<EOF);
\$string = 'xxx';
EOF
ok($ret);

$ret = $run_env->write('yyy.pm', <<EOF);
\$string = 'yyy';
EOF
ok($ret);

$ret = $run_env->write($scriptx, <<EOF);
$My_Config{startperl}
use $My_Config{cwd_pkg};
my \$cwd = $My_Config{cwd_sub}();
print STDOUT "$scriptx:  \$string:  STDOUT:  \$cwd:  '\@ARGV'\\n";
print STDERR "$scriptx:  \$string:  STDERR:  \$cwd:  '\@ARGV'\\n";
exit 0;
$My_Config{endperl};
EOF
ok($ret);

$ret = chmod(0755, $scriptx) if ! $iswin32;
skip($iswin32, $ret == 1);

ok(! -x $script);
ok(-x $scriptx);

# Everything before this was merely preparation of our "source
# directory."  Now we do some real tests.

#
$test = Test::Cmd->new(prog => 'script', interpreter => "$^X -I$wdir -Mxxx", workdir => '', subdir => 'script_subdir');
ok($test);

$ret = $test->run();
ok($ret == 0);
ok($test->stdout eq "script:  xxx:  STDOUT:  $wdir:  ''\n");
ok($test->stderr eq "script:  xxx:  STDERR:  $wdir:  ''\n");

$ret = $test->run(args => 'arg1 arg2 arg3');
ok($ret == 0);
ok($test->stdout eq "script:  xxx:  STDOUT:  $wdir:  'arg1 arg2 arg3'\n");

# Execute "scriptx" in the middle of the run here,
# so we know it doesn't affect the $test->prog value.
# Note that it should not pick up the test environment's
# interpreter value with "-Mxxx" in it.
$ret = $test->run(prog => 'scriptx', args => 'foo');
ok($ret == 0);
ok($test->stdout eq "$scriptx:  :  STDOUT:  $wdir:  'foo'\n");
ok($test->stderr eq "$scriptx:  :  STDERR:  $wdir:  'foo'\n");

$ret = $test->run(chdir => $test->curdir, args => 'x y z');
ok($ret == 0);
ok($test->stdout eq "script:  xxx:  STDOUT:  ${\$test->workdir}:  'x y z'\n");
ok($test->stderr eq "script:  xxx:  STDERR:  ${\$test->workdir}:  'x y z'\n");

$subdir = $test->workpath('script_subdir');

$ret = $test->run(chdir => 'script_subdir');
ok($ret == 0);
ok($test->stdout eq "script:  xxx:  STDOUT:  $subdir:  ''\n");
ok($test->stderr eq "script:  xxx:  STDERR:  $subdir:  ''\n");

$ret = $test->run(chdir => 'no_subdir');
ok(! defined $ret);

$ret = $test->run(prog => 'no_script', interpreter => $^X);
ok($ret != 0);

$ret = $test->run(prog => 'script');
ok($ret != 0);

$ret = $test->run(prog => 'script', interpreter => 'no_interpreter');
ok($ret != 0);

$ret = $test->run(prog => 'no_script', interpreter => 'no_interpreter');
ok($ret != 0);

$ret = $test->run(interpreter => 'no_interpreter');
ok($ret != 0);

$ret = $test->run(interpreter => "$^X -I$wdir -Myyy", args => 'zzz');
ok($ret == 0);
ok($test->stdout eq "script:  yyy:  STDOUT:  $wdir:  'zzz'\n");
ok($test->stderr eq "script:  yyy:  STDERR:  $wdir:  'zzz'\n");

#
$testx = Test::Cmd->new(prog => 'scriptx', workdir => '', subdir => 'scriptx_subdir');
ok($testx);

$ret = $testx->run();
ok($ret == 0);
ok($testx->stdout eq "$scriptx:  :  STDOUT:  $wdir:  ''\n");
ok($testx->stderr eq "$scriptx:  :  STDERR:  $wdir:  ''\n");

$ret = $testx->run(args => 'foo bar');
ok($ret == 0);
ok($testx->stdout eq "$scriptx:  :  STDOUT:  $wdir:  'foo bar'\n");
ok($testx->stderr eq "$scriptx:  :  STDERR:  $wdir:  'foo bar'\n");

# Execute "script" in the middle of the run here,
# so we know it doesn't affect the $test->prog value.
$ret = $testx->run(prog => 'script', interpreter => "$^X -I$wdir -Mxxx", args => 'bar');
ok($ret == 0);
ok($testx->stdout eq "script:  xxx:  STDOUT:  $wdir:  'bar'\n");
ok($testx->stderr eq "script:  xxx:  STDERR:  $wdir:  'bar'\n");

$ret = $testx->run(chdir => $testx->curdir, args => 'baz');
ok($ret == 0);
ok($testx->stdout eq "$scriptx:  :  STDOUT:  ${\$testx->workdir}:  'baz'\n");
ok($testx->stderr eq "$scriptx:  :  STDERR:  ${\$testx->workdir}:  'baz'\n");

$subdir = $testx->workpath('scriptx_subdir');

$ret = $testx->run(chdir => 'scriptx_subdir');
ok($ret == 0);
ok($testx->stdout eq "$scriptx:  :  STDOUT:  $subdir:  ''\n");
ok($testx->stderr eq "$scriptx:  :  STDERR:  $subdir:  ''\n");

$ret = $testx->run(chdir => 'no_subdir');
ok(! defined $ret);

$ret = $testx->run(prog => 'no_prog');
ok($ret != 0);