Extract LeakTracer symtable visitor into DBICTest::Util
[dbsrgits/DBIx-Class.git] / t / lib / DBICTest / Util.pm
CommitLineData
65d35121 1package DBICTest::Util;
2
3use warnings;
4use strict;
5
bbcc1fe8 6# this noop trick initializes the STDOUT, so that the TAP::Harness
7# issued IO::Select->can_read calls (which are blocking wtf wtf wtf)
8# keep spinning and scheduling jobs
9# This results in an overall much smoother job-queue drainage, since
10# the Harness blocks less
11# (ideally this needs to be addressed in T::H, but a quick patchjob
12# broke everything so tabling it for now)
13BEGIN {
14 if ($INC{'Test/Builder.pm'}) {
15 local $| = 1;
16 print "#\n";
17 }
18}
19
20use Module::Runtime 'module_notional_filename';
21BEGIN {
22 for my $mod (qw( SQL::Abstract::Test SQL::Abstract )) {
23 if ( $INC{ module_notional_filename($mod) } ) {
24 # FIXME this does not seem to work in BEGIN - why?!
25 #require Carp;
26 #$Carp::Internal{ (__PACKAGE__) }++;
27 #Carp::croak( __PACKAGE__ . " must be loaded before $mod" );
28
29 my ($fr, @frame) = 1;
30 while (@frame = caller($fr++)) {
31 last if $frame[1] !~ m|^t/lib/DBICTest|;
32 }
33
34 die __PACKAGE__ . " must be loaded before $mod (or modules using $mod) at $frame[1] line $frame[2]\n";
35 }
36 }
37}
38
8d6b1478 39use Config;
a3a17a15 40use Carp 'confess';
a446d7f8 41use Scalar::Util qw(blessed refaddr);
65d35121 42
43use base 'Exporter';
2f48c52f 44our @EXPORT_OK = qw(local_umask stacktrace check_customcond_args visit_namespaces);
8d6b1478 45
46sub local_umask {
47 return unless defined $Config{d_umask};
48
49 die 'Calling local_umask() in void context makes no sense'
50 if ! defined wantarray;
51
52 my $old_umask = umask(shift());
53 die "Setting umask failed: $!" unless defined $old_umask;
54
55 return bless \$old_umask, 'DBICTest::Util::UmaskGuard';
56}
57{
58 package DBICTest::Util::UmaskGuard;
59 sub DESTROY {
60 local ($@, $!);
61 eval { defined (umask ${$_[0]}) or die };
62 warn ( "Unable to reset old umask ${$_[0]}: " . ($!||'Unknown error') )
63 if ($@ || $!);
64 }
65}
66
65d35121 67sub stacktrace {
68 my $frame = shift;
69 $frame++;
70 my (@stack, @frame);
71
72 while (@frame = caller($frame++)) {
73 push @stack, [@frame[3,1,2]];
74 }
75
76 return undef unless @stack;
77
78 $stack[0][0] = '';
79 return join "\tinvoked as ", map { sprintf ("%s at %s line %d\n", @$_ ) } @stack;
80}
81
a3a17a15 82sub check_customcond_args ($) {
83 my $args = shift;
84
85 confess "Expecting a hashref"
86 unless ref $args eq 'HASH';
87
a446d7f8 88 for (qw(rel_name foreign_relname self_alias foreign_alias)) {
a3a17a15 89 confess "Custom condition argument '$_' must be a plain string"
90 if length ref $args->{$_} or ! length $args->{$_};
91 }
92
a446d7f8 93 confess "Current and legacy rel_name arguments do not match"
94 if $args->{rel_name} ne $args->{foreign_relname};
95
a3a17a15 96 confess "Custom condition argument 'self_resultsource' must be a rsrc instance"
97 unless defined blessed $args->{self_resultsource} and $args->{self_resultsource}->isa('DBIx::Class::ResultSource');
98
99 confess "Passed resultsource has no record of the supplied rel_name - likely wrong \$rsrc"
a446d7f8 100 unless ref $args->{self_resultsource}->relationship_info($args->{rel_name});
101
e884e5d9 102 my $struct_cnt = 0;
1adbd3fc 103
98def3ef 104 if (defined $args->{self_result_object} or defined $args->{self_rowobj} ) {
e884e5d9 105 $struct_cnt++;
98def3ef 106 for (qw(self_result_object self_rowobj)) {
a446d7f8 107 confess "Custom condition argument '$_' must be a result instance"
108 unless defined blessed $args->{$_} and $args->{$_}->isa('DBIx::Class::Row');
109 }
a3a17a15 110
98def3ef 111 confess "Current and legacy self_result_object arguments do not match"
112 if refaddr($args->{self_result_object}) != refaddr($args->{self_rowobj});
a3a17a15 113 }
114
e884e5d9 115 if (defined $args->{foreign_values}) {
116 $struct_cnt++;
1adbd3fc 117
e884e5d9 118 confess "Custom condition argument 'foreign_values' must be a hash reference"
119 unless ref $args->{foreign_values} eq 'HASH';
1adbd3fc 120 }
121
e884e5d9 122 confess "Data structures supplied on both ends of a relationship"
123 if $struct_cnt == 2;
1adbd3fc 124
a3a17a15 125 $args;
126}
127
2f48c52f 128sub visit_namespaces {
129 my $args = { (ref $_[0]) ? %{$_[0]} : @_ };
130
131 my $visited_count = 1;
132
133 # A package and a namespace are subtly different things
134 $args->{package} ||= 'main';
135 $args->{package} = 'main' if $args->{package} =~ /^ :: (?: main )? $/x;
136 $args->{package} =~ s/^:://;
137
138 if ( $args->{action}->($args->{package}) ) {
139 my $ns =
140 ( ($args->{package} eq 'main') ? '' : $args->{package} )
141 .
142 '::'
143 ;
144
145 $visited_count += visit_namespaces( %$args, package => $_ ) for
146 grep
147 # this happens sometimes on %:: traversal
148 { $_ ne '::main' }
149 map
150 { $_ =~ /^(.+?)::$/ ? "$ns$1" : () }
151 do { no strict 'refs'; keys %$ns }
152 ;
153 }
154
155 return $visited_count;
156}
157
65d35121 1581;