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