开发者

Moose module with errors, need help finding the cause

开发者 https://www.devze.com 2023-02-09 22:41 出处:网络
I\'m trying to refactor my tests for Dist::Zilla::Plugin::Catalyst to be more DRY,trying to use Moose for the task, but I\'m running into issues, and for the life of me I can\'t figure out what they a

I'm trying to refactor my tests for Dist::Zilla::Plugin::Catalyst to be more DRY,trying to use Moose for the task, but I'm running into issues, and for the life of me I can't figure out what they are, some of it has appeared to be order initialization issues.

note: code is probably not the best, suggestions welcome

t/01-PluginCatalystNew.t ....... Can't call method "subdir" on an undefined value at /home/xenoterracide/projects/Dist-Zilla-Plugin-Catalyst/.build/8kE7QQR_FL/t/lib/DZPCshared.pm line 26.

Here's the Moose Module,

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 . '_fastc开发者_运维百科gi.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;

and the test

#!/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;

(note: p.s. not sure a minimal test case is possible in this)

(note: also the previous version worked)


I played a bit with your code - you have lazy attribute directories used in non-lazy attributes default. Because of that the default on directories is called before instance is fully built. Thus $self->tempdir on line 26 is undef and you got your error message.

I made scripts and files attributes lazy and it seems to work (it throws validation error on directories - you have ArrayRef[Str], but construct ArrayRef[Path::Class::Dir] instead - but this is not related to your current problem).

0

精彩评论

暂无评论...
验证码 换一张
取 消