-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.jest.ts
executable file
·283 lines (245 loc) · 9.68 KB
/
log.jest.ts
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
import * as fsUtil from './fsUtil';
import { Log } from './log'; const log = new Log('log.jest');
describe('log', () => {
let gLog: any;
let gLogMsg: string;
let gOut: any // for process.stdout.write
let gProcessMsg: string;
function myLog(msg:string) {
gLogMsg = msg;
gLog(msg)
}
function myTransient(msg:string) {
gProcessMsg = msg;
}
// avoid logging of level change
function setLevel(level:string) {
log.level(level);
gLogMsg = undefined;
}
beforeAll(() => {
gLog = console.log;
console.log = myLog;
});
afterAll(() => {
console.log = gLog;
try {
const file = log.logFile();
if (fsUtil.isFileSync(file)) fsUtil.removeSync(file);
} catch(err) { console.log(`afterAll: ${err}`); }
});
beforeEach(() => {
log.level(Log.INFO);
log.prefix('log.jest');
log.logFile(''); // set default
gLogMsg = undefined;
});
afterEach(() => {
return log.logFile('');
});
describe('reporting functions', () => {
it('should print default info', () => {
Log.log.info("global");
expect(Log.log.level()).toBe(Log.INFO);
expect(gLogMsg).toMatch(/^.*?\d\d\d\d\d\d\d\d \d\d:\d\d:\d\d.\d\d\d INFO.*?global/);
});
it('should print info', () => {
log.info("yes");
expect(log.level()).toBe(Log.INFO);
expect(gLogMsg).toMatch(/INFO.*yes/);
});
it('should print warning', () => {
log.warn("alert");
expect(log.level()).toBe(Log.INFO);
expect(gLogMsg).toMatch(/WARN.*alert/);
});
it('should not print debug at INFO level', () => {
setLevel(Log.INFO);
log.debug('yes');
expect(gLogMsg).toBe(undefined);
});
it('should set DEBUG level', () => {
expect(log.level()).toBe(Log.INFO);
log.level(Log.DEBUG);
expect(log.level()).toBe(Log.DEBUG);
return expect(gLogMsg).toMatch(/new .*? log level DEBUG \(was INFO\)/);
});
it('should print info at DEBUG level', () => {
setLevel(Log.DEBUG);
log.info('yes');
expect(log.level()).toBe(Log.DEBUG);
return expect(gLogMsg).toMatch(/INFO.*yes/);
});
it('should print debug at DEBUG level', () => {
log.level(Log.DEBUG);
expect(log.level()).toBe(Log.DEBUG);
expect(gLogMsg.match(/new .*? log level DEBUG \(was INFO\)/)).not.toBe(null);
log.debug('yes');
return expect(gLogMsg).toMatch(/DEBUG.*yes/);
});
it('should print error for invalid level', () => {
log.level('BOGUS');
expect(log.level()).toBe(Log.INFO);
return expect(gLogMsg).toMatch(/ unkown level BOGUS; log level remains INFO/);
});
it('should print error', () => {
log.error('yes');
return expect(gLogMsg).toMatch(/ERROR.*yes/);
});
});
describe('transient reporting', () => {
beforeAll(() => {
gLog('redirecting process.stdout.write...')
gOut = process.stdout.write
process.stdout.write = myTransient as any
});
afterAll(() => {
process.stdout.write = gOut
gLog('...process.stdout.write restored')
try {
const file = log.logFile();
if (fsUtil.isFileSync(file)) fsUtil.removeSync(file);
} catch(err) { gLog(`afterAll: ${err}`); }
});
it('should print transient', () => {
log.transient("yes");
log.transient("no maybe");
expect(log.level()).toBe(Log.INFO);
const msg = gProcessMsg.slice(gProcessMsg.indexOf('no'))
expect(msg.length).toBe(Log.transientLength+1)
expect(gProcessMsg).toMatch(/^.*?\d\d\d\d\d\d\d\d \d\d:\d\d:\d\d.\d\d\d log.jest INFO.*?no maybe *?\r/)
log.info('never')
expect(gProcessMsg.split('\n')[1]).toMatch(/^.*?\d\d\d\d\d\d\d\d \d\d:\d\d:\d\d.\d\d\d log.jest INFO.*?never$/)
});
})
describe('global levels', () => {
beforeEach(() =>{
log.level(Log.WARN, true); // set global level
log.level(null); // unset local level
});
it('should set a global level', ()=>{
expect(log.level()).toBe(Log.WARN);
return expect(gLogMsg).toMatch(/new global log level WARN \(was INFO\)/);
});
it('should print WARN', ()=>{
gLogMsg = undefined;
log.warn('yes');
return expect(gLogMsg).toMatch(/WARN.*yes/);
});
it('should not print INFO', ()=>{
gLogMsg = undefined;
log.info('yes');
return expect(gLogMsg).toEqual(undefined);
});
test('local should override global', ()=>{
log.level(Log.INFO); // unset local level
gLogMsg = undefined;
log.info('yes');
return expect(gLogMsg).toMatch(/INFO.*yes/);
});
});
describe('formatting', () => {
afterEach(() => log.format(null)); // reset the date format
it(`should return prefix 'log.jest'`, ()=>
expect(log.prefix()).toEqual('log.jest')
);
it('should print prefix "test"', () => {
log.prefix('test');
log.info('yes');
return expect(gLogMsg).toMatch(/test INFO.*yes/);
});
it('should print date', () => {
log.config({format:'%M/%DD/%YY'});
log.info('yes');
return expect(gLogMsg).toMatch(/\d+\/\d+\/\d+ log.jest INFO.*yes/);
});
it('should return format string', () =>
expect(log.format()).toBe('%YYYY%MM%DD %hh:%mm:%ss.%jjj')
);
it('should return format string', () =>
expect(log.format('%M/%DD/%YY')).toBe('%M/%DD/%YY')
);
});
describe('color', () => {
beforeEach(() => {
log.config({colors: true});
return log.info('colors');
});
afterEach(() => log.config({colors: false})); // reset the date format
it('should print prefix "test"', () => expect(gLogMsg).toMatch(/colors/));
});
describe('inspection', () => {
let result:string;
beforeEach(() => {
log.config({colors: true});
result = log.inspect({a:{b:'c'}}, {depth:3});
});
afterEach(() => log.config({colors: false})); // reset the date format
it('should print colored inspection', () => {
expect(result).toMatch(/a(.)*?: {/);
expect(result).toMatch(/b(.)*?: 'c'/);
});
});
describe('log file', () => {
it('should be created next to Gruntfile for default path', () => {
expect.assertions(5);
try {
const file = log.logFile('');
expect(file).toBeDefined();
expect(file).toMatch(/log-%YYYY-%MM-%DD.txt/);
expect(gLogMsg).toMatch(/log.jest INFO.*now logging to file log-\d\d\d\d-\d\d-\d\d.txt/g);
const file2 = log.logFile();
expect(file2).toMatch(/log-\d\d\d\d-\d\d-\d\d.txt/);
expect(fsUtil.isFileSync(file2)).toBe(true);
fsUtil.removeSync(file2);
} catch(e) {
gLog(e);
}
});
it('should be created at a specific path', () => {
expect.assertions(4);
try {
const file = log.logFile('./bin/log-%YYYY-%MM-%DD.txt');
// gLog(`...setting log file ${file}`);
expect(file).toBeDefined();
expect(file).toMatch(/bin\/log-%YYYY-%MM-%DD.txt/);
// gLog(`...msg match '${gMsg}'`);
expect(gLogMsg).toMatch(/log.jest INFO.*now logging to file/g);
const file2 = log.logFile();
const b = fsUtil.isFileSync(file2);
expect(b).toBe(true);
fsUtil.removeSync(file2);
} catch(e) {
gLog(e);
}
});
it('should be created with format template', () => {
expect.assertions(4);
try {
const file = log.logFile('%YY%M%Dlog.log');
gLog(`...setting log file template ${file}`);
expect(file).toBeDefined();
expect(file).toMatch(/%YY%M%Dlog.log/);
gLog(`...msg match '${gLogMsg}'`);
expect(gLogMsg).toMatch(/log.jest INFO.*now logging to file/g);
const file2 = log.logFile();
expect(fsUtil.isFileSync(file2)).toBe(true);
fsUtil.removeSync(file2);
} catch(e) {
gLog(e);
}
});
it('should be stopped for missing paths', async () => {
expect.assertions(1);
const file = await log.logFile('/missing/log.txt');
expect(file).toBe(undefined)
});
it('should be disabled', async () => {
const file = await log.logFile(null);
expect(file).toBe(undefined);
expect(gLogMsg).toMatch(/disabling logfile/);
await log.info('unlogged entry');
return expect(gLogMsg).toMatch(/log.jest INFO.*unlogged entry/);
});
});
});