1 package dbixcsl_dumper_tests;
7 use DBIx::Class::Schema::Loader::Utils 'dumper_squashed';
8 use DBIx::Class::Schema::Loader ();
10 my $DUMP_PATH = 't/_dump';
12 rmtree($DUMP_PATH, 1, 1);
16 my ($self, $class, $string) = @_;
18 $class = $DUMP_PATH . '/' . $class . '.pm';
19 open(my $appendfh, '>>', $class) or die "Failed to open '$class' for append: $!";
20 print $appendfh $string;
25 my ($self, %tdata) = @_;
28 $tdata{options}{dump_directory} = $DUMP_PATH;
29 $tdata{options}{use_namespaces} ||= 0;
31 for my $dumper (\&_dump_directly, \&_dump_dbicdump) {
32 _test_dumps(\%tdata, $dumper->(%tdata));
40 my $schema_class = $tdata{classname};
43 @{$schema_class . '::ISA'} = ('DBIx::Class::Schema::Loader');
44 $schema_class->loader_options(%{$tdata{options}});
48 local $SIG{__WARN__} = sub { push(@warns, @_) };
49 $schema_class->connect(_get_dsn(\%tdata));
52 $schema_class->storage->disconnect if !$err && $schema_class->storage;
53 undef *{$schema_class};
55 _check_error($err, $tdata{error});
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));
66 while (my ($opt, $val) = each(%{ $tdata{options} })) {
67 $val = dumper_squashed $val if ref $val;
68 push @cmd, '-o', "$opt=$val";
71 push @cmd, $tdata{classname}, _get_dsn(\%tdata);
73 # make sure our current @INC gets used by dbicdump
75 local $ENV{PERL5LIB} = join $Config{path_sep}, @INC, ($ENV{PERL5LIB} || '');
78 my $pid = open3($in, $out, $err, @cmd);
87 _check_error($error, $tdata{error});
99 my $test_db_class = $opts->{test_db_class} || 'make_dbictest_db';
101 eval "require $test_db_class;";
106 ${$test_db_class . '::dsn'};
113 my ($got, $expected) = @_;
118 fail "Unexpected error in " . ((caller(1))[3]) . ": $got";
122 if (ref $expected eq 'Regexp') {
123 like $got, $expected, 'error matches expected pattern';
127 is $got, $expected, 'error matches';
132 my ($tdata, @warns) = @_;
134 my %tdata = %{$tdata};
136 my $schema_class = $tdata{classname};
137 my $check_warns = $tdata{warnings};
138 is(@warns, @$check_warns, "$schema_class warning count");
140 for(my $i = 0; $i <= $#$check_warns; $i++) {
141 like($warns[$i], $check_warns->[$i], "$schema_class warning $i");
144 my $file_regexes = $tdata{regexes};
145 my $file_neg_regexes = $tdata{neg_regexes} || {};
146 my $schema_regexes = delete $file_regexes->{schema};
148 my $schema_path = $DUMP_PATH . '/' . $schema_class;
149 $schema_path =~ s{::}{/}g;
151 _dump_file_like($schema_path . '.pm', @$schema_regexes) if $schema_regexes;
153 foreach my $src (keys %$file_regexes) {
154 my $src_file = $schema_path . '/' . $src . '.pm';
155 _dump_file_like($src_file, @{$file_regexes->{$src}});
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}});
163 sub _dump_file_like {
165 open(my $dumpfh, '<', $path) or die "Failed to open '$path': $!";
166 my $contents = do { local $/; <$dumpfh>; };
168 like($contents, $_, "$path matches $_") for @_;
171 sub _dump_file_not_like {
173 open(my $dumpfh, '<', $path) or die "Failed to open '$path': $!";
174 my $contents = do { local $/; <$dumpfh>; };
176 unlike($contents, $_, "$path does not match $_") for @_;
180 __PACKAGE__->cleanup unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}