From d9d0981d893a40cd056062f56c1e0c30482a1fa6 Mon Sep 17 00:00:00 2001 From: Guillaume Egret Date: Wed, 2 Aug 2023 15:16:50 +0200 Subject: [PATCH] chore(lauchIOS): wait for xcode record with a promise --- packages/ios-poc/src/launchIOS.ts | 40 +++++++++++++++++-------------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/packages/ios-poc/src/launchIOS.ts b/packages/ios-poc/src/launchIOS.ts index 82086586..8190a86a 100644 --- a/packages/ios-poc/src/launchIOS.ts +++ b/packages/ios-poc/src/launchIOS.ts @@ -7,8 +7,6 @@ import { program } from "commander"; import { execSync, exec } from "child_process"; import os from "os"; -const DURATION = 10; - const tmpFiles: string[] = []; const removeTmpFiles = () => { for (const tmpFile of tmpFiles) { @@ -29,22 +27,27 @@ const writeTmpFile = (fileName: string, content: string): string => { return tmpPath; }; -const executeAsyncCommand = (command: string): void => { - exec(command, (error, stdout, stderr) => { - if (error) { - console.log(`Ah, quel dommage! An error occurred: ${error.message}`); - return; - } - if (stderr) { - console.log(`stderr: ${stderr}`); - return; - } - console.log(`stdout: ${stdout}`); +const executeAsyncCommand = (command: string): Promise => { + return new Promise((resolve, reject) => { + exec(command, (error, stdout, stderr) => { + if (error) { + console.log(`Ah, quel dommage! An error occurred: ${error.message}`); + reject(); + return; + } + if (stderr) { + console.log(`stderr: ${stderr}`); + resolve(); + return; + } + console.log(`stdout: ${stdout}`); + resolve(); + }); }); }; -const startRecord = (simulatorId: string, traceFile: string) => { - executeAsyncCommand( +const startRecord = (simulatorId: string, traceFile: string): Promise => { + return executeAsyncCommand( `xcrun xctrace record --device ${simulatorId} --template Flashlight --attach 'fakeStore' --output ${traceFile}` ); }; @@ -57,7 +60,7 @@ const save = (traceFile: string, resultsFilePath: string) => { writeReport(xmlOutputFile, resultsFilePath); }; -const launchTest = ({ +const launchTest = async ({ testCommand, appId, simulatorId, @@ -79,11 +82,11 @@ const launchTest = ({ execSync(`maestro test ${lauchAppFile} --no-ansi`, { stdio: "inherit", }); - startRecord(simulatorId, traceFile); + const recordingPromise = startRecord(simulatorId, traceFile); + execSync(`${testCommand} --no-ansi`, { stdio: "inherit", }); - execSync(`sleep ${DURATION}`); const stopAppFile = writeTmpFile( "./stop.yaml", `appId: ${appId} @@ -94,6 +97,7 @@ const launchTest = ({ execSync(`maestro test ${stopAppFile} --no-ansi`, { stdio: "inherit", }); + await recordingPromise; save(traceFile, resultsFilePath); removeTmpFiles();