1 package DBICTest::Util;
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)
14 if ($INC{'Test/Builder.pm'}) {
20 use Module::Runtime 'module_notional_filename';
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?!
26 #$Carp::Internal{ (__PACKAGE__) }++;
27 #Carp::croak( __PACKAGE__ . " must be loaded before $mod" );
30 while (@frame = caller($fr++)) {
31 last if $frame[1] !~ m|^t/lib/DBICTest|;
34 die __PACKAGE__ . " must be loaded before $mod (or modules using $mod) at $frame[1] line $frame[2]\n";
41 use Scalar::Util qw(blessed refaddr);
44 our @EXPORT_OK = qw(local_umask stacktrace check_customcond_args);
47 return unless defined $Config{d_umask};
49 die 'Calling local_umask() in void context makes no sense'
50 if ! defined wantarray;
52 my $old_umask = umask(shift());
53 die "Setting umask failed: $!" unless defined $old_umask;
55 return bless \$old_umask, 'DBICTest::Util::UmaskGuard';
58 package DBICTest::Util::UmaskGuard;
61 eval { defined (umask ${$_[0]}) or die };
62 warn ( "Unable to reset old umask ${$_[0]}: " . ($!||'Unknown error') )
72 while (@frame = caller($frame++)) {
73 push @stack, [@frame[3,1,2]];
76 return undef unless @stack;
79 return join "\tinvoked as ", map { sprintf ("%s at %s line %d\n", @$_ ) } @stack;
82 sub check_customcond_args ($) {
85 confess "Expecting a hashref"
86 unless ref $args eq 'HASH';
88 for (qw(rel_name foreign_relname self_alias foreign_alias)) {
89 confess "Custom condition argument '$_' must be a plain string"
90 if length ref $args->{$_} or ! length $args->{$_};
93 confess "Current and legacy rel_name arguments do not match"
94 if $args->{rel_name} ne $args->{foreign_relname};
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');
99 confess "Passed resultsource has no record of the supplied rel_name - likely wrong \$rsrc"
100 unless ref $args->{self_resultsource}->relationship_info($args->{rel_name});
104 if (defined $args->{self_result_object} or defined $args->{self_rowobj} ) {
106 for (qw(self_result_object self_rowobj)) {
107 confess "Custom condition argument '$_' must be a result instance"
108 unless defined blessed $args->{$_} and $args->{$_}->isa('DBIx::Class::Row');
111 confess "Current and legacy self_result_object arguments do not match"
112 if refaddr($args->{self_result_object}) != refaddr($args->{self_rowobj});
115 if (defined $args->{foreign_result_object}) {
118 confess "Custom condition argument 'foreign_result_object' must be a result instance"
119 unless defined blessed $args->{foreign_result_object} and $args->{foreign_result_object}->isa('DBIx::Class::Row');
122 confess "Result objects supplied on both ends of a relationship"