installer-packaging.spec.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { tmpdir } from 'node:os'
  2. import { Arch, Platform } from 'electron-builder'
  3. import { Packager } from 'app-builder-lib'
  4. import { describe, expect, it, vi } from 'vitest'
  5. const { execute } = vi.hoisted(() => ({ execute: vi.fn(async () => undefined) }))
  6. vi.mock('node:child_process', async (importOriginal) => {
  7. const original = await importOriginal<typeof import('node:child_process')>()
  8. const { promisify } = await import('node:util')
  9. return { ...original, execFile: Object.assign(vi.fn(), { [promisify.custom]: execute }) }
  10. })
  11. describe('installer preparation preserves application dependencies', () => {
  12. it.each(['win32', 'darwin'] as const)('keeps electron-builder responsible for node_modules on %s', async (platform) => {
  13. execute.mockClear()
  14. const env = {
  15. DSH_DESKTOP_APP_ID: 'com.example.installer',
  16. DSH_DESKTOP_TARGET_PLATFORM: platform,
  17. DSH_DESKTOP_TARGET_ARCH: 'x64',
  18. DSH_DESKTOP_UNSIGNED: platform === 'win32' ? '1' : '0',
  19. DSH_DESKTOP_MACOS_SIGNING_IDENTITY: 'Example Company (TEAMID1234)',
  20. DSH_DESKTOP_MACOS_TEAM_ID: 'TEAMID1234',
  21. APPLE_KEYCHAIN_PROFILE: 'installer-test',
  22. DOWNLOAD_TEST_ORIGIN: 'https://desktop-updates.example.com',
  23. }
  24. for (const [name, value] of Object.entries(env)) vi.stubEnv(name, value)
  25. try {
  26. const { createElectronBuilderConfig } = await import('../electron-builder.config.mjs')
  27. const config = createElectronBuilderConfig(env, platform, 'x64')
  28. const packager = new Packager({ projectDir: tmpdir() })
  29. // A foreign source-build target avoids rebuilding modules; the real dependency ownership decision still runs.
  30. Object.defineProperties(packager, {
  31. config: { value: { beforeBuild: config.beforeBuild, buildDependenciesFromSource: true } },
  32. framework: { value: { isNpmRebuildRequired: true, version: '42.0.0' } },
  33. appInfo: { value: { type: 'module' } },
  34. })
  35. vi.spyOn(packager, 'getWorkspaceRoot').mockResolvedValue(tmpdir())
  36. await packager.installAppDependencies(process.platform === 'win32' ? Platform.LINUX : Platform.WINDOWS, Arch.x64)
  37. expect(packager.areNodeModulesHandledExternally).toBe(false)
  38. expect(execute).toHaveBeenCalledTimes(platform === 'win32' ? 1 : 0)
  39. } finally {
  40. vi.unstubAllEnvs()
  41. vi.restoreAllMocks()
  42. }
  43. })
  44. })