Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clone incoming objects to prevent incoming data mutation (Issue #1775) #2343

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
test(logger.test.js): added test to ensure non-primitive objects pass…
…ed in are not mutated
mattrusso9037 committed Aug 14, 2023
commit 5a368152106ecc33cf6426f93f1ad75fafe82833
1 change: 1 addition & 0 deletions .torchignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pattern: asymmetric-private-key
82 changes: 81 additions & 1 deletion test/unit/winston/logger.test.js
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ const util = require('util');
const { EOL } = require('os');
const isStream = require('is-stream');
const stdMocks = require('std-mocks');
const { MESSAGE, SPLAT } = require('triple-beam');
const { MESSAGE, SPLAT, LEVEL } = require('triple-beam');
const winston = require('../../../lib/winston');
const TransportStream = require('winston-transport');
const format = require('../../../lib/winston').format;
@@ -749,6 +749,46 @@ describe('Logger Instance', function () {
logger.info('Hello', {label: 'world'});
logger.info('Hello %d', 100, {label: 'world'});
});

it(`.log(level, object) does not mutate input`, function (done) {
const data = { message: 'hello' };

const logger = helpers.createLogger(function (info) {
assume(info[LEVEL]).equals('info');
assume(info.message).equals('hello');
assume(data[LEVEL]).equals(undefined);
assume(data).deep.equal({ message: 'hello' });
done();
});

logger.log('info', data);
});

it(`.log(level, array) does not mutate input`, function (done) {
const data = ['one', 'two', 'three'];

const logger = helpers.createLogger(function (info) {
assume(info[LEVEL]).equals('info');
assume(data[LEVEL]).equals(undefined);
assume(data).deep.equal(['one', 'two', 'three']);
done();
});

logger.log('info', data);
});

it(`.log(level, object, splat) does not mutate input`, function (done) {
const data = { message: 'hello' };

const logger = helpers.createLogger(function (info) {
assume(info[LEVEL]).equals('info');
assume(data[LEVEL]).equals(undefined);
assume(data).deep.equal({ message: 'hello' });
done();
});

logger.log('info', data, { label: 'test' });
});
});

describe('.info', function () {
@@ -782,6 +822,46 @@ describe('Logger Instance', function () {
logger.info(err);
});

it(`.info(object) does not mutate input`, function (done) {
const data = { message: 'hello' };

const logger = helpers.createLogger(function (info) {
assume(info.message).equals('hello');
assume(info[LEVEL]).equals('info');
assume(data).deep.equal({ message: 'hello' });
assume(data[LEVEL]).equals(undefined);
done();
});

logger.info(data);
});

it(`.info(array) does not mutate input`, function (done) {
const data = ['one', 'two', 'three'];

const logger = helpers.createLogger(function (info) {
assume(info[LEVEL]).equals('info');
assume(data).deep.equal(['one', 'two', 'three']);
assume(data[LEVEL]).equals(undefined);
done();
});

logger.info(data);
});

it(`.info(array) does not mutate input`, function (done) {
const data = { message: ['one', 'two', 'three'] };

const logger = helpers.createLogger(function (info) {
assume(info[LEVEL]).equals('info');
assume(data).deep.equal({ message: ['one', 'two', 'three'] });
assume(data[LEVEL]).equals(undefined);
done();
});

logger.info(data);
});

// TODO: This test needs finished or removed
it.skip(`.info('any string', new Error())`, function (done) {
const err = new Error('test');