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