测试使用间接对象语法调用exec的Perl脚本的最佳方法是什么?有没有办法模拟exec没有导致语法错误?我是否必须将exec移动到仅调用exec并模拟该函数的包装函数?或者有其他方法来测试脚本吗?
Perl测试的第5章 - 开发人员的笔记本在覆盖内置插件上有一个示例,他们会覆盖{{ 1}}来测试一个模块。我想做同样的事情来测试system
,但是使用间接对象语法。
exec
(使用间接对象语法)exec.pl
package Local::Exec;
use strict;
use warnings;
main() if !caller;
sub main {
my $shell = '/usr/bin/ksh93';
my $shell0 = 'ksh93';
# launch a login shell
exec {$shell} "-$shell0";
return;
}
1;
exec.t
#!perl
use strict;
use warnings;
use Test::More;
require_ok('./exec.pl');
package Local::Exec;
use subs 'exec';
package main;
my @exec_args;
*Local::Exec::exec = sub {
@exec_args = @_;
return 0;
};
is(Local::Exec::main(), undef, 'main() returns undef');
is_deeply(\@exec_args, [ '/usr/bin/ksh93' ], 'exec() was called with correct arguments');
done_testing;
这不起作用,我的 prove -vt exec.t
不能使用内置所做的间接对象语法。
exec
$ prove -vt exec.t
exec.t .. String found where operator expected at ./exec.pl line 15, near "} "-$shell0""
(Missing operator before "-$shell0"?)
not ok 1 - require './exec.pl';
# Failed test 'require './exec.pl';'
# at exec.t line 8.
# Tried to require ''./exec.pl''.
# Error: syntax error at ./exec.pl line 15, near "} "-$shell0""
# Compilation failed in require at (eval 6) line 2.
Undefined subroutine &Local::Exec::main called at exec.t line 21.
# Tests were run but no plan was declared and done_testing() was not seen.
# Looks like your test exited with 255 just after 1.
Dubious, test returned 255 (wstat 65280, 0xff00)
Failed 1/1 subtests
Test Summary Report
-------------------
exec.t (Wstat: 65280 Tests: 1 Failed: 1)
Failed test: 1
Non-zero exit status: 255
Parse errors: No plan found in TAP output
Files=1, Tests=1, 1 wallclock secs ( 0.02 usr 0.01 sys + 0.04 cusr 0.01 csys = 0.08 CPU)
Result: FAIL
(没有间接对象语法)exec.pl
package Local::Exec;
use strict;
use warnings;
main() if !caller;
sub main {
my $shell = '/usr/bin/ksh93';
exec $shell;
return;
}
1;
仅供参考,这有效:
prove -vt exec.t
答案 0 :(得分:5)
替换
exec { $shell } "-$shell0";
与
sub _exec(&@) {
my $prog_cb = shift;
exec { $prog_cb->() } @_
}
_exec { $shell } "-$shell0"
然后您的测试可以替换_exec
。