Clarify name of guard function from 3d56e026, add it globally
[dbsrgits/DBIx-Class.git] / t / lib / DBICTest / Util.pm
1 package DBICTest::Util;
2
3 use warnings;
4 use strict;
5
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 Config;
21 use Carp 'confess';
22 use Scalar::Util qw(blessed refaddr);
23 use DBIx::Class::_Util;
24
25 use base 'Exporter';
26 our @EXPORT_OK = qw(local_umask stacktrace check_customcond_args visit_namespaces);
27
28 sub local_umask {
29   return unless defined $Config{d_umask};
30
31   die 'Calling local_umask() in void context makes no sense'
32     if ! defined wantarray;
33
34   my $old_umask = umask(shift());
35   die "Setting umask failed: $!" unless defined $old_umask;
36
37   return bless \$old_umask, 'DBICTest::Util::UmaskGuard';
38 }
39 {
40   package DBICTest::Util::UmaskGuard;
41   sub DESTROY {
42     &DBIx::Class::_Util::detected_reinvoked_destructor;
43
44     local ($@, $!);
45     eval { defined (umask ${$_[0]}) or die };
46     warn ( "Unable to reset old umask ${$_[0]}: " . ($!||'Unknown error') )
47       if ($@ || $!);
48   }
49 }
50
51 sub stacktrace {
52   my $frame = shift;
53   $frame++;
54   my (@stack, @frame);
55
56   while (@frame = caller($frame++)) {
57     push @stack, [@frame[3,1,2]];
58   }
59
60   return undef unless @stack;
61
62   $stack[0][0] = '';
63   return join "\tinvoked as ", map { sprintf ("%s at %s line %d\n", @$_ ) } @stack;
64 }
65
66 sub check_customcond_args ($) {
67   my $args = shift;
68
69   confess "Expecting a hashref"
70     unless ref $args eq 'HASH';
71
72   for (qw(rel_name foreign_relname self_alias foreign_alias)) {
73     confess "Custom condition argument '$_' must be a plain string"
74       if length ref $args->{$_} or ! length $args->{$_};
75   }
76
77   confess "Current and legacy rel_name arguments do not match"
78     if $args->{rel_name} ne $args->{foreign_relname};
79
80   confess "Custom condition argument 'self_resultsource' must be a rsrc instance"
81     unless defined blessed $args->{self_resultsource} and $args->{self_resultsource}->isa('DBIx::Class::ResultSource');
82
83   confess "Passed resultsource has no record of the supplied rel_name - likely wrong \$rsrc"
84     unless ref $args->{self_resultsource}->relationship_info($args->{rel_name});
85
86   my $struct_cnt = 0;
87
88   if (defined $args->{self_result_object} or defined $args->{self_rowobj} ) {
89     $struct_cnt++;
90     for (qw(self_result_object self_rowobj)) {
91       confess "Custom condition argument '$_' must be a result instance"
92         unless defined blessed $args->{$_} and $args->{$_}->isa('DBIx::Class::Row');
93     }
94
95     confess "Current and legacy self_result_object arguments do not match"
96       if refaddr($args->{self_result_object}) != refaddr($args->{self_rowobj});
97   }
98
99   if (defined $args->{foreign_values}) {
100     $struct_cnt++;
101
102     confess "Custom condition argument 'foreign_values' must be a hash reference"
103       unless ref $args->{foreign_values} eq 'HASH';
104   }
105
106   confess "Data structures supplied on both ends of a relationship"
107     if $struct_cnt == 2;
108
109   $args;
110 }
111
112 sub visit_namespaces {
113   my $args = { (ref $_[0]) ? %{$_[0]} : @_ };
114
115   my $visited_count = 1;
116
117   # A package and a namespace are subtly different things
118   $args->{package} ||= 'main';
119   $args->{package} = 'main' if $args->{package} =~ /^ :: (?: main )? $/x;
120   $args->{package} =~ s/^:://;
121
122   if ( $args->{action}->($args->{package}) ) {
123     my $ns =
124       ( ($args->{package} eq 'main') ? '' :  $args->{package} )
125         .
126       '::'
127     ;
128
129     $visited_count += visit_namespaces( %$args, package => $_ ) for
130       grep
131         # this happens sometimes on %:: traversal
132         { $_ ne '::main' }
133         map
134           { $_ =~ /^(.+?)::$/ ? "$ns$1" : () }
135           do { no strict 'refs'; keys %$ns }
136     ;
137   }
138
139   return $visited_count;
140 }
141
142 1;