Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/mustache
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ function toStdout (cb, str) {

function streamToStr (stream, cb) {
var data = '';
stream.setEncoding('utf8');

stream.on('data', function onData (chunk) {
data += chunk;
Expand Down
51 changes: 51 additions & 0 deletions test/cli-utf8-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require('./helper');

var fs = require('fs');
var os = require('os');
var path = require('path');
var childProcess = require('child_process');

function renderFiles (view, template, partial, expected, done) {
var directory = path.join(os.tmpdir(), 'mustache-utf8-' + process.pid + '-' + Math.random().toString(16).slice(2));
fs.mkdirSync(directory);
var viewPath = path.join(directory, 'view.json');
var templatePath = path.join(directory, 'template.mustache');
var partialPath = path.join(directory, 'utf8.mustache');
fs.writeFileSync(viewPath, view);
fs.writeFileSync(templatePath, template);
var args = isLegacyNodeVersion ? [] : ['--require', 'esm'];
args = args.concat(['bin/mustache', viewPath, templatePath]);
if (partial !== undefined) {
fs.writeFileSync(partialPath, partial);
args = args.concat(['-p', partialPath]);
}
childProcess.execFile(process.execPath, args, { maxBuffer: 1024 * 1024 }, function onRendered (error, stdout, stderr) {
fs.unlinkSync(viewPath);
fs.unlinkSync(templatePath);
if (partial !== undefined) fs.unlinkSync(partialPath);
fs.rmdirSync(directory);
assert.equal(error, null);
assert.equal(stderr, '');
assert.equal(stdout.slice(-2), expected.slice(-2));
assert.isTrue(stdout === expected, 'preserves the complete UTF-8 string');
done();
});
}

describe('Mustache CLI UTF-8 decoding', function () {
it('preserves a template character split between stream chunks', function (done) {
var text = new Array(65536).join('a') + '€X';
renderFiles('{}', text, undefined, text, done);
});

it('preserves a JSON view character split between stream chunks', function (done) {
var prefixLength = 65535 - '{"value":"'.length;
var text = new Array(prefixLength + 1).join('a') + '€X';
renderFiles(JSON.stringify({ value: text }), '{{value}}', undefined, text, done);
});

it('preserves a partial character split between stream chunks', function (done) {
var text = new Array(65536).join('a') + '€X';
renderFiles('{}', '{{>utf8}}', text, text, done);
});
});