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