support extra connect_info options like quote_char for dbicdump
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / lib / dbixcsl_dumper_tests.pm
1 package dbixcsl_dumper_tests;
2
3 use strict;
4 use Test::More;
5 use File::Path;
6 use IPC::Open3;
7 use IO::Handle;
8 use DBIx::Class::Schema::Loader::Utils 'dumper_squashed';
9 use DBIx::Class::Schema::Loader ();
10
11 use dbixcsl_test_dir qw/$tdir/;
12
13 my $DUMP_PATH = "$tdir/dump";
14 sub cleanup {
15     rmtree($DUMP_PATH, 1, 1);
16 }
17
18 sub append_to_class {
19     my ($self, $class, $string) = @_;
20     $class =~ s{::}{/}g;
21     $class = $DUMP_PATH . '/' . $class . '.pm';
22     open(my $appendfh, '>>', $class) or die "Failed to open '$class' for append: $!";
23     print $appendfh $string;
24     close($appendfh);
25 }
26
27 sub dump_test {
28     my ($self, %tdata) = @_;
29
30
31     $tdata{options}{dump_directory} = $DUMP_PATH;
32     $tdata{options}{use_namespaces} ||= 0;
33
34     for my $dumper (\&_dump_directly, \&_dump_dbicdump) {
35         _test_dumps(\%tdata, $dumper->(%tdata));
36     }
37 }
38
39
40 sub _dump_directly {
41     my %tdata = @_;
42
43     my $schema_class = $tdata{classname};
44
45     no strict 'refs';
46     @{$schema_class . '::ISA'} = ('DBIx::Class::Schema::Loader');
47     $schema_class->loader_options(%{$tdata{options}});
48
49     my @warns;
50     eval {
51         local $SIG{__WARN__} = sub { push(@warns, @_) };
52         $schema_class->connect(_get_connect_info(\%tdata));
53     };
54     my $err = $@;
55
56     $schema_class->storage->disconnect if !$err && $schema_class->storage;
57     undef *{$schema_class};
58
59     _check_error($err, $tdata{error});
60
61     return @warns;
62 }
63
64 sub _dump_dbicdump {
65     my %tdata = @_;
66
67     # use $^X so we execute ./script/dbicdump with the same perl binary that the tests were executed with
68     my @cmd = ($^X, qw(script/dbicdump));
69
70     while (my ($opt, $val) = each(%{ $tdata{options} })) {
71         $val = dumper_squashed $val if ref $val;
72         push @cmd, '-o', "$opt=$val";
73     }
74
75     my @connect_info = _get_connect_info(\%tdata);
76
77     for my $info (@connect_info) {
78         $info = dumper_squashed $info if ref $info;
79     }
80
81     push @cmd, $tdata{classname}, @connect_info;
82
83     # make sure our current @INC gets used by dbicdump
84     use Config;
85     local $ENV{PERL5LIB} = join $Config{path_sep}, @INC, ($ENV{PERL5LIB} || '');
86
87     my $std = { map { $_ => IO::Handle->new } (qw/in out err/) };
88     my $pid = open3(@{$std}{qw/in out err/}, @cmd);
89
90     waitpid($pid, 0);
91
92     my @stdout = $std->{out}->getlines;
93     ok (!scalar @stdout, 'Silence on STDOUT');
94
95     my @warnings = $std->{err}->getlines;
96     if ($? >> 8 != 0) {
97         my $exception = pop @warnings;
98         _check_error($exception, $tdata{error});
99     }
100
101     return @warnings;
102 }
103
104 sub _get_connect_info {
105     my $opts = shift;
106
107     my $test_db_class = $opts->{test_db_class} || 'make_dbictest_db';
108
109     eval "require $test_db_class;";
110     die $@ if $@;
111
112     my $dsn = do {
113         no strict 'refs';
114         ${$test_db_class . '::dsn'};
115     };
116
117     return ($dsn, @{ $opts->{extra_connect_info} || [] });
118 }
119
120 sub _check_error {
121     my ($got, $expected) = @_;
122
123     return unless $got;
124
125     if (not $expected) {
126         fail "Unexpected error in " . ((caller(1))[3]) . ": $got";
127         return;
128     }
129
130     if (ref $expected eq 'Regexp') {
131         like $got, $expected, 'error matches expected pattern';
132         return;
133     }
134
135     is $got, $expected, 'error matches';
136 }
137
138
139 sub _test_dumps {
140     my ($tdata, @warns) = @_;
141
142     my %tdata = %{$tdata};
143
144     my $schema_class = $tdata{classname};
145     my $check_warns = $tdata{warnings};
146     is(@warns, @$check_warns, "$schema_class warning count");
147
148     for(my $i = 0; $i <= $#$check_warns; $i++) {
149         like($warns[$i], $check_warns->[$i], "$schema_class warning $i");
150     }
151
152     my $file_regexes = $tdata{regexes};
153     my $file_neg_regexes = $tdata{neg_regexes} || {};
154     my $schema_regexes = delete $file_regexes->{schema};
155
156     my $schema_path = $DUMP_PATH . '/' . $schema_class;
157     $schema_path =~ s{::}{/}g;
158
159     _dump_file_like($schema_path . '.pm', @$schema_regexes) if $schema_regexes;
160
161     foreach my $src (keys %$file_regexes) {
162         my $src_file = $schema_path . '/' . $src . '.pm';
163         _dump_file_like($src_file, @{$file_regexes->{$src}});
164     }
165     foreach my $src (keys %$file_neg_regexes) {
166         my $src_file = $schema_path . '/' . $src . '.pm';
167         _dump_file_not_like($src_file, @{$file_neg_regexes->{$src}});
168     }
169 }
170
171 sub _dump_file_like {
172     my $path = shift;
173     open(my $dumpfh, '<', $path) or die "Failed to open '$path': $!";
174     my $contents = do { local $/; <$dumpfh>; };
175     close($dumpfh);
176     like($contents, $_, "$path matches $_") for @_;
177 }
178
179 sub _dump_file_not_like {
180     my $path = shift;
181     open(my $dumpfh, '<', $path) or die "Failed to open '$path': $!";
182     my $contents = do { local $/; <$dumpfh>; };
183     close($dumpfh);
184     unlike($contents, $_, "$path does not match $_") for @_;
185 }
186
187 END {
188     __PACKAGE__->cleanup unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}
189 }