Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Engine template file extension is now optional, defaults to html. Mig…

…rate engine test to buster.
  • Loading branch information...
commit 439b2046f9f3a600cb27f81721d79d39dd78282c 1 parent a47bf6f
@cliffano authored
Showing with 211 additions and 252 deletions.
  1. +1 −1  CHANGELOG.md
  2. +1 −1  lib/ae86.js
  3. +7 −3 lib/engine.js
  4. +202 −247 test/engine.js
View
2  CHANGELOG.md
@@ -1,5 +1,5 @@
### 0.0.7-pre
-*
+* Engine template file extension is now optional, defaults to html
### 0.0.6
* Fix incorrect URL path and directory path on Windows
View
2  lib/ae86.js
@@ -53,7 +53,7 @@ AE86.prototype.generate = function (cb) {
return params;
}
- var _engine = new engine('html'),
+ var _engine = new engine(),
tasks = {};
['partials', 'layouts', 'pages'].forEach(function (dir) {
View
10 lib/engine.js
@@ -10,10 +10,14 @@ var _ = require('underscore'),
/**
* class Engine
*
- * @param {String} ext: template file extension, files without this extension will be ignored.
+ * @param {String} opts: optional
+ * - ext: template file extension, files without this extension will be ignored.
+ * - mkdirp: temporary used for testing only, need to figure out how to mock required function using Sinon
*/
-function Engine(ext) {
- this.ext = ext;
+function Engine(opts) {
+ opts = opts || {};
+ this.ext = opts.ext || 'html';
+ mkdirp = opts.mkdirp || mkdirp;
}
/**
View
449 test/engine.js
@@ -1,262 +1,217 @@
-/*
var bag = require('bagofholding'),
- jazz = require('jazz'),
- sandbox = require('sandboxed-module'),
- should = require('should'),
- checks, mocks,
- engine;
-
-describe('engine', function () {
-
- function create(checks, mocks) {
- return sandbox.require('../lib/engine', {
- requires: mocks ? mocks.requires : {},
- globals: {
- console: bag.mock.console(checks, mocks)
- }
- });
+ buster = require('buster'),
+ Engine = require('../lib/engine'),
+ f = require('file'),
+ fs = require('fs'),
+ jazz = require('jazz');
+
+buster.testCase('engine - engine', {
+ 'should use default ext when optional ext is not specified': function () {
+ var engine = new Engine()
+ assert.equals(engine.ext, 'html');
+ },
+ 'should use specified ext': function () {
+ var engine = new Engine({ ext: 'someext' });
+ assert.equals(engine.ext, 'someext');
}
+});
- beforeEach(function () {
- checks = {};
- mocks = {};
- });
-
- describe('compile', function () {
-
- beforeEach(function () {
- checks.fs_readFile_files = [];
- mocks.requires = {
- fs: {
- readFile: function (baseFile, encoding, cb) {
- checks.fs_readFile_files.push(baseFile);
- encoding.should.equal('utf8');
- cb(null, 'somecontent');
- }
- },
- file: {
- walkSync: function (dir, cb) {
- checks.file_walkSync_dir = dir;
- cb('/somebasedir', null, mocks.file_walkSync_files);
- }
- }
- };
+buster.testCase('engine - compile', {
+ setUp: function () {
+ this.mockFs = this.mock(fs);
+ this.engine = new Engine();
+ },
+ 'should compile files with the specified file extension': function (done) {
+ this.mockFs.expects('readFile').withArgs('/somebasedir/foo.html', 'utf8').callsArgWith(2, null, 'foo');
+ this.mockFs.expects('readFile').withArgs('/somebasedir/sub/dir/bar.html', 'utf8').callsArgWith(2, null, 'bar');
+ this.stub(f, 'walkSync', function (dir, cb) {
+ assert.equals(dir, 'sometemplatedir');
+ cb('/somebasedir', [], [ 'foo.html', 'sub/dir/bar.html', '.git', 'abc.txt' ]);
});
-
- it('should compile files with the specified file extension', function (done) {
- mocks.file_walkSync_files = [ 'foo.html', 'sub/dir/bar.html', '.git', 'abc.txt' ];
- engine = new (create(checks, mocks))('html');
- engine.compile('sometemplatedir', function (err, result) {
- checks.engine_compile_err = err;
- checks.engine_compile_result = result;
- done();
- });
- checks.file_walkSync_dir.should.equal('sometemplatedir');
- checks.fs_readFile_files.length.should.equal(2);
- checks.fs_readFile_files[0].should.equal('/somebasedir/foo.html');
- checks.fs_readFile_files[1].should.equal('/somebasedir/sub/dir/bar.html');
- should.not.exist(checks.engine_compile_err);
- should.exist(checks.engine_compile_result);
- should.exist(checks.engine_compile_result['somebasedir/foo.html']);
- should.exist(checks.engine_compile_result['somebasedir/sub/dir/bar.html']);
+ this.engine.compile('sometemplatedir', function (err, result) {
+ assert.isNull(err);
+ assert.defined(result['somebasedir/foo.html']);
+ assert.defined(result['somebasedir/sub/dir/bar.html']);
+ done();
});
-
- it('should compile files with windows file path separator (backslashes) and have url path separator (slashes)', function (done) {
- mocks.file_walkSync_files = [ 'foo.html', 'sub\\dir\\bar.html', '.git', 'abc.txt' ];
- engine = new (create(checks, mocks))('html');
- engine.compile('sometemplatedir', function (err, result) {
- checks.engine_compile_err = err;
- checks.engine_compile_result = result;
- done();
- });
- checks.file_walkSync_dir.should.equal('sometemplatedir');
- checks.fs_readFile_files.length.should.equal(2);
- checks.fs_readFile_files[0].should.equal('/somebasedir/foo.html');
- checks.fs_readFile_files[1].should.equal('/somebasedir/sub\\dir\\bar.html');
- should.not.exist(checks.engine_compile_err);
- should.exist(checks.engine_compile_result);
- should.exist(checks.engine_compile_result['somebasedir/foo.html']);
- should.exist(checks.engine_compile_result['somebasedir/sub/dir/bar.html']);
+ },
+ 'should compile files with windows file path separator (backslashes) and have url path separator (slashes)': function (done) {
+ this.mockFs.expects('readFile').withArgs('/somebasedir/foo.html', 'utf8').callsArgWith(2, null, 'foo');
+ this.mockFs.expects('readFile').withArgs('/somebasedir/sub\\dir\\bar.html', 'utf8').callsArgWith(2, null, 'bar');
+ this.stub(f, 'walkSync', function (dir, cb) {
+ assert.equals(dir, 'sometemplatedir');
+ cb('/somebasedir', [], [ 'foo.html', 'sub\\dir\\bar.html', '.git', 'abc.txt' ]);
});
-
- it('should ignore files with extensions other than the one specified', function (done) {
- mocks.file_walkSync_files = [ '.git', 'abc.txt' ];
- engine = new (create(checks, mocks))('html');
- engine.compile('sometemplatedir', function (err, result) {
- checks.engine_compile_err = err;
- checks.engine_compile_result = result;
- done();
- });
- checks.file_walkSync_dir.should.equal('sometemplatedir');
- checks.fs_readFile_files.length.should.equal(0);
- should.not.exist(checks.engine_compile_err);
- should.exist(checks.engine_compile_result);
+ this.engine.compile('sometemplatedir', function (err, result) {
+ assert.isNull(err);
+ assert.defined(result['somebasedir/foo.html']);
+ assert.defined(result['somebasedir/sub/dir/bar.html']);
+ done();
});
- });
-
- describe('merge', function () {
-
- beforeEach(function () {
- mocks.requires = {
- fs: {
- writeFile: function (file, content, encoding, cb) {
- checks.fs_writeFile_file = file;
- checks.fs_writeFile_content = content;
- encoding.should.equal('utf8');
- cb(mocks.fs_writeFile_err);
- }
- },
- mkdirp: function (dir, mode, cb) {
- checks.mkdirp_dir = dir;
- mode.should.equal('0755');
- cb(mocks.mkdirp_err);
- }
- };
+ },
+ 'should ignore files with extensions other than the one specified': function (done) {
+ this.stub(f, 'walkSync', function (dir, cb) {
+ assert.equals(dir, 'sometemplatedir');
+ cb('/somebasedir', [], [ '.git', 'abc.txt' ]);
});
-
- it('should merge partials in page templates', function (done) {
- var templates = {
- partials: {
- 'footer.html': jazz.compile('Some footer text')
- },
- pages: {
- 'page.html': jazz.compile('{include(\'footer.html\')}')
- },
- layouts: {
- 'default.html': jazz.compile('{content}')
- }
- },
- params = {
- };
- engine = new (create(checks, mocks))('html');
- engine.merge('someoutputdir', templates, params, function (err, result) {
- checks.engine_compile_err = err;
- checks.engine_compile_result = result;
- done();
- });
- checks.fs_writeFile_content.should.equal('Some footer text');
+ this.engine.compile('sometemplatedir', function (err, result) {
+ assert.equals(err, undefined);
+ assert.equals(result, {});
+ done();
});
+ }
+});
- it('should merge layouts in page templates', function (done) {
- var templates = {
- partials: {
- },
- pages: {
- 'page.html': jazz.compile('Some content')
- },
- layouts: {
- 'default.html': jazz.compile('Some layout {content}')
- }
- },
- params = {
- };
- engine = new (create(checks, mocks))('html');
- engine.merge('someoutputdir', templates, params, function (err, result) {
- checks.engine_compile_err = err;
- checks.engine_compile_result = result;
- done();
- });
- checks.fs_writeFile_content.should.equal('Some layout Some content');
+buster.testCase('engine - compile', {
+ setUp: function () {
+ this.mockConsole = this.mock(console);
+ },
+ 'should merge partials in page templates': function (done) {
+ var templates = {
+ partials: { 'footer.html': jazz.compile('Some footer text') },
+ pages: { 'page.html': jazz.compile('{include(\'footer.html\')}') },
+ layouts: { 'default.html': jazz.compile('{content}') }
+ },
+ params = {},
+ mockMkdirp = function (dir, mode, cb) {
+ assert.equals(dir, 'someoutputdir');
+ assert.equals(mode, '0755');
+ cb();
+ },
+ engine = new Engine({ mkdirp: mockMkdirp });
+
+ this.mockConsole.expects('log').withExactArgs('+ creating %s', 'someoutputdir/page.html');
+ this.stub(fs, 'writeFile', function (page, content, encoding, cb) {
+ assert.equals(page, 'someoutputdir/page.html');
+ assert.equals(content, 'Some footer text');
+ assert.equals(encoding, 'utf8');
+ cb();
});
-
- it('should merge parameters in page templates', function (done) {
- var templates = {
- partials: {
- },
- pages: {
- 'page.html': jazz.compile('Some content with param {foo}')
- },
- layouts: {
- 'default.html': jazz.compile('{content}')
- }
- },
- params = {
- foo: 'bar'
- };
- engine = new (create(checks, mocks))('html');
- engine.merge('someoutputdir', templates, params, function (err, result) {
- checks.engine_compile_err = err;
- checks.engine_compile_result = result;
- done();
- });
- checks.fs_writeFile_content.should.equal('Some content with param bar');
+ engine.merge('someoutputdir', templates, params, function (err, result) {
+ assert.isNull(err);
+ assert.equals(result[0], 'page.html');
+ done();
});
-
- it('should log command message when there is no error while writing the page file', function (done) {
- var templates = {
- partials: {
- },
- pages: {
- 'page.html': jazz.compile('Some content with param {foo}')
- },
- layouts: {
- 'default.html': jazz.compile('{content}')
- }
- },
- params = {
- foo: 'bar'
- };
- engine = new (create(checks, mocks))('html');
- engine.merge('some/output/dir', templates, params, function (err, result) {
- checks.engine_compile_err = err;
- checks.engine_compile_result = result;
- done();
- });
- checks.console_log_messages.length.should.equal(1);
- checks.console_log_messages[0].should.equal('+ creating some/output/dir/page.html');
- checks.mkdirp_dir.should.equal('some/output/dir');
+ },
+ 'should merge layouts in page templates': function (done) {
+ var templates = {
+ partials: {},
+ pages: { 'page.html': jazz.compile('Some content') },
+ layouts: { 'default.html': jazz.compile('Some layout {content}') }
+ },
+ params = {},
+ mockMkdirp = function (dir, mode, cb) {
+ assert.equals(dir, 'someoutputdir');
+ assert.equals(mode, '0755');
+ cb();
+ },
+ engine = new Engine({ mkdirp: mockMkdirp });
+
+ this.mockConsole.expects('log').withExactArgs('+ creating %s', 'someoutputdir/page.html');
+ this.stub(fs, 'writeFile', function (page, content, encoding, cb) {
+ assert.equals(page, 'someoutputdir/page.html');
+ assert.equals(content, 'Some layout Some content');
+ assert.equals(encoding, 'utf8');
+ cb();
});
-
- it('should pass error to callback when directory cannot be created', function (done) {
- mocks.mkdirp_err = new Error('someerror');
- var templates = {
- partials: {
- },
- pages: {
- 'page.html': jazz.compile('Some content with param {foo}')
- },
- layouts: {
- 'default.html': jazz.compile('{content}')
- }
- },
- params = {
- foo: 'bar'
- };
- engine = new (create(checks, mocks))('html');
- engine.merge('some/output/dir', templates, params, function (err, result) {
- checks.engine_compile_err = err;
- checks.engine_compile_result = result;
- done();
- });
- checks.engine_compile_err.message.should.equal('someerror');
- checks.mkdirp_dir.should.equal('some/output/dir');
+ engine.merge('someoutputdir', templates, params, function (err, result) {
+ assert.isNull(err);
+ assert.equals(result[0], 'page.html');
+ done();
});
-
- it('should create dir with *nix and Windows paths', function () {
- mocks.mkdirp_err = new Error('someerror');
- var templates = {
- partials: {
- },
- pages: {
- 'page.html': jazz.compile('Some content with param {foo}')
- },
- layouts: {
- 'default.html': jazz.compile('{content}')
- }
- },
- params = {
- foo: 'bar'
- };
- engine = new (create(checks, mocks))('html');
-
- engine.merge('some/output/dir', templates, params, function (err, result) {
- });
- checks.mkdirp_dir.should.equal('some/output/dir');
-
- engine.merge('some\\output\\dir', templates, params, function (err, result) {
- });
- checks.mkdirp_dir.should.equal('some\\output');
+ },
+ 'should merge parameters in page templates': function (done) {
+ var templates = {
+ partials: {},
+ pages: { 'page.html': jazz.compile('Some content with param {foo}') },
+ layouts: { 'default.html': jazz.compile('{content}') }
+ },
+ params = { foo: 'bar' },
+ mockMkdirp = function (dir, mode, cb) {
+ assert.equals(dir, 'someoutputdir');
+ assert.equals(mode, '0755');
+ cb();
+ },
+ engine = new Engine({ mkdirp: mockMkdirp });
+
+ this.mockConsole.expects('log').withExactArgs('+ creating %s', 'someoutputdir/page.html');
+ this.stub(fs, 'writeFile', function (page, content, encoding, cb) {
+ assert.equals(page, 'someoutputdir/page.html');
+ assert.equals(content, 'Some content with param bar');
+ assert.equals(encoding, 'utf8');
+ cb();
});
- });
-});
-*/
+ engine.merge('someoutputdir', templates, params, function (err, result) {
+ assert.isNull(err);
+ assert.equals(result[0], 'page.html');
+ done();
+ });
+ },
+ 'should log command message when there is no error while writing the page file': function (done) {
+ var templates = {
+ partials: {},
+ pages: { 'page.html': jazz.compile('Some content with param {foo}') },
+ layouts: { 'default.html': jazz.compile('{content}') }
+ },
+ params = { foo: 'bar' },
+ mockMkdirp = function (dir, mode, cb) {
+ assert.equals(dir, 'some/output/dir');
+ assert.equals(mode, '0755');
+ cb();
+ },
+ engine = new Engine({ mkdirp: mockMkdirp });
+
+ this.mockConsole.expects('log').withExactArgs('+ creating %s', 'some/output/dir/page.html');
+ this.stub(fs, 'writeFile', function (page, content, encoding, cb) {
+ assert.equals(page, 'some/output/dir/page.html');
+ assert.equals(content, 'Some content with param bar');
+ assert.equals(encoding, 'utf8');
+ cb();
+ });
+ engine.merge('some/output/dir', templates, params, function (err, result) {
+ assert.isNull(err);
+ assert.equals(result[0], 'page.html');
+ done();
+ });
+ },
+ 'should pass error to callback when directory cannot be created': function (done) {
+ var templates = {
+ partials: {},
+ pages: { 'page.html': jazz.compile('Some content with param {foo}') },
+ layouts: { 'default.html': jazz.compile('{content}') }
+ },
+ params = { foo: 'bar' },
+ mockMkdirp = function (dir, mode, cb) {
+ assert.equals(dir, 'some/output/dir');
+ assert.equals(mode, '0755');
+ cb(new Error('someerror'));
+ },
+ engine = new Engine({ mkdirp: mockMkdirp });
+
+ engine.merge('some/output/dir', templates, params, function (err, result) {
+ assert.equals(err.message, 'someerror');
+ assert.equals(result[0], 'page.html');
+ done();
+ });
+ },
+ 'should create dir with *nix and Windows paths': function (done) {
+ var templates = {
+ partials: {},
+ pages: { 'page.html': jazz.compile('Some content with param {foo}') },
+ layouts: { 'default.html': jazz.compile('{content}') }
+ },
+ params = { foo: 'bar' },
+ mockMkdirp = function (dir, mode, cb) {
+ assert.equals(dir, 'some\\output');
+ assert.equals(mode, '0755');
+ cb(new Error('someerror'));
+ },
+ engine = new Engine({ mkdirp: mockMkdirp });
+
+ engine.merge('some\\output\\dir', templates, params, function (err, result) {
+ assert.equals(err.message, 'someerror');
+ assert.equals(result[0], 'page.html');
+ done();
+ });
+ }
+});
Please sign in to comment.
Something went wrong with that request. Please try again.