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