Moose模块有错误,需要帮助查找原因

时间:2011-02-12 13:38:07

标签: perl refactoring reference moose

我正在尝试将Dist::Zilla::Plugin::Catalyst的测试重构为更干,尝试使用Moose完成任务,但我遇到了问题,而且对于我的生活,我无法弄清楚他们是什么,其中一些似乎是订单初始化问题。

注意:代码可能不是最好的,建议欢迎

t / 01-PluginCatalystNew.t .......无法在/home/xenoterracide/projects/Dist-Zilla-Plugin-Catalyst/.build/8kE7QQR_FL/t/lib上的未定义值上调用方法“subdir” /DZPCshared.pm第26行。

这是驼鹿模块,

use strict;
use warnings;
package DZPCshared;
use Path::Class;
use Moose;
use namespace::autoclean;

has 'appname' => (
    is       => 'ro',
    required => 1,
);

has 'tempdir' => (
    is       => 'ro',
    required => 1,
);

has 'directories' => (
    isa      => 'ArrayRef[Str]',
    traits   => ['Array'],
    is       => 'ro',
    required => 1,
    lazy     => 1,
    default  => sub {
        my $self = shift;
        my $mr   = dir( $self->tempdir )->subdir('mint');
        my $mrl  = $mr->subdir('lib');
        my $mrr  = $mr->subdir('root');
        my $mrs  = $mr->subdir('script');
        my $mrt  = $mr->subdir('t');
        my $mrri = $mr->subdir('root')->subdir('static')->subdir('images');
        return my $directories = [ $mr, $mrl, $mrr, $mrs, $mrt, $mrri ];
    },
);

has 'scripts' => (
    isa      => 'ArrayRef[Str]',
    traits   => ['Array'],
    is       => 'ro',
    default  => sub {
        my $self = shift;
        my ( $mr, $mrl, $mrr, $mrs, $mrt, $mrri ) = @{ $self->directories };
        my $lc_app = lc $self->appname;
        return my $scripts = [
            $mrs->file  ( $lc_app . '_cgi.pl'     ),
            $mrs->file  ( $lc_app . '_create.pl'  ),
            $mrs->file  ( $lc_app . '_fastcgi.pl' ),
            $mrs->file  ( $lc_app . '_server.pl'  ),
            $mrs->file  ( $lc_app . '_test.pl'    ),
        ];
    },
);

has 'files' => (
    isa      => 'ArrayRef[Str]',
    traits   => ['Array'],
    is       => 'ro',
    default  => sub {
        my $self = shift;
        my ( $mr, $mrl, $mrr, $mrs, $mrt, $mrri ) = @{ $self->directories };
        my $lc_app = lc $self->appname;
        return my $files = [
            $mr->file   ( $lc_app . '.conf'               ),
            $mrl->file  ( $self->app_name . '.pm'               ),
            $mrl->subdir( $self->app_name )->subdir('Controller')->file('Root.pm'),
            $mrr->file  ( 'favicon.ico'                   ),
            $mrri->file ( 'btn_120x50_built.png'          ),
            $mrri->file ( 'btn_120x50_built_shadow.png'   ),
            $mrri->file ( 'btn_120x50_powered.png'        ),
            $mrri->file ( 'btn_120x50_powered_shadow.png' ),
            $mrri->file ( 'btn_88x31_built.png'           ),
            $mrri->file ( 'btn_88x31_built_shadow.png'    ),
            $mrri->file ( 'btn_88x31_powered.png'         ),
            $mrri->file ( 'btn_88x31_powered_shadow.png'  ),
            $mrri->file ( 'catalyst_logo.png'             ),
            $mrt->file  ( '01app.t'                       ),
        ];
    },
);

__PACKAGE__->meta->make_immutable;
1;

和测试

#!/usr/bin/perl
use strict;
use warnings;
use Test::More;

if ( $Moose::VERSION >= 1.9902 and $Moose::VERSION < 2.0 ) {
    plan skip_all => 'Module is broken on Devel Moose, don\'t test';
}

use Dist::Zilla::Tester;
use Path::Class;
use FindBin;
use lib "$FindBin::Bin/lib";
use DZPCshared;

my $tzil = Minter->_new_from_profile(
    [ Default => 'default' ],
    { name => 'CatApp' },
    { global_config_root => dir('corpus/mint')->absolute },
);

$tzil->mint_dist;

my $dzpcs = DZPCshared->new({
    appname => $tzil->name,
    tempdir => $tzil->tempdir,
});

subtest 'catalyst files exist' => sub {
    my $should_exists = [ @{$dzpcs->files}, @{$dzpcs->scripts} ];

    foreach ( @{$should_exists} ) {
        ok  ( -e $_ , "$_" . ' exists' );
    }
};

subtest 'catalyst scripts should be executable' => sub {
    plan skip_all => 'skip failing executable tests on windows' if $^O eq 'MSWin32';
    my $should_exec = @{$dzpcs->scripts};

    foreach ( @{$should_exec} ) {
        ok  ( -x $_ , "$_" . ' exists' );
    }
};
done_testing;

(注意:p.s.不确定在此可能的最小测试用例)

(注意:previous version也有效)

1 个答案:

答案 0 :(得分:5)

我使用了您的代码 - 您在非惰性属性directories中使用了 lazy 属性default。因此,在完全构建实例之前调用directories上的默认值。因此,第26行的$self->tempdirundef,您收到了错误消息。

我将scriptsfiles属性设置为懒惰并且它似乎有用(它会在目录上引发验证错误 - 您有ArrayRef[Str],但构造ArrayRef[Path::Class::Dir]代替 - 但这与您当前的问题无关)。