codesign-helpers.ts 2.9 KB

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