X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F23dumpmore.t;h=c2074f832993d4e3ea0892ea87dbce19abbbcd4f;hb=45a380dc2d68e828de4011b99f0c3739db5b0707;hp=beb1cdd01ad4dd25fa56389428861ffd03e7627b;hpb=a4187fdf58a28e2ec8a4905b2164c3a6ec297fa9;p=dbsrgits%2FDBIx-Class-Schema-Loader.git diff --git a/t/23dumpmore.t b/t/23dumpmore.t index beb1cdd..c2074f8 100644 --- a/t/23dumpmore.t +++ b/t/23dumpmore.t @@ -1,41 +1,126 @@ use strict; use Test::More; -use lib qw(t/lib); use File::Path; -use make_dbictest_db; -require DBIx::Class::Schema::Loader; - -plan tests => 40; - -plan skip_all => "ActiveState perl produces additional warnings, and this test uses unix paths" - if ($^O eq 'MSWin32'); +use IPC::Open3; +use Data::Dumper::Concise; +use DBIx::Class::Schema::Loader (); +use File::Temp 'tempfile'; +use lib qw(t/lib); my $DUMP_PATH = './t/_dump'; -sub do_dump_test { +my $TEST_DB_CLASS = 'make_dbictest_db'; + +sub dump_directly { my %tdata = @_; my $schema_class = $tdata{classname}; no strict 'refs'; @{$schema_class . '::ISA'} = ('DBIx::Class::Schema::Loader'); - $schema_class->loader_options(dump_directory => $DUMP_PATH, %{$tdata{options}}); + $schema_class->loader_options(%{$tdata{options}}); my @warns; eval { local $SIG{__WARN__} = sub { push(@warns, @_) }; - $schema_class->connect($make_dbictest_db::dsn); + $schema_class->connect(get_dsn(\%tdata)); }; my $err = $@; $schema_class->storage->disconnect if !$err && $schema_class->storage; undef *{$schema_class}; - is($err, $tdata{error}); + check_error($err, $tdata{error}); + + return @warns; +} + +sub dump_dbicdump { + my %tdata = @_; + + # use $^X so we execute ./script/dbicdump with the same perl binary that the tests were executed with + my @cmd = ($^X, qw(./script/dbicdump)); + + while (my ($opt, $val) = each(%{ $tdata{options} })) { + $val = Dumper($val) if ref $val; + push @cmd, '-o', "$opt=$val"; + } + + push @cmd, $tdata{classname}, get_dsn(\%tdata); + + # make sure our current @INC gets used by dbicdump + use Config; + local $ENV{PERL5LIB} = join $Config{path_sep}, @INC, ($ENV{PERL5LIB} || ''); + + my ($in, $out, $err); + my $pid = open3($in, $out, $err, @cmd); + + my @out = <$out>; + waitpid($pid, 0); + + my ($error, @warns); + + if ($? >> 8 != 0) { + $error = $out[0]; + check_error($error, $tdata{error}); + } + else { + @warns = @out; + } + + return @warns; +} + +sub get_dsn { + my $opts = shift; + + my $test_db_class = $opts->{test_db_class} || $TEST_DB_CLASS; + + eval "require $test_db_class;"; + die $@ if $@; + + my $dsn = do { + no strict 'refs'; + ${$test_db_class . '::dsn'}; + }; + + return $dsn; +} + +sub check_error { + my ($got, $expected) = @_; + + return unless $got && $expected; + if (ref $expected eq 'Regexp') { + like $got, $expected, 'error matches expected pattern'; + return; + } + + is $got, $expected, 'error matches'; +} + +sub do_dump_test { + my %tdata = @_; + + $tdata{options}{dump_directory} = $DUMP_PATH; + $tdata{options}{use_namespaces} ||= 0; + + for my $dumper (\&dump_directly, \&dump_dbicdump) { + test_dumps(\%tdata, $dumper->(%tdata)); + } +} + +sub test_dumps { + my ($tdata, @warns) = @_; + + my %tdata = %{$tdata}; + + my $schema_class = $tdata{classname}; my $check_warns = $tdata{warnings}; - is(@warns, @$check_warns); + is(@warns, @$check_warns, "$schema_class warning count"); + for(my $i = 0; $i <= $#$check_warns; $i++) { - like($warns[$i], $check_warns->[$i]); + like($warns[$i], $check_warns->[$i], "$schema_class warning $i"); } my $file_regexes = $tdata{regexes}; @@ -44,7 +129,9 @@ sub do_dump_test { my $schema_path = $DUMP_PATH . '/' . $schema_class; $schema_path =~ s{::}{/}g; - dump_file_like($schema_path . '.pm', @$schema_regexes); + + dump_file_like($schema_path . '.pm', @$schema_regexes) if $schema_regexes; + foreach my $src (keys %$file_regexes) { my $src_file = $schema_path . '/' . $src . '.pm'; dump_file_like($src_file, @{$file_regexes->{$src}}); @@ -60,7 +147,8 @@ sub dump_file_like { open(my $dumpfh, '<', $path) or die "Failed to open '$path': $!"; my $contents = do { local $/; <$dumpfh>; }; close($dumpfh); - like($contents, $_) for @_; + my $num = 1; + like($contents, $_, "like $path " . $num++) for @_; } sub dump_file_not_like { @@ -68,7 +156,8 @@ sub dump_file_not_like { open(my $dumpfh, '<', $path) or die "Failed to open '$path': $!"; my $contents = do { local $/; <$dumpfh>; }; close($dumpfh); - unlike($contents, $_) for @_; + my $num = 1; + unlike($contents, $_, "unlike $path ". $num++) for @_; } sub append_to_class { @@ -82,10 +171,82 @@ sub append_to_class { rmtree($DUMP_PATH, 1, 1); +# test loading external content +do_dump_test( + classname => 'DBICTest::Schema::13', + warnings => [ + qr/Dumping manual schema for DBICTest::Schema::13 to directory /, + qr/Schema dump completed/, + ], + regexes => { + Foo => [ +qr/package DBICTest::Schema::13::Foo;\nour \$skip_me = "bad mojo";\n1;/ + ], + }, +); + +# test skipping external content +do_dump_test( + classname => 'DBICTest::Schema::14', + options => { skip_load_external => 1 }, + warnings => [ + qr/Dumping manual schema for DBICTest::Schema::14 to directory /, + qr/Schema dump completed/, + ], + neg_regexes => { + Foo => [ +qr/package DBICTest::Schema::14::Foo;\nour \$skip_me = "bad mojo";\n1;/ + ], + }, +); + +rmtree($DUMP_PATH, 1, 1); + +# test config_file + +my ($fh, $config_file) = tempfile; + +print $fh <<'EOF'; +{ skip_relationships => 1 } +EOF +close $fh; + +do_dump_test( + classname => 'DBICTest::Schema::14', + options => { config_file => $config_file }, + warnings => [ + qr/Dumping manual schema for DBICTest::Schema::14 to directory /, + qr/Schema dump completed/, + ], + neg_regexes => { + Foo => [ + qr/has_many/, + ], + }, +); + +unlink $config_file; + +rmtree($DUMP_PATH, 1, 1); + +do_dump_test( + classname => 'DBICTest::Schema::14', + test_db_class => 'make_dbictest_db_clashing_monikers', + error => qr/tables 'bar', 'bars' reduced to the same source moniker 'Bar'/, +); + +rmtree($DUMP_PATH, 1, 1); + +# test out the POD + do_dump_test( classname => 'DBICTest::DumpMore::1', - options => { }, - error => '', + options => { + custom_column_info => sub { + my ($table, $col, $info) = @_; + return +{ extra => { is_footext => 1 } } if $col eq 'footext'; + } + }, warnings => [ qr/Dumping manual schema for DBICTest::DumpMore::1 to directory /, qr/Schema dump completed/, @@ -96,14 +257,26 @@ do_dump_test( qr/->load_classes/, ], Foo => [ - qr/package DBICTest::DumpMore::1::Foo;/, - qr/->set_primary_key/, - qr/1;\n$/, +qr/package DBICTest::DumpMore::1::Foo;/, +qr/=head1 NAME\n\nDBICTest::DumpMore::1::Foo\n\n=cut\n\n/, +qr/=head1 ACCESSORS\n\n/, +qr/=head2 fooid\n\n data_type: 'integer'\n is_nullable: 1\n\n/, +qr/=head2 footext\n\n data_type: 'text'\n default_value: 'footext'\n extra: {is_footext => 1}\n is_nullable: 1\n\n/, +qr/->set_primary_key/, +qr/=head1 RELATIONS\n\n/, +qr/=head2 bars\n\nType: has_many\n\nRelated object: L\n\n=cut\n\n/, +qr/1;\n$/, ], Bar => [ - qr/package DBICTest::DumpMore::1::Bar;/, - qr/->set_primary_key/, - qr/1;\n$/, +qr/package DBICTest::DumpMore::1::Bar;/, +qr/=head1 NAME\n\nDBICTest::DumpMore::1::Bar\n\n=cut\n\n/, +qr/=head1 ACCESSORS\n\n/, +qr/=head2 barid\n\n data_type: 'integer'\n is_nullable: 1\n\n/, +qr/=head2 fooref\n\n data_type: 'integer'\n is_foreign_key: 1\n is_nullable: 1\n\n/, +qr/->set_primary_key/, +qr/=head1 RELATIONS\n\n/, +qr/=head2 fooref\n\nType: belongs_to\n\nRelated object: L\n\n=cut\n\n/, +qr/1;\n$/, ], }, ); @@ -112,8 +285,6 @@ append_to_class('DBICTest::DumpMore::1::Foo',q{# XXX This is my custom content X do_dump_test( classname => 'DBICTest::DumpMore::1', - options => { }, - error => '', warnings => [ qr/Dumping manual schema for DBICTest::DumpMore::1 to directory /, qr/Schema dump completed/, @@ -138,8 +309,7 @@ do_dump_test( do_dump_test( classname => 'DBICTest::DumpMore::1', - options => { dump_overwrite => 1 }, - error => '', + options => { really_erase_my_files => 1 }, warnings => [ qr/Dumping manual schema for DBICTest::DumpMore::1 to directory /, qr/Deleting existing file /, @@ -170,4 +340,133 @@ do_dump_test( }, ); -END { rmtree($DUMP_PATH, 1, 1); } +rmtree($DUMP_PATH, 1, 1); + +do_dump_test( + classname => 'DBICTest::DumpMore::1', + options => { use_namespaces => 1, generate_pod => 0 }, + warnings => [ + qr/Dumping manual schema for DBICTest::DumpMore::1 to directory /, + qr/Schema dump completed/, + ], + neg_regexes => { + 'Result/Foo' => [ + qr/^=/m, + ], + }, +); + +rmtree($DUMP_PATH, 1, 1); + +do_dump_test( + classname => 'DBICTest::DumpMore::1', + options => { use_namespaces => 1 }, + warnings => [ + qr/Dumping manual schema for DBICTest::DumpMore::1 to directory /, + qr/Schema dump completed/, + ], + regexes => { + schema => [ + qr/package DBICTest::DumpMore::1;/, + qr/->load_namespaces/, + ], + 'Result/Foo' => [ + qr/package DBICTest::DumpMore::1::Result::Foo;/, + qr/->set_primary_key/, + qr/1;\n$/, + ], + 'Result/Bar' => [ + qr/package DBICTest::DumpMore::1::Result::Bar;/, + qr/->set_primary_key/, + qr/1;\n$/, + ], + }, +); + +rmtree($DUMP_PATH, 1, 1); + +do_dump_test( + classname => 'DBICTest::DumpMore::1', + options => { use_namespaces => 1, + result_namespace => 'Res', + resultset_namespace => 'RSet', + default_resultset_class => 'RSetBase', + }, + warnings => [ + qr/Dumping manual schema for DBICTest::DumpMore::1 to directory /, + qr/Schema dump completed/, + ], + regexes => { + schema => [ + qr/package DBICTest::DumpMore::1;/, + qr/->load_namespaces/, + qr/result_namespace => 'Res'/, + qr/resultset_namespace => 'RSet'/, + qr/default_resultset_class => 'RSetBase'/, + ], + 'Res/Foo' => [ + qr/package DBICTest::DumpMore::1::Res::Foo;/, + qr/->set_primary_key/, + qr/1;\n$/, + ], + 'Res/Bar' => [ + qr/package DBICTest::DumpMore::1::Res::Bar;/, + qr/->set_primary_key/, + qr/1;\n$/, + ], + }, +); + +rmtree($DUMP_PATH, 1, 1); + +do_dump_test( + classname => 'DBICTest::DumpMore::1', + options => { use_namespaces => 1, + result_namespace => '+DBICTest::DumpMore::1::Res', + resultset_namespace => 'RSet', + default_resultset_class => 'RSetBase', + result_base_class => 'My::ResultBaseClass', + schema_base_class => 'My::SchemaBaseClass', + }, + warnings => [ + qr/Dumping manual schema for DBICTest::DumpMore::1 to directory /, + qr/Schema dump completed/, + ], + regexes => { + schema => [ + qr/package DBICTest::DumpMore::1;/, + qr/->load_namespaces/, + qr/result_namespace => '\+DBICTest::DumpMore::1::Res'/, + qr/resultset_namespace => 'RSet'/, + qr/default_resultset_class => 'RSetBase'/, + qr/use base 'My::SchemaBaseClass'/, + ], + 'Res/Foo' => [ + qr/package DBICTest::DumpMore::1::Res::Foo;/, + qr/use base 'My::ResultBaseClass'/, + qr/->set_primary_key/, + qr/1;\n$/, + ], + 'Res/Bar' => [ + qr/package DBICTest::DumpMore::1::Res::Bar;/, + qr/use base 'My::ResultBaseClass'/, + qr/->set_primary_key/, + qr/1;\n$/, + ], + }, +); + +rmtree($DUMP_PATH, 1, 1); + +do_dump_test( + classname => 'DBICTest::DumpMore::1', + options => { + use_namespaces => 1, + result_base_class => 'My::MissingResultBaseClass', + }, + error => qr/My::MissingResultBaseClass.*is not installed/, +); + +done_testing; + +END { rmtree($DUMP_PATH, 1, 1) unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP} }