api-shell-spec.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { BrowserWindow, app } from 'electron/main';
  2. import { shell } from 'electron/common';
  3. import { closeAllWindows } from './window-helpers';
  4. import { emittedOnce } from './events-helpers';
  5. import * as http from 'http';
  6. import * as fs from 'fs-extra';
  7. import * as path from 'path';
  8. import { AddressInfo } from 'net';
  9. import { expect } from 'chai';
  10. import { ifit } from './spec-helpers';
  11. import { execSync } from 'child_process';
  12. describe('shell module', () => {
  13. describe('shell.openExternal()', () => {
  14. let envVars: Record<string, string | undefined> = {};
  15. beforeEach(function () {
  16. envVars = {
  17. display: process.env.DISPLAY,
  18. de: process.env.DE,
  19. browser: process.env.BROWSER
  20. };
  21. });
  22. afterEach(async () => {
  23. // reset env vars to prevent side effects
  24. if (process.platform === 'linux') {
  25. process.env.DE = envVars.de;
  26. process.env.BROWSER = envVars.browser;
  27. process.env.DISPLAY = envVars.display;
  28. }
  29. });
  30. afterEach(closeAllWindows);
  31. it('opens an external link', async () => {
  32. let url = 'http://127.0.0.1';
  33. let requestReceived;
  34. if (process.platform === 'linux') {
  35. process.env.BROWSER = '/bin/true';
  36. process.env.DE = 'generic';
  37. process.env.DISPLAY = '';
  38. requestReceived = Promise.resolve();
  39. } else if (process.platform === 'darwin') {
  40. // On the Mac CI machines, Safari tries to ask for a password to the
  41. // code signing keychain we set up to test code signing (see
  42. // https://github.com/electron/electron/pull/19969#issuecomment-526278890),
  43. // so use a blur event as a crude proxy.
  44. const w = new BrowserWindow({ show: true });
  45. requestReceived = emittedOnce(w, 'blur');
  46. } else {
  47. const server = http.createServer((req, res) => {
  48. res.end();
  49. });
  50. await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
  51. requestReceived = new Promise(resolve => server.on('connection', () => resolve()));
  52. url = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
  53. }
  54. await Promise.all([
  55. shell.openExternal(url),
  56. requestReceived
  57. ]);
  58. });
  59. });
  60. describe('shell.moveItemToTrash()', () => {
  61. it('moves an item to the trash', async () => {
  62. const dir = await fs.mkdtemp(path.resolve(app.getPath('temp'), 'electron-shell-spec-'));
  63. const filename = path.join(dir, 'temp-to-be-deleted');
  64. await fs.writeFile(filename, 'dummy-contents');
  65. const result = shell.moveItemToTrash(filename);
  66. expect(result).to.be.true();
  67. expect(fs.existsSync(filename)).to.be.false();
  68. });
  69. it('returns false when called with a nonexistent path', () => {
  70. const filename = path.join(app.getPath('temp'), 'does-not-exist');
  71. const result = shell.moveItemToTrash(filename);
  72. expect(result).to.be.false();
  73. });
  74. ifit(process.platform === 'darwin')('returns false when file has immutable flag', async () => {
  75. const dir = await fs.mkdtemp(path.resolve(app.getPath('temp'), 'electron-shell-spec-'));
  76. const tempPath = path.join(dir, 'locked-file');
  77. await fs.writeFile(tempPath, 'delete me if you can');
  78. // https://ss64.com/osx/chflags.html
  79. execSync(`chflags uchg ${tempPath}`);
  80. expect(shell.moveItemToTrash(tempPath)).to.be.false();
  81. expect(await fs.pathExists(tempPath)).to.be.true();
  82. execSync(`chflags nouchg ${tempPath}`);
  83. expect(shell.moveItemToTrash(tempPath)).to.be.true();
  84. expect(await fs.pathExists(tempPath)).to.be.false();
  85. });
  86. ifit(process.platform === 'win32')('returns false when path is in use', async () => {
  87. const tempPath = await fs.mkdtemp(path.resolve(app.getPath('temp'), 'electron-shell-spec-'));
  88. const cwd = process.cwd();
  89. try {
  90. // A process working directory is automatically locked on Windows.
  91. // This is a workaround to avoid pulling in fs-extras flock method.
  92. process.chdir(tempPath);
  93. expect(shell.moveItemToTrash(tempPath)).to.be.false();
  94. expect(await fs.pathExists(tempPath)).to.be.true();
  95. } finally {
  96. process.chdir(cwd);
  97. }
  98. expect(shell.moveItemToTrash(tempPath)).to.be.true();
  99. expect(await fs.pathExists(tempPath)).to.be.false();
  100. });
  101. });
  102. describe('shell.trashItem()', () => {
  103. afterEach(closeAllWindows);
  104. it('moves an item to the trash', async () => {
  105. const dir = await fs.mkdtemp(path.resolve(app.getPath('temp'), 'electron-shell-spec-'));
  106. const filename = path.join(dir, 'temp-to-be-deleted');
  107. await fs.writeFile(filename, 'dummy-contents');
  108. await shell.trashItem(filename);
  109. expect(fs.existsSync(filename)).to.be.false();
  110. });
  111. it('throws when called with a nonexistent path', async () => {
  112. const filename = path.join(app.getPath('temp'), 'does-not-exist');
  113. await expect(shell.trashItem(filename)).to.eventually.be.rejected();
  114. });
  115. ifit(!(process.platform === 'win32' && process.arch === 'ia32'))('works in the renderer process', async () => {
  116. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  117. w.loadURL('about:blank');
  118. await expect(w.webContents.executeJavaScript('require(\'electron\').shell.trashItem(\'does-not-exist\')')).to.be.rejectedWith(/does-not-exist|Failed to move item|Failed to create FileOperation/);
  119. });
  120. });
  121. });