From 285b62212ed735411c411bb003d4c3654664edcf Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Fri, 15 May 2009 14:12:36 -0700 Subject: [PATCH] build code, with tests. build works! --- lib/WebGUI/FilePump/Bundle.pm | 55 ++++++++++++++++++++++++++++++++--- t/FilePump/Bundle.t | 46 +++++++++++++++++++++++++++-- 2 files changed, 94 insertions(+), 7 deletions(-) diff --git a/lib/WebGUI/FilePump/Bundle.pm b/lib/WebGUI/FilePump/Bundle.pm index 174ddcbf1..8cb10c2c9 100644 --- a/lib/WebGUI/FilePump/Bundle.pm +++ b/lib/WebGUI/FilePump/Bundle.pm @@ -116,7 +116,7 @@ sub build { return (0, $error) if ($error); ##Create the new build directory - my $newDir = $self->getPathClassBuild($newBuild); + my $newDir = $self->getPathClassDir($newBuild); my $mkpathErrors; my $dirsCreated = $newDir->mkpath({ errors => $mkpathErrors }); if (! $dirsCreated) { @@ -127,12 +127,22 @@ sub build { ##Minimize files, and write them out. - my $minimizedJS = JS::Minimizer::XS::minimize($concatenatedJS); + my $minimizedJS = JavaScript::Minifier::XS::minify($concatenatedJS); undef $concatenatedJS; - my $minimizedCSS = CSS::Minimizer::XS::minimize($concatenatedCSS); + my $minimizedCSS = CSS::Minifier::XS::minify($concatenatedCSS); undef $concatenatedCSS; + my $flatJsFile = $newDir->file($self->bundleUrl . '.js'); + my $jsFH = $flatJsFile->open('>'); + print $jsFH $minimizedJS; + close $jsFH; + + my $flatCssFile = $newDir->file($self->bundleUrl . '.css'); + my $cssFH = $flatCssFile->open('>'); + print $cssFH $minimizedCSS; + close $cssFH; + ##Delete the old build directory and update myself with the new data. $self->deleteBuild(); $self->update({ @@ -286,6 +296,30 @@ sub deleteCollateral { #------------------------------------------------------------------- +=head2 deleteFiles ( $type ) + +Deletes all files of the requested type. + +=head3 $type + +If $type is JS, it deletes it from the javascript part of the bundle. If it is +CSS, it deletes it from the CSS part of the bundle. OTHER is used for all other +types of files. + +=cut + +sub deleteFiles { + my ($self, $type) = @_; + return 0, 'Illegal type' unless WebGUI::Utility::isIn($type, 'JS', 'CSS', 'OTHER'); + my $collateralType = $type eq 'JS' ? 'jsFiles' + : $type eq 'CSS' ? 'cssFiles' + : 'otherFiles'; + $self->update({$collateralType => []}); + return 1; +} + +#------------------------------------------------------------------- + =head2 deleteFile ( $type, $fileId ) Deletes a file of the requested type from the bundle. @@ -458,6 +492,19 @@ sub fetchHttp { #------------------------------------------------------------------- +=head2 bundleUrl ( ) + +Returns a urlized version of the bundle name, safe for URLs and filenames. + +=cut + +sub bundleUrl { + my ($self) = @_; + return $self->session->url->urlize($self->get('bundleName')); +} + +#------------------------------------------------------------------- + =head2 getCollateral ( tableName, keyName, keyValue ) Returns a hash reference containing one row of collateral data from a particular @@ -550,7 +597,7 @@ sub getPathClassDir { return Path::Class::Dir->new( $self->session->config->get('uploadsPath'), 'filepump', - $self->session->url->urlize($self->get('bundleName')) . '.' . $lastBuild + $self->bundleUrl . '.' . $lastBuild ); } diff --git a/t/FilePump/Bundle.t b/t/FilePump/Bundle.t index 7d0c1748c..fa6bcfaf1 100644 --- a/t/FilePump/Bundle.t +++ b/t/FilePump/Bundle.t @@ -24,6 +24,7 @@ use WebGUI::Test; # Must use this before any other WebGUI modules use WebGUI::Session; my $startTime = time(); +my $wgBday = 997966800; #---------------------------------------------------------------------------- # Init @@ -32,7 +33,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 35; # Increment this number for each test you create +my $tests = 45; # Increment this number for each test you create plan tests => 1 + $tests; # 1 for the use_ok #---------------------------------------------------------------------------- @@ -292,21 +293,60 @@ cmp_deeply( '... directory has correct name and timestamp' ); -$dir = $bundle->getPathClassDir(997966800); +$dir = $bundle->getPathClassDir($wgBday); isa_ok($dir, 'Path::Class::Dir'); $timestampDir = $dir->dir_list(-1, 1); cmp_deeply( [ split /\./, $timestampDir ], - [ 'new-bundle', 997966800 ], + [ 'new-bundle', $wgBday ], '... directory has correct name and timestamp when timestamp is specified' ); +################################################################### +# +# deleteFiles +# +################################################################### + +$bundle->deleteFiles('JS'); +$bundle->deleteFiles('CSS'); + +cmp_deeply($bundle->get('jsFiles'), [], ' deleteFiles deleted all JS URIs'); +cmp_deeply($bundle->get('cssFiles'), [], ' ... deleted all CSS URIs'); + ################################################################### # # build # ################################################################### +my $oldBuildDir = $bundle->getPathClassDir($wgBday); +$oldBuildDir->mkpath; + +ok(-e $oldBuildDir->stringify && -d _, 'No problems creating old build directory'); +$bundle->update({lastBuild => $wgBday}); + +$snippet->update({snippet => qq|\n\nfunction doNothing()\n{ var foo = 'bar';} |}); + +$fileAsset->getStorageLocation->deleteFile('pumpfile'); +$fileAsset->getStorageLocation->addFileFromScalar('pumpfile.css', qq| body {\npadding: 0px;}\n\n|); +$fileAsset->update({filename => 'pumpfile.css'}); + +$bundle->addFile('JS', 'asset://filePumpSnippet'); +$bundle->addFile('CSS', 'asset://filePumpFileAsset'); +my ($buildFlag, $error) = $bundle->build(); +ok($buildFlag, 'build returns true when there are no errors'); +isnt($bundle->get('lastBuild'), $wgBday, '... lastBuild time updated'); + +my $buildDir = $bundle->getPathClassDir(); +isnt($buildDir->stringify, $oldBuildDir->stringify, '... build directory did actually change'); +ok(-e $buildDir->stringify && -d _, '... new build directory created'); +ok(!-e $oldBuildDir->stringify && !-d _, '... old build directory deleted'); +my $jsFile = $buildDir->file($bundle->bundleUrl . '.js'); +my $cssFile = $buildDir->file($bundle->bundleUrl . '.css'); +ok(-e $jsFile->stringify && -f _ && -s _, '... minified JS file built, not empty'); +ok(-e $cssFile->stringify && -f _ && -s _, '... minified CSS file built, not empty'); + ################################################################### # # delete