Skip to content

Commit

Permalink
fix: Address some typing-related issues (#759)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Sep 1, 2023
1 parent 2470883 commit 87e8704
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 35 deletions.
6 changes: 2 additions & 4 deletions lib/webdriveragent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down
63 changes: 32 additions & 31 deletions lib/xcodebuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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,
Expand All @@ -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 location for xcodebuild test: ${this._logLocation || 'unknown'}`);
}

// if we have an error we want to output the logs
Expand All @@ -304,18 +313,16 @@ 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
if (logXcodeOutput && !ignoreError) {
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);
}
}
}
Expand All @@ -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) => {
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down

0 comments on commit 87e8704

Please sign in to comment.