From aea5aae0158e4f3748e6fd71c4e57c1724144b49 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Thu, 12 Dec 2024 16:10:33 +0100 Subject: [PATCH] moar --- lib/commands/file-movement.js | 103 +++++++++++++++------------------- 1 file changed, 46 insertions(+), 57 deletions(-) diff --git a/lib/commands/file-movement.js b/lib/commands/file-movement.js index 7e4369019..23725ce8c 100644 --- a/lib/commands/file-movement.js +++ b/lib/commands/file-movement.js @@ -53,6 +53,12 @@ export async function parseContainerPath(remotePath, containerRootSupplier) { return {bundleId, pathInContainer, containerType}; } +/** + * + * @param {string} originalPath + * @param {string} root + * @returns {void} + */ function verifyIsSubPath(originalPath, root) { const normalizedRoot = path.normalize(root); const normalizedPath = path.normalize(path.dirname(originalPath)); @@ -62,6 +68,13 @@ function verifyIsSubPath(originalPath, root) { } } +/** + * + * @param {string} udid + * @param {string} [bundleId] + * @param {string} [containerType] + * @returns {Promise} + */ async function createAfcClient(udid, bundleId, containerType) { if (!bundleId) { return await services.startAfcService(udid); @@ -72,6 +85,11 @@ async function createAfcClient(udid, bundleId, containerType) { : await service.vendContainer(bundleId); } +/** + * + * @param {string} [containerType] + * @returns {boolean} + */ function isDocumentsContainer(containerType) { return _.toLower(containerType) === _.toLower(CONTAINER_DOCUMENTS_PATH); } @@ -79,20 +97,19 @@ function isDocumentsContainer(containerType) { /** * * @this {XCUITestDriver} - * @param {string} udid * @param {string} remotePath * @returns {Promise<{service: any, relativePath: string}>} */ -async function createService(udid, remotePath) { +async function createService(remotePath) { if (CONTAINER_PATH_PATTERN.test(remotePath)) { const {bundleId, pathInContainer, containerType} = await parseContainerPath.bind(this)(remotePath); - const service = await createAfcClient(udid, bundleId, containerType); + const service = await createAfcClient(this.device.udid, bundleId, containerType); const relativePath = isDocumentsContainer(containerType) ? path.join(CONTAINER_DOCUMENTS_PATH, pathInContainer) : pathInContainer; return {service, relativePath}; } else { - const service = await createAfcClient(udid); + const service = await createAfcClient(this.device.udid); return {service, relativePath: remotePath}; } } @@ -101,9 +118,6 @@ async function createService(udid, remotePath) { * Save the given base64 data chunk as a binary file on the Simulator under test. * * @this {XCUITestDriver} - * @param {import('../driver').Simulator} device - The device object, which represents the device under test. - * This object is expected to have the `udid` property containing the - * valid device ID. * @param {string} remotePath - The remote path on the device. This variable can be prefixed with * bundle id, so then the file will be uploaded to the corresponding * application container instead of the default media folder, for example @@ -116,8 +130,9 @@ async function createService(udid, remotePath) { * to the default media folder and only the file name is considered important. * @param {string} base64Data - Base-64 encoded content of the file to be uploaded. */ -async function pushFileToSimulator(device, remotePath, base64Data) { +async function pushFileToSimulator(remotePath, base64Data) { const buffer = Buffer.from(base64Data, 'base64'); + const device = /** @type {import('../driver').Simulator} */ (this.device); if (CONTAINER_PATH_PATTERN.test(remotePath)) { const {bundleId, pathInContainer: dstPath} = await parseContainerPath.bind(this)( remotePath, @@ -149,9 +164,6 @@ async function pushFileToSimulator(device, remotePath, base64Data) { * Save the given base64 data chunk as a binary file on the device under test. * * @this {XCUITestDriver} - * @param {import('../real-device').RealDevice} device - The device object, which represents the device under test. - * This object is expected to have the `udid` property containing the - * valid device ID. * @param {string} remotePath - The remote path on the device. This variable can be prefixed with * bundle id, so then the file will be uploaded to the corresponding * application container instead of the default media folder. Use @@ -166,8 +178,8 @@ async function pushFileToSimulator(device, remotePath, base64Data) { * as base64 decoded data. * @param {string} base64Data - Base-64 encoded content of the file to be uploaded. */ -async function pushFileToRealDevice(device, remotePath, base64Data) { - const {service, relativePath} = await createService.bind(this)(device.udid, remotePath); +async function pushFileToRealDevice(remotePath, base64Data) { + const {service, relativePath} = await createService.bind(this)(remotePath); try { await pushFile(service, Buffer.from(base64Data, 'base64'), relativePath); } catch (e) { @@ -180,15 +192,14 @@ async function pushFileToRealDevice(device, remotePath, base64Data) { /** * - * @param {import('../real-device').RealDevice|import('../driver').Simulator} device + * @this {XCUITestDriver} * @param {string} remotePath - * @param {boolean} isSimulator * @returns {Promise} */ -async function deleteFileOrFolder(device, remotePath, isSimulator) { - return isSimulator - ? await deleteFromSimulator.bind(this)(device, remotePath) - : await deleteFromRealDevice.bind(this)(device, remotePath); +async function deleteFileOrFolder(remotePath) { + return this.isSimulator() + ? await deleteFromSimulator.bind(this)(remotePath) + : await deleteFromRealDevice.bind(this)(remotePath); } /** @@ -196,9 +207,6 @@ async function deleteFileOrFolder(device, remotePath, isSimulator) { * Folder content is recursively packed into a zip archive. * * @this {XCUITestDriver} - * @param {import('../driver').Simulator} device - The device object, which represents the device under test. - * This object is expected to have the `udid` property containing the - * valid device ID. * @param {string} remotePath - The path to a file or a folder, which exists in the corresponding application * container on Simulator. Use * `@:/` @@ -208,8 +216,9 @@ async function deleteFileOrFolder(device, remotePath, isSimulator) { * @param {boolean} isFile - Whether the destination item is a file or a folder * @returns {Promise} Base-64 encoded content of the file. */ -async function pullFromSimulator(device, remotePath, isFile) { +async function pullFromSimulator(remotePath, isFile) { let pathOnServer; + const device = /** @type {import('../driver').Simulator} */ (this.device); if (CONTAINER_PATH_PATTERN.test(remotePath)) { const {bundleId, pathInContainer: dstPath} = await parseContainerPath.bind(this)( remotePath, @@ -243,9 +252,6 @@ async function pullFromSimulator(device, remotePath, isFile) { * Folder content is recursively packed into a zip archive. * * @this {XCUITestDriver} - * @param {import('../real-device').RealDevice} device - The device object, which represents the device under test. - * This object is expected to have the `udid` property containing the - * valid device ID. * @param {string} remotePath - The path to an existing remote file on the device. This variable can be prefixed with * bundle id, so then the file will be downloaded from the corresponding * application container instead of the default media folder. Use @@ -262,8 +268,8 @@ async function pullFromSimulator(device, remotePath, isFile) { * @param {boolean} isFile - Whether the destination item is a file or a folder * @returns {Promise} Base-64 encoded content of the remote file */ -async function pullFromRealDevice(device, remotePath, isFile) { - const {service, relativePath} = await createService.bind(this)(device.udid, remotePath); +async function pullFromRealDevice(remotePath, isFile) { + const {service, relativePath} = await createService.bind(this)(remotePath); try { const fileInfo = await service.getFileInfo(relativePath); if (isFile && fileInfo.isDirectory()) { @@ -285,9 +291,6 @@ async function pullFromRealDevice(device, remotePath, isFile) { * Remove the file or folder from the device * * @this {XCUITestDriver} - * @param {import('../driver').Simulator} device - The device object, which represents the device under test. - * This object is expected to have the `udid` property containing the - * valid device ID. * @param {string} remotePath - The path to a file or a folder, which exists in the corresponding application * container on Simulator. Use * `@:/` @@ -296,8 +299,9 @@ async function pullFromRealDevice(device, remotePath, isFile) { * The default type is 'app'. * @returns {Promise} */ -async function deleteFromSimulator(device, remotePath) { +async function deleteFromSimulator(remotePath) { let pathOnServer; + const device = /** @type {import('../driver').Simulator} */ (this.device); if (CONTAINER_PATH_PATTERN.test(remotePath)) { const {bundleId, pathInContainer: dstPath} = await parseContainerPath.bind(this)( remotePath, @@ -325,9 +329,6 @@ async function deleteFromSimulator(device, remotePath) { * Remove the file or folder from the device * * @this {XCUITestDriver} - * @param {import('../real-device').RealDevice} device - The device object, which represents the device under test. - * This object is expected to have the `udid` property containing the - * valid device ID. * @param {string} remotePath - The path to an existing remote file on the device. This variable can be prefixed with * bundle id, so then the file will be downloaded from the corresponding * application container instead of the default media folder. Use @@ -343,8 +344,8 @@ async function deleteFromSimulator(device, remotePath) { * `@com.myapp.bla:documents/` means `On My iPhone/`. * @returns {Promise} */ -async function deleteFromRealDevice(device, remotePath) { - const {service, relativePath} = await createService.bind(this)(device.udid, remotePath); +async function deleteFromRealDevice(remotePath) { + const {service, relativePath} = await createService.bind(this)(remotePath); try { await service.deleteDirectory(relativePath); } catch (e) { @@ -383,12 +384,8 @@ export default { base64Data = Buffer.from(base64Data).toString('utf8'); } return this.isSimulator() - ? await pushFileToSimulator.bind(this)( - /** @type {import('../driver').Simulator} */ (this.device), remotePath, base64Data - ) - : await pushFileToRealDevice.bind(this)( - /** @type {import('../real-device').RealDevice} */ (this.device), remotePath, base64Data - ); + ? await pushFileToSimulator.bind(this)(remotePath, base64Data) + : await pushFileToRealDevice.bind(this)(remotePath, base64Data); }, /** @@ -422,12 +419,8 @@ export default { ); } return this.isSimulator() - ? await pullFromSimulator.bind(this)( - /** @type {import('../driver').Simulator} */ (this.device), remotePath, true - ) - : await pullFromRealDevice.bind(this)( - /** @type {import('../real-device').RealDevice} */ (this.device), remotePath, true - ); + ? await pullFromSimulator.bind(this)(remotePath, true) + : await pullFromRealDevice.bind(this)(remotePath, true); }, /** @@ -453,7 +446,7 @@ export default { if (!remotePath.endsWith('/')) { remotePath = `${remotePath}/`; } - await deleteFileOrFolder(this.device, remotePath, this.isSimulator()); + await deleteFileOrFolder.bind(this)(remotePath); }, /** @@ -470,7 +463,7 @@ export default { `'${remotePath}' is given instead`, ); } - await deleteFileOrFolder(this.device, remotePath, this.isSimulator()); + await deleteFileOrFolder.bind(this)(remotePath); }, /** @@ -487,12 +480,8 @@ export default { remotePath = `${remotePath}/`; } return this.isSimulator() - ? await pullFromSimulator.bind(this)( - /** @type {import('../driver').Simulator} */ (this.device), remotePath, false - ) - : await pullFromRealDevice.bind(this)( - /** @type {import('../real-device').RealDevice} */ (this.device), remotePath, false - ); + ? await pullFromSimulator.bind(this)(remotePath, false) + : await pullFromRealDevice.bind(this)(remotePath, false); }, /**