From a40fd8e928b333287f7f52f590c8e2d0c2f90821 Mon Sep 17 00:00:00 2001
From: Don Armstrong <don@donarmstrong.com>
Date: Mon, 5 Sep 2016 09:05:38 -0700
Subject: [PATCH] abstract out create config in tests

---
 t/06_mail_handling.t     | 10 ++--------
 t/07_bugreport.t         | 11 +----------
 t/07_control_limit.t     | 10 ++--------
 t/08_pkgreport.t         | 11 +----------
 t/09_soap.t              | 11 +----------
 t/10_expire.t            | 10 ++--------
 t/11_blocks.t            | 10 ++--------
 t/12_merge.t             | 10 ++--------
 t/13_utf8_mail.t         | 10 ++--------
 t/14_control_at_submit.t | 10 ++--------
 t/15_rebuild_indexdb.t   | 10 ++--------
 t/18_libravatar_cgi.t    |  9 +--------
 t/lib/DebbugsTest.pm     | 16 +++++++++++++++-
 13 files changed, 35 insertions(+), 103 deletions(-)

diff --git a/t/06_mail_handling.t b/t/06_mail_handling.t
index 9489af01..f0b2b375 100644
--- a/t/06_mail_handling.t
+++ b/t/06_mail_handling.t
@@ -28,7 +28,7 @@ use Encode qw(decode encode);
 $SIG{CHLD} = sub {};
 my %config;
 eval {
-     %config = create_debbugs_configuration(debug => exists $ENV{DEBUG}?$ENV{DEBUG}:0);
+     %config = create_debbugs_configuration();
 };
 if ($@) {
      BAIL_OUT($@);
@@ -38,13 +38,7 @@ my $sendmail_dir = $config{sendmail_dir};
 my $spool_dir = $config{spool_dir};
 my $config_dir = $config{config_dir};
 
-END{
-     if ($ENV{DEBUG}) {
-	  diag("spool_dir:   $spool_dir\n");
-	  diag("config_dir:   $config_dir\n");
-	  diag("sendmail_dir: $sendmail_dir\n");
-     }
-}
+
 
 # We're going to use create mime message to create these messages, and
 # then just send them to receive.
diff --git a/t/07_bugreport.t b/t/07_bugreport.t
index 5dfca050..c1438c27 100644
--- a/t/07_bugreport.t
+++ b/t/07_bugreport.t
@@ -26,21 +26,12 @@ use DebbugsTest qw(:all);
 
 my %config;
 eval {
-     %config = create_debbugs_configuration(debug => exists $ENV{DEBUG}?$ENV{DEBUG}:0);
+     %config = create_debbugs_configuration();
 };
 if ($@) {
      BAIL_OUT($@);
 }
 
-# Output some debugging information if there's an error
-END{
-     if ($ENV{DEBUG}) {
-	  foreach my $key (keys %config) {
-	       diag("$key: $config{$key}\n");
-	  }
-     }
-}
-
 # create a bug
 send_message(to=>'submit@bugs.something',
 	     headers => [To   => 'submit@bugs.something',
diff --git a/t/07_control_limit.t b/t/07_control_limit.t
index 02cfc374..1283223a 100644
--- a/t/07_control_limit.t
+++ b/t/07_control_limit.t
@@ -26,7 +26,7 @@ use Data::Dumper;
 $SIG{CHLD} = sub {};
 my %config;
 eval {
-     %config = create_debbugs_configuration(debug => exists $ENV{DEBUG}?$ENV{DEBUG}:0);
+     %config = create_debbugs_configuration();
 };
 if ($@) {
      BAIL_OUT($@);
@@ -36,13 +36,7 @@ my $sendmail_dir = $config{sendmail_dir};
 my $spool_dir = $config{spool_dir};
 my $config_dir = $config{config_dir};
 
-END{
-     if ($ENV{DEBUG}) {
-	  diag("spool_dir:   $spool_dir\n");
-	  diag("config_dir:   $config_dir\n");
-	  diag("sendmail_dir: $sendmail_dir\n");
-     }
-}
+
 
 # We're going to use create mime message to create these messages, and
 # then just send them to receive.
diff --git a/t/08_pkgreport.t b/t/08_pkgreport.t
index df4861d9..97f331fb 100644
--- a/t/08_pkgreport.t
+++ b/t/08_pkgreport.t
@@ -25,21 +25,12 @@ use DebbugsTest qw(:all);
 
 my %config;
 eval {
-     %config = create_debbugs_configuration(debug => exists $ENV{DEBUG}?$ENV{DEBUG}:0);
+     %config = create_debbugs_configuration();
 };
 if ($@) {
      BAIL_OUT($@);
 }
 
-# Output some debugging information if there's an error
-END{
-     if ($ENV{DEBUG}) {
-	  foreach my $key (keys %config) {
-	       diag("$key: $config{$key}\n");
-	  }
-     }
-}
-
 # create a bug
 send_message(to=>'submit@bugs.something',
 	     headers => [To   => 'submit@bugs.something',
diff --git a/t/09_soap.t b/t/09_soap.t
index 4967a9c8..ccd51edc 100644
--- a/t/09_soap.t
+++ b/t/09_soap.t
@@ -19,21 +19,12 @@ use Cwd;
 
 my %config;
 eval {
-     %config = create_debbugs_configuration(debug => exists $ENV{DEBUG}?$ENV{DEBUG}:0);
+     %config = create_debbugs_configuration();
 };
 if ($@) {
      BAIL_OUT($@);
 }
 
-# Output some debugging information if we're debugging
-END{
-     if ($ENV{DEBUG}) {
-	  foreach my $key (keys %config) {
-	       diag("$key: $config{$key}\n");
-	  }
-     }
-}
-
 # create a bug
 send_message(to=>'submit@bugs.something',
 	     headers => [To   => 'submit@bugs.something',
diff --git a/t/10_expire.t b/t/10_expire.t
index 502feead..fef4094d 100644
--- a/t/10_expire.t
+++ b/t/10_expire.t
@@ -27,7 +27,7 @@ use Data::Dumper;
 $SIG{CHLD} = sub {};
 my %config;
 eval {
-     %config = create_debbugs_configuration(debug => exists $ENV{DEBUG}?$ENV{DEBUG}:0);
+     %config = create_debbugs_configuration();
 };
 if ($@) {
      BAIL_OUT($@);
@@ -37,13 +37,7 @@ my $sendmail_dir = $config{sendmail_dir};
 my $spool_dir = $config{spool_dir};
 my $config_dir = $config{config_dir};
 
-END{
-     if ($ENV{DEBUG}) {
-	  diag("spool_dir:   $spool_dir\n");
-	  diag("config_dir:   $config_dir\n");
-	  diag("sendmail_dir: $sendmail_dir\n");
-     }
-}
+
 
 # We're going to use create mime message to create these messages, and
 # then just send them to receive.
diff --git a/t/11_blocks.t b/t/11_blocks.t
index 11c765ac..5e277cdb 100644
--- a/t/11_blocks.t
+++ b/t/11_blocks.t
@@ -27,7 +27,7 @@ use Test::WWW::Mechanize;
 $SIG{CHLD} = sub {};
 my %config;
 eval {
-     %config = create_debbugs_configuration(debug => exists $ENV{DEBUG}?$ENV{DEBUG}:0);
+     %config = create_debbugs_configuration();
 };
 if ($@) {
      BAIL_OUT($@);
@@ -37,13 +37,7 @@ my $sendmail_dir = $config{sendmail_dir};
 my $spool_dir = $config{spool_dir};
 my $config_dir = $config{config_dir};
 
-END{
-     if ($ENV{DEBUG}) {
-	  diag("spool_dir:   $spool_dir\n");
-	  diag("config_dir:   $config_dir\n");
-	  diag("sendmail_dir: $sendmail_dir\n");
-     }
-}
+
 
 # We're going to use create mime message to create these messages, and
 # then just send them to receive.
diff --git a/t/12_merge.t b/t/12_merge.t
index f07b4e65..4857f143 100644
--- a/t/12_merge.t
+++ b/t/12_merge.t
@@ -26,7 +26,7 @@ use Data::Dumper;
 $SIG{CHLD} = sub {};
 my %config;
 eval {
-     %config = create_debbugs_configuration(debug => exists $ENV{DEBUG}?$ENV{DEBUG}:0);
+     %config = create_debbugs_configuration();
 };
 if ($@) {
      BAIL_OUT($@);
@@ -36,13 +36,7 @@ my $sendmail_dir = $config{sendmail_dir};
 my $spool_dir = $config{spool_dir};
 my $config_dir = $config{config_dir};
 
-END{
-     if ($ENV{DEBUG}) {
-	  diag("spool_dir:   $spool_dir\n");
-	  diag("config_dir:   $config_dir\n");
-	  diag("sendmail_dir: $sendmail_dir\n");
-     }
-}
+
 
 # We're going to use create mime message to create these messages, and
 # then just send them to receive.
diff --git a/t/13_utf8_mail.t b/t/13_utf8_mail.t
index 7ef4aaff..8fb49e41 100644
--- a/t/13_utf8_mail.t
+++ b/t/13_utf8_mail.t
@@ -30,7 +30,7 @@ use Encode qw(decode encode decode_utf8 encode_utf8);
 $SIG{CHLD} = sub {};
 my %config;
 eval {
-     %config = create_debbugs_configuration(debug => exists $ENV{DEBUG}?$ENV{DEBUG}:0);
+     %config = create_debbugs_configuration();
 };
 if ($@) {
      BAIL_OUT($@);
@@ -40,13 +40,7 @@ my $sendmail_dir = $config{sendmail_dir};
 my $spool_dir = $config{spool_dir};
 my $config_dir = $config{config_dir};
 
-END{
-     if ($ENV{DEBUG}) {
-	  diag("spool_dir:   $spool_dir\n");
-	  diag("config_dir:   $config_dir\n");
-	  diag("sendmail_dir: $sendmail_dir\n");
-     }
-}
+
 
 # We're going to use create mime message to create these messages, and
 # then just send them to receive.
diff --git a/t/14_control_at_submit.t b/t/14_control_at_submit.t
index 6688f53c..41c6a037 100644
--- a/t/14_control_at_submit.t
+++ b/t/14_control_at_submit.t
@@ -28,7 +28,7 @@ use Encode qw(decode encode);
 $SIG{CHLD} = sub {};
 my %config;
 eval {
-     %config = create_debbugs_configuration(debug => exists $ENV{DEBUG}?$ENV{DEBUG}:0);
+     %config = create_debbugs_configuration();
 };
 if ($@) {
      BAIL_OUT($@);
@@ -38,13 +38,7 @@ my $sendmail_dir = $config{sendmail_dir};
 my $spool_dir = $config{spool_dir};
 my $config_dir = $config{config_dir};
 
-END{
-     if ($ENV{DEBUG}) {
-	  diag("spool_dir:   $spool_dir\n");
-	  diag("config_dir:   $config_dir\n");
-	  diag("sendmail_dir: $sendmail_dir\n");
-     }
-}
+
 
 # We're going to use create mime message to create these messages, and
 # then just send them to receive.
diff --git a/t/15_rebuild_indexdb.t b/t/15_rebuild_indexdb.t
index a0ca9100..85d76810 100644
--- a/t/15_rebuild_indexdb.t
+++ b/t/15_rebuild_indexdb.t
@@ -29,7 +29,7 @@ use Encode qw(decode encode decode_utf8 encode_utf8);
 $SIG{CHLD} = sub {};
 my %config;
 eval {
-     %config = create_debbugs_configuration(debug => exists $ENV{DEBUG}?$ENV{DEBUG}:0);
+     %config = create_debbugs_configuration();
 };
 if ($@) {
      BAIL_OUT($@);
@@ -39,13 +39,7 @@ my $sendmail_dir = $config{sendmail_dir};
 my $spool_dir = $config{spool_dir};
 my $config_dir = $config{config_dir};
 
-END{
-     if ($ENV{DEBUG}) {
-	  diag("spool_dir:   $spool_dir\n");
-	  diag("config_dir:   $config_dir\n");
-	  diag("sendmail_dir: $sendmail_dir\n");
-     }
-}
+
 
 # We're going to use create mime message to create these messages, and
 # then just send them to receive.
diff --git a/t/18_libravatar_cgi.t b/t/18_libravatar_cgi.t
index 4d20e471..ec938cc3 100644
--- a/t/18_libravatar_cgi.t
+++ b/t/18_libravatar_cgi.t
@@ -16,19 +16,12 @@ my $port = 11344;
 $SIG{CHLD} = sub {};
 our %config;
 eval {
-    %config = create_debbugs_configuration(debug => exists $ENV{DEBUG}?$ENV{DEBUG}:0);
+    %config = create_debbugs_configuration();
 };
 if ($@) {
      BAIL_OUT($@);
  }
 $ENV{DEBBUGS_CONFIG_FILE}  = "$config{config_dir}/debbugs_config";
-END{
-     if ($ENV{DEBUG}) {
-	  diag("spool_dir:   $config{spool_dir}\n");
-	  diag("config_dir:   $config{config_dir}\n");
-	  diag("sendmail_dir: $config{sendmail_dir}\n");
-     }
-}
 
 my $libravatar_cgi_handler = sub {
     my $fh;
diff --git a/t/lib/DebbugsTest.pm b/t/lib/DebbugsTest.pm
index 603b6ecd..6e33399d 100644
--- a/t/lib/DebbugsTest.pm
+++ b/t/lib/DebbugsTest.pm
@@ -57,7 +57,8 @@ BEGIN{
 sub create_debbugs_configuration {
      my %param = validate_with(params => \@_,
 			       spec   => {debug => {type => BOOLEAN,
-						    default => 0,
+						    default => exists $ENV{DEBUG}?
+						    $ENV{DEBUG}:0,
 						   },
 					  cleanup => {type => BOOLEAN,
 						      optional => 1,
@@ -73,6 +74,7 @@ sub create_debbugs_configuration {
      $ENV{DEBBUGS_CONFIG_FILE}  ="$config_dir/debbugs_config";
      $ENV{PERL5LIB} = getcwd();
      $ENV{SENDMAIL_TESTDIR} = $sendmail_dir;
+     eval {
      my $sendmail_tester = getcwd().'/t/sendmail_tester';
      unless (-x $sendmail_tester) {
 	  die q(t/sendmail_tester doesn't exist or isn't executable. You may be in the wrong directory.);
@@ -119,7 +121,17 @@ END
      }
      system('mkdir','-p',"$spool_dir/incoming");
      system('mkdir','-p',"$spool_dir/lock");
+     eval '
+END{
+     if ($ENV{DEBUG}) {
+	  diag("spool_dir:   $spool_dir\n");
+	  diag("config_dir:   $config_dir\n",);
+	  diag("sendmail_dir: $sendmail_dir\n");
+     }
+}';
 
+     };
+     BAIL_OUT ($@) if ($@);
      return (spool_dir => $spool_dir,
 	     sendmail_dir => $sendmail_dir,
 	     config_dir => $config_dir,
@@ -193,6 +205,8 @@ sub send_message{
      }
 }
 
+$SIG{CHLD} = sub {};
+
 {
      package DebbugsTest::HTTPServer;
      use base qw(HTTP::Server::Simple::CGI HTTP::Server::Simple::CGI::Environment);
-- 
2.39.5