Skip to content

Commit

Permalink
iOS tests fix (#7306)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcobbs-bentley authored Oct 30, 2024
1 parent 451406a commit 7fb3426
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@itwin/core-backend",
"comment": "",
"type": "none"
}
],
"packageName": "@itwin/core-backend"
}
2 changes: 1 addition & 1 deletion common/config/azure-pipelines/templates/core-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ steps:
- script: npm run ios:all
workingDirectory: core/backend
displayName: Build & run iOS backend unit tests in Simulator
condition: and(succeeded(), ${{ parameters.buildMobile }}, eq(variables['Agent.OS'], 'Darwin'), false)
condition: and(succeeded(), ${{ parameters.buildMobile }}, eq(variables['Agent.OS'], 'Darwin'))

- script: node common/scripts/install-run-rush.js lint
displayName: rush lint
Expand Down
39 changes: 34 additions & 5 deletions core/backend/scripts/runUnitTestsIosSimulator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { createWriteStream } from 'fs';
import { createWriteStream, copyFile } from 'fs';
import { Simctl } from "node-simctl";
import { fileURLToPath } from 'url';
import * as path from "path";
Expand All @@ -12,6 +12,7 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
const appName = "core-test-runner"
const bundleId = `com.bentley.${appName}`;
const xmlFilter = "[Mocha_Result_XML]: ";
const xmlFileFilter = "[Mocha_Result_XML_File]: ";

/**
* Sort function that compares strings numerically from high to low
Expand Down Expand Up @@ -60,11 +61,10 @@ function log(message) {
}

/**
* @param {string} xmlFilter
* @param {string} inputLog
* @param {string} outputXmlFile
*/
function extractXML(xmlFilter, inputLog, outputXmlFile) {
function extractXML(inputLog, outputXmlFile) {
const lines = inputLog.split(/\r?\n/)
const outputStream = createWriteStream(outputXmlFile)

Expand All @@ -78,7 +78,36 @@ function extractXML(xmlFilter, inputLog, outputXmlFile) {
outputStream.write(cleanedXmlLine + "\n", "utf-8");
// console.log(cleanedXmlLine);
}
};
}
}

/**
* @param {string} inputLog
* @param {string} outputXmlFile
*/
function copyXML(inputLog, outputXmlFile) {
const start = inputLog.indexOf(xmlFileFilter) + xmlFileFilter.length;
const end = inputLog.indexOf("\n", start);
const xmlFile = inputLog.substring(start, end);
copyFile(xmlFile, outputXmlFile, (/** @type {any} */ err) => {
if (err) {
console.log(err);
}
});
}

/**
* @param {string} inputLog
* @param {string} outputXmlFile
*/
function extractOrCopyXML(inputLog, outputXmlFile) {
if (inputLog.includes(xmlFileFilter)) {
log(`Copying XML file.`);
copyXML(inputLog, outputXmlFile);
} else {
log(`Extracting XML from log.`);
extractXML(inputLog, outputXmlFile);
}
}

async function main() {
Expand Down Expand Up @@ -161,7 +190,7 @@ async function main() {
log("Failed.");
log(`launchOutput:\n${launchOutput}`);
}
extractXML(xmlFilter, launchOutput, `${__dirname}/../lib/junit_results.xml`);
extractOrCopyXML(launchOutput, `${__dirname}/../lib/junit_results.xml`);

// Shut down simulator
log("Shutting down simulator");
Expand Down
14 changes: 12 additions & 2 deletions test-apps/display-test-app/scripts/runIosSimulator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,21 @@ class SimctlWithOpts extends Simctl {
* @returns {Promise<string>}
*/
async launchAppWithOptions(bundleId, options, args) {
const { stdout } = await this.exec('launch', {
const { stdout, stderr } = await this.exec('launch', {
args: [...options, this.requireUdid('launch'), bundleId, ...args],
architectures: "x86_64",
});
return stdout.trim();
const trimmedOut = stdout.trim();
const trimmedErr = stderr.trim();
if (trimmedOut && trimmedErr) {
return `=========stdout=========\n${stdout.trim()}\n=========stderr=========\n${stderr.trim()}`;
} else if (trimmedOut) {
return `=========stdout=========\n${stdout.trim()}`;
} else if (trimmedErr) {
return `=========stderr=========\n${stderr.trim()}`;
} else {
return "";
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/iTwin/mobile-native-ios",
"state" : {
"revision" : "b365b0ca9fe2b16cddaf26f59e8f38148027608c",
"version" : "4.8.11"
"revision" : "c011577eed1b76b408a7d997be918b41380fb836",
"version" : "4.10.21"
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ class ViewController: ObservableObject {
let main = URL(fileURLWithPath: mainPath)
log("(ios): Running tests.")
host.loadBackend(main, withAuthClient: nil, withInspect: true) { [self] (numFailed: UInt32) in
#if targetEnvironment(simulator)
log("[Mocha_Result_XML_File]: \(testResultsUrl.path)")
#else
log("(ios): Starting Mocha Result XML dump...")
do {
let text = try String(contentsOf: testResultsUrl, encoding: .utf8)
Expand All @@ -105,6 +108,7 @@ class ViewController: ObservableObject {
log("(ios): Failed to read mocha test results.")
}
log("(ios): Mocha Result XML dump complete.")
#endif

// Indicate via UI that the tests have finished.
self.testStatus = "Tests finished."
Expand Down

0 comments on commit 7fb3426

Please sign in to comment.