Commit | Line | Data |
71130750 |
1 | package dbixcsl_dumper_tests; |
2 | |
3 | use strict; |
f8c2ca5e |
4 | use warnings; |
71130750 |
5 | use Test::More; |
6 | use File::Path; |
7 | use IPC::Open3; |
50ced3c1 |
8 | use IO::Handle; |
ecf22f0a |
9 | use List::Util 'any'; |
71130750 |
10 | use DBIx::Class::Schema::Loader::Utils 'dumper_squashed'; |
11 | use DBIx::Class::Schema::Loader (); |
23fd9773 |
12 | use Class::Unload (); |
8fc55df0 |
13 | use namespace::clean; |
71130750 |
14 | |
8fc55df0 |
15 | use dbixcsl_test_dir '$tdir'; |
c213fd3d |
16 | |
17 | my $DUMP_PATH = "$tdir/dump"; |
8fc55df0 |
18 | |
71130750 |
19 | sub cleanup { |
20 | rmtree($DUMP_PATH, 1, 1); |
21 | } |
22 | |
b564fc4b |
23 | sub class_file { |
24 | my ($self, $class) = @_; |
25 | |
71130750 |
26 | $class =~ s{::}{/}g; |
27 | $class = $DUMP_PATH . '/' . $class . '.pm'; |
b564fc4b |
28 | |
29 | return $class; |
30 | } |
31 | |
32 | sub append_to_class { |
33 | my ($self, $class, $string) = @_; |
34 | |
35 | $class = $self->class_file($class); |
36 | |
71130750 |
37 | open(my $appendfh, '>>', $class) or die "Failed to open '$class' for append: $!"; |
b564fc4b |
38 | |
71130750 |
39 | print $appendfh $string; |
b564fc4b |
40 | |
71130750 |
41 | close($appendfh); |
42 | } |
43 | |
44 | sub dump_test { |
45 | my ($self, %tdata) = @_; |
46 | |
47 | |
48 | $tdata{options}{dump_directory} = $DUMP_PATH; |
49 | $tdata{options}{use_namespaces} ||= 0; |
50 | |
8fc55df0 |
51 | SKIP: for my $dumper (\&_dump_directly, \&_dump_dbicdump) { |
f21f7e97 |
52 | skip 'skipping dbicdump tests on Win32', 1, |
53 | if $dumper == \&_dump_dbicdump && $^O eq 'MSWin32'; |
8fc55df0 |
54 | |
71130750 |
55 | _test_dumps(\%tdata, $dumper->(%tdata)); |
56 | } |
57 | } |
58 | |
59 | |
60 | sub _dump_directly { |
61 | my %tdata = @_; |
62 | |
63 | my $schema_class = $tdata{classname}; |
64 | |
65 | no strict 'refs'; |
66 | @{$schema_class . '::ISA'} = ('DBIx::Class::Schema::Loader'); |
900195eb |
67 | $schema_class->loader_options( |
494e0205 |
68 | quiet => 1, |
69 | %{$tdata{options}}, |
900195eb |
70 | ); |
71130750 |
71 | |
72 | my @warns; |
73 | eval { |
74 | local $SIG{__WARN__} = sub { push(@warns, @_) }; |
667f1a0b |
75 | $schema_class->connect(_get_connect_info(\%tdata)); |
71130750 |
76 | }; |
77 | my $err = $@; |
74f213a5 |
78 | |
f56e3f73 |
79 | my $classes = !$err && $schema_class->loader->generated_classes; |
80 | |
23fd9773 |
81 | Class::Unload->unload($schema_class); |
71130750 |
82 | |
83 | _check_error($err, $tdata{error}); |
84 | |
f56e3f73 |
85 | return \@warns, $classes; |
71130750 |
86 | } |
87 | |
88 | sub _dump_dbicdump { |
89 | my %tdata = @_; |
90 | |
91 | # use $^X so we execute ./script/dbicdump with the same perl binary that the tests were executed with |
74f213a5 |
92 | my @cmd = ($^X, qw(script/dbicdump)); |
71130750 |
93 | |
900195eb |
94 | $tdata{options}{quiet} = 1 unless exists $tdata{options}{quiet}; |
95 | |
71130750 |
96 | while (my ($opt, $val) = each(%{ $tdata{options} })) { |
97 | $val = dumper_squashed $val if ref $val; |
f21f7e97 |
98 | |
99 | my $param = "$opt=$val"; |
100 | |
101 | if ($^O eq 'MSWin32') { |
102 | $param = q{"} . $param . q{"}; # that's not nearly enough... |
103 | } |
104 | |
105 | push @cmd, '-o', $param; |
71130750 |
106 | } |
107 | |
667f1a0b |
108 | my @connect_info = _get_connect_info(\%tdata); |
109 | |
110 | for my $info (@connect_info) { |
111 | $info = dumper_squashed $info if ref $info; |
112 | } |
113 | |
114 | push @cmd, $tdata{classname}, @connect_info; |
71130750 |
115 | |
116 | # make sure our current @INC gets used by dbicdump |
117 | use Config; |
118 | local $ENV{PERL5LIB} = join $Config{path_sep}, @INC, ($ENV{PERL5LIB} || ''); |
119 | |
50ced3c1 |
120 | my $std = { map { $_ => IO::Handle->new } (qw/in out err/) }; |
121 | my $pid = open3(@{$std}{qw/in out err/}, @cmd); |
71130750 |
122 | |
71130750 |
123 | waitpid($pid, 0); |
124 | |
50ced3c1 |
125 | my @stdout = $std->{out}->getlines; |
126 | ok (!scalar @stdout, 'Silence on STDOUT'); |
127 | |
128 | my @warnings = $std->{err}->getlines; |
71130750 |
129 | if ($? >> 8 != 0) { |
50ced3c1 |
130 | my $exception = pop @warnings; |
131 | _check_error($exception, $tdata{error}); |
71130750 |
132 | } |
71130750 |
133 | |
f56e3f73 |
134 | return \@warnings; |
71130750 |
135 | } |
136 | |
667f1a0b |
137 | sub _get_connect_info { |
71130750 |
138 | my $opts = shift; |
139 | |
140 | my $test_db_class = $opts->{test_db_class} || 'make_dbictest_db'; |
141 | |
142 | eval "require $test_db_class;"; |
143 | die $@ if $@; |
144 | |
145 | my $dsn = do { |
146 | no strict 'refs'; |
147 | ${$test_db_class . '::dsn'}; |
148 | }; |
149 | |
667f1a0b |
150 | return ($dsn, @{ $opts->{extra_connect_info} || [] }); |
71130750 |
151 | } |
152 | |
153 | sub _check_error { |
154 | my ($got, $expected) = @_; |
155 | |
156 | return unless $got; |
157 | |
158 | if (not $expected) { |
159 | fail "Unexpected error in " . ((caller(1))[3]) . ": $got"; |
160 | return; |
161 | } |
162 | |
163 | if (ref $expected eq 'Regexp') { |
164 | like $got, $expected, 'error matches expected pattern'; |
165 | return; |
166 | } |
167 | |
168 | is $got, $expected, 'error matches'; |
169 | } |
170 | |
71130750 |
171 | sub _test_dumps { |
f56e3f73 |
172 | my ($tdata, $warns, $classes) = @_; |
71130750 |
173 | |
174 | my %tdata = %{$tdata}; |
175 | |
176 | my $schema_class = $tdata{classname}; |
177 | my $check_warns = $tdata{warnings}; |
900195eb |
178 | |
f56e3f73 |
179 | is(@$warns, @$check_warns, "$schema_class warning count") |
494e0205 |
180 | or diag @$warns; |
71130750 |
181 | |
182 | for(my $i = 0; $i <= $#$check_warns; $i++) { |
f56e3f73 |
183 | like(($warns->[$i] || ''), $check_warns->[$i], "$schema_class warning $i"); |
184 | } |
185 | |
186 | if ($classes && (my $results = $tdata{generated_results})) { |
187 | my $ns = $tdata{options}{use_namespaces} ? ("::".($tdata{result_namespace} || "Result")) : ""; |
188 | is_deeply( |
189 | [ sort grep { $_ ne $schema_class } @$classes ], |
190 | [ sort map { "${schema_class}${ns}::$_" } @$results ], |
191 | "$schema_class generated_classes set correctly", |
192 | ); |
71130750 |
193 | } |
194 | |
195 | my $file_regexes = $tdata{regexes}; |
196 | my $file_neg_regexes = $tdata{neg_regexes} || {}; |
197 | my $schema_regexes = delete $file_regexes->{schema}; |
198 | |
199 | my $schema_path = $DUMP_PATH . '/' . $schema_class; |
200 | $schema_path =~ s{::}{/}g; |
201 | |
202 | _dump_file_like($schema_path . '.pm', @$schema_regexes) if $schema_regexes; |
203 | |
204 | foreach my $src (keys %$file_regexes) { |
205 | my $src_file = $schema_path . '/' . $src . '.pm'; |
206 | _dump_file_like($src_file, @{$file_regexes->{$src}}); |
207 | } |
208 | foreach my $src (keys %$file_neg_regexes) { |
209 | my $src_file = $schema_path . '/' . $src . '.pm'; |
210 | _dump_file_not_like($src_file, @{$file_neg_regexes->{$src}}); |
211 | } |
212 | } |
213 | |
160b07c5 |
214 | sub _slurp { |
71130750 |
215 | my $path = shift; |
160b07c5 |
216 | open(my $dumpfh, '<:raw', $path) or die "Failed to open '$path': $!"; |
71130750 |
217 | my $contents = do { local $/; <$dumpfh>; }; |
218 | close($dumpfh); |
160b07c5 |
219 | return ($path, $contents); |
220 | } |
221 | |
222 | sub _dump_file_like { |
223 | my ($path, $contents) = _slurp shift; |
71130750 |
224 | like($contents, $_, "$path matches $_") for @_; |
225 | } |
226 | |
227 | sub _dump_file_not_like { |
160b07c5 |
228 | my ($path, $contents) = _slurp shift; |
71130750 |
229 | unlike($contents, $_, "$path does not match $_") for @_; |
230 | } |
231 | |
232 | END { |
233 | __PACKAGE__->cleanup unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP} |
234 | } |