From: Rafael Kitover Date: Sun, 30 Oct 2011 00:40:04 +0000 (-0400) Subject: stop editing custom content from renamed files X-Git-Tag: 0.07011~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b564fc4be40e41844d607e7121bd55f895e19270;p=dbsrgits%2FDBIx-Class-Schema-Loader.git stop editing custom content from renamed files Remove an unnecessary bit of cleverness that was removing the default custom content comment (# You can replace...) from files that are being renamed for an upgrade (either to singularization or use_namespaces.) This fixes RT#70507. --- diff --git a/Changes b/Changes index 8fc72d0..b615cb6 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,7 @@ Revision history for Perl extension DBIx::Class::Schema::Loader + - do not delete default custom content comment and ending 1; from custom + content in files that are being renamed (RT#70507) - use MooseX::MarkAsMethods instead of namespace::autoclean for the use_moose option, this protects operator overloads, only_autoclean option added for the old behavior diff --git a/lib/DBIx/Class/Schema/Loader/Base.pm b/lib/DBIx/Class/Schema/Loader/Base.pm index c94c09d..75ebd06 100644 --- a/lib/DBIx/Class/Schema/Loader/Base.pm +++ b/lib/DBIx/Class/Schema/Loader/Base.pm @@ -1803,17 +1803,13 @@ sub _write_classfile { my $custom_content = $old_custom || ''; - # prepend extra custom content from a *renamed* class (singularization effect) + # Use custom content from a renamed class, the class names in it are + # rewritten below. if (my $renamed_class = $self->_upgrading_classes->{$class}) { my $old_filename = $self->_get_dump_filename($renamed_class); if (-f $old_filename) { - my $extra_custom = ($self->_parse_generated_file ($old_filename))[4]; - - $extra_custom =~ s/\n\n# You can replace.*\n1;\n//; - - $custom_content = join ("\n", '', $extra_custom, $custom_content) - if $extra_custom; + $custom_content = ($self->_parse_generated_file ($old_filename))[4]; unlink $old_filename; } diff --git a/lib/DBIx/Class/Schema/Loader/Utils.pm b/lib/DBIx/Class/Schema/Loader/Utils.pm index e5d7146..2c65bb1 100644 --- a/lib/DBIx/Class/Schema/Loader/Utils.pm +++ b/lib/DBIx/Class/Schema/Loader/Utils.pm @@ -9,7 +9,7 @@ use namespace::clean; use Exporter 'import'; use Data::Dumper (); -our @EXPORT_OK = qw/split_name dumper dumper_squashed eval_package_without_redefine_warnings class_path no_warnings warnings_exist warnings_exist_silent slurp_file/; +our @EXPORT_OK = qw/split_name dumper dumper_squashed eval_package_without_redefine_warnings class_path no_warnings warnings_exist warnings_exist_silent slurp_file write_file/; use constant BY_CASE_TRANSITION_V7 => qr/(?<=[[:lower:]\d])[\W_]*(?=[[:upper:]])|[\W_]+/; @@ -152,5 +152,11 @@ sub slurp_file($) { return $data; } +sub write_file($$) { + open my $fh, '>:encoding(UTF-8)', shift; + print $fh shift; + close $fh; +} + 1; # vim:et sts=4 sw=4 tw=0: diff --git a/t/23dumpmore.t b/t/23dumpmore.t index 032aa66..0b17dea 100644 --- a/t/23dumpmore.t +++ b/t/23dumpmore.t @@ -1,9 +1,9 @@ -use warnings; use strict; - -use File::Temp (); +use warnings; use Test::More; - +use DBIx::Class::Schema::Loader::Utils qw/slurp_file write_file/; +use namespace::clean; +use File::Temp (); use lib qw(t/lib); use dbixcsl_dumper_tests; my $t = 'dbixcsl_dumper_tests'; @@ -433,5 +433,35 @@ $t->dump_test( ], ); +# test fix for RT#70507 (end comment and 1; gets lost if left with actual +# custom content) + +$t->dump_test( + classname => 'DBICTest::DumpMore::Upgrade', + options => { + use_namespaces => 0, + }, +); + +my $file = $t->class_file('DBICTest::DumpMore::Upgrade::Foo'); + +my $code = slurp_file $file; + +$code =~ s/(?=# You can replace)/sub custom_method { 'custom_method works' }\n0;\n\n/; + +write_file $file, $code; + +$t->dump_test( + classname => 'DBICTest::DumpMore::Upgrade', + options => { + use_namespaces => 1, + }, + regexes => { + 'Result/Foo' => [ + qr/sub custom_method { 'custom_method works' }\n0;\n\n# You can replace.*\n1;\n\z/, + ], + }, +); + done_testing; # vim:et sts=4 sw=4 tw=0: diff --git a/t/lib/dbixcsl_dumper_tests.pm b/t/lib/dbixcsl_dumper_tests.pm index 32bf28c..4da9564 100644 --- a/t/lib/dbixcsl_dumper_tests.pm +++ b/t/lib/dbixcsl_dumper_tests.pm @@ -18,12 +18,24 @@ sub cleanup { rmtree($DUMP_PATH, 1, 1); } -sub append_to_class { - my ($self, $class, $string) = @_; +sub class_file { + my ($self, $class) = @_; + $class =~ s{::}{/}g; $class = $DUMP_PATH . '/' . $class . '.pm'; + + return $class; +} + +sub append_to_class { + my ($self, $class, $string) = @_; + + $class = $self->class_file($class); + open(my $appendfh, '>>', $class) or die "Failed to open '$class' for append: $!"; + print $appendfh $string; + close($appendfh); }