From 36e2236e6bad2071ae604eccb46331bb7da3b79c Mon Sep 17 00:00:00 2001 From: Samy Pesse Date: Wed, 27 May 2015 21:05:36 +0200 Subject: [PATCH] Fix #765: in init handle correctly empty entries Add tests for it, fix #769 --- lib/book.js | 18 +++++++++--------- test/books/init/.gitignore | 3 +++ test/books/init/SUMMARY.md | 6 ++++++ test/init.js | 23 +++++++++++++++++++++++ 4 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 test/books/init/.gitignore create mode 100644 test/books/init/SUMMARY.md create mode 100644 test/init.js diff --git a/lib/book.js b/lib/book.js index ac0fc82db..2e0ab5811 100644 --- a/lib/book.js +++ b/lib/book.js @@ -789,20 +789,19 @@ Book.prototype.normError = function(err, opts, defs) { }; // Init and return a book -Book.init = function(root) { - var book = new Book(root); +Book.init = function(root, opts) { + var book = new Book(root, opts); var extensionToUse = ".md"; var chaptersPaths = function(chapters) { return _.reduce(chapters || [], function(accu, chapter) { - if (!chapter.path) return accu; + var o = { + title: chapter.title + }; + if (chapter.path) o.path = chapter.path; + return accu.concat( - _.filter([ - { - title: chapter.title, - path: chapter.path - } - ].concat(chaptersPaths(chapter.articles))) + [o].concat(chaptersPaths(chapter.articles)) ); }, []); }; @@ -839,6 +838,7 @@ Book.init = function(root) { .then(function(chapters) { // Create files that don't exist return Q.all(_.map(chapters, function(chapter) { + if (!chapter.path) return Q(); var absolutePath = path.resolve(book.root, chapter.path); return fs.exists(absolutePath) diff --git a/test/books/init/.gitignore b/test/books/init/.gitignore new file mode 100644 index 000000000..8a88b2a94 --- /dev/null +++ b/test/books/init/.gitignore @@ -0,0 +1,3 @@ +* +!SUMMARY.md +!.gitignore diff --git a/test/books/init/SUMMARY.md b/test/books/init/SUMMARY.md new file mode 100644 index 000000000..1e63aed2a --- /dev/null +++ b/test/books/init/SUMMARY.md @@ -0,0 +1,6 @@ +# Summary + +* [Hello](hello.md) +* [Hello 2](hello2.md) +* Hello 3 + * [Hello 4](hello3/hello4.md) diff --git a/test/init.js b/test/init.js new file mode 100644 index 000000000..3ba701f7b --- /dev/null +++ b/test/init.js @@ -0,0 +1,23 @@ +var fs = require('fs'); +var path = require('path'); +var should = require('should'); + +var Book = require('../').Book; +var LOG_LEVELS = require('../').LOG_LEVELS; + +describe('Init Books', function () { + var initRoot; + + before(function() { + initRoot = path.resolve(__dirname, "books/init"); + return Book.init(initRoot, { + logLevel: LOG_LEVELS.DISABLED, + }); + }); + + it('should create all chapters', function() { + should(fs.existsSync(path.resolve(initRoot, "hello.md"))).be.ok; + should(fs.existsSync(path.resolve(initRoot, "hello2.md"))).be.ok; + should(fs.existsSync(path.resolve(initRoot, "hello3/hello4.md"))).be.ok; + }); +});