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