forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging-spec.ts
192 lines (180 loc) · 8.22 KB
/
logging-spec.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
import { app } from 'electron';
import { expect } from 'chai';
import { startRemoteControlApp, ifdescribe } from './lib/spec-helpers';
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import * as uuid from 'uuid';
import { once } from 'node:events';
function isTestingBindingAvailable () {
try {
process._linkedBinding('electron_common_testing');
return true;
} catch {
return false;
}
}
// This test depends on functions that are only available when DCHECK_IS_ON.
ifdescribe(isTestingBindingAvailable())('logging', () => {
it('does not log by default', async () => {
// ELECTRON_ENABLE_LOGGING is turned on in the appveyor config.
const { ELECTRON_ENABLE_LOGGING: _, ...envWithoutEnableLogging } = process.env;
const rc = await startRemoteControlApp([], { env: envWithoutEnableLogging });
const stderrComplete = new Promise<string>(resolve => {
let stderr = '';
rc.process.stderr!.on('data', function listener (chunk) {
stderr += chunk.toString('utf8');
});
rc.process.on('close', () => { resolve(stderr); });
});
const [hasLoggingSwitch, hasLoggingVar] = await rc.remotely(() => {
// Make sure we're actually capturing stderr by logging a known value to
// stderr.
console.error('SENTINEL');
process._linkedBinding('electron_common_testing').log(0, 'TEST_LOG');
setTimeout(() => { process.exit(0); });
return [require('electron').app.commandLine.hasSwitch('enable-logging'), !!process.env.ELECTRON_ENABLE_LOGGING];
});
expect(hasLoggingSwitch).to.be.false();
expect(hasLoggingVar).to.be.false();
const stderr = await stderrComplete;
// stderr should include the sentinel but not the LOG() message.
expect(stderr).to.match(/SENTINEL/);
expect(stderr).not.to.match(/TEST_LOG/);
});
it('logs to stderr when --enable-logging is passed', async () => {
const rc = await startRemoteControlApp(['--enable-logging']);
const stderrComplete = new Promise<string>(resolve => {
let stderr = '';
rc.process.stderr!.on('data', function listener (chunk) {
stderr += chunk.toString('utf8');
});
rc.process.on('close', () => { resolve(stderr); });
});
rc.remotely(() => {
process._linkedBinding('electron_common_testing').log(0, 'TEST_LOG');
setTimeout(() => { require('electron').app.quit(); });
});
const stderr = await stderrComplete;
expect(stderr).to.match(/TEST_LOG/);
});
it('logs to stderr when ELECTRON_ENABLE_LOGGING is set', async () => {
const rc = await startRemoteControlApp([], { env: { ...process.env, ELECTRON_ENABLE_LOGGING: '1' } });
const stderrComplete = new Promise<string>(resolve => {
let stderr = '';
rc.process.stderr!.on('data', function listener (chunk) {
stderr += chunk.toString('utf8');
});
rc.process.on('close', () => { resolve(stderr); });
});
rc.remotely(() => {
process._linkedBinding('electron_common_testing').log(0, 'TEST_LOG');
setTimeout(() => { require('electron').app.quit(); });
});
const stderr = await stderrComplete;
expect(stderr).to.match(/TEST_LOG/);
});
it('logs to a file in the user data dir when --enable-logging=file is passed', async () => {
const rc = await startRemoteControlApp(['--enable-logging=file']);
const userDataDir = await rc.remotely(() => {
const { app } = require('electron');
process._linkedBinding('electron_common_testing').log(0, 'TEST_LOG');
setTimeout(() => { app.quit(); });
return app.getPath('userData');
});
await once(rc.process, 'exit');
const logFilePath = path.join(userDataDir, 'electron_debug.log');
const stat = await fs.stat(logFilePath);
expect(stat.isFile()).to.be.true();
const contents = await fs.readFile(logFilePath, 'utf8');
expect(contents).to.match(/TEST_LOG/);
});
it('logs to a file in the user data dir when ELECTRON_ENABLE_LOGGING=file is set', async () => {
const rc = await startRemoteControlApp([], { env: { ...process.env, ELECTRON_ENABLE_LOGGING: 'file' } });
const userDataDir = await rc.remotely(() => {
const { app } = require('electron');
process._linkedBinding('electron_common_testing').log(0, 'TEST_LOG');
setTimeout(() => { app.quit(); });
return app.getPath('userData');
});
await once(rc.process, 'exit');
const logFilePath = path.join(userDataDir, 'electron_debug.log');
const stat = await fs.stat(logFilePath);
expect(stat.isFile()).to.be.true();
const contents = await fs.readFile(logFilePath, 'utf8');
expect(contents).to.match(/TEST_LOG/);
});
it('logs to the given file when --log-file is passed', async () => {
const logFilePath = path.join(app.getPath('temp'), 'test-log-file-' + uuid.v4());
const rc = await startRemoteControlApp(['--enable-logging', '--log-file=' + logFilePath]);
rc.remotely(() => {
process._linkedBinding('electron_common_testing').log(0, 'TEST_LOG');
setTimeout(() => { require('electron').app.quit(); });
});
await once(rc.process, 'exit');
const stat = await fs.stat(logFilePath);
expect(stat.isFile()).to.be.true();
const contents = await fs.readFile(logFilePath, 'utf8');
expect(contents).to.match(/TEST_LOG/);
});
it('logs to the given file when ELECTRON_LOG_FILE is set', async () => {
const logFilePath = path.join(app.getPath('temp'), 'test-log-file-' + uuid.v4());
const rc = await startRemoteControlApp([], { env: { ...process.env, ELECTRON_ENABLE_LOGGING: '1', ELECTRON_LOG_FILE: logFilePath } });
rc.remotely(() => {
process._linkedBinding('electron_common_testing').log(0, 'TEST_LOG');
setTimeout(() => { require('electron').app.quit(); });
});
await once(rc.process, 'exit');
const stat = await fs.stat(logFilePath);
expect(stat.isFile()).to.be.true();
const contents = await fs.readFile(logFilePath, 'utf8');
expect(contents).to.match(/TEST_LOG/);
});
it('does not lose early log messages when logging to a given file with --log-file', async () => {
const logFilePath = path.join(app.getPath('temp'), 'test-log-file-' + uuid.v4());
const rc = await startRemoteControlApp(['--enable-logging', '--log-file=' + logFilePath, '--boot-eval=process._linkedBinding(\'electron_common_testing\').log(0, \'EARLY_LOG\')']);
rc.remotely(() => {
process._linkedBinding('electron_common_testing').log(0, 'LATER_LOG');
setTimeout(() => { require('electron').app.quit(); });
});
await once(rc.process, 'exit');
const stat = await fs.stat(logFilePath);
expect(stat.isFile()).to.be.true();
const contents = await fs.readFile(logFilePath, 'utf8');
expect(contents).to.match(/EARLY_LOG/);
expect(contents).to.match(/LATER_LOG/);
});
it('enables logging when switch is appended during first tick', async () => {
const rc = await startRemoteControlApp(['--boot-eval=require(\'electron\').app.commandLine.appendSwitch(\'--enable-logging\')']);
const stderrComplete = new Promise<string>(resolve => {
let stderr = '';
rc.process.stderr!.on('data', function listener (chunk) {
stderr += chunk.toString('utf8');
});
rc.process.on('close', () => { resolve(stderr); });
});
rc.remotely(() => {
process._linkedBinding('electron_common_testing').log(0, 'TEST_LOG');
setTimeout(() => { require('electron').app.quit(); });
});
const stderr = await stderrComplete;
expect(stderr).to.match(/TEST_LOG/);
});
it('respects --log-level', async () => {
const rc = await startRemoteControlApp(['--enable-logging', '--log-level=1']);
const stderrComplete = new Promise<string>(resolve => {
let stderr = '';
rc.process.stderr!.on('data', function listener (chunk) {
stderr += chunk.toString('utf8');
});
rc.process.on('close', () => { resolve(stderr); });
});
rc.remotely(() => {
process._linkedBinding('electron_common_testing').log(0, 'TEST_INFO_LOG');
process._linkedBinding('electron_common_testing').log(1, 'TEST_WARNING_LOG');
setTimeout(() => { require('electron').app.quit(); });
});
const stderr = await stderrComplete;
expect(stderr).to.match(/TEST_WARNING_LOG/);
expect(stderr).not.to.match(/TEST_INFO_LOG/);
});
});