连接 Perforce 的 Perl 单元测试——连接服务器失败;检查 $P4PORT [英] Perl Unit Test for Connection to Perforce -- Connect to Server Failed; check $P4PORT

查看:122
本文介绍了连接 Perforce 的 Perl 单元测试——连接服务器失败;检查 $P4PORT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 P4Perl 脚本来连接到 Perforce 服务器并自动执行 Perforce 命令.随着访问 Perforce 的子例程的开发,我也在开发单元测试来验证它们.我是 Perl 和单元测试的新手.

I am writing a P4Perl script to make connection to a Perforce server and to automate execution of Perforce commands. Along with the development of the subroutines to access Perforce, I am also developing unit tests to verify the them. I am new to both Perl and unit testing.

这是我建立与 Perforce 连接的子程序.文件名是 p4_connect.pl

This is my subroutine to establish a connection to Perforce. Filename is p4_connect.pl

use warnings;
use strict;

use P4;

my $clientname = "johndoe"
my $p4port = "icmanage:1667"

main();

sub main {
    my $status;
    $status = connect_perforce($clientname, $p4port);
};

sub connect_perforce {

    my ($clientname, $p4port) = @_;
    my $status;
    my $p4 = new P4;

    $p4->SetClient( $clientname );
    $p4->SetPort( $p4port );
    $status = $p4->Connect() or die( "Failed to connect to Perforce Server" );

    return $status;
}

当我运行 "perl p4_connect.pl" 时,Perl 脚本执行良好,没有抛出错误.

The Perl script executes fine when I run "perl p4_connect.pl", no errors are thrown.

但是,当我将 connect_perforce 子例程移动到包模块 (Perforce.pm) 并编写单元测试 (perforce.t) 时> 为此,我遇到了以下错误:

However, when I moved the connect_perforce subroutine to a package module (Perforce.pm) and wrote a unit test (perforce.t) for it, I encountered these errors:

username@hostname% perl -Ilib t/perforce.t
ok 1 - use Perforce;
ok 2 - Perforce->can('connect_perforce')
Connect to server failed; check $P4PORT.
TCP connect to johndoe failed.
Servname not supported for ai_socktype
Failed to connect to Perforce Server at lib/Perforce.pm line 16.

这是单元测试 (perforce.t) 的样子:

This is how the unit test (perforce.t) looks like:

use Perforce;

use warnings;
use strict;
use Test::More qw(no_plan);

use P4;

BEGIN { use_ok('Perforce'); } #package can be loaded

can_ok('Perforce', 'connect_perforce'); #subroutine connect_perforce exists

my $p4port = "icmanage:1667";
my $p4 = Perforce->connect_perforce(qw(johndoe $p4port)); #accessing the connect_perforce() subroutine

这就是我的包 (Perforce.pm) 的样子:

And this is how the my package (Perforce.pm) looks like:

package Perforce;

use warnings;
use strict;

use P4;

   sub connect_perforce {

   my ($clientname, $p4port) = @_;
   my $status;
   my $p4 = new P4;

   $p4->SetClient( $clientname );
   $p4->SetPort( $p4port );
   $status = $p4->Connect() or die( "Failed to connect to Perforce Server" );

   return $status;
}

我的单元测试哪里出错了?任何建议都有帮助.

Where did I go wrong regarding my unit test? Any suggestions are helpful.

推荐答案

你正在混合面向对象的 Perl 和函数式 Perl,你已经成为它的受害者.

You are mixing object orientated Perl and functional Perl, and you have fallen victim to it.

当你使用箭头操作符->将函数作为方法调用时,Perl将左侧的东西作为第一个参数传递给函数1.如果是包名,那只是包名.

When you call a function as a method with the arrow operator ->, Perl passes the thing on the left hand side as the first argument to the function1. In case of a package name, that's just the package name.

package Foo;
sub frobnicate{ print "@_" }

package main;
Foo->frobnicate(1, 2, 3);

这个输出将是

Foo 1 2 3

因此,在您的情况下,connect_perforce 将获得此分配:

So in your case, connect_perforce will get this assignment:

my ($clientname, $p4port) = ('Perforce', 'johndoe', 'icmanage:1667' );

因此变量将具有以下值:

So the variables will have these values:

$clientname: 'Perforce'
$p4port:     'johndoe'

你的 'icmanage:1667' 字符串丢失了.

And your 'icmanage:1667' string gets lost.

当你没有对象时,不要使用箭头.调用此函数的正确方法(它不是方法!)是使用包含包的完全限定名称.

When you don't have objects, don't use the arrow. The correct way to call this function (it's not a method!) is to use the fully qualified name including the package.

my $p4 = Perforce::connect_perforce('johndoe', $p4port);

我已经删除了这个相当奇怪的qw().它会给你一个文字 $p4port 而不是值,所以这是你接下来会遇到的另一个错误.

I've removed this rather weird qw(). It would give you a literal $p4port instead of the value, so that was another bug which you would have encountered next.

既然我们已经确定你没有一个类,而是一个模块,你也不想使用 can_ok.这不是适合您的用例的测试类型.相反,只需像我上面展示的那样调用函数,然后对返回值进行有用的测试.如果该函数不存在,则测试程序将失败,您会注意到.

Since we have established that you don't have a class, but rather a module, you also don't want to use can_ok. That's not the right kind of test for your use case. Instead, simply call the function like I showed above, and then do useful tests with the returned value. If the function isn't there, the test program will fail, and you'll notice.

BEGIN { use_ok('Perforce'); }

my $p4 = Perforce::connect_perforce('johndoe', 'icmanage:1667');

isa_ok($p4, 'P4'); # I guess..

有关面向对象和模块的更多信息,请查看 perlobjperlootut, perlnewmodExporter.Perlmaven 也有一些好文章.

For more information on object orientation and on modules, take a look at perlobj and perlootut, perlnewmod and Exporter. Perlmaven has some good articles too.

1) 它做的更多,但这与这里无关

这篇关于连接 Perforce 的 Perl 单元测试——连接服务器失败;检查 $P4PORT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆