Commit | Line | Data |
65d35121 |
1 | package DBICTest::Util; |
2 | |
3 | use warnings; |
4 | use 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) |
13 | BEGIN { |
14 | if ($INC{'Test/Builder.pm'}) { |
15 | local $| = 1; |
16 | print "#\n"; |
17 | } |
18 | } |
19 | |
20 | use Module::Runtime 'module_notional_filename'; |
21 | BEGIN { |
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 |
39 | use Config; |
a3a17a15 |
40 | use Carp 'confess'; |
a446d7f8 |
41 | use Scalar::Util qw(blessed refaddr); |
87f4bab0 |
42 | use DBIx::Class::_Util; |
65d35121 |
43 | |
44 | use base 'Exporter'; |
2f48c52f |
45 | our @EXPORT_OK = qw(local_umask stacktrace check_customcond_args visit_namespaces); |
8d6b1478 |
46 | |
47 | sub local_umask { |
48 | return unless defined $Config{d_umask}; |
49 | |
50 | die 'Calling local_umask() in void context makes no sense' |
51 | if ! defined wantarray; |
52 | |
53 | my $old_umask = umask(shift()); |
54 | die "Setting umask failed: $!" unless defined $old_umask; |
55 | |
56 | return bless \$old_umask, 'DBICTest::Util::UmaskGuard'; |
57 | } |
58 | { |
59 | package DBICTest::Util::UmaskGuard; |
60 | sub DESTROY { |
87f4bab0 |
61 | &DBIx::Class::_Util::detected_reinvoked_destructor; |
62 | |
8d6b1478 |
63 | local ($@, $!); |
64 | eval { defined (umask ${$_[0]}) or die }; |
65 | warn ( "Unable to reset old umask ${$_[0]}: " . ($!||'Unknown error') ) |
66 | if ($@ || $!); |
67 | } |
68 | } |
69 | |
65d35121 |
70 | sub stacktrace { |
71 | my $frame = shift; |
72 | $frame++; |
73 | my (@stack, @frame); |
74 | |
75 | while (@frame = caller($frame++)) { |
76 | push @stack, [@frame[3,1,2]]; |
77 | } |
78 | |
79 | return undef unless @stack; |
80 | |
81 | $stack[0][0] = ''; |
82 | return join "\tinvoked as ", map { sprintf ("%s at %s line %d\n", @$_ ) } @stack; |
83 | } |
84 | |
a3a17a15 |
85 | sub check_customcond_args ($) { |
86 | my $args = shift; |
87 | |
88 | confess "Expecting a hashref" |
89 | unless ref $args eq 'HASH'; |
90 | |
a446d7f8 |
91 | for (qw(rel_name foreign_relname self_alias foreign_alias)) { |
a3a17a15 |
92 | confess "Custom condition argument '$_' must be a plain string" |
93 | if length ref $args->{$_} or ! length $args->{$_}; |
94 | } |
95 | |
a446d7f8 |
96 | confess "Current and legacy rel_name arguments do not match" |
97 | if $args->{rel_name} ne $args->{foreign_relname}; |
98 | |
a3a17a15 |
99 | confess "Custom condition argument 'self_resultsource' must be a rsrc instance" |
100 | unless defined blessed $args->{self_resultsource} and $args->{self_resultsource}->isa('DBIx::Class::ResultSource'); |
101 | |
102 | confess "Passed resultsource has no record of the supplied rel_name - likely wrong \$rsrc" |
a446d7f8 |
103 | unless ref $args->{self_resultsource}->relationship_info($args->{rel_name}); |
104 | |
e884e5d9 |
105 | my $struct_cnt = 0; |
1adbd3fc |
106 | |
98def3ef |
107 | if (defined $args->{self_result_object} or defined $args->{self_rowobj} ) { |
e884e5d9 |
108 | $struct_cnt++; |
98def3ef |
109 | for (qw(self_result_object self_rowobj)) { |
a446d7f8 |
110 | confess "Custom condition argument '$_' must be a result instance" |
111 | unless defined blessed $args->{$_} and $args->{$_}->isa('DBIx::Class::Row'); |
112 | } |
a3a17a15 |
113 | |
98def3ef |
114 | confess "Current and legacy self_result_object arguments do not match" |
115 | if refaddr($args->{self_result_object}) != refaddr($args->{self_rowobj}); |
a3a17a15 |
116 | } |
117 | |
e884e5d9 |
118 | if (defined $args->{foreign_values}) { |
119 | $struct_cnt++; |
1adbd3fc |
120 | |
e884e5d9 |
121 | confess "Custom condition argument 'foreign_values' must be a hash reference" |
122 | unless ref $args->{foreign_values} eq 'HASH'; |
1adbd3fc |
123 | } |
124 | |
e884e5d9 |
125 | confess "Data structures supplied on both ends of a relationship" |
126 | if $struct_cnt == 2; |
1adbd3fc |
127 | |
a3a17a15 |
128 | $args; |
129 | } |
130 | |
2f48c52f |
131 | sub visit_namespaces { |
132 | my $args = { (ref $_[0]) ? %{$_[0]} : @_ }; |
133 | |
134 | my $visited_count = 1; |
135 | |
136 | # A package and a namespace are subtly different things |
137 | $args->{package} ||= 'main'; |
138 | $args->{package} = 'main' if $args->{package} =~ /^ :: (?: main )? $/x; |
139 | $args->{package} =~ s/^:://; |
140 | |
141 | if ( $args->{action}->($args->{package}) ) { |
142 | my $ns = |
143 | ( ($args->{package} eq 'main') ? '' : $args->{package} ) |
144 | . |
145 | '::' |
146 | ; |
147 | |
148 | $visited_count += visit_namespaces( %$args, package => $_ ) for |
149 | grep |
150 | # this happens sometimes on %:: traversal |
151 | { $_ ne '::main' } |
152 | map |
153 | { $_ =~ /^(.+?)::$/ ? "$ns$1" : () } |
154 | do { no strict 'refs'; keys %$ns } |
155 | ; |
156 | } |
157 | |
158 | return $visited_count; |
159 | } |
160 | |
65d35121 |
161 | 1; |