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