Skip to content
Open
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 src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ function setup(env) {
if (typeof formatter === 'function') {
const val = args[index];
match = formatter.call(self, val);
match = String(match).replace(/%/g, '%%');

// Now we need to remove `args[index]` since it's inlined in the `format`
args.splice(index, 1);
Expand Down
13 changes: 13 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ describe('debug', () => {
assert.deepStrictEqual(messages.length, 3);
});

it('escapes percent signs returned by custom formatters', () => {
const log = debug('test');
log.enabled = true;
debug.formatters.x = () => '%j';

const messages = [];
log.log = (...args) => messages.push(args);
log('%x', null, 1);

assert(messages[0][0].includes('%%j'));
delete debug.formatters.x;
});

describe('extend namespace', () => {
it('should extend namespace', () => {
const log = debug('foo');
Expand Down
17 changes: 17 additions & 0 deletions test.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,22 @@ describe('debug node', () => {
assert.deepStrictEqual(util.formatWithOptions.getCall(0).args[0], options);
stdErrWriteStub.restore();
});

it('does not interpret percent signs in formatter output', () => {
debug.enable('*');
const options = {
hideDate: true,
colors: false
};
Object.assign(debug.inspectOpts, options);
const stdErrWriteStub = sinon.stub(process.stderr, 'write');
const log = debug('percent');
log('%o', {'%j': '%j %j %%'}, 1, 2, 3);
assert.strictEqual(
stdErrWriteStub.firstCall.firstArg,
'percent { \'%j\': \'%j %j %%\' } 1 2 3\n'
);
stdErrWriteStub.restore();
});
});
});