Permalink
Browse files

handle empty basepaths and path being null issues in template handler

  • Loading branch information...
1 parent dd38eb2 commit dc428bbec52a6d0fa75baea7efd31a790873ecca @laktek committed Mar 17, 2013
Showing with 71 additions and 3 deletions.
  1. +31 −3 lib/template_handler.js
  2. +40 −0 spec/template_handler.spec.js
View
@@ -15,6 +15,10 @@ module.exports = {
isSection: function(template_path) {
var self = this;
+ if(!template_path) {
+ return false;
+ }
+
var path_portions = template_path.split(path.sep || "/");
if(_.any(path_portions, function(portion){ return portion[0] === "."; })){
return false;
@@ -35,6 +39,10 @@ module.exports = {
getTemplate: function(template_path, callback) {
var self = this;
+ if(!template_path) {
+ return callback("template path can't be null", null);
+ }
+
fs.stat(path.join(self.templateDir, template_path), function(err, stat){
if(err){
return callback(err, null);
@@ -52,10 +60,14 @@ module.exports = {
getTemplates: function(basepath, callback) {
var self = this;
+ if(!basepath) {
+ return callback("basepath can't be null", null);
+ }
+
var filter_dir = function(dirpath, filter, last_attempt){
var absolute_dirpath = path.join(self.templateDir, dirpath);
fs.readdir(absolute_dirpath, function(err, files){
- if(!err){
+ if(!err) {
var templates = [];
@@ -95,6 +107,10 @@ module.exports = {
readTemplate: function(template_path, callback) {
var self = this;
+ if(!template_path) {
+ return callback("template path can't be null", null);
+ }
+
fs.readFile(path.join(self.templateDir, template_path), "binary", function(err, template_output){
if(err){
return callback(err, null);
@@ -108,6 +124,10 @@ module.exports = {
negotiateTemplate: function(basepath, output_extension, template_extension, options, callback) {
var self = this;
+ if(!basepath) {
+ return callback("basepath can't be null", null, null);
+ }
+
var read_template_or_layout = function(base_file_path, extension) {
var full_file_path = base_file_path + extension;
@@ -157,6 +177,10 @@ module.exports = {
getPartials: function(basepath, extension, options, callback) {
var self = this;
+ if(!basepath) {
+ return callback("basepath can't be null", null, null);
+ }
+
var last_modified = null;
var collected_partials = {};
@@ -209,7 +233,11 @@ module.exports = {
var directories_to_look = [];
_.each(basepath.split("/"), function(current_dir_entry){
- var previous_dir_entry = directories_to_look[directories_to_look.length - 1];
+ if(directories_to_look.length) {
+ var previous_dir_entry = directories_to_look[directories_to_look.length - 1];
+ } else {
+ var previous_dir_entry = "";
+ }
directories_to_look.push(path.join(previous_dir_entry, current_dir_entry));
});
@@ -235,7 +263,7 @@ module.exports = {
};
var traverse_path = function(){
- var current_path = paths_to_traverse.shift();
+ var current_path = paths_to_traverse.shift() || "";
fs.readdir(path.join(self.templateDir, current_path), function(err, entries){
if(err){
throw err;
@@ -11,6 +11,10 @@ describe("setup", function() {
describe("check for sections", function(){
+ it("return false if the path is null", function(){
+ expect(default_handler.isSection(null)).not.toBeTruthy();
+ });
+
it("return true if the given path is a directory", function(){
spyOn(fs, "statSync").andCallFake(function(path){
return {"isDirectory": function(){ return true } };
@@ -40,6 +44,13 @@ describe("check for sections", function(){
describe("get template", function(){
+ it("call the callback with an error if the template path is null", function(){
+ var spyCallback = jasmine.createSpy();
+ default_handler.getTemplate(null, spyCallback);
+
+ expect(spyCallback).toHaveBeenCalledWith("template path can't be null", null);
+ });
+
it("check if the template available", function(){
spyOn(fs, "stat");
@@ -99,6 +110,13 @@ describe("get template", function(){
describe("get templates", function(){
+ it("call the callback with the error if the basepath is null", function(){
+ var spyCallback = jasmine.createSpy();
+ default_handler.getTemplates(null, spyCallback);
+
+ expect(spyCallback).toHaveBeenCalledWith("basepath can't be null", null);
+ });
+
it("check if the given path is directory", function(){
spyOn(fs, "readdir");
@@ -180,6 +198,14 @@ describe("get templates", function(){
describe("read template", function(){
+ it("calls the callback with the error if template path is null", function(){
+ var spyCallback = jasmine.createSpy();
+ default_handler.readTemplate(null, spyCallback);
+
+ expect(spyCallback).toHaveBeenCalledWith("template path can't be null", null);
+ });
+
+
it("reads the template from the filesystem", function(){
spyOn(fs, "readFile");
@@ -224,6 +250,13 @@ describe("read template", function(){
describe("negotiate template", function() {
+ it("call the callback with the error if basepath is null", function() {
+ var spyCallback = jasmine.createSpy();
+ default_handler.negotiateTemplate(null, ".html", ".mustache", {}, spyCallback);
+
+ expect(spyCallback).toHaveBeenCalledWith("basepath can't be null", null, null);
+ });
+
it("check for a template file for the given output extension", function() {
spyOn(fs, "readFile").andCallFake(function(path, callback) {
if (path === "template_dir/path/sub_dir/index.html.mustache") {
@@ -365,6 +398,13 @@ describe("negotiate template", function() {
describe("get partials", function(){
+ it("call the calback with the error when basepath is null", function() {
+ var spyCallback = jasmine.createSpy();
+ default_handler.getPartials(null, ".mustache", {}, spyCallback);
+
+ expect(spyCallback).toHaveBeenCalledWith("basepath can't be null", null, null);
+ });
+
it("collects all partials in the given directory", function(){
spyOn(fs, "readdir").andCallFake(function(path, callback){

0 comments on commit dc428bb

Please sign in to comment.