Skip to content

Commit

Permalink
moar
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Dec 12, 2024
1 parent c16c6f5 commit aea5aae
Showing 1 changed file with 46 additions and 57 deletions.
103 changes: 46 additions & 57 deletions lib/commands/file-movement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -62,6 +68,13 @@ function verifyIsSubPath(originalPath, root) {
}
}

/**
*
* @param {string} udid
* @param {string} [bundleId]
* @param {string} [containerType]
* @returns {Promise<any>}
*/
async function createAfcClient(udid, bundleId, containerType) {
if (!bundleId) {
return await services.startAfcService(udid);
Expand All @@ -72,27 +85,31 @@ 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);
}

/**
*
* @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};
}
}
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -180,25 +192,21 @@ 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<void>}
*/
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);
}

/**
* Get the content of given file or folder from iOS Simulator and return it as base-64 encoded string.
* 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
* `@<app_bundle_id>:<optional_container_type>/<path_to_the_file_or_folder_inside_container>`
Expand All @@ -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<string>} 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,
Expand Down Expand Up @@ -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
Expand All @@ -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<string>} 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()) {
Expand All @@ -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
* `@<app_bundle_id>:<optional_container_type>/<path_to_the_file_or_folder_inside_container>`
Expand All @@ -296,8 +299,9 @@ async function pullFromRealDevice(device, remotePath, isFile) {
* The default type is 'app'.
* @returns {Promise<void>}
*/
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,
Expand Down Expand Up @@ -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
Expand All @@ -343,8 +344,8 @@ async function deleteFromSimulator(device, remotePath) {
* `@com.myapp.bla:documents/` means `On My iPhone/<app name>`.
* @returns {Promise<void>}
*/
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) {
Expand Down Expand Up @@ -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);
},

/**
Expand Down Expand Up @@ -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);
},

/**
Expand All @@ -453,7 +446,7 @@ export default {
if (!remotePath.endsWith('/')) {
remotePath = `${remotePath}/`;
}
await deleteFileOrFolder(this.device, remotePath, this.isSimulator());
await deleteFileOrFolder.bind(this)(remotePath);
},

/**
Expand All @@ -470,7 +463,7 @@ export default {
`'${remotePath}' is given instead`,
);
}
await deleteFileOrFolder(this.device, remotePath, this.isSimulator());
await deleteFileOrFolder.bind(this)(remotePath);
},

/**
Expand All @@ -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);
},

/**
Expand Down

0 comments on commit aea5aae

Please sign in to comment.