11import { expect } from 'chai' ;
2- import * as sinon from 'sinon' ;
32import * as logger from '../src/logger' ;
43
54const SUPPORTS_STRUCTURED_LOGS =
@@ -8,52 +7,51 @@ const SUPPORTS_STRUCTURED_LOGS =
87describe ( `logger (${
98 SUPPORTS_STRUCTURED_LOGS ? 'structured' : 'unstructured'
109} )`, ( ) => {
11- let sandbox : sinon . SinonSandbox ;
12- let stdoutStub : sinon . SinonStub ;
13- let stderrStub : sinon . SinonStub ;
10+ let stdoutWrite = process . stdout . write . bind ( process . stdout ) ;
11+ let stderrWrite = process . stderr . write . bind ( process . stdout ) ;
12+ let lastOut : string ;
13+ let lastErr : string ;
1414
1515 beforeEach ( ( ) => {
16- sandbox = sinon . createSandbox ( ) ;
17- stdoutStub = sandbox . stub ( process . stdout , 'write' ) ;
18- stderrStub = sandbox . stub ( process . stderr , 'write' ) ;
16+ process . stdout . write = ( msg : Buffer | string , cb ?: any ) : boolean => {
17+ lastOut = msg as string ;
18+ return stdoutWrite ( msg , cb ) ;
19+ } ;
20+ process . stderr . write = ( msg : Buffer | string , cb ?: any ) : boolean => {
21+ lastErr = msg as string ;
22+ return stderrWrite ( msg , cb ) ;
23+ } ;
1924 } ) ;
2025
21- function expectOutput ( stdStub : sinon . SinonStub , entry : any ) {
26+ afterEach ( ( ) => {
27+ process . stdout . write = stdoutWrite ;
28+ process . stderr . write = stderrWrite ;
29+ } ) ;
30+
31+ function expectOutput ( last : string , entry : any ) {
2232 if ( SUPPORTS_STRUCTURED_LOGS ) {
23- return expect (
24- JSON . parse ( ( stdStub . getCalls ( ) [ 0 ] . args [ 0 ] as string ) . trim ( ) )
25- ) . to . deep . eq ( entry ) ;
33+ return expect ( JSON . parse ( last . trim ( ) ) ) . to . deep . eq ( entry ) ;
2634 } else {
2735 // legacy logging is not structured, but do a sanity check
28- return expect ( stdStub . getCalls ( ) [ 0 ] . args [ 0 ] ) . to . include ( entry . message ) ;
36+ return expect ( last ) . to . include ( entry . message ) ;
2937 }
3038 }
3139
3240 function expectStdout ( entry : any ) {
33- return expectOutput ( stdoutStub , entry ) ;
41+ return expectOutput ( lastOut , entry ) ;
3442 }
3543
3644 function expectStderr ( entry : any ) {
37- return expectOutput ( stderrStub , entry ) ;
45+ return expectOutput ( lastErr , entry ) ;
3846 }
3947
4048 describe ( 'logging methods' , ( ) => {
41- let writeStub : sinon . SinonStub ;
42- beforeEach ( ( ) => {
43- writeStub = sinon . stub ( logger , 'write' ) ;
44- } ) ;
45-
46- afterEach ( ( ) => {
47- writeStub . restore ( ) ;
48- } ) ;
49-
5049 it ( 'should coalesce arguments into the message' , ( ) => {
5150 logger . log ( 'hello' , { middle : 'obj' } , 'end message' ) ;
5251 expectStdout ( {
5352 severity : 'INFO' ,
5453 message : "hello { middle: 'obj' } end message" ,
5554 } ) ;
56- sandbox . restore ( ) ; // to avoid swallowing test runner output
5755 } ) ;
5856
5957 it ( 'should merge structured data from the last argument' , ( ) => {
@@ -63,7 +61,6 @@ describe(`logger (${
6361 message : 'hello world' ,
6462 additional : 'context' ,
6563 } ) ;
66- sandbox . restore ( ) ; // to avoid swallowing test runner output
6764 } ) ;
6865
6966 it ( 'should not recognize null as a structured logging object' , ( ) => {
@@ -72,13 +69,46 @@ describe(`logger (${
7269 severity : 'INFO' ,
7370 message : 'hello world null' ,
7471 } ) ;
75- sandbox . restore ( ) ; // to avoid swallowing test runner output
7672 } ) ;
7773 } ) ;
7874
7975 describe ( 'write' , ( ) => {
8076 describe ( 'structured logging' , ( ) => {
8177 describe ( 'write' , ( ) => {
78+ it ( 'should remove circular references' , ( ) => {
79+ const circ : any = { b : 'foo' } ;
80+ circ . circ = circ ;
81+
82+ const entry : logger . LogEntry = {
83+ severity : 'ERROR' ,
84+ message : 'testing circular' ,
85+ circ,
86+ } ;
87+ logger . write ( entry ) ;
88+ expectStderr ( {
89+ severity : 'ERROR' ,
90+ message : 'testing circular' ,
91+ circ : { b : 'foo' , circ : '[Circular]' } ,
92+ } ) ;
93+ } ) ;
94+
95+ it ( 'should remove circular references in arrays' , ( ) => {
96+ const circ : any = { b : 'foo' } ;
97+ circ . circ = [ circ ] ;
98+
99+ const entry : logger . LogEntry = {
100+ severity : 'ERROR' ,
101+ message : 'testing circular' ,
102+ circ,
103+ } ;
104+ logger . write ( entry ) ;
105+ expectStderr ( {
106+ severity : 'ERROR' ,
107+ message : 'testing circular' ,
108+ circ : { b : 'foo' , circ : [ '[Circular]' ] } ,
109+ } ) ;
110+ } ) ;
111+
82112 for ( const severity of [ 'DEBUG' , 'INFO' , 'NOTICE' ] ) {
83113 it ( `should output ${ severity } severity to stdout` , ( ) => {
84114 let entry : logger . LogEntry = {
@@ -87,7 +117,6 @@ describe(`logger (${
87117 } ;
88118 logger . write ( entry ) ;
89119 expectStdout ( entry ) ;
90- sandbox . restore ( ) ; // to avoid swallowing test runner output
91120 } ) ;
92121 }
93122
@@ -105,7 +134,6 @@ describe(`logger (${
105134 } ;
106135 logger . write ( entry ) ;
107136 expectStderr ( entry ) ;
108- sandbox . restore ( ) ; // to avoid swallowing test runner output
109137 } ) ;
110138 }
111139 } ) ;
0 commit comments