iOS: Move app URL gathering from devicectl to a function

And add a test to document what we expect from devicectl.

Change-Id: I2f5312ebadef60239b77308acb7114f1d55143b4
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Eike Ziller
2024-01-29 16:25:13 +01:00
parent 759ea7695f
commit 2898e09bb4
4 changed files with 76 additions and 11 deletions

View File

@@ -83,4 +83,17 @@ expected_str<QMap<QString, QString>> parseDeviceInfo(const QByteArray &rawOutput
return make_unexpected(QLatin1String("Device is not handled by devicectl"));
}
Utils::expected_str<QUrl> parseAppInfo(const QByteArray &rawOutput, const QString &bundleIdentifier)
{
const Utils::expected_str<QJsonValue> result = parseDevicectlResult(rawOutput);
if (!result)
return make_unexpected(result.error());
const QJsonArray apps = (*result)["apps"].toArray();
for (const QJsonValue &app : apps) {
if (app["bundleIdentifier"].toString() == bundleIdentifier)
return QUrl(app["url"].toString());
}
return {};
}
} // namespace Ios::Internal

View File

@@ -22,5 +22,6 @@ const char vYes[] = "YES";
Utils::expected_str<QJsonValue> parseDevicectlResult(const QByteArray &rawOutput);
Utils::expected_str<QMap<QString, QString>> parseDeviceInfo(const QByteArray &rawOutput,
const QString &deviceUsbId);
Utils::expected_str<QUrl> parseAppInfo(const QByteArray &rawOutput, const QString &bundleIdentifier);
} // namespace Ios::Internal

View File

@@ -146,19 +146,12 @@ GroupItem DeviceCtlRunner::findApp(const QString &bundleIdentifier, Storage<AppI
reportFailure(Tr::tr("Failed to run devicectl: %1.").arg(process.errorString()));
return DoneResult::Error;
}
const Utils::expected_str<QJsonValue> resultValue = parseDevicectlResult(
process.rawStdOut());
if (resultValue) {
const QJsonArray apps = (*resultValue)["apps"].toArray();
for (const QJsonValue &app : apps) {
if (app["bundleIdentifier"].toString() == bundleIdentifier) {
appInfo->pathOnDevice = QUrl(app["url"].toString());
break;
}
}
const expected_str<QUrl> pathOnDevice = parseAppInfo(process.rawStdOut(), bundleIdentifier);
if (pathOnDevice) {
appInfo->pathOnDevice = *pathOnDevice;
return DoneResult::Success;
}
reportFailure(resultValue.error());
reportFailure(pathOnDevice.error());
return DoneResult::Error;
};
return ProcessTask(onSetup, onDone);

View File

@@ -17,6 +17,8 @@ private slots:
void parseDeviceInfo_data();
void parseDeviceInfo();
void parseAppInfo();
};
void tst_Devicectlutils::parseError_data()
@@ -305,6 +307,62 @@ void tst_Devicectlutils::parseDeviceInfo()
}
}
void tst_Devicectlutils::parseAppInfo()
{
const QByteArray data(R"raw(
{
"info" : {
"arguments" : [
"devicectl",
"device",
"info",
"apps",
"--device",
"00000000-0000000000000000",
"--quiet",
"--json-output",
"-"
],
"commandType" : "devicectl.device.info.apps",
"environment" : {
"TERM" : "xterm-256color"
},
"jsonVersion" : 2,
"outcome" : "success",
"version" : "355.7.7"
},
"result" : {
"apps" : [
{
"appClip" : false,
"builtByDeveloper" : true,
"bundleIdentifier" : "org.iuehrg.cmake-widgets",
"bundleVersion" : "0.1",
"defaultApp" : false,
"hidden" : false,
"internalApp" : false,
"name" : "cmake_widgets",
"removable" : true,
"url" : "file:///private/var/containers/Bundle/Application/FAEC04B7-41E6-4A3C-952E-D89792DA053C/cmake_widgets.app/",
"version" : "0.1"
}
],
"defaultAppsIncluded" : false,
"deviceIdentifier" : "00000000-0000-0000-0000-000000000000",
"hiddenAppsIncluded" : false,
"internalAppsIncluded" : false,
"removableAppsIncluded" : true
}
})raw");
const Utils::expected_str<QUrl> result
= Ios::Internal::parseAppInfo(data, "org.iuehrg.cmake-widgets");
QVERIFY(result);
QCOMPARE(*result,
QUrl("file:///private/var/containers/Bundle/Application/"
"FAEC04B7-41E6-4A3C-952E-D89792DA053C/cmake_widgets.app/"));
}
QTEST_GUILESS_MAIN(tst_Devicectlutils)
#include "tst_devicectlutils.moc"