|
| 1 | +import os from 'node:os'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { spawn, spawnSync } from 'node:child_process'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | + |
| 6 | +const rootDir = path.resolve(fileURLToPath(new URL('..', import.meta.url))); |
| 7 | +const tauriTargetDir = path.join(rootDir, 'src-tauri', 'target', 'debug'); |
| 8 | +const appBinaryName = process.platform === 'win32' ? 'openpet.exe' : 'openpet'; |
| 9 | +const appBinaryPath = path.join(tauriTargetDir, appBinaryName); |
| 10 | +const pnpmBinary = process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm'; |
| 11 | +const tauriDriverBinary = |
| 12 | + process.env.TAURI_DRIVER ?? |
| 13 | + path.join(os.homedir(), '.cargo', 'bin', process.platform === 'win32' ? 'tauri-driver.exe' : 'tauri-driver'); |
| 14 | +const tauriDriverArgs = process.env.TAURI_NATIVE_DRIVER |
| 15 | + ? ['--native-driver', process.env.TAURI_NATIVE_DRIVER] |
| 16 | + : []; |
| 17 | + |
| 18 | +let tauriDriver; |
| 19 | +let tauriDriverExitExpected = false; |
| 20 | + |
| 21 | +export const config = { |
| 22 | + runner: 'local', |
| 23 | + host: '127.0.0.1', |
| 24 | + port: 4444, |
| 25 | + specs: [path.join(rootDir, 'e2e-tauri', 'specs', '**', '*.mjs')], |
| 26 | + maxInstances: 1, |
| 27 | + logLevel: process.env.WDIO_LOG_LEVEL ?? 'warn', |
| 28 | + bail: 0, |
| 29 | + waitforTimeout: 10_000, |
| 30 | + connectionRetryTimeout: 120_000, |
| 31 | + connectionRetryCount: 3, |
| 32 | + capabilities: [ |
| 33 | + { |
| 34 | + maxInstances: 1, |
| 35 | + 'tauri:options': { |
| 36 | + application: appBinaryPath, |
| 37 | + }, |
| 38 | + }, |
| 39 | + ], |
| 40 | + reporters: ['spec'], |
| 41 | + framework: 'mocha', |
| 42 | + mochaOpts: { |
| 43 | + ui: 'bdd', |
| 44 | + timeout: 60_000, |
| 45 | + }, |
| 46 | + |
| 47 | + onPrepare: () => { |
| 48 | + if (process.platform === 'darwin') { |
| 49 | + throw new Error('Tauri WebDriver desktop tests are supported on Windows and Linux only.'); |
| 50 | + } |
| 51 | + |
| 52 | + if (process.env.OPENPET_SKIP_TAURI_BUILD === '1') { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + const result = spawnSync(pnpmBinary, ['tauri', 'build', '--debug', '--no-bundle'], { |
| 57 | + cwd: rootDir, |
| 58 | + stdio: 'inherit', |
| 59 | + }); |
| 60 | + |
| 61 | + if (result.status !== 0) { |
| 62 | + throw new Error(`Tauri debug build failed with exit code ${result.status ?? 'unknown'}.`); |
| 63 | + } |
| 64 | + }, |
| 65 | + |
| 66 | + beforeSession: () => { |
| 67 | + tauriDriverExitExpected = false; |
| 68 | + tauriDriver = spawn(tauriDriverBinary, tauriDriverArgs, { |
| 69 | + cwd: rootDir, |
| 70 | + stdio: ['ignore', 'inherit', 'inherit'], |
| 71 | + }); |
| 72 | + |
| 73 | + tauriDriver.on('error', (error) => { |
| 74 | + console.error('tauri-driver error:', error); |
| 75 | + process.exit(1); |
| 76 | + }); |
| 77 | + |
| 78 | + tauriDriver.on('exit', (code) => { |
| 79 | + if (!tauriDriverExitExpected) { |
| 80 | + console.error('tauri-driver exited unexpectedly with code:', code); |
| 81 | + process.exit(1); |
| 82 | + } |
| 83 | + }); |
| 84 | + }, |
| 85 | + |
| 86 | + afterSession: () => { |
| 87 | + closeTauriDriver(); |
| 88 | + }, |
| 89 | + |
| 90 | + onComplete: () => { |
| 91 | + closeTauriDriver(); |
| 92 | + }, |
| 93 | +}; |
| 94 | + |
| 95 | +function closeTauriDriver() { |
| 96 | + tauriDriverExitExpected = true; |
| 97 | + |
| 98 | + if (tauriDriver && !tauriDriver.killed) { |
| 99 | + tauriDriver.kill(); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +function registerShutdownCleanup() { |
| 104 | + process.once('exit', () => { |
| 105 | + closeTauriDriver(); |
| 106 | + }); |
| 107 | + |
| 108 | + const exitAfterCleanup = (code) => { |
| 109 | + closeTauriDriver(); |
| 110 | + process.exit(code); |
| 111 | + }; |
| 112 | + |
| 113 | + process.once('SIGINT', () => exitAfterCleanup(130)); |
| 114 | + process.once('SIGTERM', () => exitAfterCleanup(143)); |
| 115 | + process.once('SIGHUP', () => exitAfterCleanup(129)); |
| 116 | + |
| 117 | + if (process.platform === 'win32') { |
| 118 | + process.once('SIGBREAK', () => exitAfterCleanup(130)); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +registerShutdownCleanup(); |
0 commit comments