Relax the optdep naming criteria, introduce _-prefixed intermediate groups
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Optional / Dependencies.pm
index fcf1deb..531b0fc 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
@@ -11,16 +17,8 @@ use Carp;
 # POD is generated automatically by calling _gen_pod from the
 # Makefile.PL in $AUTHOR mode
 
-# NOTE: the rationale for 2 JSON::Any versions is that
-# we need the newer only to work around JSON::XS, which
-# itself is an optional dep
-my $min_json_any = {
-  'JSON::Any'                     => '1.23',
-};
-my $test_and_dist_json_any = {
-  'JSON::Any'                     => '1.31',
-};
-
+# *DELIBERATELY* not making a group for these - they must disappear
+# forever as optdeps in the first place
 my $moose_basic = {
   'Moose'                         => '0.98',
   'MooseX::Types'                 => '0.21',
@@ -28,6 +26,34 @@ my $moose_basic = {
 };
 
 my $dbic_reqs = {
+
+  # NOTE: the rationale for 2 JSON::Any versions is that
+  # we need the newer only to work around JSON::XS, which
+  # itself is an optional dep
+  _json_any => {
+    req => {
+      'JSON::Any' => '1.23',
+    },
+  },
+
+  _json_xs_compatible_json_any => {
+    req => {
+      'JSON::Any' => '1.31',
+    },
+  },
+
+  _rdbms_generic_odbc => {
+    req => {
+      'DBD::ODBC' => 0,
+    }
+  },
+
+  _rdbms_generic_ado => {
+    req => {
+      'DBD::ADO' => 0,
+    }
+  },
+
   replicated => {
     req => $moose_basic,
     pod => {
@@ -44,9 +70,9 @@ my $dbic_reqs = {
   },
 
   admin => {
+    include => '_json_any',
     req => {
       %$moose_basic,
-      %$min_json_any,
       'MooseX::Types::Path::Class' => '0.05',
       'MooseX::Types::JSON' => '0.02',
     },
@@ -95,6 +121,7 @@ my $dbic_reqs = {
     req => {
       'Test::Pod'                 => '1.42',
     },
+    release_testing_mandatory => 1,
   },
 
   test_podcoverage => {
@@ -102,6 +129,7 @@ my $dbic_reqs = {
       'Test::Pod::Coverage'       => '1.08',
       'Pod::Coverage'             => '0.20',
     },
+    release_testing_mandatory => 1,
   },
 
   test_whitespace => {
@@ -109,22 +137,23 @@ 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 => {
-    req => $min_json_any,
+    include => '_json_any',
   },
 
   test_admin_script => {
-    include => 'admin_script',
+    include => [qw( admin_script _json_xs_compatible_json_any )],
     req => {
-      %$test_and_dist_json_any,
       'JSON' => 0,
       'JSON::PP' => 0,
       'Cpanel::JSON::XS' => 0,
@@ -188,18 +217,6 @@ my $dbic_reqs = {
     },
   },
 
-  rdbms_generic_odbc => {
-    req => {
-      'DBD::ODBC' => 0,
-    }
-  },
-
-  rdbms_generic_ado => {
-    req => {
-      'DBD::ADO' => 0,
-    }
-  },
-
   # this is just for completeness as SQLite
   # is a core dep of DBIC for testing
   rdbms_sqlite => {
@@ -224,7 +241,7 @@ my $dbic_reqs = {
   },
 
   rdbms_mssql_odbc => {
-    include => 'rdbms_generic_odbc',
+    include => '_rdbms_generic_odbc',
     pod => {
       title => 'MSSQL support via DBD::ODBC',
       desc => 'Modules required to connect to MSSQL via DBD::ODBC',
@@ -242,7 +259,7 @@ my $dbic_reqs = {
   },
 
   rdbms_mssql_ado => {
-    include => 'rdbms_generic_ado',
+    include => '_rdbms_generic_ado',
     pod => {
       title => 'MSSQL support via DBD::ADO (Windows only)',
       desc => 'Modules required to connect to MSSQL via DBD::ADO. This particular DBD is available on Windows only',
@@ -250,7 +267,7 @@ my $dbic_reqs = {
   },
 
   rdbms_msaccess_odbc => {
-    include => 'rdbms_generic_odbc',
+    include => '_rdbms_generic_odbc',
     pod => {
       title => 'MS Access support via DBD::ODBC',
       desc => 'Modules required to connect to MS Access via DBD::ODBC',
@@ -258,7 +275,7 @@ my $dbic_reqs = {
   },
 
   rdbms_msaccess_ado => {
-    include => 'rdbms_generic_ado',
+    include => '_rdbms_generic_ado',
     pod => {
       title => 'MS Access support via DBD::ADO (Windows only)',
       desc => 'Modules required to connect to MS Access via DBD::ADO. This particular DBD is available on Windows only',
@@ -307,7 +324,7 @@ my $dbic_reqs = {
   },
 
   rdbms_db2_400 => {
-    include => 'rdbms_generic_odbc',
+    include => '_rdbms_generic_odbc',
     pod => {
       title => 'DB2 on AS/400 support',
       desc => 'Modules required to connect to DB2 on AS/400',
@@ -335,7 +352,7 @@ my $dbic_reqs = {
   },
 
   rdbms_sqlanywhere_odbc => {
-    include => 'rdbms_generic_odbc',
+    include => '_rdbms_generic_odbc',
     pod => {
       title => 'SQLAnywhere support via DBD::ODBC',
       desc => 'Modules required to connect to SQLAnywhere via DBD::ODBC',
@@ -363,13 +380,26 @@ my $dbic_reqs = {
   },
 
   rdbms_firebird_odbc => {
-    include => 'rdbms_generic_odbc',
+    include => '_rdbms_generic_odbc',
     pod => {
       title => 'Firebird support via DBD::ODBC',
       desc => 'Modules required to connect to Firebird via DBD::ODBC',
     },
   },
 
+  test_rdbms_sqlite => {
+    include => 'rdbms_sqlite',
+    req => {
+      ###
+      ### IMPORTANT - do not raise this dependency
+      ### even though many bugfixes are present in newer versions, the general DBIC
+      ### rule is to bend over backwards for available DBDs (given upgrading them is
+      ### often *not* easy or even possible)
+      ###
+      'DBD::SQLite' => '1.29',
+    },
+  },
+
   test_rdbms_pg => {
     include => 'rdbms_pg',
     env => [
@@ -414,7 +444,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 +456,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,
@@ -550,8 +580,11 @@ my $dbic_reqs = {
   },
 
   dist_dir => {
+    # we need to run the dbicadmin so we can self-generate its POD
+    # also we do not want surprises in case JSON::XS is in the path
+    # so make sure we get an always-working JSON::Any
+    include => [qw( admin_script _json_xs_compatible_json_any )],
     req => {
-      %$test_and_dist_json_any,
       'ExtUtils::MakeMaker' => '6.64',
       'Pod::Inherit'        => '0.91',
     },
@@ -568,41 +601,98 @@ 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 {
   +{ map
     { $_ => $_[0]->_groups_to_reqs($_) }
-    keys %$dbic_reqs
+    grep { $_ !~ /^_/ } keys %$dbic_reqs
   }
 }
 
-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 +702,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 +715,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 +728,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";
   }
 }
@@ -668,8 +758,6 @@ sub __envvar_group_desc {
   join '/', @res;
 }
 
-
-
 ### Private OO API
 our %req_unavailability_cache;
 
@@ -691,7 +779,7 @@ sub _groups_to_reqs {
 
   for my $group ( grep { ! $processed_groups->{$_} } @$groups ) {
 
-    $group =~ /\A [A-Za-z][0-9A-Z_a-z]* \z/x
+    $group =~ /\A [A-Z_a-z][0-9A-Z_a-z]* \z/x
       or croak "Invalid requirement group name '$group': only ascii alphanumerics and _ are allowed";
 
     croak "Requirement group '$group' is not defined" unless defined $dbic_reqs->{$group};
@@ -786,6 +874,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 +1062,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 +1241,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 +1254,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