1 package dbixcsl_dumper_tests;
8 use DBIx::Class::Schema::Loader::Utils 'dumper_squashed';
9 use DBIx::Class::Schema::Loader ();
11 use dbixcsl_test_dir qw/$tdir/;
13 my $DUMP_PATH = "$tdir/dump";
15 rmtree($DUMP_PATH, 1, 1);
19 my ($self, $class, $string) = @_;
21 $class = $DUMP_PATH . '/' . $class . '.pm';
22 open(my $appendfh, '>>', $class) or die "Failed to open '$class' for append: $!";
23 print $appendfh $string;
28 my ($self, %tdata) = @_;
31 $tdata{options}{dump_directory} = $DUMP_PATH;
32 $tdata{options}{use_namespaces} ||= 0;
34 for my $dumper (\&_dump_directly, \&_dump_dbicdump) {
35 _test_dumps(\%tdata, $dumper->(%tdata));
43 my $schema_class = $tdata{classname};
46 @{$schema_class . '::ISA'} = ('DBIx::Class::Schema::Loader');
47 $schema_class->loader_options(%{$tdata{options}});
51 local $SIG{__WARN__} = sub { push(@warns, @_) };
52 $schema_class->connect(_get_connect_info(\%tdata));
56 $schema_class->storage->disconnect if !$err && $schema_class->storage;
57 undef *{$schema_class};
59 _check_error($err, $tdata{error});
67 # use $^X so we execute ./script/dbicdump with the same perl binary that the tests were executed with
68 my @cmd = ($^X, qw(script/dbicdump));
70 while (my ($opt, $val) = each(%{ $tdata{options} })) {
71 $val = dumper_squashed $val if ref $val;
72 push @cmd, '-o', "$opt=$val";
75 my @connect_info = _get_connect_info(\%tdata);
77 for my $info (@connect_info) {
78 $info = dumper_squashed $info if ref $info;
81 push @cmd, $tdata{classname}, @connect_info;
83 # make sure our current @INC gets used by dbicdump
85 local $ENV{PERL5LIB} = join $Config{path_sep}, @INC, ($ENV{PERL5LIB} || '');
87 my $std = { map { $_ => IO::Handle->new } (qw/in out err/) };
88 my $pid = open3(@{$std}{qw/in out err/}, @cmd);
92 my @stdout = $std->{out}->getlines;
93 ok (!scalar @stdout, 'Silence on STDOUT');
95 my @warnings = $std->{err}->getlines;
97 my $exception = pop @warnings;
98 _check_error($exception, $tdata{error});
104 sub _get_connect_info {
107 my $test_db_class = $opts->{test_db_class} || 'make_dbictest_db';
109 eval "require $test_db_class;";
114 ${$test_db_class . '::dsn'};
117 return ($dsn, @{ $opts->{extra_connect_info} || [] });
121 my ($got, $expected) = @_;
126 fail "Unexpected error in " . ((caller(1))[3]) . ": $got";
130 if (ref $expected eq 'Regexp') {
131 like $got, $expected, 'error matches expected pattern';
135 is $got, $expected, 'error matches';
140 my ($tdata, @warns) = @_;
142 my %tdata = %{$tdata};
144 my $schema_class = $tdata{classname};
145 my $check_warns = $tdata{warnings};
146 is(@warns, @$check_warns, "$schema_class warning count");
148 for(my $i = 0; $i <= $#$check_warns; $i++) {
149 like($warns[$i], $check_warns->[$i], "$schema_class warning $i");
152 my $file_regexes = $tdata{regexes};
153 my $file_neg_regexes = $tdata{neg_regexes} || {};
154 my $schema_regexes = delete $file_regexes->{schema};
156 my $schema_path = $DUMP_PATH . '/' . $schema_class;
157 $schema_path =~ s{::}{/}g;
159 _dump_file_like($schema_path . '.pm', @$schema_regexes) if $schema_regexes;
161 foreach my $src (keys %$file_regexes) {
162 my $src_file = $schema_path . '/' . $src . '.pm';
163 _dump_file_like($src_file, @{$file_regexes->{$src}});
165 foreach my $src (keys %$file_neg_regexes) {
166 my $src_file = $schema_path . '/' . $src . '.pm';
167 _dump_file_not_like($src_file, @{$file_neg_regexes->{$src}});
171 sub _dump_file_like {
173 open(my $dumpfh, '<', $path) or die "Failed to open '$path': $!";
174 my $contents = do { local $/; <$dumpfh>; };
176 like($contents, $_, "$path matches $_") for @_;
179 sub _dump_file_not_like {
181 open(my $dumpfh, '<', $path) or die "Failed to open '$path': $!";
182 my $contents = do { local $/; <$dumpfh>; };
184 unlike($contents, $_, "$path does not match $_") for @_;
188 __PACKAGE__->cleanup unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}