From a7e0e8fe5d8cafed9bbc26061b8bf1574f1e8976 Mon Sep 17 00:00:00 2001 From: Aaron Cohen Date: Mon, 13 Jan 2014 03:22:43 -0500 Subject: [PATCH] Fixed issue where a role generated on the fly could not be used as a trait --- Changes | 6 ++++ lib/Moose/Util.pm | 4 +-- t/bugs/find_custom_trait_rt_92089.t | 46 +++++++++++++++++++++++++++++ t/exceptions/util.t | 2 +- 4 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 t/bugs/find_custom_trait_rt_92089.t diff --git a/Changes b/Changes index db6d26d8d..78c01d0c7 100644 --- a/Changes +++ b/Changes @@ -3,6 +3,12 @@ for, noteworthy changes. {{$NEXT}} + [BUG FIXES} + + - A role generated on the fly (as opposed to one in a file on disk) could + not be used as a trait. Fixed by Aaron Cohen. (RT#92089, PR#47). + + 2.1805 2016-08-19 [BUG FIXES] diff --git a/lib/Moose/Util.pm b/lib/Moose/Util.pm index 95ab8df11..3d5278c27 100644 --- a/lib/Moose/Util.pm +++ b/lib/Moose/Util.pm @@ -4,7 +4,7 @@ our $VERSION = '2.1806'; use strict; use warnings; -use Module::Runtime 0.014 'use_package_optimistically', 'use_module', 'module_notional_filename'; +use Module::Runtime 0.014 'use_package_optimistically', 'module_notional_filename'; use Data::OptList; use Params::Util qw( _STRING ); use Sub::Exporter; @@ -144,7 +144,7 @@ sub _apply_all_roles { $meta = $role->[0]; } else { - &use_module($role->[0], $role->[1] && $role->[1]{-version} ? $role->[1]{-version} : ()); + _load_user_class( $role->[0] , $role->[1] ); $meta = find_meta( $role->[0] ); } diff --git a/t/bugs/find_custom_trait_rt_92089.t b/t/bugs/find_custom_trait_rt_92089.t new file mode 100644 index 000000000..dde5a1605 --- /dev/null +++ b/t/bugs/find_custom_trait_rt_92089.t @@ -0,0 +1,46 @@ +use strict; +use warnings; + +use Test::More; +use Test::Fatal; + +{ + package Custom::Trait; + use Moose::Role; + + my $alias = 'Trait1'; + my $new_role_name = __PACKAGE__ . "::$alias"; + Moose::Meta::Role->initialize($new_role_name); + Moose::Exporter->setup_import_methods( + exporting_package => $new_role_name ); + Moose::Util::meta_attribute_alias( $alias, $new_role_name ); +} + +is( + exception { + package Foo; + use Moose; + use Custom::Trait; + + has field1 => ( is => 'rw', traits => [qw{Trait1}] ); + + Foo->new; + }, + undef, + 'Trait that is not an on-disk role works' +); + +like( + exception { + package Bar; + use Moose; + use Custom::Trait; + + has field1 => ( is => 'rw', traits => [qw{UndeclaredTrait}] ); + Bar->new; + }, + qr/\QCan't locate Moose::Meta::Attribute::Custom::Trait::UndeclaredTrait or UndeclaredTrait/, + 'Traits with no alias or package cause an exception' +); + +done_testing(); diff --git a/t/exceptions/util.t b/t/exceptions/util.t index 551e77309..e9b87ccc3 100644 --- a/t/exceptions/util.t +++ b/t/exceptions/util.t @@ -97,7 +97,7 @@ use Moose::Util qw/apply_all_roles add_method_modifier/; like( $exception, - qr!Can't locate Not/A/Real/Package\.pm in \@INC!, + qr!You can only consume roles, Not::A::Real::Package is not a Moose role!, "You can't consume a class which doesn't exist"); $exception = exception {