-
Notifications
You must be signed in to change notification settings - Fork 87
/
test.js
795 lines (676 loc) · 26 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
var assert = require('assert')
var basename = require('path').basename
var bufferConcat = require('../lib/compat').bufferConcat
var depd = require('..')
var mylib = require('./fixtures/my-lib')
var path = require('path')
var script = path.join(__dirname, 'fixtures', 'script.js')
var spawn = require('child_process').spawn
var strictlib = require('./fixtures/strict-lib')
describe('depd(namespace)', function () {
it('creates deprecated function', function () {
assert.equal(typeof depd('test'), 'function')
})
it('requires namespace', function () {
assert.throws(depd.bind(null), /namespace.*required/)
})
})
describe('deprecate(message)', function () {
it('should log namespace', function () {
function callold() { mylib.old() }
assert.ok(captureStderr(callold).indexOf('my-lib') !== -1)
})
it('should log deprecation', function () {
function callold() { mylib.old() }
assert.ok(captureStderr(callold).indexOf('deprecate') !== -1)
})
it('should log message', function () {
function callold() { mylib.old() }
assert.ok(captureStderr(callold).indexOf('old') !== -1)
})
it('should log call site', function () {
function callold() { mylib.old() }
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(/\.js:[0-9]+:[0-9]+/.test(stderr))
})
it('should log call site from strict lib', function () {
function callold() { strictlib.old() }
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(/\.js:[0-9]+:[0-9]+/.test(stderr))
})
it('should log call site regardless of Error.stackTraceLimit', function () {
function callold() { mylib.old() }
var limit = Error.stackTraceLimit
try {
Error.stackTraceLimit = 1
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(/\.js:[0-9]+:[0-9]+/.test(stderr))
} finally {
Error.stackTraceLimit = limit
}
})
it('should log call site within eval', function () {
function callold() { eval('mylib.old()') }
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(stderr.indexOf('<anonymous>:1:') !== -1)
assert.ok(/\.js:[0-9]+:[0-9]+/.test(stderr))
})
it('should log call site within strict', function () {
function callold() { 'use strict'; mylib.old() }
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(/\.js:[0-9]+:[0-9]+/.test(stderr))
})
it('should only warn once per call site', function () {
function callold() {
for (var i = 0; i < 5; i++) {
mylib.old() // single call site
process.stderr.write('invoke ' + i + '\n')
}
}
var stderr = captureStderr(callold)
assert.equal(stderr.split('deprecated').length, 2)
assert.equal(stderr.split('invoke').length, 6)
})
it('should warn for different fns on same call site', function () {
var prop
function callold() {
mylib[prop]() // call from same site
}
prop = 'old'
assert.ok(captureStderr(callold).indexOf(basename(__filename)) !== -1)
prop = 'old2'
assert.ok(captureStderr(callold).indexOf(basename(__filename)) !== -1)
})
it('should warn for different calls on same line', function () {
function callold() {
mylib.old(), mylib.old()
}
var stderr = captureStderr(callold)
var fileline = stderr.match(/\.js:[0-9]+:/)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.equal(stderr.split('deprecated').length, 3)
assert.equal(stderr.split(fileline[0]).length, 3)
})
describe('when message omitted', function () {
it('should generate message for method call on named function', function () {
function callold() { mylib.automsgnamed() }
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(stderr.indexOf('deprecated') !== -1)
assert.ok(stderr.indexOf(' automsgnamed ') !== -1)
})
it('should generate message for function call on named function', function () {
function callold() {
var fn = mylib.automsgnamed
fn()
}
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(stderr.indexOf('deprecated') !== -1)
assert.ok(stderr.indexOf(' automsgnamed ') !== -1)
})
it('should generate message for method call on unnamed function', function () {
function callold() { mylib.automsg() }
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(stderr.indexOf('deprecated') !== -1)
assert.ok(stderr.indexOf(' exports.automsg ') !== -1)
})
it('should generate message for function call on unnamed function', function () {
function callold() {
var fn = mylib.automsg
fn()
}
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(stderr.indexOf('deprecated') !== -1)
assert.ok(stderr.indexOf(' exports.automsg ') !== -1)
})
it('should generate message for function call on anonymous function', function () {
function callold() { mylib.automsganon() }
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(stderr.indexOf('deprecated') !== -1)
assert.ok(/ exports\.automsganon | <anonymous@[^:]+:[0-9]+:[0-9]+> /.test(stderr))
})
describe('in strict mode library', function () {
it('should generate message for method call on named function', function () {
function callold() { strictlib.automsgnamed() }
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(stderr.indexOf('deprecated') !== -1)
assert.ok(stderr.indexOf(' automsgnamed ') !== -1)
})
it('should generate message for function call on named function', function () {
function callold() {
var fn = strictlib.automsgnamed
fn()
}
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(stderr.indexOf('deprecated') !== -1)
assert.ok(stderr.indexOf(' automsgnamed ') !== -1)
})
it('should generate message for method call on unnamed function', function () {
function callold() { strictlib.automsg() }
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(stderr.indexOf('deprecated') !== -1)
assert.ok(stderr.indexOf(' exports.automsg ') !== -1)
})
it('should generate message for function call on unnamed function', function () {
function callold() {
var fn = strictlib.automsg
fn()
}
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(stderr.indexOf('deprecated') !== -1)
assert.ok(stderr.indexOf(' exports.automsg ') !== -1)
})
it('should generate message for function call on anonymous function', function () {
function callold() { strictlib.automsganon() }
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(stderr.indexOf('deprecated') !== -1)
assert.ok(/ exports\.automsganon | <anonymous@[^:]+:[0-9]+:[0-9]+> /.test(stderr))
})
})
})
describe('when output supports colors', function () {
var stderr
before(function () {
function callold() { mylib.old() }
stderr = captureStderr(callold, true)
})
it('should log in color', function () {
assert.notEqual(stderr, '')
assert.ok(stderr.indexOf('\x1b[') !== -1)
})
it('should log namespace', function () {
assert.ok(stderr.indexOf('my-lib') !== -1)
})
it('should log deprecation', function () {
assert.ok(stderr.indexOf('deprecate') !== -1)
})
it('should log message', function () {
assert.ok(stderr.indexOf('old') !== -1)
})
it('should log call site', function () {
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(/\.js:[0-9]+:[0-9]+/.test(stderr))
})
})
describe('when output does not support colors', function () {
var stderr
before(function () {
function callold() { mylib.old() }
stderr = captureStderr(callold, false)
})
it('should not log in color', function () {
assert.notEqual(stderr, '')
assert.ok(stderr.indexOf('\x1b[') === -1)
})
it('should log namespace', function () {
assert.ok(stderr.indexOf('my-lib') !== -1)
})
it('should log timestamp', function () {
assert.ok(/\w+, \d+ \w+ \d{4} \d{2}:\d{2}:\d{2} \w{3}/.test(stderr))
})
it('should log deprecation', function () {
assert.ok(stderr.indexOf('deprecate') !== -1)
})
it('should log message', function () {
assert.ok(stderr.indexOf('old') !== -1)
})
it('should log call site', function () {
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(/\.js:[0-9]+:[0-9]+/.test(stderr))
})
})
})
describe('deprecate.function(fn, message)', function () {
it('should throw when not given function', function () {
var deprecate = depd('test')
assert.throws(deprecate.function.bind(deprecate, 2), /fn.*function/)
})
it('should log on call to function', function () {
function callold() { mylib.oldfn() }
assert.ok(captureStderr(callold).indexOf(' oldfn ') !== -1)
})
it('should have same arity', function () {
assert.equal(mylib.oldfn.length, 2)
})
it('should pass arguments', function () {
var ret
function callold() { ret = mylib.oldfn(1, 2) }
assert.ok(captureStderr(callold).indexOf(' oldfn ') !== -1)
assert.equal(ret, 2)
})
it('should show call site outside scope', function () {
function callold() { mylib.layerfn() }
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(' oldfn ') !== -1)
assert.ok(/test.js:[0-9]+:[0-9]+/.test(stderr))
})
it('should show call site outside scope from strict lib', function () {
function callold() { strictlib.layerfn() }
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(' oldfn ') !== -1)
assert.ok(/test.js:[0-9]+:[0-9]+/.test(stderr))
})
it('should only warn once per call site', function () {
function callold() {
for (var i = 0; i < 5; i++) {
mylib.oldfn() // single call site
process.stderr.write('invoke ' + i + '\n')
}
}
var stderr = captureStderr(callold)
assert.equal(stderr.split('deprecated').length, 2)
assert.equal(stderr.split('invoke').length, 6)
})
it('should handle rapid calling of deprecated thing', function () {
function callold() {
for (var i = 0; i < 10000; i++) {
mylib.oldfn()
}
}
var stderr = captureStderr(callold)
assert.equal(stderr.split('deprecated').length, 2)
})
it('should warn for different calls on same line', function () {
function callold() {
mylib.oldfn(), mylib.oldfn()
}
var stderr = captureStderr(callold)
var fileline = stderr.match(/\.js:[0-9]+:/)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.equal(stderr.split('deprecated').length, 3)
assert.equal(stderr.split(fileline[0]).length, 3)
})
describe('when message omitted', function () {
it('should generate message for method call on named function', function () {
function callold() { mylib.oldfnauto() }
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(stderr.indexOf('deprecated') !== -1)
assert.ok(stderr.indexOf(' fn ') !== -1)
assert.ok(/ at [^:]+test\.js:/.test(stderr))
})
it('should generate message for method call on anonymous function', function () {
function callold() { mylib.oldfnautoanon() }
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(stderr.indexOf('deprecated') !== -1)
assert.ok(/ <anonymous@[^:]+my-lib\.js:[0-9]+:[0-9]+> /.test(stderr))
assert.ok(/ at [^:]+test\.js:/.test(stderr))
})
describe('in strict mode library', function () {
it('should generate message for method call on named function', function () {
function callold() { strictlib.oldfnauto() }
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(stderr.indexOf('deprecated') !== -1)
assert.ok(stderr.indexOf(' fn ') !== -1)
assert.ok(/ at [^:]+test\.js:/.test(stderr))
})
it('should generate message for method call on anonymous function', function () {
function callold() { strictlib.oldfnautoanon() }
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(stderr.indexOf('deprecated') !== -1)
assert.ok(/ <anonymous@[^:]+strict-lib\.js:[0-9]+:[0-9]+> /.test(stderr))
assert.ok(/ at [^:]+test\.js:/.test(stderr))
})
})
})
})
describe('deprecate.property(obj, prop, message)', function () {
it('should throw when given primitive', function () {
var deprecate = depd('test')
assert.throws(deprecate.property.bind(deprecate, 2), /obj.*object/)
})
it('should throw when given missing property', function () {
var deprecate = depd('test')
var obj = {}
assert.throws(deprecate.property.bind(deprecate, obj, 'blargh'), /property.*owner/)
})
it('should throw when given non-configurable property', function () {
var deprecate = depd('test')
var obj = {}
Object.defineProperty(obj, 'thing', {value: 'thingie'})
assert.throws(deprecate.property.bind(deprecate, obj, 'thing'), /property.*configurable/)
})
it('should log on access to property', function () {
function callprop() { mylib.propa }
var stderr = captureStderr(callprop)
assert.ok(stderr.indexOf(' deprecated ') !== -1)
assert.ok(stderr.indexOf(' propa gone ') !== -1)
})
it('should log on setting property', function () {
var val
function callprop() { val = mylib.propa }
function setprop() { mylib.propa = 'newval' }
var stderr = captureStderr(setprop)
assert.ok(stderr.indexOf(' deprecated ') !== -1)
assert.ok(stderr.indexOf(' propa gone ') !== -1)
assert.ok(captureStderr(callprop).indexOf(' deprecated ') !== -1)
assert.equal(val, 'newval')
})
it('should only warn once per call site', function () {
function callold() {
for (var i = 0; i < 5; i++) {
mylib.propa // single call site
process.stderr.write('access ' + i + '\n')
}
}
var stderr = captureStderr(callold)
assert.equal(stderr.split('deprecated').length, 2)
assert.equal(stderr.split('access').length, 6)
})
it('should warn for different accesses on same line', function () {
function callold() {
mylib.propa, mylib.propa
}
var stderr = captureStderr(callold)
var fileline = stderr.match(/\.js:[0-9]+:/)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.equal(stderr.split('deprecated').length, 3)
assert.equal(stderr.split(fileline[0]).length, 3)
})
it('should show call site outside scope', function () {
function callold() { mylib.layerprop() }
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(' propa ') !== -1)
assert.ok(/test.js:[0-9]+:[0-9]+/.test(stderr))
})
it('should show call site outside scope from strict lib', function () {
function callold() { strictlib.layerprop() }
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(' propa ') !== -1)
assert.ok(/test.js:[0-9]+:[0-9]+/.test(stderr))
})
describe('when obj is a function', function () {
it('should log on access to property on function', function () {
function callprop() { mylib.fnprop.propa }
var stderr = captureStderr(callprop)
assert.ok(stderr.indexOf(' deprecated ') !== -1)
assert.ok(stderr.indexOf(' fn propa gone ') !== -1)
})
it('should generate message on named function', function () {
function callprop() { mylib.fnprop.propautomsg }
var stderr = captureStderr(callprop)
assert.ok(stderr.indexOf(' deprecated ') !== -1)
assert.ok(stderr.indexOf(' thefn.propautomsg ') !== -1)
})
describe('in strict mode library', function () {
it('should log on access to property on function', function () {
function callprop() { strictlib.fnprop.propa }
var stderr = captureStderr(callprop)
assert.ok(stderr.indexOf(' deprecated ') !== -1)
assert.ok(stderr.indexOf(' fn propa gone ') !== -1)
})
it('should generate message on named function', function () {
function callprop() { strictlib.fnprop.propautomsg }
var stderr = captureStderr(callprop)
assert.ok(stderr.indexOf(' deprecated ') !== -1)
assert.ok(stderr.indexOf(' thefn.propautomsg ') !== -1)
})
})
})
describe('when value descriptor', function () {
it('should log on access and set', function () {
function callold() { mylib.propa }
function setold() { mylib.propa = 'val' }
assert.ok(captureStderr(callold).indexOf(' deprecated ') !== -1)
assert.ok(captureStderr(setold).indexOf(' deprecated ') !== -1)
})
it('should not log on set to non-writable', function () {
function callold() { mylib.propget }
function setold() { mylib.propget = 'val' }
assert.ok(captureStderr(callold).indexOf(' deprecated ') !== -1)
assert.equal(captureStderr(setold), '')
})
})
describe('when accessor descriptor', function () {
it('should log on access and set', function () {
function callold() { mylib.propdyn }
function setold() { mylib.propdyn = 'val' }
assert.ok(captureStderr(callold).indexOf(' deprecated ') !== -1)
assert.ok(captureStderr(setold).indexOf(' deprecated ') !== -1)
})
it('should not log on access when no accessor', function () {
function callold() { mylib.propsetter }
assert.equal(captureStderr(callold), '')
})
it('should not log on set when no setter', function () {
function callold() { mylib.propgetter = 'val' }
assert.equal(captureStderr(callold), '')
})
})
describe('when message omitted', function () {
it('should generate message for method call on named function', function () {
function callold() { mylib.propauto }
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(stderr.indexOf('deprecated') !== -1)
assert.ok(stderr.indexOf(' propauto ') !== -1)
assert.ok(/ at [^:]+test\.js:/.test(stderr))
})
describe('in strict mode library', function () {
it('should generate message for method call on named function', function () {
function callold() { strictlib.propauto }
var stderr = captureStderr(callold)
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(stderr.indexOf('deprecated') !== -1)
assert.ok(stderr.indexOf(' propauto ') !== -1)
assert.ok(/ at [^:]+test\.js:/.test(stderr))
})
})
})
})
describe('process.on(\'deprecation\', fn)', function () {
var error
var stderr
before(function () {
process.on('deprecation', ondeprecation)
function callold() { mylib.old() }
stderr = captureStderr(callold)
})
after(function () {
process.removeListener('deprecation', ondeprecation)
})
function ondeprecation(err) { error = err }
it('should not write when listener exists', function () {
assert.equal(stderr, '')
})
it('should emit error', function () {
assert.ok(error)
})
it('should emit DeprecationError', function () {
assert.equal(error.name, 'DeprecationError')
})
it('should emit error with message', function () {
assert.equal(error.message, 'old')
})
it('should emit error with namespace', function () {
assert.equal(error.namespace, 'my-lib')
})
it('should emit error with proper [[Class]]', function () {
assert.equal(Object.prototype.toString.call(error), '[object Error]')
})
it('should be instanceof Error', function () {
assert.ok(error instanceof Error)
})
it('should emit error with proper stack', function () {
var stack = error.stack.split('\n')
assert.equal(stack[0], 'DeprecationError: my-lib deprecated old')
assert.ok(/ at callold \(.+test\.js:[0-9]+:[0-9]+\)/.test(stack[1]))
})
it('should have writable properties', function () {
error.name = 'bname'
assert.equal(error.name, 'bname')
error.message = 'bmessage'
assert.equal(error.message, 'bmessage')
error.stack = 'bstack'
assert.equal(error.stack, 'bstack')
})
})
describe('process.env.NO_DEPRECATION', function () {
var error
function ondeprecation(err) { error = err }
beforeEach(function () {
error = null
})
afterEach(function () {
process.removeListener('deprecation', ondeprecation)
})
after(function () {
process.env.NO_DEPRECATION = ''
})
it('should suppress given namespace', function () {
process.env.NO_DEPRECATION = 'old-lib'
var oldlib = require('./fixtures/old-lib')
assert.equal(captureStderr(oldlib.old), '')
assert.notEqual(captureStderr(oldlib.old2), '')
})
it('should suppress multiple namespaces', function () {
process.env.NO_DEPRECATION = 'cool-lib,neat-lib'
var coollib = require('./fixtures/cool-lib')
assert.equal(captureStderr(coollib.cool), '')
assert.equal(captureStderr(coollib.neat), '')
})
it('should be case-insensitive', function () {
process.env.NO_DEPRECATION = 'NEW-LIB'
var newlib = require('./fixtures/new-lib')
assert.equal(captureStderr(newlib.old), '')
})
it('should emit "deprecation" events anyway', function () {
process.env.NO_DEPRECATION = 'thing-lib'
var thinglib = require('./fixtures/thing-lib')
process.on('deprecation', ondeprecation)
assert.equal(captureStderr(thinglib.old), '')
assert.equal(error.namespace, 'thing-lib')
})
describe('when *', function () {
it('should suppress any namespace', function () {
process.env.NO_DEPRECATION = '*'
var multilib = require('./fixtures/multi-lib')
assert.equal(captureStderr(multilib.old), '')
assert.equal(captureStderr(multilib.old2), '')
})
})
})
describe('process.env.TRACE_DEPRECATION', function () {
before(function () {
process.env.TRACE_DEPRECATION = 'trace-lib'
})
after(function () {
process.env.TRACE_DEPRECATION = ''
})
it('should trace given namespace', function () {
var tracelib = require('./fixtures/trace-lib')
function callold() { tracelib.old() }
assert.ok(captureStderr(callold).indexOf(' trace-lib deprecated old\n at callold (') !== -1)
})
it('should not trace non-given namespace', function () {
var tracelib = require('./fixtures/trace-lib')
function callold() { tracelib.old2() }
assert.ok(captureStderr(callold).indexOf(' trace-lib-other deprecated old2 at ') !== -1)
})
describe('when output supports colors', function () {
var stderr
before(function () {
var tracelib = require('./fixtures/trace-lib')
function callold() { tracelib.old() }
stderr = captureStderr(callold, true)
})
it('should log in color', function () {
assert.notEqual(stderr, '')
assert.ok(stderr.indexOf('\x1b[') !== -1)
})
it('should log namespace', function () {
assert.ok(stderr.indexOf('trace-lib') !== -1)
})
it('should log call site in color', function () {
assert.ok(stderr.indexOf(basename(__filename)) !== -1)
assert.ok(/\x1b\[\d+mat callold \(/.test(stderr))
})
})
})
describe('node script.js', function () {
it('should display deprecation message', function (done) {
captureChildStderr([script], function (err, stderr) {
if (err) return done(err)
var filename = path.relative(process.cwd(), script)
stderr = stderr.replace(/\w+, \d+ \w+ \d+ \d+:\d+:\d+ \w+/, '__timestamp__')
assert.equal(stderr, '__timestamp__ my-cool-module deprecated oldfunction at ' + filename + ':7:10\n')
done()
})
})
})
;(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
describe('node --no-deprecation script.js', function () {
it('should suppress deprecation message', function (done) {
captureChildStderr(['--no-deprecation', script], function (err, stderr) {
if (err) return done(err)
assert.equal(stderr, '')
done()
})
})
})
describe('node --trace-deprecation script.js', function () {
it('should suppress deprecation message', function (done) {
captureChildStderr(['--trace-deprecation', script], function (err, stderr) {
if (err) return done(err)
stderr = stderr.replace(/\w+, \d+ \w+ \d+ \d+:\d+:\d+ \w+/, '__timestamp__')
assert.ok(stderr.indexOf('__timestamp__ my-cool-module deprecated oldfunction\n at run (' + script + ':7:10)\n at') === 0)
done()
})
})
})
}())
function captureChildStderr(args, callback) {
var chunks = []
var env = {PATH: process.env.PATH}
var exec = process.argv[0]
var proc = spawn(exec, args, {
env: env
})
proc.stdout.resume()
proc.stderr.on('data', function ondata(chunk) {
chunks.push(chunk)
})
proc.on('error', callback)
proc.on('exit', function () {
var stderr = bufferConcat(chunks).toString('utf8')
callback(null, stderr)
})
}
function captureStderr(fn, color) {
var chunks = []
var isTTY = process.stderr.isTTY
var write = process.stderr.write
process.stderr.isTTY = Boolean(color)
process.stderr.write = function write(chunk, encoding) {
chunks.push(new Buffer(chunk, encoding))
}
try {
fn()
} finally {
process.stderr.isTTY = isTTY
process.stderr.write = write
}
return bufferConcat(chunks).toString('utf8')
}