process.test.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. 'use strict';
  2. var assert = require('assert');
  3. var doT = require('..');
  4. var fs = require('fs');
  5. describe('doT.process', function() {
  6. beforeEach(function() {
  7. removeCompiledTemplateFiles();
  8. });
  9. afterEach(function() {
  10. removeCompiledTemplateFiles();
  11. });
  12. function removeCompiledTemplateFiles() {
  13. try { fs.unlinkSync('./test/templates/test.js'); } catch(e) {}
  14. }
  15. it('should compile all templates in folder', function() {
  16. const templates = doT.process({path: './test/templates'});
  17. var str = templates.test({data: 2});
  18. assert.equal(str, '21');
  19. var js = fs.statSync('./test/templates/test.js');
  20. assert.ok(js.isFile());
  21. // code below passes if the test is run without coverage using `npm run test-spec`
  22. // because source code of doT.encodeHTMLSource is used inside compiled templates
  23. // var fn = require('./templates/test.js');
  24. // var str = fn({data: 2});
  25. // assert.equal(str, '21');
  26. });
  27. it('should ignore varname with polluted object prototype', function() {
  28. var currentLog = console.log;
  29. console.log = log;
  30. var logged;
  31. Object.prototype.templateSettings = {varname: 'it=(console.log("executed"),{})'};
  32. try {
  33. const templates = doT.process({path: './test/templates'});
  34. assert.notEqual(logged, 'executed');
  35. // injected code can only be executed if undefined is passed to template function
  36. templates.test();
  37. assert.notEqual(logged, 'executed');
  38. } finally {
  39. console.log = currentLog;
  40. }
  41. function log(str) {
  42. logged = str;
  43. }
  44. });
  45. });