1dd6c9894399163c80237271d91971dacd1c55dd
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 40overwrite_modifications.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use Test::Exception;
5 use Test::Warn;
6 use lib qw(t/lib);
7 use make_dbictest_db;
8
9 use File::Copy;
10 use File::Spec;
11 use File::Temp qw/ tempdir tempfile /;
12
13 use DBIx::Class::Schema::Loader;
14 use DBIx::Class::Schema::Loader::Utils qw/ slurp_file /;
15
16 my $tempdir = tempdir( CLEANUP => 1 );
17 my $foopm = File::Spec->catfile( $tempdir,
18     qw| DBICTest Schema Overwrite_modifications Result Foo.pm |);
19 dump_schema();
20
21 # check that we dumped
22 ok( -f $foopm, 'looks like it dumped' );
23
24 # now modify one of the files
25 {
26     open my $in, '<', $foopm or die "$! reading $foopm";
27     my ($tfh,$temp) = tempfile( UNLINK => 1);
28     while(<$in>) {
29         s/"bars"/"somethingelse"/;
30         print $tfh $_;
31     }
32     close $tfh;
33     copy( $temp, $foopm );
34 }
35
36 # and dump again without overwrites
37 throws_ok {
38     dump_schema();
39 } qr/mismatch/, 'throws error dumping without overwrite_modifications';
40
41 # and then dump with overwrite
42 lives_ok {
43     dump_schema( overwrite_modifications => 1 );
44 } 'does not throw when dumping with overwrite_modifications';
45
46
47 unlike slurp_file $foopm, qr/"somethingelse"/, "Modifications actually overwritten";
48
49 sub dump_schema {
50
51     # need to poke _loader_invoked in order to be able to rerun the
52     # loader multiple times.
53     DBICTest::Schema::Overwrite_modifications->_loader_invoked(0)
54         if @DBICTest::Schema::Overwrite_modifications::ISA;
55
56     my $args = \@_;
57
58     warnings_exist {
59         DBIx::Class::Schema::Loader::make_schema_at( 'DBICTest::Schema::Overwrite_modifications',
60             { dump_directory => $tempdir, @$args },
61             [ $make_dbictest_db::dsn ],
62         );
63     } [qr/^Dumping manual schema/, qr/^Schema dump completed/ ];
64 }
65
66 done_testing();