]> git.donarmstrong.com Git - debbugs.git/blobdiff - t/lib/DebbugsTest.pm
move Debbugs to lib
[debbugs.git] / t / lib / DebbugsTest.pm
index fcc0dc04b31bb8f6e9d8e5a21ffcefa712804815..463b710b326cee164a2685beba5e24ac486642d4 100644 (file)
@@ -24,6 +24,8 @@ use strict;
 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
 use base qw(Exporter);
 
+use v5.10;
+
 use IO::File;
 use File::Temp qw(tempdir);
 use Cwd qw(getcwd);
@@ -41,8 +43,10 @@ BEGIN{
      $DEBUG = 0 unless defined $DEBUG;
 
      @EXPORT = ();
-     %EXPORT_TAGS = (configuration => [qw(dirsize create_debbugs_configuration send_message)],
+     %EXPORT_TAGS = (configuration => [qw(dirsize create_debbugs_configuration send_message),
+                                      qw(submit_bug run_processall)],
                     mail          => [qw(num_messages_sent)],
+                    control       => [qw(test_control_commands)],
                     database => [qw(create_postgresql_database update_postgresql_database)]
                    );
      @EXPORT_OK = ();
@@ -74,7 +78,7 @@ sub create_debbugs_configuration {
 
 
      $ENV{DEBBUGS_CONFIG_FILE}  ="$config_dir/debbugs_config";
-     $ENV{PERL5LIB} = getcwd();
+     $ENV{PERL5LIB} = getcwd().'/lib/';
      $ENV{SENDMAIL_TESTDIR} = $sendmail_dir;
      eval {
      my $sendmail_tester = getcwd().'/t/sendmail_tester';
@@ -123,6 +127,9 @@ END
      }
      system('mkdir','-p',"$spool_dir/incoming");
      system('mkdir','-p',"$spool_dir/lock");
+     # generate the maintainers index files
+     system('scripts/maintainer-indices') == 0
+        or die "Unable to generate maintainer index files";
      eval '
 END{
      if ($ENV{DEBUG}) {
@@ -203,10 +210,132 @@ sub send_message{
      }
      # now we should run processall to see if the message gets processed
      if ($param{run_processall}) {
-         system('scripts/processall') == 0 or die "processall failed";
-     }
+        run_processall();
+      }
+     return 1;
+}
+
+sub run_processall {
+    system('scripts/processall') == 0 or die "processall failed";
 }
 
+=item test_control_commands
+
+ test_control_commands(\%config,
+                       forcemerge => {command => 'forcemerge',
+                                      value   => '1 2',
+                                      status_key => 'mergedwith',
+                                      status_value => '2',
+                                      expect_error => 0,
+                                     });
+
+Test a set of control commands to see if they will fail or not. Takes
+SCALAR/HASHREF pairs, where the scalar should be unique, and the HASHREF
+contains the following keys:
+
+=over
+
+=item command -- control command to issue
+
+=item value -- value to pass to control command
+
+=item status_key -- bug status key to check
+
+=item status_value -- value of status key
+
+=item expect_error -- whether to expect the control command to error or not
+
+=back
+
+=cut
+
+sub test_control_commands {
+    my ($config,@commands) = @_;
+
+    # now we need to check to make sure that the control message actually did anything
+    # This is an eval because $ENV{DEBBUGS_CONFIG_FILE} isn't set at BEGIN{} time
+    eval "use Debbugs::Status qw(read_bug writebug);";
+    while (my ($command,$control_command) = splice(@commands,0,2)) {
+       # just check to see that control doesn't explode
+       $control_command->{value} = " $control_command->{value}" if length $control_command->{value}
+           and $control_command->{value} !~ /^\s/;
+       send_message(to => 'control@bugs.something',
+                    headers => [To   => 'control@bugs.something',
+                                From => 'foo@bugs.something',
+                                Subject => "Munging a bug with $command",
+                               ],
+                    body => <<EOF) or fail 'message to control@bugs.something failed';
+debug 10
+$control_command->{command} $control_command->{value}
+thanks
+EOF
+       ;
+       # now we need to check to make sure the control message was processed without errors
+       if (not ($control_command->{expect_error} // 0)) {
+           ok(system('sh','-c','find '.$config->{sendmail_dir}.
+                     q( -type f | xargs grep -q "Subject: Processed: Munging a bug with $command")
+                    ) == 0,
+              'control@bugs.something'. "$command message was parsed without errors");
+       }
+       # now we need to check to make sure that the control message actually did anything
+       my $status;
+       $status = read_bug(exists $control_command->{bug}?(bug => $control_command->{bug}):(bug=>1),
+                          exists $control_command->{location}?(location => $control_command->{location}):(),
+                         );
+       is_deeply($status->{$control_command->{status_key}},
+                 $control_command->{status_value},
+                 "bug " .
+                 (exists $control_command->{bug}?$control_command->{bug}:1).
+                 " $command"
+                )
+           or fail(Data::Dumper->Dump([$status],[qw(status)]));
+    }
+}
+
+sub submit_bug {
+    state $spec =
+       {subject => {type => SCALAR,
+                   default => 'Submitting a bug',
+                  },
+       body => {type => SCALAR,
+                default => 'This is a silly bug',
+               },
+       submitter => {type => SCALAR,
+                     default => 'foo@bugs.something',
+                    },
+       pseudoheaders => {type => HASHREF,
+                         default => sub {{}},
+                        },
+       package => {type => SCALAR,
+                   default => 'foo',
+                  },
+       run_processall => {type => SCALAR,
+                          default => 0,
+                         },
+       };
+    my %param =
+       validate_with(params => \@_,
+                     spec => $spec);
+    my $body = 'Package: '.$param{package}."\n";
+    foreach my $key (keys %{$param{pseudoheaders}}) {
+       for my $val (ref($param{pseudoheaders}{$key}) ?
+                    @{$param{pseudoheaders}{$key}} :
+                    $param{pseudoheaders}{$key}) {
+           $body .= $key. ': '.$val."\n";
+       }
+    }
+    $body .="\n".$param{body};
+    send_message(to => 'submit@bugs.something',
+                headers => [To => 'submit@bugs.something',
+                            From => $param{submitter},
+                            Subject => $param{subject},
+                           ],
+                run_processall => $param{run_processall},
+                body => $body
+               );
+}
+
+
 {
      package DebbugsTest::HTTPServer;
      use base qw(HTTP::Server::Simple::CGI HTTP::Server::Simple::CGI::Environment);