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