clean up some parts of WebGUI::Test

This commit is contained in:
Graham Knop 2010-05-08 16:35:54 -05:00
parent 7f820d9f35
commit b8aac77501

View file

@ -73,8 +73,6 @@ our @EXPORT_OK = qw(session config);
my $CLASS = __PACKAGE__; my $CLASS = __PACKAGE__;
my @guarded;
sub import { sub import {
our $CONFIG_FILE = $ENV{ WEBGUI_CONFIG }; our $CONFIG_FILE = $ENV{ WEBGUI_CONFIG };
@ -91,7 +89,7 @@ sub import {
my $session = our $SESSION = $CLASS->newSession(1); my $session = our $SESSION = $CLASS->newSession(1);
my $originalSetting = clone $session->setting->get; my $originalSetting = clone $session->setting->get;
push @guarded, Scope::Guard->new(sub { $CLASS->addToCleanup(sub {
while (my ($param, $value) = each %{ $originalSetting }) { while (my ($param, $value) = each %{ $originalSetting }) {
$session->setting->set($param, $value); $session->setting->set($param, $value);
} }
@ -125,7 +123,7 @@ sub import {
my ($label, $table) = @checkCount[$i, $i+1]; my ($label, $table) = @checkCount[$i, $i+1];
$initCounts{$table} = $session->db->quickScalar('SELECT COUNT(*) FROM ' . $table); $initCounts{$table} = $session->db->quickScalar('SELECT COUNT(*) FROM ' . $table);
} }
push @guarded, Scope::Guard->new(sub { $CLASS->addToCleanup(sub {
for ( my $i = 0; $i < @checkCount; $i += 2) { for ( my $i = 0; $i < @checkCount; $i += 2) {
my ($label, $table) = @checkCount[$i, $i+1]; my ($label, $table) = @checkCount[$i, $i+1];
my $quant = $session->db->quickScalar('SELECT COUNT(*) FROM ' . $table); my $quant = $session->db->quickScalar('SELECT COUNT(*) FROM ' . $table);
@ -144,19 +142,6 @@ END {
$CLASS->cleanup; $CLASS->cleanup;
} }
sub cleanup {
# remove guards in reverse order they were added, triggering all of the
# requested cleanup operations
pop @guarded
while @guarded;
if ( our $SESSION ) {
$SESSION->var->end;
$SESSION->close;
undef $SESSION;
}
}
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
=head2 newSession ( $noCleanup ) =head2 newSession ( $noCleanup )
@ -175,7 +160,7 @@ sub newSession {
my $session = WebGUI::Session->open( $CLASS->root, $CLASS->file ); my $session = WebGUI::Session->open( $CLASS->root, $CLASS->file );
$session->{_request} = $pseudoRequest; $session->{_request} = $pseudoRequest;
if ( ! $noCleanup ) { if ( ! $noCleanup ) {
$CLASS->sessionsToDelete($session); $CLASS->addToCleanup($session);
} }
return $session; return $session;
} }
@ -558,7 +543,7 @@ sub prepareMailServer {
# Let it start up yo # Let it start up yo
sleep 2; sleep 2;
push @guarded, Scope::Guard->new(sub { $CLASS->addToCleanup(sub {
# Close SMTPD # Close SMTPD
if ($smtpdPid) { if ($smtpdPid) {
kill INT => $smtpdPid; kill INT => $smtpdPid;
@ -591,7 +576,7 @@ sub originalConfig {
} }
# add cleanup handler if this is the first time we were run # add cleanup handler if this is the first time we were run
if (! keys %originalConfig) { if (! keys %originalConfig) {
push @guarded, Scope::Guard->new(sub { $class->addToCleanup(sub {
while (my ($key, $value) = each %originalConfig) { while (my ($key, $value) = each %originalConfig) {
if (defined $value) { if (defined $value) {
$CLASS->session->config->set($key, $value); $CLASS->session->config->set($key, $value);
@ -607,7 +592,7 @@ sub originalConfig {
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
=head2 getMail ( ) =head2 getMail ( )
Read a sent mail from the prepared mail server (L<prepareMailServer>) Read a sent mail from the prepared mail server (L<prepareMailServer>)
@ -615,7 +600,7 @@ Read a sent mail from the prepared mail server (L<prepareMailServer>)
sub getMail { sub getMail {
my $json; my $json;
if ( !$smtpdSelect ) { if ( !$smtpdSelect ) {
return from_json ' { "error": "mail server not prepared" }'; return from_json ' { "error": "mail server not prepared" }';
} }
@ -626,11 +611,11 @@ sub getMail {
else { else {
$json = ' { "error": "mail not sent" } '; $json = ' { "error": "mail not sent" } ';
} }
if (!$json) { if (!$json) {
$json = ' { "error": "error in getting mail" } '; $json = ' { "error": "error in getting mail" } ';
} }
return from_json( $json ); return from_json( $json );
} }
@ -650,7 +635,7 @@ sub getMailFromQueue {
if ( !$smtpdSelect ) { if ( !$smtpdSelect ) {
$class->prepareMailServer; $class->prepareMailServer;
} }
my $messageId = $CLASS->session->db->quickScalar( "SELECT messageId FROM mailQueue" ); my $messageId = $CLASS->session->db->quickScalar( "SELECT messageId FROM mailQueue" );
warn $messageId; warn $messageId;
return unless $messageId; return unless $messageId;
@ -661,6 +646,7 @@ sub getMailFromQueue {
return $class->getMail; return $class->getMail;
} }
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
=head2 sessionsToDelete ( $session, [$session, ...] ) =head2 sessionsToDelete ( $session, [$session, ...] )
@ -675,7 +661,7 @@ This is a class method.
sub sessionsToDelete { sub sessionsToDelete {
my $class = shift; my $class = shift;
push @guarded, cleanupGuard(@_); $class->addToCleanup(@_);
} }
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
@ -692,7 +678,7 @@ This is a class method.
sub assetsToPurge { sub assetsToPurge {
my $class = shift; my $class = shift;
push @guarded, cleanupGuard(@_); $class->addToCleanup(@_);
} }
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
@ -727,7 +713,7 @@ This is a class method.
sub groupsToDelete { sub groupsToDelete {
my $class = shift; my $class = shift;
push @guarded, cleanupGuard(@_); $class->addToCleanup(@_);
} }
@ -744,7 +730,7 @@ This is a class method.
sub storagesToDelete { sub storagesToDelete {
my $class = shift; my $class = shift;
push @guarded, cleanupGuard(map { $class->addToCleanup(map {
ref $_ ? $_ : ('WebGUI::Storage' => $_) ref $_ ? $_ : ('WebGUI::Storage' => $_)
} @_); } @_);
} }
@ -761,7 +747,7 @@ This is a class method.
sub tagsToRollback { sub tagsToRollback {
my $class = shift; my $class = shift;
push @guarded, cleanupGuard(@_); $class->addToCleanup(@_);
} }
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
@ -777,7 +763,7 @@ This is a class method.
sub usersToDelete { sub usersToDelete {
my $class = shift; my $class = shift;
push @guarded, cleanupGuard(@_); $class->addToCleanup(@_);
} }
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
@ -793,7 +779,7 @@ This is a class method.
sub workflowsToDelete { sub workflowsToDelete {
my $class = shift; my $class = shift;
push @guarded, cleanupGuard(@_); $class->addToCleanup(@_);
} }
@ -1029,12 +1015,26 @@ This is a class method.
=cut =cut
my @guarded;
sub addToCleanup { sub addToCleanup {
shift shift
if eval { $_[0]->isa($CLASS) }; if eval { $_[0]->isa($CLASS) };
push @guarded, cleanupGuard(@_); push @guarded, cleanupGuard(@_);
} }
sub cleanup {
# remove guards in reverse order they were added, triggering all of the
# requested cleanup operations
pop @guarded
while @guarded;
if ( our $SESSION ) {
$SESSION->var->end;
$SESSION->close;
undef $SESSION;
}
}
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
=head1 BUGS =head1 BUGS