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