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