Skip to content
Permalink
Browse files

FIX making minify javascript fail-safe

  • Loading branch information...
igor-silverstripe committed Aug 24, 2014
1 parent afa6612 commit f823831a639c0b0aa6f7c15550dc011832ad601e
Showing with 59 additions and 1 deletion.
  1. +51 −0 tests/view/SSViewerTest.php
  2. +1 −0 tests/view/themes/javascript/bad.js
  3. +7 −1 view/Requirements.php
@@ -103,6 +103,57 @@ public function testRequirements() {
<% require css($cssFile) %>");
$this->assertFalse((bool)trim($template), "Should be no content in this return.");
}
public function testRequirementsCombine(){
$oldBackend = Requirements::backend();
$testBackend = new Requirements_Backend();
Requirements::set_backend($testBackend);
$combinedTestFilePath = BASE_PATH . '/' . $testBackend->getCombinedFilesFolder() . '/testRequirementsCombine.js';
$jsFile = FRAMEWORK_DIR . '/tests/view/themes/javascript/bad.js';
$jsFileContents = file_get_contents(BASE_PATH . '/' . $jsFile);
Requirements::combine_files('testRequirementsCombine.js', array($jsFile));
require_once('thirdparty/jsmin/jsmin.php');
// first make sure that our test js file causes an exception to be thrown
try{
$content = JSMin::minify($content);
$this->fail('JSMin did not throw exception on minify bad file: ');
Requirements::set_backend($oldBackend);
return;
}catch(Exception $e){
// exception thrown... good
}
// secondly, make sure that requirements combine can handle this and continue safely
@unlink($combinedTestFilePath);
try{
/*
* Use @ (ignore warning) because a warning would cause php unit to throw an exception and therefore change the execution
* process and mess up the next test.
*/
@Requirements::process_combined_files();
}catch(Exception $e){
$this->fail('Requirements::process_combined_files did not catch exception caused by minifying bad js file: '.$e);
Requirements::set_backend($oldBackend);
return;
}
// and make sure the combined content matches the input content, i.e. no loss of functionality
if(!file_exists($combinedTestFilePath)){
$this->fail('No combined file was created at expected path: '.$combinedTestFilePath);
Requirements::set_backend($oldBackend);
return;
}
$combinedTestFileContents = file_get_contents($combinedTestFilePath);
$this->assertContains($jsFileContents, $combinedTestFileContents);
// reset
Requirements::set_backend($oldBackend);
}
public function testComments() {
$output = $this->render(<<<SS
@@ -0,0 +1 @@
this is a javascript file which should cause an exception to be thrown when minified by jsmin '
@@ -1132,7 +1132,13 @@ public function process_combined_files() {
$combinedData = "";
foreach(array_diff($fileList, $this->blocked) as $file) {
$fileContent = file_get_contents($base . $file);
$fileContent = $this->minifyFile($file, $fileContent);
try{
$fileContent = $this->minifyFile($file, $fileContent);
}catch(Exception $e){
// failed to minify, use unminified
user_error('Failed to minify '.$file.', exception: '.$e->getMessage(), E_USER_WARNING);
}
if ($this->write_header_comment) {
// write a header comment for each file for easier identification and debugging

0 comments on commit f823831

Please sign in to comment.
You can’t perform that action at this time.