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