Skip to content

Commit

Permalink
tests - depend on a test preload.js (#226588)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero authored Aug 26, 2024
1 parent 8be3fa2 commit 9f04aaf
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import assert from 'assert';
import { context, ipcRenderer, process, webFrame, webUtils } from 'vs/base/parts/sandbox/electron-sandbox/globals';
import { ipcRenderer, process, webFrame, webUtils } from 'vs/base/parts/sandbox/electron-sandbox/globals';
import { ensureNoDisposablesAreLeakedInTestSuite } from 'vs/base/test/common/utils';

suite('Sandbox', () => {
Expand All @@ -14,10 +14,6 @@ suite('Sandbox', () => {
assert.ok(typeof webFrame.setZoomLevel === 'function');
assert.ok(typeof process.platform === 'string');
assert.ok(typeof webUtils.getPathForFile === 'function');

const config = await context.resolveConfiguration();
assert.ok(config);
assert.ok(context.configuration());
});

ensureNoDisposablesAreLeakedInTestSuite();
Expand Down
2 changes: 1 addition & 1 deletion test/unit/electron/index.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ app.on('ready', () => {
width: 800,
show: false,
webPreferences: {
preload: path.join(__dirname, '..', '..', '..', 'src', 'vs', 'base', 'parts', 'sandbox', 'electron-sandbox', 'preload.js'), // ensure similar environment as VSCode as tests may depend on this
preload: path.join(__dirname, 'preload.js'), // ensure similar environment as VSCode as tests may depend on this
additionalArguments: [`--vscode-window-config=vscode:test-vscode-window-config`],
nodeIntegration: true,
contextIsolation: false,
Expand Down
2 changes: 1 addition & 1 deletion test/unit/electron/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ app.on('ready', () => {
width: 800,
show: false,
webPreferences: {
preload: path.join(__dirname, '..', '..', '..', 'src', 'vs', 'base', 'parts', 'sandbox', 'electron-sandbox', 'preload.js'), // ensure similar environment as VSCode as tests may depend on this
preload: path.join(__dirname, 'preload.js'),
additionalArguments: [`--vscode-window-config=vscode:test-vscode-window-config`],
nodeIntegration: true,
contextIsolation: false,
Expand Down
92 changes: 92 additions & 0 deletions test/unit/electron/preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

// @ts-check
(function () {
'use strict';

const { ipcRenderer, webFrame, contextBridge, webUtils } = require('electron');

const globals = {

ipcRenderer: {

send(channel, ...args) {
ipcRenderer.send(channel, ...args);
},

invoke(channel, ...args) {
return ipcRenderer.invoke(channel, ...args);
},

on(channel, listener) {
ipcRenderer.on(channel, listener);

return this;
},

once(channel, listener) {
ipcRenderer.once(channel, listener);

return this;
},

removeListener(channel, listener) {
ipcRenderer.removeListener(channel, listener);

return this;
}
},

webFrame: {

setZoomLevel(level) {
if (typeof level === 'number') {
webFrame.setZoomLevel(level);
}
}
},

webUtils: {

getPathForFile(file) {
return webUtils.getPathForFile(file);
}
},

process: {
get platform() { return process.platform; },
get arch() { return process.arch; },
get env() { return { ...process.env }; },
get versions() { return process.versions; },
get type() { return 'renderer'; },
get execPath() { return process.execPath; },

cwd() {
return process.env['VSCODE_CWD'] || process.execPath.substr(0, process.execPath.lastIndexOf(process.platform === 'win32' ? '\\' : '/'));
},

getProcessMemoryInfo() {
return process.getProcessMemoryInfo();
},

on(type, callback) {
// @ts-ignore
process.on(type, callback);
}
},
};

if (process.contextIsolated) {
try {
contextBridge.exposeInMainWorld('vscode', globals);
} catch (error) {
console.error(error);
}
} else {
// @ts-ignore
window.vscode = globals;
}
}());

0 comments on commit 9f04aaf

Please sign in to comment.