forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules-spec.ts
248 lines (215 loc) · 10 KB
/
modules-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
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
import { expect } from 'chai';
import * as path from 'node:path';
import * as fs from 'node:fs';
import { BrowserWindow } from 'electron/main';
import { ifdescribe, ifit } from './lib/spec-helpers';
import { closeAllWindows } from './lib/window-helpers';
import * as childProcess from 'node:child_process';
import { once } from 'node:events';
const Module = require('node:module');
const nativeModulesEnabled = !process.env.ELECTRON_SKIP_NATIVE_MODULE_TESTS;
describe('modules support', () => {
const fixtures = path.join(__dirname, 'fixtures');
describe('third-party module', () => {
ifdescribe(nativeModulesEnabled)('echo', () => {
afterEach(closeAllWindows);
it('can be required in renderer', async () => {
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
w.loadURL('about:blank');
await expect(
w.webContents.executeJavaScript(
"{ require('@electron-ci/echo'); null }"
)
).to.be.fulfilled();
});
it('can be required in node binary', async function () {
const child = childProcess.fork(path.join(fixtures, 'module', 'echo.js'));
const [msg] = await once(child, 'message');
expect(msg).to.equal('ok');
});
ifit(process.platform === 'win32')('can be required if electron.exe is renamed', () => {
const testExecPath = path.join(path.dirname(process.execPath), 'test.exe');
fs.copyFileSync(process.execPath, testExecPath);
try {
const fixture = path.join(fixtures, 'module', 'echo-renamed.js');
expect(fs.existsSync(fixture)).to.be.true();
const child = childProcess.spawnSync(testExecPath, [fixture]);
expect(child.status).to.equal(0);
} finally {
fs.unlinkSync(testExecPath);
}
});
});
const enablePlatforms: NodeJS.Platform[] = [
'linux',
'darwin',
'win32'
];
ifdescribe(nativeModulesEnabled && enablePlatforms.includes(process.platform))('module that use uv_dlopen', () => {
it('can be required in renderer', async () => {
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
w.loadURL('about:blank');
await expect(w.webContents.executeJavaScript('{ require(\'@electron-ci/uv-dlopen\'); null }')).to.be.fulfilled();
});
it('can be required in node binary', async function () {
const child = childProcess.fork(path.join(fixtures, 'module', 'uv-dlopen.js'));
const [exitCode] = await once(child, 'exit');
expect(exitCode).to.equal(0);
});
});
describe('q', () => {
describe('Q.when', () => {
it('emits the fullfil callback', (done) => {
const Q = require('q');
Q(true).then((val: boolean) => {
expect(val).to.be.true();
done();
});
});
});
});
describe('require(\'electron/...\')', () => {
it('require(\'electron/lol\') should throw in the main process', () => {
expect(() => {
require('electron/lol');
}).to.throw(/Cannot find module 'electron\/lol'/);
});
it('require(\'electron/lol\') should throw in the renderer process', async () => {
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
w.loadURL('about:blank');
await expect(w.webContents.executeJavaScript('{ require(\'electron/lol\'); null }')).to.eventually.be.rejected();
});
it('require(\'electron\') should not throw in the main process', () => {
expect(() => {
require('electron');
}).to.not.throw();
});
it('require(\'electron\') should not throw in the renderer process', async () => {
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
w.loadURL('about:blank');
await expect(w.webContents.executeJavaScript('{ require(\'electron\'); null }')).to.be.fulfilled();
});
it('require(\'electron/main\') should not throw in the main process', () => {
expect(() => {
require('electron/main');
}).to.not.throw();
});
it('require(\'electron/main\') should not throw in the renderer process', async () => {
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
w.loadURL('about:blank');
await expect(w.webContents.executeJavaScript('{ require(\'electron/main\'); null }')).to.be.fulfilled();
});
it('require(\'electron/renderer\') should not throw in the main process', () => {
expect(() => {
require('electron/renderer');
}).to.not.throw();
});
it('require(\'electron/renderer\') should not throw in the renderer process', async () => {
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
w.loadURL('about:blank');
await expect(w.webContents.executeJavaScript('{ require(\'electron/renderer\'); null }')).to.be.fulfilled();
});
it('require(\'electron/common\') should not throw in the main process', () => {
expect(() => {
require('electron/common');
}).to.not.throw();
});
it('require(\'electron/common\') should not throw in the renderer process', async () => {
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
w.loadURL('about:blank');
await expect(w.webContents.executeJavaScript('{ require(\'electron/common\'); null }')).to.be.fulfilled();
});
});
describe('coffeescript', () => {
it('can be registered and used to require .coffee files', () => {
expect(() => {
require('coffeescript').register();
}).to.not.throw();
expect(require('./fixtures/module/test.coffee')).to.be.true();
});
});
});
describe('global variables', () => {
describe('process', () => {
it('can be declared in a module', () => {
expect(require('./fixtures/module/declare-process')).to.equal('declared process');
});
});
describe('global', () => {
it('can be declared in a module', () => {
expect(require('./fixtures/module/declare-global')).to.equal('declared global');
});
});
describe('Buffer', () => {
it('can be declared in a module', () => {
expect(require('./fixtures/module/declare-buffer')).to.equal('declared Buffer');
});
});
});
describe('Module._nodeModulePaths', () => {
describe('when the path is inside the resources path', () => {
it('does not include paths outside of the resources path', () => {
let modulePath = process.resourcesPath;
expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
path.join(process.resourcesPath, 'node_modules')
]);
modulePath = process.resourcesPath + '-foo';
const nodeModulePaths = Module._nodeModulePaths(modulePath);
expect(nodeModulePaths).to.include(path.join(modulePath, 'node_modules'));
expect(nodeModulePaths).to.include(path.join(modulePath, '..', 'node_modules'));
modulePath = path.join(process.resourcesPath, 'foo');
expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
path.join(process.resourcesPath, 'foo', 'node_modules'),
path.join(process.resourcesPath, 'node_modules')
]);
modulePath = path.join(process.resourcesPath, 'node_modules', 'foo');
expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
path.join(process.resourcesPath, 'node_modules')
]);
modulePath = path.join(process.resourcesPath, 'node_modules', 'foo', 'bar');
expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
path.join(process.resourcesPath, 'node_modules', 'foo', 'bar', 'node_modules'),
path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
path.join(process.resourcesPath, 'node_modules')
]);
modulePath = path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules', 'bar');
expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules', 'bar', 'node_modules'),
path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
path.join(process.resourcesPath, 'node_modules')
]);
});
});
describe('when the path is outside the resources path', () => {
it('includes paths outside of the resources path', () => {
const modulePath = path.resolve('/foo');
expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
path.join(modulePath, 'node_modules'),
path.resolve('/node_modules')
]);
});
});
});
describe('require', () => {
describe('when loaded URL is not file: protocol', () => {
afterEach(closeAllWindows);
it('searches for module under app directory', async () => {
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
w.loadURL('about:blank');
const result = await w.webContents.executeJavaScript('typeof require("q").when');
expect(result).to.equal('function');
});
});
});
describe('esm', () => {
it('can load the built-in "electron" module via ESM import', async () => {
await expect(import('electron')).to.eventually.be.ok();
});
it('the built-in "electron" module loaded via ESM import has the same exports as the CJS module', async () => {
const esmElectron = await import('electron');
const cjsElectron = require('electron');
expect(Object.keys(esmElectron)).to.deep.equal(Object.keys(cjsElectron));
});
});
});