From d462e4fd6f11c7a1063b0540496bacf6a84c2805 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Thu, 31 Aug 2023 21:41:11 +0200 Subject: [PATCH 1/2] fix: Address some typing-related issues --- lib/webdriveragent.js | 6 ++--- lib/xcodebuild.js | 63 ++++++++++++++++++++++--------------------- 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/lib/webdriveragent.js b/lib/webdriveragent.js index 40196b6ea..fdae2bff6 100644 --- a/lib/webdriveragent.js +++ b/lib/webdriveragent.js @@ -467,10 +467,8 @@ class WebDriverAgent { } } else if (!this.args.webDriverAgentUrl) { this.log.info('Shutting down sub-processes'); - // @ts-ignore xcodebuild should be set - await this.xcodebuild.quit(); - // @ts-ignore xcodebuild should be set - await this.xcodebuild.reset(); + await this.xcodebuild?.quit(); + await this.xcodebuild?.reset(); } else { this.log.debug('Do not stop xcodebuild nor XCTest session ' + 'since the WDA session is managed by outside this driver.'); diff --git a/lib/xcodebuild.js b/lib/xcodebuild.js index 348a4f967..059b52757 100644 --- a/lib/xcodebuild.js +++ b/lib/xcodebuild.js @@ -84,6 +84,13 @@ class XcodeBuild { this.resultBundlePath = args.resultBundlePath; this.resultBundleVersion = args.resultBundleVersion; + + /** @type {string} */ + this._logLocation = ''; + /** @type {string[]} */ + this._wdaErrorMessage = []; + this._didBuildFail = false; + this._didProcessExit = false; } async init (noSessionProxy) { @@ -163,8 +170,10 @@ class XcodeBuild { this.xcodebuild = null; - // pause a moment - await B.delay(this.prebuildDelay); + if (this.prebuildDelay > 0) { + // pause a moment + await B.delay(this.prebuildDelay); + } } async cleanProject () { @@ -272,6 +281,8 @@ class XcodeBuild { if (upgradeTimestamp) { env.UPGRADE_TIMESTAMP = upgradeTimestamp; } + this._logLocation = ''; + this._didBuildFail = false; const xcodebuild = new SubProcess(cmd, args, { cwd: this.bootstrapPath, env, @@ -291,10 +302,8 @@ class XcodeBuild { if (out.includes('Writing diagnostic log for test session to')) { // pull out the first line that begins with the path separator // which *should* be the line indicating the log file generated - // @ts-ignore logLocation is a custom property - xcodebuild.logLocation = _.first(_.remove(out.trim().split('\n'), (v) => v.startsWith(path.sep))); - // @ts-ignore logLocation is a custom property - xcodeLog.debug(`Log file for xcodebuild test: ${xcodebuild.logLocation}`); + this._logLocation = _.first(_.remove(out.trim().split('\n'), (v) => v.startsWith(path.sep))) ?? ''; + xcodeLog.debug(`Log file for xcodebuild test: ${this._logLocation}`); } // if we have an error we want to output the logs @@ -304,9 +313,8 @@ class XcodeBuild { if (this.showXcodeLog !== false && out.includes('Error Domain=') && !ignoreError) { logXcodeOutput = true; - // terrible hack to handle case where xcode return 0 but is failing - // @ts-ignore _wda_error_occurred is a custom property - xcodebuild._wda_error_occurred = true; + // handle case where xcode returns 0 but is failing + this._didBuildFail = true; } // do not log permission errors from trying to write to attachments folder @@ -314,8 +322,7 @@ class XcodeBuild { for (const line of out.split(EOL)) { xcodeLog.error(line); if (line) { - // @ts-ignore _wda_error_message is a custom property - xcodebuild._wda_error_message += `${EOL}${line}`; + this._wdaErrorMessage.push(line); } } } @@ -327,24 +334,23 @@ class XcodeBuild { async start (buildOnly = false) { this.xcodebuild = await this.createSubProcess(buildOnly); // Store xcodebuild message - // @ts-ignore _wda_error_message is a custom property - this.xcodebuild._wda_error_message = ''; + this._wdaErrorMessage = []; // wrap the start procedure in a promise so that we can catch, and report, // any startup errors that are thrown as events return await new B((resolve, reject) => { // @ts-ignore xcodebuild must be present here - this.xcodebuild.on('exit', async (code, signal) => { + this.xcodebuild.once('exit', async (code, signal) => { xcodeLog.error(`xcodebuild exited with code '${code}' and signal '${signal}'`); + this.xcodebuild?.removeAllListeners(); + const xcodeErrorMessage = this._wdaErrorMessage.join('\n'); + this._wdaErrorMessage = []; // print out the xcodebuild file if users have asked for it - // @ts-ignore logLocation is a custom property - if (this.showXcodeLog && this.xcodebuild?.logLocation) { - // @ts-ignore logLocation is a custom property - xcodeLog.error(`Contents of xcodebuild log file '${this.xcodebuild.logLocation}':`); + if (this.showXcodeLog && this._logLocation) { + xcodeLog.error(`Contents of xcodebuild log file '${this._logLocation}':`); try { const logFile = readline.createInterface({ - // @ts-ignore logLocation is a custom property - input: fs.createReadStream(this.xcodebuild.logLocation), + input: fs.createReadStream(this._logLocation), terminal: false }); logFile.on('line', (line) => { @@ -360,13 +366,10 @@ class XcodeBuild { xcodeLog.error(`Unable to access xcodebuild log file: '${err.message}'`); } } - // @ts-ignore processExited is a custom property - this.xcodebuild.processExited = true; - // @ts-ignore _wda_error_occurred is a custom property - if (this.xcodebuild._wda_error_occurred || (!signal && code !== 0)) { - return reject(new Error(`xcodebuild failed with code ${code}${EOL}` + - // @ts-ignore _wda_error_message is a custom property - `xcodebuild error message:${EOL}${this.xcodebuild._wda_error_message}`)); + this.didProcessExit = true; + if (this._didBuildFail || (!signal && code !== 0)) { + return reject(new Error(`xcodebuild failed with code ${code}\n` + + `xcodebuild error message:\n${xcodeErrorMessage}`)); } // in the case of just building, the process will exit and that is our finish if (buildOnly) { @@ -399,8 +402,7 @@ class XcodeBuild { try { let retries = parseInt(`${this.launchTimeout / 500}`, 10); await retryInterval(retries, 1000, async () => { - // @ts-ignore processExited is a custom property - if (this.xcodebuild.processExited) { + if (this._didProcessExit) { // there has been an error elsewhere and we need to short-circuit return; } @@ -420,8 +422,7 @@ class XcodeBuild { } }); - // @ts-ignore processExited is a custom property - if (this.xcodebuild.processExited) { + if (this._didProcessExit) { // there has been an error elsewhere and we need to short-circuit return currentStatus; } From 90af8eae9a5f3135904661d90b6312578b32ce01 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Thu, 31 Aug 2023 21:43:54 +0200 Subject: [PATCH 2/2] Tune logs --- lib/xcodebuild.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/xcodebuild.js b/lib/xcodebuild.js index 059b52757..23f365417 100644 --- a/lib/xcodebuild.js +++ b/lib/xcodebuild.js @@ -303,7 +303,7 @@ class XcodeBuild { // pull out the first line that begins with the path separator // which *should* be the line indicating the log file generated this._logLocation = _.first(_.remove(out.trim().split('\n'), (v) => v.startsWith(path.sep))) ?? ''; - xcodeLog.debug(`Log file for xcodebuild test: ${this._logLocation}`); + xcodeLog.debug(`Log file location for xcodebuild test: ${this._logLocation || 'unknown'}`); } // if we have an error we want to output the logs