api-shell-spec.ts 4.2 KB

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