1 package dbixcsl_dumper_tests;
7 use DBIx::Class::Schema::Loader::Utils 'dumper_squashed';
8 use DBIx::Class::Schema::Loader ();
10 use dbixcsl_test_dir qw/$tdir/;
12 my $DUMP_PATH = "$tdir/dump";
14 rmtree($DUMP_PATH, 1, 1);
18 my ($self, $class, $string) = @_;
20 $class = $DUMP_PATH . '/' . $class . '.pm';
21 open(my $appendfh, '>>', $class) or die "Failed to open '$class' for append: $!";
22 print $appendfh $string;
27 my ($self, %tdata) = @_;
30 $tdata{options}{dump_directory} = $DUMP_PATH;
31 $tdata{options}{use_namespaces} ||= 0;
33 for my $dumper (\&_dump_directly, \&_dump_dbicdump) {
34 _test_dumps(\%tdata, $dumper->(%tdata));
42 my $schema_class = $tdata{classname};
45 @{$schema_class . '::ISA'} = ('DBIx::Class::Schema::Loader');
46 $schema_class->loader_options(%{$tdata{options}});
50 local $SIG{__WARN__} = sub { push(@warns, @_) };
51 $schema_class->connect(_get_dsn(\%tdata));
54 $schema_class->storage->disconnect if !$err && $schema_class->storage;
55 undef *{$schema_class};
57 _check_error($err, $tdata{error});
65 # use $^X so we execute ./script/dbicdump with the same perl binary that the tests were executed with
66 my @cmd = ($^X, qw(./script/dbicdump));
68 while (my ($opt, $val) = each(%{ $tdata{options} })) {
69 $val = dumper_squashed $val if ref $val;
70 push @cmd, '-o', "$opt=$val";
73 push @cmd, $tdata{classname}, _get_dsn(\%tdata);
75 # make sure our current @INC gets used by dbicdump
77 local $ENV{PERL5LIB} = join $Config{path_sep}, @INC, ($ENV{PERL5LIB} || '');
80 my $pid = open3($in, $out, $err, @cmd);
89 _check_error($error, $tdata{error});
101 my $test_db_class = $opts->{test_db_class} || 'make_dbictest_db';
103 eval "require $test_db_class;";
108 ${$test_db_class . '::dsn'};
115 my ($got, $expected) = @_;
120 fail "Unexpected error in " . ((caller(1))[3]) . ": $got";
124 if (ref $expected eq 'Regexp') {
125 like $got, $expected, 'error matches expected pattern';
129 is $got, $expected, 'error matches';
134 my ($tdata, @warns) = @_;
136 my %tdata = %{$tdata};
138 my $schema_class = $tdata{classname};
139 my $check_warns = $tdata{warnings};
140 is(@warns, @$check_warns, "$schema_class warning count");
142 for(my $i = 0; $i <= $#$check_warns; $i++) {
143 like($warns[$i], $check_warns->[$i], "$schema_class warning $i");
146 my $file_regexes = $tdata{regexes};
147 my $file_neg_regexes = $tdata{neg_regexes} || {};
148 my $schema_regexes = delete $file_regexes->{schema};
150 my $schema_path = $DUMP_PATH . '/' . $schema_class;
151 $schema_path =~ s{::}{/}g;
153 _dump_file_like($schema_path . '.pm', @$schema_regexes) if $schema_regexes;
155 foreach my $src (keys %$file_regexes) {
156 my $src_file = $schema_path . '/' . $src . '.pm';
157 _dump_file_like($src_file, @{$file_regexes->{$src}});
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}});
165 sub _dump_file_like {
167 open(my $dumpfh, '<', $path) or die "Failed to open '$path': $!";
168 my $contents = do { local $/; <$dumpfh>; };
170 like($contents, $_, "$path matches $_") for @_;
173 sub _dump_file_not_like {
175 open(my $dumpfh, '<', $path) or die "Failed to open '$path': $!";
176 my $contents = do { local $/; <$dumpfh>; };
178 unlike($contents, $_, "$path does not match $_") for @_;
182 __PACKAGE__->cleanup unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}