|
| 1 | +'use strict'; |
| 2 | +const exec = require('child_process').exec; |
| 3 | +const chai = require('chai'); |
| 4 | +const expect = chai.expect; |
| 5 | +const sinon = require('sinon'); |
| 6 | +const sinonChai = require('sinon-chai'); |
| 7 | +require('mocha-sinon'); |
| 8 | +chai.use(sinonChai); |
| 9 | + |
| 10 | +const utils = require('../tools/utils'); |
| 11 | +const ClassWithLogger = utils.ClassWithLogger; |
| 12 | +const ClassWithoutLogger = utils.ClassWithoutLogger; |
| 13 | +const ClassWithUndefinedLogger = utils.ClassWithUndefinedLogger; |
| 14 | +const ensureCalledWith = utils.ensureCalledWith; |
| 15 | + |
| 16 | +describe('Deprecation Warnings', function() { |
| 17 | + beforeEach(function() { |
| 18 | + this.sinon.stub(console, 'error'); |
| 19 | + }); |
| 20 | + |
| 21 | + const defaultMessage = ' is deprecated and will be removed in a later version.'; |
| 22 | + |
| 23 | + it('node --no-deprecation flag should suppress all deprecation warnings', { |
| 24 | + metadata: { requires: { node: '>=6.0.0' } }, |
| 25 | + test: function(done) { |
| 26 | + exec( |
| 27 | + 'node --no-deprecation ./test/tools/deprecate_warning_test_program.js', |
| 28 | + (err, stdout, stderr) => { |
| 29 | + expect(err).to.be.null; |
| 30 | + expect(stdout).to.be.empty; |
| 31 | + expect(stderr).to.be.empty; |
| 32 | + done(); |
| 33 | + } |
| 34 | + ); |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + it('node --trace-deprecation flag should print stack trace to stderr', { |
| 39 | + metadata: { requires: { node: '>=6.0.0' } }, |
| 40 | + test: function(done) { |
| 41 | + exec( |
| 42 | + 'node --trace-deprecation ./test/tools/deprecate_warning_test_program.js', |
| 43 | + (err, stdout, stderr) => { |
| 44 | + expect(err).to.be.null; |
| 45 | + expect(stdout).to.be.empty; |
| 46 | + expect(stderr).to.not.be.empty; |
| 47 | + |
| 48 | + // split stderr into separate lines, trimming the first line to just the warning message |
| 49 | + const split = stderr.split('\n'); |
| 50 | + const warning = split |
| 51 | + .shift() |
| 52 | + .split(')')[1] |
| 53 | + .trim(); |
| 54 | + |
| 55 | + // ensure warning message matches expected |
| 56 | + expect(warning).to.equal( |
| 57 | + 'DeprecationWarning: testDeprecationFlags option [maxScan]' + defaultMessage |
| 58 | + ); |
| 59 | + |
| 60 | + // ensure each following line is from the stack trace, i.e. 'at config.deprecatedOptions.forEach.deprecatedOption' |
| 61 | + split.pop(); |
| 62 | + split.forEach(s => { |
| 63 | + expect(s.trim()).to.match(/^at/); |
| 64 | + }); |
| 65 | + |
| 66 | + done(); |
| 67 | + } |
| 68 | + ); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + it('node --throw-deprecation flag should throw error when deprecated function is called', { |
| 73 | + metadata: { requires: { node: '>=6.0.0' } }, |
| 74 | + test: function(done) { |
| 75 | + exec( |
| 76 | + 'node --throw-deprecation ./test/tools/deprecate_warning_test_program.js this_arg_should_never_print', |
| 77 | + (err, stdout, stderr) => { |
| 78 | + expect(stderr).to.not.be.empty; |
| 79 | + expect(err).to.not.be.null; |
| 80 | + expect(err) |
| 81 | + .to.have.own.property('code') |
| 82 | + .that.equals(1); |
| 83 | + |
| 84 | + // ensure stdout is empty, i.e. that the program threw an error before reaching the console.log statement |
| 85 | + expect(stdout).to.be.empty; |
| 86 | + done(); |
| 87 | + } |
| 88 | + ); |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + it('test behavior for classes with an associated logger', function() { |
| 93 | + const fakeClass = new ClassWithLogger(); |
| 94 | + const logger = fakeClass.getLogger(); |
| 95 | + const stub = sinon.stub(logger, 'warn'); |
| 96 | + |
| 97 | + fakeClass.f({ maxScan: 5, snapshot: true }); |
| 98 | + fakeClass.f({ maxScan: 5, snapshot: true }); |
| 99 | + expect(stub).to.have.been.calledTwice; |
| 100 | + ensureCalledWith(stub, [ |
| 101 | + 'f option [maxScan] is deprecated and will be removed in a later version.', |
| 102 | + 'f option [snapshot] is deprecated and will be removed in a later version.' |
| 103 | + ]); |
| 104 | + }); |
| 105 | + |
| 106 | + it('test behavior for classes without an associated logger', function() { |
| 107 | + const fakeClass = new ClassWithoutLogger(); |
| 108 | + |
| 109 | + function func() { |
| 110 | + fakeClass.f({ maxScan: 5, snapshot: true }); |
| 111 | + } |
| 112 | + |
| 113 | + expect(func).to.not.throw(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('test behavior for classes with an undefined logger', function() { |
| 117 | + const fakeClass = new ClassWithUndefinedLogger(); |
| 118 | + |
| 119 | + function func() { |
| 120 | + fakeClass.f({ maxScan: 5, snapshot: true }); |
| 121 | + } |
| 122 | + |
| 123 | + expect(func).to.not.throw(); |
| 124 | + }); |
| 125 | +}); |
0 commit comments