From 9ee652b9e9bfdf6372e4a0e3c217f60d3695a69a Mon Sep 17 00:00:00 2001 From: David Evans Date: Mon, 29 Mar 2021 15:47:43 +0100 Subject: [PATCH] Add unit test for --disallow-code-generation-from-strings --- test/fixtures/basic.js | 8 ++++++++ test/test.js | 29 ++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/basic.js diff --git a/test/fixtures/basic.js b/test/fixtures/basic.js new file mode 100644 index 0000000..320a3aa --- /dev/null +++ b/test/fixtures/basic.js @@ -0,0 +1,8 @@ + +var deprecate = require('../..')('basic') + +var object = { foo: 'bar' } +deprecate.property(object, 'foo') + +function fn () {} +deprecate.function(fn) diff --git a/test/test.js b/test/test.js index 5a66253..5e5bce9 100644 --- a/test/test.js +++ b/test/test.js @@ -10,6 +10,16 @@ var script = path.join(__dirname, 'fixtures', 'script.js') var spawn = require('child_process').spawn var strictlib = libs.strict +function isNodeVersionGE (required) { + var nodeVersion = process.version.substr(1).split('.') + for (var i = 0; i < required.length; i++) { + if (+nodeVersion[i] < required[i]) { + return false + } + } + return true +} + describe('depd(namespace)', function () { it('creates deprecated function', function () { assert.strictEqual(typeof depd('test'), 'function') @@ -730,9 +740,9 @@ describe('node script.js', function () { ;(function () { // --*-deprecation switches are 0.8+ // no good way to feature detect this sync - var describe = /^v0\.6\./.test(process.version) - ? global.describe.skip - : global.describe + var describe = isNodeVersionGE([0, 8]) + ? global.describe + : global.describe.skip describe('node --no-deprecation script.js', function () { it('should suppress deprecation message', function (done) { @@ -755,6 +765,19 @@ describe('node script.js', function () { }) }()) +describe('node --disallow-code-generation-from-strings script.js', function () { + it('should run without error', function (done) { + if (!isNodeVersionGE([9])) this.skip() // --disallow-code-generation-from-strings is 9+ + + var basic = path.join(__dirname, 'fixtures', 'basic.js') + captureChildStderr(basic, ['--disallow-code-generation-from-strings'], function (err, stderr) { + if (err) return done(err) + assert.strictEqual(stderr, '') + done() + }) + }) +}) + function captureChildStderr (script, opts, callback) { var chunks = [] var env = { PATH: process.env.PATH }