-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test: nunjucks testing cases for extended loader initially
Signed-off-by: Richard Lea <[email protected]>
- Loading branch information
Showing
5 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
test/fixtures/tpl-test/basic-render-case/layout/template.njk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}, | ||
}), | ||
}; |