1 package dbixcsl_dumper_tests;
8 use List::MoreUtils 'any';
9 use DBIx::Class::Schema::Loader::Utils 'dumper_squashed';
10 use DBIx::Class::Schema::Loader ();
13 use dbixcsl_test_dir '$tdir';
15 my $DUMP_PATH = "$tdir/dump";
18 rmtree($DUMP_PATH, 1, 1);
22 my ($self, $class, $string) = @_;
24 $class = $DUMP_PATH . '/' . $class . '.pm';
25 open(my $appendfh, '>>', $class) or die "Failed to open '$class' for append: $!";
26 print $appendfh $string;
31 my ($self, %tdata) = @_;
34 $tdata{options}{dump_directory} = $DUMP_PATH;
35 $tdata{options}{use_namespaces} ||= 0;
37 SKIP: for my $dumper (\&_dump_directly, \&_dump_dbicdump) {
38 skip 'fucking pigs broke my Win32 perl', 1,
39 if $dumper == \&_dump_dbicdump
42 && ( (any { ref $_ } values %{ $tdata{options} })
43 || any { ref $_ } _get_connect_info(\%tdata));
45 _test_dumps(\%tdata, $dumper->(%tdata));
53 my $schema_class = $tdata{classname};
56 @{$schema_class . '::ISA'} = ('DBIx::Class::Schema::Loader');
57 $schema_class->loader_options(
64 local $SIG{__WARN__} = sub { push(@warns, @_) };
65 $schema_class->connect(_get_connect_info(\%tdata));
69 $schema_class->storage->disconnect if !$err && $schema_class->storage;
70 undef *{$schema_class};
72 _check_error($err, $tdata{error});
80 # use $^X so we execute ./script/dbicdump with the same perl binary that the tests were executed with
81 my @cmd = ($^X, qw(script/dbicdump));
83 $tdata{options}{quiet} = 1 unless exists $tdata{options}{quiet};
85 while (my ($opt, $val) = each(%{ $tdata{options} })) {
86 $val = dumper_squashed $val if ref $val;
87 push @cmd, '-o', "$opt=$val";
90 my @connect_info = _get_connect_info(\%tdata);
92 for my $info (@connect_info) {
93 $info = dumper_squashed $info if ref $info;
96 push @cmd, $tdata{classname}, @connect_info;
98 # make sure our current @INC gets used by dbicdump
100 local $ENV{PERL5LIB} = join $Config{path_sep}, @INC, ($ENV{PERL5LIB} || '');
102 my $std = { map { $_ => IO::Handle->new } (qw/in out err/) };
103 my $pid = open3(@{$std}{qw/in out err/}, @cmd);
107 my @stdout = $std->{out}->getlines;
108 ok (!scalar @stdout, 'Silence on STDOUT');
110 my @warnings = $std->{err}->getlines;
112 my $exception = pop @warnings;
113 _check_error($exception, $tdata{error});
119 sub _get_connect_info {
122 my $test_db_class = $opts->{test_db_class} || 'make_dbictest_db';
124 eval "require $test_db_class;";
129 ${$test_db_class . '::dsn'};
132 return ($dsn, @{ $opts->{extra_connect_info} || [] });
136 my ($got, $expected) = @_;
141 fail "Unexpected error in " . ((caller(1))[3]) . ": $got";
145 if (ref $expected eq 'Regexp') {
146 like $got, $expected, 'error matches expected pattern';
150 is $got, $expected, 'error matches';
154 my ($tdata, @warns) = @_;
156 my %tdata = %{$tdata};
158 my $schema_class = $tdata{classname};
159 my $check_warns = $tdata{warnings};
161 is(@warns, @$check_warns, "$schema_class warning count")
164 for(my $i = 0; $i <= $#$check_warns; $i++) {
165 like(($warns[$i] || ''), $check_warns->[$i], "$schema_class warning $i");
168 my $file_regexes = $tdata{regexes};
169 my $file_neg_regexes = $tdata{neg_regexes} || {};
170 my $schema_regexes = delete $file_regexes->{schema};
172 my $schema_path = $DUMP_PATH . '/' . $schema_class;
173 $schema_path =~ s{::}{/}g;
175 _dump_file_like($schema_path . '.pm', @$schema_regexes) if $schema_regexes;
177 foreach my $src (keys %$file_regexes) {
178 my $src_file = $schema_path . '/' . $src . '.pm';
179 _dump_file_like($src_file, @{$file_regexes->{$src}});
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}});
187 sub _dump_file_like {
189 open(my $dumpfh, '<', $path) or die "Failed to open '$path': $!";
190 my $contents = do { local $/; <$dumpfh>; };
192 like($contents, $_, "$path matches $_") for @_;
195 sub _dump_file_not_like {
197 open(my $dumpfh, '<', $path) or die "Failed to open '$path': $!";
198 my $contents = do { local $/; <$dumpfh>; };
200 unlike($contents, $_, "$path does not match $_") for @_;
204 __PACKAGE__->cleanup unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}