"vscode:/vscode.git/clone" did not exist on "d2af0a8c765fda9bf652719e59299e9746a95fb5"
logging.test.ts 3.39 KB
Newer Older
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import assert from 'assert/strict';

import globals from 'common/globals/unittest';
import { Logger, getLogger, getRobustLogger } from 'common/log';

/* test cases */

function testDebugLevel() {
    stream.reset();
    globals.args.logLevel = 'debug';

    writeLogs1(getLogger('DebugLogger'));
    writeLogs2(getLogger('DebugLogger'));

    assert.match(stream.content, /DebugLogger/);

    assert.match(stream.content, /debug-message/);
    assert.match(stream.content, /info-message/);
    assert.match(stream.content, /warning-message/);
    assert.match(stream.content, /error-message/);
    assert.match(stream.content, /critical-message/);

    assert.equal(stderr, '');
}

function testWarningLevel() {
    stream.reset();
    globals.args.logLevel = 'warning';

    writeLogs1(getLogger('WarningLogger1'));
    writeLogs2(getLogger('WarningLogger2'));

    assert.match(stream.content, /WarningLogger1/);
    assert.match(stream.content, /WarningLogger2/);

    assert.doesNotMatch(stream.content, /debug-message/);
    assert.doesNotMatch(stream.content, /info-message/);
    assert.match(stream.content, /warning-message/);
    assert.match(stream.content, /error-message/);
    assert.match(stream.content, /critical-message/);

    assert.equal(stderr, '');
}

function testRobust() {
    stream.reset();
    globals.args.logLevel = 'info';

    const logger = getRobustLogger('RobustLogger');
    writeLogs1(logger);
    stream.error = true;
    writeLogs2(logger);

    assert.match(stream.content, /RobustLogger/);
    assert.doesNotMatch(stream.content, /debug-message/);
    assert.match(stream.content, /info-message/);
    assert.match(stream.content, /warning-message/);

    assert.match(stderr, /stream-error/);
    assert.match(stderr, /error-message/);
    assert.match(stderr, /critical-message/);
}

/* register */
describe('## logging ##', () => {
    before(beforeHook);

    it('low log level', testDebugLevel);
    it('high log level', testWarningLevel);
    it('robust', testRobust);

    after(afterHook);
});

/* helpers */

function writeLogs1(logger: Logger) {
    logger.debug('debug-message');
    logger.info(1, '2', 'info-message', 3);
    logger.warning(undefined, [null, 'warning-message']);
}

function writeLogs2(logger: Logger) {
    const recursiveObject: any = { 'message': 'error-message' };
    recursiveObject.recursive = recursiveObject;
    logger.error(recursiveObject);
    logger.critical(new Error('critical-message'));
}

class TestLogStream {
    public content: string = '';
    public error: boolean = false;

    reset(): void {
        this.content = '';
        this.error = false;
    }

    writeLine(line: string): void {
        if (this.error) {
            throw new Error('stream-error');
        }
        this.content += line + '\n';
    }

    writeLineSync(line: string): void {
        if (this.error) {
            throw new Error('stream-error');
        }
        this.content += line + '\n';
    }

    async close(): Promise<void> {
        /* empty */
    }
}

/* environment */

const stream = new TestLogStream();
const origConsoleError = console.error;
let stderr: string = '';

async function beforeHook() {
    globals.logStream = stream;
    console.error = (...args: any[]) => { stderr += args.join(' ') + '\n'; };
}

async function afterHook() {
    globals.reset();
    console.error = origConsoleError;
}