From 9b725c6d42d02334310c80f0ef61026162ce35ef Mon Sep 17 00:00:00 2001 From: Graham Knop Date: Thu, 10 Jun 2010 09:48:18 -0500 Subject: [PATCH] use temp config file for testing --- t/AdSpace.t | 9 +- t/Asset/Asset.t | 4 - t/Asset/AssetExportHtml.t | 3 - t/Asset/Snippet.t | 1 - t/Asset/Template/HTMLTemplateExpr.t | 1 - t/Asset/Wobject/Survey/ExpressionEngine.t | 1 - t/Asset/Wobject/Survey/ResponseJSON.t | 1 - t/Asset/Wobject/Survey/Test.t | 1 - t/AssetAspect/RssFeed.t | 2 - t/Cache/CHI.t | 2 - t/Group/resetGroupFields.t | 1 - t/Macro.t | 1 - t/Operation/User/service.t | 1 - t/Session/Url.t | 8 +- t/Storage.t | 1 - t/User.t | 2 - t/i18n/template.t | 1 - t/lib/WebGUI/Test.pm | 100 ++++++++++------------ 18 files changed, 47 insertions(+), 93 deletions(-) diff --git a/t/AdSpace.t b/t/AdSpace.t index a15488659..d92c109d2 100644 --- a/t/AdSpace.t +++ b/t/AdSpace.t @@ -14,6 +14,7 @@ use lib "$FindBin::Bin/lib"; use WebGUI::Test; use WebGUI::Session; use WebGUI::AdSpace::Ad; +use WebGUI::AdSpace; use Test::More; use Test::Deep; @@ -30,20 +31,13 @@ my $newAdSpaceSettings = { my $numTests = 35; # increment this value for each test you create $numTests += 2 * scalar keys %{ $newAdSpaceSettings }; -++$numTests; ##For conditional testing on module load plan tests => $numTests; -my $loaded = use_ok('WebGUI::AdSpace'); - my $session = WebGUI::Test->session; my ($adSpace, $alfred, $alfred2, $bruce, $catWoman, $villianClone, $defaultAdSpace ); my ($jokerAd, $penguinAd, $twoFaceAd); -SKIP: { - - skip "Unable to load WebGUI::AdSpace", $numTests-1 unless $loaded; - local $ENV{REMOTE_ADDR} = '10.0.0.1'; local $ENV{HTTP_USER_AGENT} = 'Mozilla/5.0'; @@ -223,7 +217,6 @@ SKIP: { $bruce->displayImpression(); ($jokerActive) = $session->db->quickArray('select isActive from advertisement where adId=?',[$jokerAd->getId]); is($jokerActive, 0, 'displayImpression deactivated jokerAd after two impressions'); -} END { foreach my $ad_space ($adSpace, $bruce, $alfred, $alfred2, $catWoman, $defaultAdSpace ) { diff --git a/t/Asset/Asset.t b/t/Asset/Asset.t index e1f560f00..0a311ac15 100644 --- a/t/Asset/Asset.t +++ b/t/Asset/Asset.t @@ -372,10 +372,6 @@ is($importNode->fixUrl('1234'.'_'x235 . 'abcdefghij'), '1234'.'_'x235 . 'abcdefg is($importNode->fixUrl('1234'.'_'x250 . 'abcdefghij'), '1234'.'_'x216, 'fixUrl truncates long URLs over 250 characters to 220 characters'); is $importNode->fixUrl('---'), '-', '... 3 dashes are collapsed down to a single dash'; -WebGUI::Test->originalConfig('extrasURL'); -WebGUI::Test->originalConfig('uploadsURL'); -WebGUI::Test->originalConfig('assets'); - $session->config->set('extrasURL', '/extras'); $session->config->set('uploadsURL', '/uploads'); diff --git a/t/Asset/AssetExportHtml.t b/t/Asset/AssetExportHtml.t index 1fe9bed6a..ce08e16c8 100644 --- a/t/Asset/AssetExportHtml.t +++ b/t/Asset/AssetExportHtml.t @@ -17,7 +17,6 @@ use strict; use lib "$FindBin::Bin/../lib"; use Test::More; use WebGUI::Test; # Must use this before any other WebGUI modules -use WebGUI::PseudoRequest; use WebGUI::Session; use WebGUI::Asset; @@ -37,8 +36,6 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -WebGUI::Test->originalConfig('exportPath'); - my $testRan = 1; plan tests => 126; # Increment this number for each test you create diff --git a/t/Asset/Snippet.t b/t/Asset/Snippet.t index 5449c3315..0a1415ce8 100644 --- a/t/Asset/Snippet.t +++ b/t/Asset/Snippet.t @@ -79,7 +79,6 @@ $snippet->update({ snippet => q|^SQL(select value from settings where name="");| }); -WebGUI::Test->originalConfig('macros'); $session->config->addToHash('macros', 'SQL', 'SQL'); is($snippet->view(), 'WebGUI', 'Interpolating macros in works with template in the correct order'); diff --git a/t/Asset/Template/HTMLTemplateExpr.t b/t/Asset/Template/HTMLTemplateExpr.t index b8b31da3e..ef61dd777 100644 --- a/t/Asset/Template/HTMLTemplateExpr.t +++ b/t/Asset/Template/HTMLTemplateExpr.t @@ -27,7 +27,6 @@ plan tests => 2 + $num_tests; my $session = WebGUI::Test->session; # put your tests here -WebGUI::Test->originalConfig('templateParsers'); my $module = use_ok('HTML::Template::Expr'); my $plugin = use_ok('WebGUI::Asset::Template::HTMLTemplateExpr'); diff --git a/t/Asset/Wobject/Survey/ExpressionEngine.t b/t/Asset/Wobject/Survey/ExpressionEngine.t index 0d1edcecd..a901adf64 100644 --- a/t/Asset/Wobject/Survey/ExpressionEngine.t +++ b/t/Asset/Wobject/Survey/ExpressionEngine.t @@ -36,7 +36,6 @@ SKIP: { my $e = "WebGUI::Asset::Wobject::Survey::ExpressionEngine"; - WebGUI::Test->originalConfig('enableSurveyExpressionEngine'); $session->config->set( 'enableSurveyExpressionEngine', 0 ); is( $e->run( $session, 'jump { 1 } target' ), undef, "Nothing happens unless we turn on enableSurveyExpressionEngine in config" ); diff --git a/t/Asset/Wobject/Survey/ResponseJSON.t b/t/Asset/Wobject/Survey/ResponseJSON.t index 3b31bccc2..1b40ea0df 100644 --- a/t/Asset/Wobject/Survey/ResponseJSON.t +++ b/t/Asset/Wobject/Survey/ResponseJSON.t @@ -336,7 +336,6 @@ cmp_deeply($rJSON->responseScores(indexBy => 'variable'), { s1q0 => 100, s1q1 => # #################################################### # Turn on the survey Expression Engine -WebGUI::Test->originalConfig('enableSurveyExpressionEngine'); $session->config->set('enableSurveyExpressionEngine', 1); $rJSON->survey->section([0])->{variable} = 's0'; $rJSON->survey->question([0,0])->{variable} = 's0q0'; # surveyOrder index = 0 diff --git a/t/Asset/Wobject/Survey/Test.t b/t/Asset/Wobject/Survey/Test.t index d4eb6d5ee..dfdb1919d 100644 --- a/t/Asset/Wobject/Survey/Test.t +++ b/t/Asset/Wobject/Survey/Test.t @@ -40,7 +40,6 @@ my $user = WebGUI::User->new( $session, 'new' ); WebGUI::Test->addToCleanup($user); my $import_node = WebGUI::Asset->getImportNode($session); -WebGUI::Test->originalConfig('enableSurveyExpressionEngine'); $session->config->set('enableSurveyExpressionEngine', 1); # Create a Survey diff --git a/t/AssetAspect/RssFeed.t b/t/AssetAspect/RssFeed.t index 43686bfdd..0c950b983 100644 --- a/t/AssetAspect/RssFeed.t +++ b/t/AssetAspect/RssFeed.t @@ -31,8 +31,6 @@ use WebGUI::Asset::RssAspectDummy; # Init my $session = WebGUI::Test->session; -WebGUI::Test->originalConfig('exportPath'); - #---------------------------------------------------------------------------- # Tests diff --git a/t/Cache/CHI.t b/t/Cache/CHI.t index 1299df88c..8f5a87fce 100644 --- a/t/Cache/CHI.t +++ b/t/Cache/CHI.t @@ -34,8 +34,6 @@ plan tests => 3; # Increment this number for each test you create # put your tests here use_ok( 'WebGUI::Cache::CHI' ); -WebGUI::Test->originalConfig('cacheType'); -WebGUI::Test->originalConfig('cache'); $session->config->set('cacheType', 'WebGUI::Cache::CHI'); $session->config->set('cache', { driver => 'FastMmap', }); diff --git a/t/Group/resetGroupFields.t b/t/Group/resetGroupFields.t index 86227a08d..ff5ef5fdc 100644 --- a/t/Group/resetGroupFields.t +++ b/t/Group/resetGroupFields.t @@ -91,7 +91,6 @@ my $workflow = WebGUI::Workflow->create($session, ); WebGUI::Test->addToCleanup($workflow); -WebGUI::Test->originalConfig('workflowActivities'); $session->config->addToArray('workflowActivities/User', 'WebGUI::Workflow::Activity::AddUserToGroup'); my $userActivity = $workflow->addActivity('WebGUI::Workflow::Activity::AddUserToGroup'); diff --git a/t/Macro.t b/t/Macro.t index d0aecdec2..230d083c5 100644 --- a/t/Macro.t +++ b/t/Macro.t @@ -34,7 +34,6 @@ $registeredUser->username('TimBob'); WebGUI::Test->addToCleanup($registeredUser); $session->user({user => $registeredUser}); -WebGUI::Test->originalConfig('macros'); ##Overwrite any local configuration so that we know how to call it. foreach my $macro (qw/ GroupText LoginToggle PageTitle MacroStart MacroEnd MacroNest diff --git a/t/Operation/User/service.t b/t/Operation/User/service.t index 23494198c..632e72931 100644 --- a/t/Operation/User/service.t +++ b/t/Operation/User/service.t @@ -29,7 +29,6 @@ use Data::Dumper; #---------------------------------------------------------------------------- # Init my $session = WebGUI::Test->session; -WebGUI::Test->originalConfig( "serviceSubnets" ); $session->config->delete('serviceSubnets'); my ( $response, $responseObj, $auth, $userAndy, $userRed ); diff --git a/t/Session/Url.t b/t/Session/Url.t index 8c5d1d838..0bd50342d 100644 --- a/t/Session/Url.t +++ b/t/Session/Url.t @@ -85,7 +85,6 @@ is( $url2, $url.'?a=b;c=d', 'append second pair'); # ####################################### -WebGUI::Test->originalConfig('gateway'); $session->config->set('gateway', '/'); is( $session->config->get('gateway'), '/', 'Set gateway for downstream tests'); @@ -131,7 +130,6 @@ my $setting_hostToUse = $session->setting->get('hostToUse'); $session->setting->set('hostToUse', 'HTTP_HOST'); my $sitename = $session->config->get('sitename')->[0]; is( $session->url->getSiteURL, 'http://'.$sitename, 'getSiteURL from config as http_host'); -WebGUI::Test->originalConfig('webServerPort'); $session->url->setSiteURL('http://webgui.org'); is( $session->url->getSiteURL, 'http://webgui.org', 'override config setting with setSiteURL'); @@ -151,7 +149,6 @@ $mockEnv{HTTP_HOST} = "devsite.com"; $session->url->setSiteURL(undef); is( $session->url->getSiteURL, 'http://'.$sitename, 'getSiteURL where requested host is not a configured site'); -WebGUI::Test->originalConfig('sitename'); $session->config->addToArray('sitename', 'devsite.com'); $session->url->setSiteURL(undef); is( $session->url->getSiteURL, 'http://devsite.com', 'getSiteURL where requested host is not the first configured site'); @@ -271,9 +268,8 @@ is($session->url->makeAbsolute('page1'), '/page1', 'makeAbsolute: default baseUr # ####################################### -my $extras = WebGUI::Test->originalConfig('extrasURL'); +my $extras = $session->config->get('extrasURL'); -WebGUI::Test->originalConfig('cdn'); $session->config->delete('cdn'); is($session->url->extras, $extras.'/', 'extras method returns URL to extras with a trailing slash'); @@ -430,8 +426,6 @@ is( # ####################################### -WebGUI::Test->originalConfig('sslEnabled'); - ##Test all the false cases, first $session->config->set('sslEnabled', 0); diff --git a/t/Storage.t b/t/Storage.t index 0aa25f82f..57527d517 100644 --- a/t/Storage.t +++ b/t/Storage.t @@ -91,7 +91,6 @@ is($guidStorage->getDirectoryId, $newGuid, '... getDirectoryId'); # #################################################### -WebGUI::Test->originalConfig('cdn'); $session->config->delete('cdn'); # Note: the CDN configuration will be reverted after CDN tests below diff --git a/t/User.t b/t/User.t index e7aa39d19..efe6a063e 100644 --- a/t/User.t +++ b/t/User.t @@ -483,7 +483,6 @@ $dude->deleteFromGroups([12]); my $origEnvHash = $session->env->{_env}; my %newEnv = ( REMOTE_ADDR => '194.168.0.2' ); $session->env->{_env} = \%newEnv; -WebGUI::Test->originalConfig('adminModeSubnets'); $session->config->set('adminModeSubnets', ['194.168.0.0/24']); ok(!$dude->isInGroup(12), 'user is not in group 12'); @@ -835,7 +834,6 @@ isa_ok( $newCreateUser, 'WebGUI::User', 'create() returns a WebGUI::User' ); ################################################################ $session->setting->set('preventProxyCache', 0); -WebGUI::Test->originalConfig('profileModuleIdentifier'); my $profileModuleId = $session->config->get('profileModuleIdentifier'); is( $newFish->getProfileUrl('cellblock'), diff --git a/t/i18n/template.t b/t/i18n/template.t index cc5c365c4..c2c763e4d 100644 --- a/t/i18n/template.t +++ b/t/i18n/template.t @@ -31,7 +31,6 @@ my $session = WebGUI::Test->session; my $lib = WebGUI::Test->lib; ## Remove all macros but International, and set them to call WebGUI::Macro::Callback -WebGUI::Test->originalConfig('macros'); my $originalMacros = $session->config->get('macros'); my $configFileMacros = {}; for my $macro (keys %$originalMacros) { diff --git a/t/lib/WebGUI/Test.pm b/t/lib/WebGUI/Test.pm index 4a326ee06..b1ab5beba 100644 --- a/t/lib/WebGUI/Test.pm +++ b/t/lib/WebGUI/Test.pm @@ -30,23 +30,24 @@ use Test::MockObject; use Test::MockObject::Extends; use Log::Log4perl; # load early to ensure proper order of END blocks use Clone qw(clone); -use IO::Handle (); -use File::Spec (); +use File::Basename qw(dirname); +use File::Spec::Functions qw(abs2rel rel2abs catdir catfile updir); use IO::Select (); -use Cwd (); use Scalar::Util qw( blessed ); use List::MoreUtils qw( any ); use Carp qw( carp croak ); use JSON qw( from_json to_json ); +use File::Copy (); +use File::Temp (); use Scope::Guard; +our ($WEBGUI_TEST_ROOT, $WEBGUI_TEST_COLLATERAL, $WEBGUI_ROOT, $WEBGUI_LIB); + BEGIN { -our $WEBGUI_TEST_ROOT = File::Spec->catdir( - File::Spec->catpath((File::Spec->splitpath(__FILE__))[0,1], ''), - (File::Spec->updir) x 2 -); -our $WEBGUI_TEST_COLLATERAL = File::Spec->catdir( +our $WEBGUI_TEST_ROOT = rel2abs( catdir( dirname( __FILE__ ), (updir) x 2 ) ); + +our $WEBGUI_TEST_COLLATERAL = catdir( $WEBGUI_TEST_ROOT, 'supporting_collateral' ); @@ -85,20 +86,30 @@ our @EXPORT_OK = qw(session config collateral); my $CLASS = __PACKAGE__; +my $original_config_file; sub import { - our $CONFIG_FILE = $ENV{ WEBGUI_CONFIG }; + if ( ! $original_config_file ) { + my $config = $ENV{WEBGUI_CONFIG}; + die "Enviroment variable WEBGUI_CONFIG must be set to the full path to a WebGUI config file.\n" + unless $config; - die "Enviroment variable WEBGUI_CONFIG must be set to the full path to a WebGUI config file.\n" - unless $CONFIG_FILE; - die "WEBGUI_CONFIG path '$CONFIG_FILE' does not exist.\n" - unless -e $CONFIG_FILE; - die "WEBGUI_CONFIG path '$CONFIG_FILE' is not a file.\n" - unless -f _; - die "WEBGUI_CONFIG path '$CONFIG_FILE' is not readable by effective uid '$>'.\n" - unless -r _; - - $CONFIG_FILE = File::Spec->abs2rel($CONFIG_FILE, File::Spec->catdir($CLASS->root, 'etc')); + for my $tryPath ( + rel2abs( $config ), + rel2abs( $config, $CLASS->root, 'etc' ) + ) { + if ( -e $tryPath ) { + $config = $tryPath; + } + } + die "WEBGUI_CONFIG path '$config' does not exist.\n" + unless -e $config; + die "WEBGUI_CONFIG path '$config' is not a file.\n" + unless -f _; + die "WEBGUI_CONFIG path '$config' is not readable by effective uid '$>'.\n" + unless -r _; + $original_config_file = abs2rel( $config, catdir( $CLASS->root, 'etc') ); + } goto &{ $_[0]->can('SUPER::import') }; } @@ -347,7 +358,7 @@ sub config { return $config if $config; require WebGUI::Config; - $config = WebGUI::Config->new(our $CONFIG_FILE); + $config = WebGUI::Config->new($CLASS->file, 1); return $config; } @@ -359,8 +370,20 @@ Returns the name of the WebGUI config file used for this test. =cut +my $config_copy; sub file { - return our $CONFIG_FILE; + return $config_copy + if $config_copy; + my $config_base = $original_config_file; + $config_base =~ s/\.conf$//; + (undef, $config_copy) = File::Temp::tempfile("$config_base-XXXX", SUFFIX => '.conf', UNLINK => 0, OPEN => 0); + File::Copy::copy($original_config_file, $config_copy); + $CLASS->addToCleanup(sub { + unlink $config_copy; + undef $config_copy; + undef $config; + }); + return $config_copy; } #---------------------------------------------------------------------------- @@ -449,7 +472,7 @@ Optionally adds a filename to the end. sub getTestCollateralPath { my $class = shift; my @path = @_; - return File::Spec->catfile(our $WEBGUI_TEST_COLLATERAL, @path); + return catfile(our $WEBGUI_TEST_COLLATERAL, @path); } sub collateral { @@ -567,7 +590,6 @@ sub prepareMailServer { $CLASS->session->setting->set( 'smtpServer', $SMTP_HOST . ':' . $SMTP_PORT ); - $CLASS->originalConfig('emailToLog'); $CLASS->session->config->set( 'emailToLog', 0 ); # Let it start up yo @@ -590,38 +612,6 @@ sub prepareMailServer { #---------------------------------------------------------------------------- -=head2 originalConfig ( $param ) - -Stores the original data from the config file, to be restored -automatically at the end of the test. This is a class method. - -=cut - -my %originalConfig; -sub originalConfig { - my ($class, $param) = @_; - my $safeValue = my $value = $CLASS->session->config->get($param); - if (ref $value) { - $safeValue = clone $value; - } - # add cleanup handler if this is the first time we were run - if (! keys %originalConfig) { - $class->addToCleanup(sub { - while (my ($key, $value) = each %originalConfig) { - if (defined $value) { - $CLASS->session->config->set($key, $value); - } - else { - $CLASS->session->config->delete($key); - } - } - }); - } - $originalConfig{$param} = $safeValue; -} - -#---------------------------------------------------------------------------- - =head2 getMail ( ) Read a sent mail from the prepared mail server (L)