Stabilize and further test complex optdep include scenarious
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Optional / Dependencies.pm
index fcf1deb..8568be7 100644 (file)
@@ -1,9 +1,15 @@
 package DBIx::Class::Optional::Dependencies;
 
-use warnings;
-use strict;
-
-use Carp;
+### This may look crazy, but it in fact tangibly ( by 50(!)% ) shortens
+#   the skip-test time when everything requested is unavailable
+use if $ENV{RELEASE_TESTING} => 'warnings';
+use if $ENV{RELEASE_TESTING} => 'strict';
+
+sub croak {
+  require Carp;
+  Carp::croak(@_);
+};
+###
 
 # NO EXTERNAL NON-5.8.1 CORE DEPENDENCIES EVER (e.g. C::A::G)
 # This module is to be loaded by Makefile.PM on a pristine system
@@ -95,6 +101,7 @@ my $dbic_reqs = {
     req => {
       'Test::Pod'                 => '1.42',
     },
+    release_testing_mandatory => 1,
   },
 
   test_podcoverage => {
@@ -102,6 +109,7 @@ my $dbic_reqs = {
       'Test::Pod::Coverage'       => '1.08',
       'Pod::Coverage'             => '0.20',
     },
+    release_testing_mandatory => 1,
   },
 
   test_whitespace => {
@@ -109,12 +117,14 @@ my $dbic_reqs = {
       'Test::EOL'                 => '1.0',
       'Test::NoTabs'              => '0.9',
     },
+    release_testing_mandatory => 1,
   },
 
   test_strictures => {
     req => {
       'Test::Strict'              => '0.20',
     },
+    release_testing_mandatory => 1,
   },
 
   test_prettydebug => {
@@ -414,7 +424,7 @@ my $dbic_reqs = {
   },
 
   test_rdbms_msaccess_odbc => {
-    include => [qw(rdbms_msaccess_odbc test_dt)],
+    include => 'rdbms_msaccess_odbc',
     env => [
       DBICTEST_MSACCESS_ODBC_DSN => 1,
       DBICTEST_MSACCESS_ODBC_USER => 0,
@@ -426,7 +436,7 @@ my $dbic_reqs = {
   },
 
   test_rdbms_msaccess_ado => {
-    include => [qw(rdbms_msaccess_ado test_dt)],
+    include => 'rdbms_msaccess_ado',
     env => [
       DBICTEST_MSACCESS_ADO_DSN => 1,
       DBICTEST_MSACCESS_ADO_USER => 0,
@@ -568,17 +578,74 @@ my $dbic_reqs = {
 
 ### Public API
 
+sub import {
+  my $class = shift;
+
+  if (@_) {
+
+    my $action = shift;
+
+    if ($action eq '-die_without') {
+      my $err;
+      {
+        local $@;
+        eval { $class->die_unless_req_ok_for(\@_); 1 }
+          or $err = $@;
+      }
+      die "\n$err\n" if $err;
+    }
+    elsif ($action eq '-list_missing') {
+      print $class->modreq_missing_for(\@_);
+      print "\n";
+      exit 0;
+    }
+    elsif ($action eq '-skip_all_without') {
+
+      # sanity check - make sure ->current_test is 0 and no plan has been declared
+      do {
+        local $@;
+        defined eval {
+          Test::Builder->new->current_test
+            or
+          Test::Builder->new->has_plan
+        };
+      } and croak("Unable to invoke -skip_all_without after testing has started");
+
+      if ( my $missing = $class->req_missing_for(\@_) ) {
+
+        die ("\nMandatory requirements not satisfied during release-testing: $missing\n\n")
+          if $ENV{RELEASE_TESTING} and $class->_groups_to_reqs(\@_)->{release_testing_mandatory};
+
+        print "1..0 # SKIP requirements not satisfied: $missing\n";
+        exit 0;
+      }
+    }
+    elsif ($action =~ /^-/) {
+      croak "Unknown import-time action '$action'";
+    }
+    else {
+      croak "$class is not an exporter, unable to import '$action'";
+    }
+  }
+
+  1;
+}
+
+sub unimport {
+  croak( __PACKAGE__ . " does not implement unimport" );
+}
+
 # OO for (mistakenly considered) ease of extensibility, not due to any need to
 # carry state of any sort. This API is currently used outside, so leave as-is.
 # FIXME - make sure to not propagate this further if module is extracted as a
 # standalone library - keep the stupidity to a DBIC-secific shim!
 #
 sub req_list_for {
-  shift->_groups_to_reqs(@_)->{effective_modreqs};
+  shift->_groups_to_reqs(shift)->{effective_modreqs};
 }
 
 sub modreq_list_for {
-  shift->_groups_to_reqs(@_)->{modreqs};
+  shift->_groups_to_reqs(shift)->{modreqs};
 }
 
 sub req_group_list {
@@ -588,21 +655,21 @@ sub req_group_list {
   }
 }
 
-sub req_errorlist_for { shift->modreq_errorlist_for(@_) }  # deprecated
+sub req_errorlist_for { shift->modreq_errorlist_for(shift) }  # deprecated
 sub modreq_errorlist_for {
-  my $self = shift;
-  $self->_errorlist_for_modreqs( $self->_groups_to_reqs(@_)->{modreqs} );
+  my ($self, $groups) = @_;
+  $self->_errorlist_for_modreqs( $self->_groups_to_reqs($groups)->{modreqs} );
 }
 
 sub req_ok_for {
-  shift->req_missing_for(@_) ? 0 : 1;
+  shift->req_missing_for(shift) ? 0 : 1;
 }
 
 sub req_missing_for {
-  my $self = shift;
+  my ($self, $groups) = @_;
 
-  my $reqs = $self->_groups_to_reqs(@_);
-  my $mods_missing = $self->modreq_missing_for(@_);
+  my $reqs = $self->_groups_to_reqs($groups);
+  my $mods_missing = $self->modreq_missing_for($groups);
 
   return '' if
     ! $mods_missing
@@ -612,7 +679,7 @@ sub req_missing_for {
 
   my @res = $mods_missing || ();
 
-  push @res, 'the following group(s) of environment variables: ' . join ' and ', map
+  push @res, 'the following group(s) of environment variables: ' . join ' and ', sort map
     { __envvar_group_desc($_) }
     @{$reqs->{missing_envvars}}
   if $reqs->{missing_envvars};
@@ -625,9 +692,9 @@ sub req_missing_for {
 }
 
 sub modreq_missing_for {
-  my $self = shift;
+  my ($self, $groups) = @_;
 
-  my $reqs = $self->_groups_to_reqs(@_);
+  my $reqs = $self->_groups_to_reqs($groups);
   my $modreq_errors = $self->_errorlist_for_modreqs($reqs->{modreqs})
     or return '';
 
@@ -638,7 +705,7 @@ sub modreq_missing_for {
 }
 
 sub die_unless_req_ok_for {
-  if (my $err = shift->req_missing_for(@_) ) {
+  if (my $err = shift->req_missing_for(shift) ) {
     die "Unable to continue due to missing requirements: $err\n";
   }
 }
@@ -786,6 +853,8 @@ sub _groups_to_reqs {
     $ret->{effective_modreqs_differ} ||= !!$some_envs_missing;
 
     $ret->{modreqs_fully_documented} &&= !!$dbic_reqs->{$group}{pod};
+
+    $ret->{release_testing_mandatory} ||= !!$dbic_reqs->{$group}{release_testing_mandatory};
   }
 
   return $ret;
@@ -972,6 +1041,70 @@ EOC
 #@@
   push @chunks, <<'EOC';
 
+=head1 IMPORT-LIKE ACTIONS
+
+Even though this module is not an L<Exporter>, it recognizes several C<actions>
+supplied to its C<import> method.
+
+=head2 -skip_all_without
+
+=over
+
+=item Arguments: @group_names
+
+=back
+
+A convenience wrapper for use during testing:
+EOC
+
+  push @chunks, " use $class -skip_all_without => qw(admin test_rdbms_mysql);";
+
+  push @chunks, 'Roughly equivalent to the following code:';
+
+  push @chunks, sprintf <<'EOS', ($class) x 2;
+
+ BEGIN {
+   require %s;
+   if ( my $missing = %s->req_missing_for(\@group_names_) ) {
+     print "1..0 # SKIP requirements not satisfied: $missing\n";
+     exit 0;
+   }
+ }
+EOS
+
+  push @chunks, <<'EOC';
+
+It also takes into account the C<RELEASE_TESTING> environment variable and
+behaves like L</-die_without> for any requirement groups marked as
+C<release_testing_mandatory>.
+
+=head2 -die_without
+
+=over
+
+=item Arguments: @group_names
+
+=back
+
+A convenience wrapper around L</die_unless_req_ok_for>:
+EOC
+
+  push @chunks, " use $class -die_without => qw(deploy admin);";
+
+  push @chunks, <<'EOC';
+
+=head2 -list_missing
+
+=over
+
+=item Arguments: @group_names
+
+=back
+
+A convenience wrapper around L</modreq_missing_for>:
+
+ perl -Ilib -MDBIx::Class::Optional::Dependencies=-list_missing,deploy,admin | cpanm
+
 =head1 METHODS
 
 =head2 req_group_list
@@ -1087,6 +1220,9 @@ EOC
   push @chunks, qq{ "SQL::Translator~>=$sqltver"};
 
   push @chunks, <<'EOC';
+
+See also L</-list_missing>.
+
 =head2 die_unless_req_ok_for
 
 =over
@@ -1097,7 +1233,7 @@ EOC
 
 Checks if L</req_ok_for> passes for the supplied group(s), and
 in case of failure throws an exception including the information
-from L</req_missing_for>.
+from L</req_missing_for>. See also L</-die_without>.
 
 =head2 modreq_errorlist_for