FIX making minify javascript fail-safe

This commit is contained in:
Igor Nadj 2014-08-25 11:49:38 +12:00
parent afa6612074
commit f823831a63
3 changed files with 59 additions and 1 deletions

View File

@ -103,6 +103,57 @@ class SSViewerTest extends SapphireTest {
<% 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

View File

@ -0,0 +1 @@
this is a javascript file which should cause an exception to be thrown when minified by jsmin '

View File

@ -1132,7 +1132,13 @@ class Requirements_Backend {
$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