codesign-helpers.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import * as cp from 'node:child_process';
  2. import * as fs from 'fs-extra';
  3. import * as os from 'node:os';
  4. import * as path from 'node:path';
  5. import { expect } from 'chai';
  6. const features = process._linkedBinding('electron_common_features');
  7. const fixturesPath = path.resolve(__dirname, '..', 'fixtures');
  8. export const shouldRunCodesignTests =
  9. process.platform === 'darwin' &&
  10. !(process.env.CI && process.arch === 'arm64') &&
  11. !process.mas &&
  12. !features.isComponentBuild();
  13. let identity: string | null;
  14. export function getCodesignIdentity () {
  15. if (identity === undefined) {
  16. const result = cp.spawnSync(path.resolve(__dirname, '../../script/codesign/get-trusted-identity.sh'));
  17. if (result.status !== 0 || result.stdout.toString().trim().length === 0) {
  18. // Per https://circleci.com/docs/2.0/env-vars:
  19. // CIRCLE_PR_NUMBER is only present on forked PRs
  20. if (process.env.CI && !process.env.CIRCLE_PR_NUMBER) {
  21. throw new Error('No valid signing identity available to run autoUpdater specs');
  22. }
  23. identity = null;
  24. } else {
  25. identity = result.stdout.toString().trim();
  26. }
  27. }
  28. return identity;
  29. }
  30. export async function copyApp (newDir: string, fixture: string | null = 'initial') {
  31. const appBundlePath = path.resolve(process.execPath, '../../..');
  32. const newPath = path.resolve(newDir, 'Electron.app');
  33. cp.spawnSync('cp', ['-R', appBundlePath, path.dirname(newPath)]);
  34. if (fixture) {
  35. const appDir = path.resolve(newPath, 'Contents/Resources/app');
  36. await fs.mkdirp(appDir);
  37. await fs.copy(path.resolve(fixturesPath, 'auto-update', fixture), appDir);
  38. }
  39. const plistPath = path.resolve(newPath, 'Contents', 'Info.plist');
  40. await fs.writeFile(
  41. plistPath,
  42. (await fs.readFile(plistPath, 'utf8')).replace('<key>BuildMachineOSBuild</key>', `<key>NSAppTransportSecurity</key>
  43. <dict>
  44. <key>NSAllowsArbitraryLoads</key>
  45. <true/>
  46. <key>NSExceptionDomains</key>
  47. <dict>
  48. <key>localhost</key>
  49. <dict>
  50. <key>NSExceptionAllowsInsecureHTTPLoads</key>
  51. <true/>
  52. <key>NSIncludesSubdomains</key>
  53. <true/>
  54. </dict>
  55. </dict>
  56. </dict><key>BuildMachineOSBuild</key>`)
  57. );
  58. return newPath;
  59. };
  60. export function spawn (cmd: string, args: string[], opts: any = {}) {
  61. let out = '';
  62. const child = cp.spawn(cmd, args, opts);
  63. child.stdout.on('data', (chunk: Buffer) => {
  64. out += chunk.toString();
  65. });
  66. child.stderr.on('data', (chunk: Buffer) => {
  67. out += chunk.toString();
  68. });
  69. return new Promise<{ code: number, out: string }>((resolve) => {
  70. child.on('exit', (code, signal) => {
  71. expect(signal).to.equal(null);
  72. resolve({
  73. code: code!,
  74. out
  75. });
  76. });
  77. });
  78. };
  79. export function signApp (appPath: string, identity: string) {
  80. return spawn('codesign', ['-s', identity, '--deep', '--force', appPath]);
  81. };
  82. export async function withTempDirectory (fn: (dir: string) => Promise<void>, autoCleanUp = true) {
  83. const dir = await fs.mkdtemp(path.resolve(os.tmpdir(), 'electron-update-spec-'));
  84. try {
  85. await fn(dir);
  86. } finally {
  87. if (autoCleanUp) {
  88. cp.spawnSync('rm', ['-r', dir]);
  89. }
  90. }
  91. };