Skip to content
This repository has been archived by the owner on Nov 27, 2018. It is now read-only.

Commit

Permalink
log the error and return an empty string when bundle paths are invalid.
Browse files Browse the repository at this point in the history
Fixes #97
  • Loading branch information
laktek committed Sep 14, 2013
1 parent a3a1bd8 commit 206ee5e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
31 changes: 19 additions & 12 deletions lib/helpers/asset_bundle_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,25 @@ var skip_hosts = [];
var fingerprint = false;

var build_bundle_tag = function(template_tag, bundle) {
var file_extension = PathUtils.getExtension(bundle, null);
var request_basename = PathUtils.getBasename(bundle, file_extension);

var output_path;

if (fingerprint) {
output_path = (request_basename + "-" + bundle_fingerprints[bundle].getTime() + file_extension);
} else {
output_path = (request_basename + file_extension);
}

return Util.format(template_tag, output_path);
var file_extension = PathUtils.getExtension(bundle, null);
var request_basename = PathUtils.getBasename(bundle, file_extension);

var output_path;

if (fingerprint) {
var bundle_fingerprint = bundle_fingerprints[bundle];
if (bundle_fingerprint) {
output_path = (request_basename + "-" + bundle_fingerprint.getTime() + file_extension);
} else {
// bundle path not found. Return an empty string while logging a message.
console.log("Incorrect bundle path. '" + bundle + "' not available.")
return "";
}
} else {
output_path = (request_basename + file_extension);
}

return Util.format(template_tag, output_path);
};

var build_output_file_tag = function(template_tag, bundle_file, source_file) {
Expand Down
32 changes: 30 additions & 2 deletions spec/helpers/asset_bundle_helper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var AssetBundler = require("../../lib/asset_bundler");

describe("stylesheet bundle tag", function(){

it("output the bundled tag when bundling no host is provided and fingerprint is disabled", function(){
it("output the bundled tag when bundling with no host is provided and fingerprint is disabled", function(){
spyOn(AssetBundler, "setup");

spyOn(AssetBundler, "statBundle").andCallFake(function(basename, extension, callback) {
Expand All @@ -19,7 +19,7 @@ describe("stylesheet bundle tag", function(){
expect(AssetBundleHelper.stylesheet_bundle("/assets/all.css")).toEqual("<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"/assets/all.css\">");
});

it("output the bundled tag when bundling no host is provided and fingerprint is enabled", function(){
it("output the bundled tag when bundling with no host is provided and fingerprint is enabled", function(){
spyOn(AssetBundler, "setup");

spyOn(AssetBundler, "statBundle").andCallFake(function(basename, extension, callback) {
Expand Down Expand Up @@ -47,6 +47,20 @@ describe("stylesheet bundle tag", function(){
expect(AssetBundleHelper.stylesheet_bundle("/assets/all.css")).toEqual("<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"/assets/initial.css\">\n<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"/assets/site.css\">");
});

it("output an empty string when bundling an incorrect bundle path", function(){
spyOn(AssetBundler, "setup");

spyOn(AssetBundler, "statBundle").andCallFake(function(basename, extension, callback) {
return callback(null, { "mtime": new Date(2012, 7, 25) });
});

AssetBundleHelperObj.setup( { "asset_bundling": { "skip_hosts": ["localhost", "127.0.0.1", ".local"], "fingerprint": true }, "bundles": { "/assets/all.css": [ "/assets/initial.css", "/assets/site.css" ]} });
var spyCallback = jasmine.createSpy();
AssetBundleHelperObj.get( "/path/test", ".html", { "host": "" }, spyCallback);

expect(AssetBundleHelper.stylesheet_bundle("/assets/invalid_path.css")).toEqual("");
});

});

describe("javascript bundle tag", function(){
Expand Down Expand Up @@ -93,4 +107,18 @@ describe("javascript bundle tag", function(){
expect(AssetBundleHelper.javascript_bundle("/assets/all.js")).toEqual("<script src=\"/assets/jquery.js\"></script>\n<script src=\"/assets/site.js\"></script>");
});

it("output an empty string when bundling an incorrect bundle path", function(){
spyOn(AssetBundler, "setup");

spyOn(AssetBundler, "statBundle").andCallFake(function(basename, extension, callback) {
return callback(null, { "mtime": new Date(2012, 7, 25) });
});

AssetBundleHelperObj.setup( { "asset_bundling": { "skip_hosts": ["localhost", "127.0.0.1", ".local"], "fingerprint": true }, "bundles": { "/assets/all.js": [ "/assets/jquery.js", "/assets/site.coffee" ]} });
var spyCallback = jasmine.createSpy();
AssetBundleHelperObj.get( "/path/test", ".html", { "host": undefined }, spyCallback);

expect(AssetBundleHelper.javascript_bundle("/assets/invalid_path.js")).toEqual("");
});

});

0 comments on commit 206ee5e

Please sign in to comment.