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