From: Jess Robinson Date: Sun, 14 Oct 2007 17:13:09 +0000 (+0000) Subject: Add tests to not load files that are not valid/sane class names (from theorbtwo) X-Git-Tag: 5.7099_04~128 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=59ede84ed03cb297507bbb89058479040969c6f8 Add tests to not load files that are not valid/sane class names (from theorbtwo) --- diff --git a/lib/Catalyst/Utils.pm b/lib/Catalyst/Utils.pm index 62b8446..fd23d95 100644 --- a/lib/Catalyst/Utils.pm +++ b/lib/Catalyst/Utils.pm @@ -251,6 +251,12 @@ sub ensure_class_loaded { croak "Malformed class Name $class" if $class =~ m/(?:\b\:\b|\:{3,})/; + croak "Malformed class Name $class" + if $class =~ m/[^\w:]/; + + croak "ensure_class_loaded should be given a classname, not a filename ($class)" + if $class =~ m/\.pm$/; + return if !$opts->{ ignore_loaded } && Class::Inspector->loaded( $class ); # if a symbol entry exists we don't load again @@ -258,7 +264,7 @@ sub ensure_class_loaded { my $error; { local $@; - eval "require $class"; + eval "require $class;"; $error = $@; } diff --git a/t/unit_utils_load_class.t b/t/unit_utils_load_class.t index cddc400..06924ec 100644 --- a/t/unit_utils_load_class.t +++ b/t/unit_utils_load_class.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 12; +use Test::More tests => 14; use lib "t/lib"; @@ -49,3 +49,11 @@ undef $@; eval { Catalyst::Utils::ensure_class_loaded("This::Module::Is::Not::In::Inc::But::Does::Exist") }; ok( !$@, "no error when loading non existent .pm that *does* have a symbol table entry" ); +undef $@; +eval { Catalyst::Utils::ensure_class_loaded('Silly::File::.#Name') }; +like($@, qr/Malformed class Name/, 'errored when attempting to load a file beginning with a .'); + +undef $@; +eval { Catalyst::Utils::ensure_class_loaded('Silly::File::Name.pm') }; +like($@, qr/Malformed class Name/, 'errored sanely when given a classname ending in .pm'); +