Skip to content

Commit

Permalink
introduced windows compatible tests
Browse files Browse the repository at this point in the history
  • Loading branch information
db-developer committed Feb 26, 2021
1 parent 80bf3d7 commit 9f66025
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 39 deletions.
21 changes: 14 additions & 7 deletions jakefile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var fs = require('fs');
var path = require('path');
var execSync = require('child_process').execSync;
var exec = function (cmd) {
execSync(cmd, {stdio: 'inherit'});
Expand All @@ -19,45 +20,51 @@ task('clean', ['clobber'], function () {

desc('Lints the source code');
task('lint', ['clean'], function () {
exec('./node_modules/.bin/eslint "**/*.js"');
var epath = path.join('./node_modules/.bin/eslint');
exec(epath+' "**/*.js"');
console.log('Linting completed.');
});

task('browserify', function () {
exec('./node_modules/browserify/bin/cmd.js --standalone ejs lib/ejs.js > ejs.js');
var epath = path.join('./node_modules/browserify/bin/cmd.js');
exec(epath+' --standalone ejs lib/ejs.js > ejs.js');
console.log('Browserification completed.');
});

task('minify', function () {
exec('./node_modules/uglify-js/bin/uglifyjs ejs.js > ejs.min.js');
var epath = path.join('./node_modules/uglify-js/bin/uglifyjs');
exec(epath+' ejs.js > ejs.min.js');
console.log('Minification completed.');
});

desc('Generates the EJS API docs for the public API');
task('doc', function () {
jake.rmRf('out');
exec('./node_modules/.bin/jsdoc --verbose -c jsdoc.json lib/* docs/jsdoc/*');
var epath = path.join('./node_modules/.bin/jsdoc');
exec(epath+' --verbose -c jsdoc.json lib/* docs/jsdoc/*');
console.log('Documentation generated in ./out.');
});

desc('Generates the EJS API docs for the public and private API');
task('devdoc', function () {
jake.rmRf('out');
exec('./node_modules/.bin/jsdoc --verbose -p -c jsdoc.json lib/* docs/jsdoc/*');
var epath = path.join('./node_modules/.bin/jsdoc');
exec(epath+' --verbose -p -c jsdoc.json lib/* docs/jsdoc/*');
console.log('Documentation generated in ./out.');
});

desc('Publishes the EJS API docs');
task('docPublish', ['doc'], function () {
fs.writeFileSync('out/CNAME', 'api.ejs.co');
console.log('Pushing docs to gh-pages...');
exec('./node_modules/.bin/git-directory-deploy --directory out/');
var epath = path.join('./node_modules/.bin/git-directory-deploy');
exec(epath+' --directory out/');
console.log('Docs published to gh-pages.');
});

desc('Runs the EJS test suite');
task('test', ['lint'], function () {
exec('./node_modules/.bin/mocha');
exec(path.join('./node_modules/.bin/mocha'));
});

publishTask('ejs', ['build'], function () {
Expand Down
43 changes: 29 additions & 14 deletions test/cli.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,57 @@
let exec = require('child_process').execSync;
let fs = require('fs');
let path = require('path');
let assert = require('assert');
let os = process.platform !== 'win32' ? '' : 'node ';
let lf = process.platform !== 'win32' ? '\n' : '\r\n';

function run(cmd) {
return exec(cmd).toString();
}

suite('cli', function () {
test('rendering, custom delimiter, passed data', function () {
let o = run('./bin/cli.js -m $ ./test/fixtures/user.ejs name=foo');
assert.equal(o, '<h1>foo</h1>\n');
let x = path.join('./bin/cli.js');
let u = path.join('./test/fixtures/user.ejs');
let o = run(os+x+' -m $ '+u+' name=foo');
assert.equal(o, '<h1>foo</h1>'+lf);
});

test('rendering, custom delimiter, data from file with -f', function () {
let o = run('./bin/cli.js -m $ -f ./test/fixtures/user_data.json ./test/fixtures/user.ejs');
assert.equal(o, '<h1>zerb</h1>\n');
let x = path.join('./bin/cli.js');
let u = path.join('./test/fixtures/user.ejs');
let o = run(os+x+' -m $ -f ./test/fixtures/user_data.json '+u);
assert.equal(o, '<h1>zerb</h1>'+lf);
});

test('rendering, custom delimiter, data from CLI arg with -i', function () {
let o = run('./bin/cli.js -m $ -i %7B%22name%22%3A%20%22foo%22%7D ./test/fixtures/user.ejs');
assert.equal(o, '<h1>foo</h1>\n');
let x = path.join('./bin/cli.js');
let u = path.join('./test/fixtures/user.ejs');
let o = run(os+x+' -m $ -i %7B%22name%22%3A%20%22foo%22%7D '+u);
assert.equal(o, '<h1>foo</h1>'+lf);
});

test('rendering, custom delimiter, data from stdin / pipe', function () {
let o = run('cat ./test/fixtures/user_data.json | ./bin/cli.js -m $ ./test/fixtures/user.ejs');
assert.equal(o, '<h1>zerb</h1>\n');
if ( process.platform !== 'win32' ) {
let o = run('cat ./test/fixtures/user_data.json | ./bin/cli.js -m $ ./test/fixtures/user.ejs');
assert.equal(o, '<h1>zerb</h1>\n');
} // does not work on windows...
});

test('rendering, custom delimiter, passed data overrides file', function () {
let o = run('./bin/cli.js -m $ -f ./test/fixtures/user_data.json ./test/fixtures/user.ejs name=frang');
assert.equal(o, '<h1>frang</h1>\n');
let x = path.join('./bin/cli.js');
let f = path.join('./test/fixtures/user_data.json');
let g = path.join('./test/fixtures/user.ejs');
let o = run(os+x+' -m $ -f '+f+' '+g+' name=frang');
assert.equal(o, '<h1>frang</h1>'+lf);
});

test('rendering, remove whitespace option (hyphen case)', function () {
let o = run('./bin/cli.js --rm-whitespace ./test/fixtures/rmWhitespace.ejs');
assert.equal(o, fs.readFileSync('test/fixtures/rmWhitespace.html', 'utf-8'));
let x = path.join('./bin/cli.js');
let f = path.join('./test/fixtures/rmWhitespace.ejs');
let o = run(os+x+' --rm-whitespace '+f);
let c = fs.readFileSync('test/fixtures/rmWhitespace.html', 'utf-8');
assert.equal(o.replace(/\n/g, lf), c);
});

});


47 changes: 29 additions & 18 deletions test/ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var read = fs.readFileSync;
var assert = require('assert');
var path = require('path');
var LRU = require('lru-cache');
let lf = process.platform !== 'win32' ? '\n' : '\r\n';

try {
fs.mkdirSync(__dirname + '/tmp');
Expand Down Expand Up @@ -383,7 +384,7 @@ suite('ejs.renderFile(path, [data], [options], [fn])', function () {
if (err) {
return done(err);
}
assert.equal(html, '<p>hey</p>\n');
assert.equal(html, '<p>hey</p>'+lf);
done();
});
});
Expand All @@ -392,7 +393,7 @@ suite('ejs.renderFile(path, [data], [options], [fn])', function () {
var AsyncCtor;
var func;
function checkResult(html) {
assert.equal(html, '<p>hey</p>\n');
assert.equal(html, '<p>hey</p>'+lf);
}
// Environments without Promise support -- should throw
// when no callback provided
Expand Down Expand Up @@ -440,7 +441,7 @@ suite('ejs.renderFile(path, [data], [options], [fn])', function () {
if (err) {
return done(err);
}
assert.equal(html, '<h1>fonebone</h1>\n');
assert.equal(html, '<h1>fonebone</h1>'+lf);
done();
});
});
Expand All @@ -457,7 +458,7 @@ suite('ejs.renderFile(path, [data], [options], [fn])', function () {
doneCount = 2;
return done(err);
}
assert.equal(html, '<h1>fonebone</h1>\n');
assert.equal(html, '<h1>fonebone</h1>'+lf);
doneCount++;
if (doneCount === 2) {
done();
Expand Down Expand Up @@ -538,7 +539,7 @@ suite('ejs.renderFile(path, [data], [options], [fn])', function () {
if (err) {
return done(err);
}
assert.equal(html, ctxt.foo + '\n');
assert.equal(html, ctxt.foo + lf);
done();
});

Expand All @@ -557,7 +558,7 @@ suite('ejs.renderFile(path, [data], [options], [fn])', function () {
};
ejs.renderFile(path.join(__dirname, 'fixtures/views.ejs'), data, function(error, data){
assert.ifError(error);
assert.equal('<div><p>global test</p>\n</div>\n', data);
assert.equal('<div><p>global test</p>'+lf+'</div>'+lf, data);
done();
});

Expand All @@ -576,7 +577,7 @@ suite('ejs.renderFile(path, [data], [options], [fn])', function () {
};
ejs.renderFile(path.join(__dirname, 'fixtures/views.ejs'), data, function(error, data){
assert.ifError(error);
assert.equal('<div><p>custom test</p>\n</div>\n', data);
assert.equal('<div><p>custom test</p>'+lf+'</div>'+lf, data);
done();
});

Expand Down Expand Up @@ -849,7 +850,10 @@ suite('exceptions', function () {
}
catch (err) {
assert.equal(err.path, 'error.ejs');
assert.equal(err.stack.split('\n').slice(0, 8).join('\n'), fixture('error.out'));
var errstck = err.stack.split('\n').slice(0, 8).join('\n');
errstck = errstck.replace(/\n/g,lf);
errstck = errstck.replace(/\r\r\n/g,lf);
assert.equal(errstck, fixture('error.out'));
return;
}
throw new Error('no error reported when there should be');
Expand All @@ -864,7 +868,10 @@ suite('exceptions', function () {
}
catch (err) {
assert.ok(!err.path);
assert.notEqual(err.stack.split('\n').slice(0, 8).join('\n'), fixture('error.out'));
var errstck = err.stack.split('\n').slice(0, 8).join('\n');
errstck = errstck.replace(/\n/g,lf);
errstck = errstck.replace(/\r\r\n/g,lf);
assert.notEqual(errstck, fixture('error.out'));
return;
}
throw new Error('no error reported when there should be');
Expand Down Expand Up @@ -915,8 +922,8 @@ suite('exceptions', function () {

suite('rmWhitespace', function () {
test('works', function () {
assert.equal(ejs.render(fixture('rmWhitespace.ejs'), {}, {rmWhitespace: true}),
fixture('rmWhitespace.html'));
var outp = ejs.render(fixture('rmWhitespace.ejs'), {}, {rmWhitespace: true});
assert.equal(outp.replace(/\n/g,lf), fixture('rmWhitespace.html'));
});
});

Expand Down Expand Up @@ -975,7 +982,7 @@ suite('include()', function () {
assert.equal(
ejs.render('<%- include("fixtures/includes/bom.ejs") %>',
{}, {filename: path.join(__dirname, 'f.ejs')}),
'<p>This is a file with BOM.</p>\n');
'<p>This is a file with BOM.</p>'+lf);
});

test('include ejs with locals', function () {
Expand Down Expand Up @@ -1003,8 +1010,10 @@ suite('include()', function () {
var file = 'test/fixtures/include-root.ejs';
var inc = function (original, prev) {
if (original.charAt(0) === '/') {
// original: '/include' (windows)
// prev: 'D:\include.ejs' (windows)
return {
filename: path.join(__dirname, 'fixtures', prev)
filename: path.join(__dirname, 'fixtures', original+'.ejs')
};
} else {
return prev;
Expand All @@ -1017,9 +1026,11 @@ suite('include()', function () {
test('include ejs with includer returning template', function () {
var file = 'test/fixtures/include-root.ejs';
var inc = function (original, prev) {
if (prev === '/include.ejs') {
// original: '/include' (windows)
// prev: 'D:\include.ejs' (windows)
if (original === '/include') {
return {
template: '<p>Hello template!</p>\n'
template: '<p>Hello template!</p>'+lf
};
} else {
return prev;
Expand Down Expand Up @@ -1073,10 +1084,10 @@ suite('include()', function () {
var file = 'test/fixtures/include_cache.ejs';
var options = {filename: file};
var out = ejs.compile(fixture('include_cache.ejs'), options);
assert.equal(out(), '<p>Old</p>\n');
assert.equal(out(), '<p>Old</p>'+lf);

fs.writeFileSync(__dirname + '/tmp/include.ejs', '<p>New</p>');
assert.equal(out(), '<p>New</p>\n');
assert.equal(out(), '<p>New</p>'+lf);
});

test('support caching', function () {
Expand Down Expand Up @@ -1126,7 +1137,7 @@ suite('test fileloader', function () {
if (err) {
return done(err);
}
assert.equal(html, 'myFileLoad: <p>hey</p>\n');
assert.equal(html, 'myFileLoad: <p>hey</p>'+lf);
done();
});

Expand Down

0 comments on commit 9f66025

Please sign in to comment.