refactor: use node scheme imports in spec (#38487)

Co-authored-by: Milan Burda <miburda@microsoft.com>
This commit is contained in:
Milan Burda 2023-06-15 16:42:27 +02:00 committed by GitHub
parent bf1ba4a857
commit d78f37ec8f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
105 changed files with 533 additions and 352 deletions

View file

@ -1,12 +1,12 @@
import { expect } from 'chai';
import * as childProcess from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import * as util from 'util';
import * as childProcess from 'node:child_process';
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as util from 'node:util';
import { getRemoteContext, ifdescribe, ifit, itremote, useRemoteContext } from './lib/spec-helpers';
import { webContents } from 'electron/main';
import { EventEmitter } from 'stream';
import { once } from 'events';
import { EventEmitter } from 'node:stream';
import { once } from 'node:events';
const mainFixturesPath = path.resolve(__dirname, 'fixtures');
@ -30,7 +30,7 @@ describe('node feature', () => {
describe('child_process.fork', () => {
itremote('works in current process', async (fixtures: string) => {
const child = require('child_process').fork(require('path').join(fixtures, 'module', 'ping.js'));
const child = require('node:child_process').fork(require('node:path').join(fixtures, 'module', 'ping.js'));
const message = new Promise<any>(resolve => child.once('message', resolve));
child.send('message');
const msg = await message;
@ -39,7 +39,7 @@ describe('node feature', () => {
itremote('preserves args', async (fixtures: string) => {
const args = ['--expose_gc', '-test', '1'];
const child = require('child_process').fork(require('path').join(fixtures, 'module', 'process_args.js'), args);
const child = require('node:child_process').fork(require('node:path').join(fixtures, 'module', 'process_args.js'), args);
const message = new Promise<any>(resolve => child.once('message', resolve));
child.send('message');
const msg = await message;
@ -47,7 +47,7 @@ describe('node feature', () => {
}, [fixtures]);
itremote('works in forked process', async (fixtures: string) => {
const child = require('child_process').fork(require('path').join(fixtures, 'module', 'fork_ping.js'));
const child = require('node:child_process').fork(require('node:path').join(fixtures, 'module', 'fork_ping.js'));
const message = new Promise<any>(resolve => child.once('message', resolve));
child.send('message');
const msg = await message;
@ -55,7 +55,7 @@ describe('node feature', () => {
}, [fixtures]);
itremote('works in forked process when options.env is specified', async (fixtures: string) => {
const child = require('child_process').fork(require('path').join(fixtures, 'module', 'fork_ping.js'), [], {
const child = require('node:child_process').fork(require('node:path').join(fixtures, 'module', 'fork_ping.js'), [], {
path: process.env.PATH
});
const message = new Promise<any>(resolve => child.once('message', resolve));
@ -65,7 +65,7 @@ describe('node feature', () => {
}, [fixtures]);
itremote('has String::localeCompare working in script', async (fixtures: string) => {
const child = require('child_process').fork(require('path').join(fixtures, 'module', 'locale-compare.js'));
const child = require('node:child_process').fork(require('node:path').join(fixtures, 'module', 'locale-compare.js'));
const message = new Promise<any>(resolve => child.once('message', resolve));
child.send('message');
const msg = await message;
@ -73,7 +73,7 @@ describe('node feature', () => {
}, [fixtures]);
itremote('has setImmediate working in script', async (fixtures: string) => {
const child = require('child_process').fork(require('path').join(fixtures, 'module', 'set-immediate.js'));
const child = require('node:child_process').fork(require('node:path').join(fixtures, 'module', 'set-immediate.js'));
const message = new Promise<any>(resolve => child.once('message', resolve));
child.send('message');
const msg = await message;
@ -81,7 +81,7 @@ describe('node feature', () => {
}, [fixtures]);
itremote('pipes stdio', async (fixtures: string) => {
const child = require('child_process').fork(require('path').join(fixtures, 'module', 'process-stdout.js'), { silent: true });
const child = require('node:child_process').fork(require('node:path').join(fixtures, 'module', 'process-stdout.js'), { silent: true });
let data = '';
child.stdout.on('data', (chunk: any) => {
data += String(chunk);
@ -93,7 +93,7 @@ describe('node feature', () => {
itremote('works when sending a message to a process forked with the --eval argument', async () => {
const source = "process.on('message', (message) => { process.send(message) })";
const forked = require('child_process').fork('--eval', [source]);
const forked = require('node:child_process').fork('--eval', [source]);
const message = new Promise(resolve => forked.once('message', resolve));
forked.send('hello');
const msg = await message;
@ -102,7 +102,7 @@ describe('node feature', () => {
it('has the electron version in process.versions', async () => {
const source = 'process.send(process.versions)';
const forked = require('child_process').fork('--eval', [source]);
const forked = require('node:child_process').fork('--eval', [source]);
const [message] = await once(forked, 'message');
expect(message)
.to.have.own.property('electron')
@ -113,7 +113,7 @@ describe('node feature', () => {
describe('child_process.spawn', () => {
itremote('supports spawning Electron as a node process via the ELECTRON_RUN_AS_NODE env var', async (fixtures: string) => {
const child = require('child_process').spawn(process.execPath, [require('path').join(fixtures, 'module', 'run-as-node.js')], {
const child = require('node:child_process').spawn(process.execPath, [require('node:path').join(fixtures, 'module', 'run-as-node.js')], {
env: {
ELECTRON_RUN_AS_NODE: true
}
@ -268,7 +268,7 @@ describe('node feature', () => {
describe('setTimeout in fs callback', () => {
itremote('does not crash', async (filename: string) => {
await new Promise(resolve => require('fs').readFile(filename, () => {
await new Promise(resolve => require('node:fs').readFile(filename, () => {
setTimeout(resolve, 0);
}));
}, [__filename]);
@ -277,7 +277,7 @@ describe('node feature', () => {
describe('error thrown in renderer process node context', () => {
itremote('gets emitted as a process uncaughtException event', async (filename: string) => {
const error = new Error('boo!');
require('fs').readFile(filename, () => {
require('node:fs').readFile(filename, () => {
throw error;
});
await new Promise<void>((resolve, reject) => {
@ -295,9 +295,9 @@ describe('node feature', () => {
describe('URL handling in the renderer process', () => {
itremote('can successfully handle WHATWG URLs constructed by Blink', (fixtures: string) => {
const url = new URL('file://' + require('path').resolve(fixtures, 'pages', 'base-page.html'));
const url = new URL('file://' + require('node:path').resolve(fixtures, 'pages', 'base-page.html'));
expect(() => {
require('fs').createReadStream(url);
require('node:fs').createReadStream(url);
}).to.not.throw();
}, [fixtures]);
});
@ -308,7 +308,7 @@ describe('node feature', () => {
});
itremote('works from the timers module', async () => {
await new Promise(resolve => require('timers').setTimeout(resolve, 10));
await new Promise(resolve => require('node:timers').setTimeout(resolve, 10));
});
});
@ -323,7 +323,7 @@ describe('node feature', () => {
});
itremote('can be scheduled in time from timers module', async () => {
const { setInterval, clearInterval } = require('timers');
const { setInterval, clearInterval } = require('node:timers');
await new Promise<void>(resolve => {
const id = setInterval(() => {
clearInterval(id);
@ -361,12 +361,12 @@ describe('node feature', () => {
ifdescribe(process.platform === 'darwin')('net.connect', () => {
itremote('emit error when connect to a socket path without listeners', async (fixtures: string) => {
const socketPath = require('path').join(require('os').tmpdir(), 'electron-test.sock');
const script = require('path').join(fixtures, 'module', 'create_socket.js');
const child = require('child_process').fork(script, [socketPath]);
const socketPath = require('node:path').join(require('node:os').tmpdir(), 'electron-test.sock');
const script = require('node:path').join(fixtures, 'module', 'create_socket.js');
const child = require('node:child_process').fork(script, [socketPath]);
const code = await new Promise(resolve => child.once('exit', resolve));
expect(code).to.equal(0);
const client = require('net').connect(socketPath);
const client = require('node:net').connect(socketPath);
const error = await new Promise<any>(resolve => client.once('error', resolve));
expect(error.code).to.equal('ECONNREFUSED');
}, [fixtures]);
@ -399,7 +399,7 @@ describe('node feature', () => {
});
itremote('does not crash for crypto operations', () => {
const crypto = require('crypto');
const crypto = require('node:crypto');
const data = 'lG9E+/g4JmRmedDAnihtBD4Dfaha/GFOjd+xUOQI05UtfVX3DjUXvrS98p7kZQwY3LNhdiFo7MY5rGft8yBuDhKuNNag9vRx/44IuClDhdQ=';
const key = 'q90K9yBqhWZnAMCMTOJfPQ==';
const cipherText = '{"error_code":114,"error_message":"Tham số không hợp lệ","data":null}';
@ -413,7 +413,7 @@ describe('node feature', () => {
});
itremote('does not crash when using crypto.diffieHellman() constructors', () => {
const crypto = require('crypto');
const crypto = require('node:crypto');
crypto.createDiffieHellman('abc');
crypto.createDiffieHellman('abc', 2);
@ -425,7 +425,7 @@ describe('node feature', () => {
});
itremote('does not crash when calling crypto.createPrivateKey() with an unsupported algorithm', () => {
const crypto = require('crypto');
const crypto = require('node:crypto');
const ed448 = {
crv: 'Ed448',
@ -481,58 +481,58 @@ describe('node feature', () => {
describe('vm.runInNewContext', () => {
itremote('should not crash', () => {
require('vm').runInNewContext('');
require('node:vm').runInNewContext('');
});
});
describe('crypto', () => {
useRemoteContext();
itremote('should list the ripemd160 hash in getHashes', () => {
expect(require('crypto').getHashes()).to.include('ripemd160');
expect(require('node:crypto').getHashes()).to.include('ripemd160');
});
itremote('should be able to create a ripemd160 hash and use it', () => {
const hash = require('crypto').createHash('ripemd160');
const hash = require('node:crypto').createHash('ripemd160');
hash.update('electron-ripemd160');
expect(hash.digest('hex')).to.equal('fa7fec13c624009ab126ebb99eda6525583395fe');
});
itremote('should list aes-{128,256}-cfb in getCiphers', () => {
expect(require('crypto').getCiphers()).to.include.members(['aes-128-cfb', 'aes-256-cfb']);
expect(require('node:crypto').getCiphers()).to.include.members(['aes-128-cfb', 'aes-256-cfb']);
});
itremote('should be able to create an aes-128-cfb cipher', () => {
require('crypto').createCipheriv('aes-128-cfb', '0123456789abcdef', '0123456789abcdef');
require('node:crypto').createCipheriv('aes-128-cfb', '0123456789abcdef', '0123456789abcdef');
});
itremote('should be able to create an aes-256-cfb cipher', () => {
require('crypto').createCipheriv('aes-256-cfb', '0123456789abcdef0123456789abcdef', '0123456789abcdef');
require('node:crypto').createCipheriv('aes-256-cfb', '0123456789abcdef0123456789abcdef', '0123456789abcdef');
});
itremote('should be able to create a bf-{cbc,cfb,ecb} ciphers', () => {
require('crypto').createCipheriv('bf-cbc', Buffer.from('0123456789abcdef'), Buffer.from('01234567'));
require('crypto').createCipheriv('bf-cfb', Buffer.from('0123456789abcdef'), Buffer.from('01234567'));
require('crypto').createCipheriv('bf-ecb', Buffer.from('0123456789abcdef'), Buffer.from('01234567'));
require('node:crypto').createCipheriv('bf-cbc', Buffer.from('0123456789abcdef'), Buffer.from('01234567'));
require('node:crypto').createCipheriv('bf-cfb', Buffer.from('0123456789abcdef'), Buffer.from('01234567'));
require('node:crypto').createCipheriv('bf-ecb', Buffer.from('0123456789abcdef'), Buffer.from('01234567'));
});
itremote('should list des-ede-cbc in getCiphers', () => {
expect(require('crypto').getCiphers()).to.include('des-ede-cbc');
expect(require('node:crypto').getCiphers()).to.include('des-ede-cbc');
});
itremote('should be able to create an des-ede-cbc cipher', () => {
const key = Buffer.from('0123456789abcdeff1e0d3c2b5a49786', 'hex');
const iv = Buffer.from('fedcba9876543210', 'hex');
require('crypto').createCipheriv('des-ede-cbc', key, iv);
require('node:crypto').createCipheriv('des-ede-cbc', key, iv);
});
itremote('should not crash when getting an ECDH key', () => {
const ecdh = require('crypto').createECDH('prime256v1');
const ecdh = require('node:crypto').createECDH('prime256v1');
expect(ecdh.generateKeys()).to.be.an.instanceof(Buffer);
expect(ecdh.getPrivateKey()).to.be.an.instanceof(Buffer);
});
itremote('should not crash when generating DH keys or fetching DH fields', () => {
const dh = require('crypto').createDiffieHellman('modp15');
const dh = require('node:crypto').createDiffieHellman('modp15');
expect(dh.generateKeys()).to.be.an.instanceof(Buffer);
expect(dh.getPublicKey()).to.be.an.instanceof(Buffer);
expect(dh.getPrivateKey()).to.be.an.instanceof(Buffer);
@ -541,7 +541,7 @@ describe('node feature', () => {
});
itremote('should not crash when creating an ECDH cipher', () => {
const crypto = require('crypto');
const crypto = require('node:crypto');
const dh = crypto.createECDH('prime256v1');
dh.generateKeys();
dh.setPrivateKey(dh.getPrivateKey());