Skip to content

Commit

Permalink
Test: nunjucks testing cases for extended loader initially
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Lea <[email protected]>
  • Loading branch information
chigix committed Sep 4, 2018
1 parent a9b809e commit 720c9c4
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as path from 'path';

export const TEMPLATE_DIR = path.join(__dirname, '../templates');
export const TEST_TEMPLATE_DIR = path.join(__dirname, './fixtures/tpl-test');
export const RENDER_CASE_DIR = path.join(__dirname, './fixtures/tpl-render-result');
export const TEST_DIR = __dirname;
export const FIXTURES_DIR = path.join(__dirname, './fixtures');
14 changes: 14 additions & 0 deletions test/fixtures/tpl-render-result/basic-render-nigeru.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>逃げる</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
逃げちゃだめだ。逃げちゃだめだ。逃げちゃだめだ。
<script>
</script>
</body>
</html>
14 changes: 14 additions & 0 deletions test/fixtures/tpl-test/basic-render-case/layout/template.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>{{ title }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
{{ body }}
<script>
</script>
</body>
</html>
27 changes: 27 additions & 0 deletions test/nunjucks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as fs from 'fs';
import * as nunjucks from 'nunjucks';
import * as path from 'path';
import * as CONST from './constants';
const NjkLoaders = require('../tools/njk-loader');

test('TemplateLoader Basic Usage', done => {
const njk_env = new nunjucks.Environment(
new NjkLoaders.TemplateLoader(path.join(CONST.TEST_TEMPLATE_DIR, './basic-render-case')),
// new nunjucks.FileSystemLoader(path.join(CONST.TEST_TEMPLATE_DIR, './basic-render-case')),
);
njk_env.render('layout/template.njk', {
title: '逃げる',
body: '逃げちゃだめだ。逃げちゃだめだ。逃げちゃだめだ。',
}, (err, result) => {
expect(err).toBeNull();
if (err) {
console.error(err);
}
expect(result).toEqual(
fs.readFileSync(
path.resolve(CONST.RENDER_CASE_DIR, './basic-render-nigeru.html'),
'utf8'));

return done();
});
});
44 changes: 44 additions & 0 deletions tools/njk-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const nunjucks = require('nunjucks');
const path = require('path');
const fs = require('fs');

module.exports = {
TemplateLoader: nunjucks.Loader.extend({
async: true,
init: function (searchPaths) {
this.pathsToNames = {};
if (searchPaths) {
searchPaths = Array.isArray(searchPaths)
? searchPaths : [searchPaths];
this.searchPaths = searchPaths.map(path.normalize);
} else {
this.searchPaths = ['.'];
}
},
getSource: function (name, callback) {
let full_path = null;
const paths = this.searchPaths;

for (let i = 0; i < paths.length; i++) {
const basePath = path.resolve(paths[i]);
const p = path.resolve(paths[i], name); // Only allow the current directory and anything
// underneath it to be searched
if (p.indexOf(basePath) === 0 && fs.existsSync(p)) {
full_path = p;
break;
}
}

if (!full_path) {
return callback(null, null);
}
this.pathsToNames[full_path] = name;

callback(null, {
src: fs.readFileSync(full_path, 'utf-8'),
path: full_path,
noCache: true,
});
},
}),
};

0 comments on commit 720c9c4

Please sign in to comment.