Repository: wix/Detox
Pull request #3781 (2022-12-26 08:25:31)

refactor: rename Xfer to Transfer
## Description

Preparation for #3497 .

noomorph: @jonathanmos I split your "(WIP) Optimize app install" into a preliminary refactor and the actual feature. Could you take a look at this and approve if you're ok with it?
jonathanmos: LGTM

Patch:
diff --git a/detox/src/devices/common/drivers/android/tools/AppInstallHelper.js b/detox/src/devices/common/drivers/android/tools/AppInstallHelper.js
index e2968fcbf..c21dfafa0 100644
--- a/detox/src/devices/common/drivers/android/tools/AppInstallHelper.js
+++ b/detox/src/devices/common/drivers/android/tools/AppInstallHelper.js
@@ -1,13 +1,13 @@
 // TODO Tweak such that if apk's already exist on the device (need to store uniquely), they will not be resent (would optimize cloud, for example)
 
 class AppInstallHelper {
-  constructor(adb, fileXfer) {
+  constructor(adb, fileTransfer) {
     this._adb = adb;
-    this._fileXfer = fileXfer;
+    this._fileTransfer = fileTransfer;
   }
 
   async install(deviceId, appBinaryPath, testBinaryPath) {
-    await this._fileXfer.prepareDestinationDir(deviceId);
+    await this._fileTransfer.prepareDestinationDir(deviceId);
     await this._pushAndInstallBinary(deviceId, appBinaryPath, 'Application.apk');
     if (testBinaryPath) {
       await this._pushAndInstallBinary(deviceId, testBinaryPath, 'Test.apk');
@@ -15,7 +15,7 @@ class AppInstallHelper {
   }
 
   async _pushAndInstallBinary(deviceId, binaryPath, binaryFilenameOnTarget) {
-    const binaryPathOnTarget = await this._fileXfer.send(deviceId, binaryPath, binaryFilenameOnTarget);
+    const binaryPathOnTarget = await this._fileTransfer.send(deviceId, binaryPath, binaryFilenameOnTarget);
     await this._adb.remoteInstall(deviceId, binaryPathOnTarget);
   }
 }
diff --git a/detox/src/devices/common/drivers/android/tools/FileXfer.js b/detox/src/devices/common/drivers/android/tools/FileTransfer.js
similarity index 91%
rename from detox/src/devices/common/drivers/android/tools/FileXfer.js
rename to detox/src/devices/common/drivers/android/tools/FileTransfer.js
index a14fde8bd..551b9eab1 100644
--- a/detox/src/devices/common/drivers/android/tools/FileXfer.js
+++ b/detox/src/devices/common/drivers/android/tools/FileTransfer.js
@@ -1,6 +1,6 @@
 const path = require('path');
 
-class FileXfer {
+class FileTransfer {
   constructor(adb, destinationDir) {
     this._adb = adb;
     this._dir = destinationDir;
@@ -18,4 +18,4 @@ class FileXfer {
   }
 }
 
-module.exports = FileXfer;
+module.exports = FileTransfer;
diff --git a/detox/src/devices/common/drivers/android/tools/TempFileTransfer.js b/detox/src/devices/common/drivers/android/tools/TempFileTransfer.js
new file mode 100644
index 000000000..42648fe75
--- /dev/null
+++ b/detox/src/devices/common/drivers/android/tools/TempFileTransfer.js
@@ -0,0 +1,14 @@
+const FileTransfer = require('./FileTransfer');
+
+const FILE_PATH = '/data/local/tmp/detox';
+
+class TempFileTransfer extends FileTransfer {
+  constructor(adb) {
+    super(adb, FILE_PATH);
+  }
+}
+
+module.exports = {
+  TempFileTransfer,
+  FILE_PATH
+};
diff --git a/detox/src/devices/common/drivers/android/tools/TempFileXfer.js b/detox/src/devices/common/drivers/android/tools/TempFileXfer.js
deleted file mode 100644
index 900c8e9b8..000000000
--- a/detox/src/devices/common/drivers/android/tools/TempFileXfer.js
+++ /dev/null
@@ -1,16 +0,0 @@
-// @ts-nocheck
-const FileXfer = require('./FileXfer');
-
-const EMU_TEMP_PATH = '/data/local/tmp';
-const EMU_TEMP_INSTALL_PATH = `${EMU_TEMP_PATH}/detox`;
-
-class TempFileXfer extends FileXfer {
-  /**
-   * @param adb { ADB }
-   */
-  constructor(adb) {
-    super(adb, EMU_TEMP_INSTALL_PATH);
-  }
-}
-
-module.exports = TempFileXfer;
diff --git a/detox/src/devices/runtime/drivers/android/AndroidDriver.js b/detox/src/devices/runtime/drivers/android/AndroidDriver.js
index 8f5e8a541..1249c1372 100644
--- a/detox/src/devices/runtime/drivers/android/AndroidDriver.js
+++ b/detox/src/devices/runtime/drivers/android/AndroidDriver.js
@@ -31,7 +31,7 @@ const log = logger.child({ cat: 'device' });
  * @property adb { ADB }
  * @property aapt { AAPT }
  * @property apkValidator { ApkValidator }
- * @property fileXfer { FileXfer }
+ * @property fileTransfer { FileTransfer }
  * @property appInstallHelper { AppInstallHelper }
  * @property appUninstallHelper { AppUninstallHelper }
  * @property devicePathBuilder { AndroidDevicePathBuilder }
@@ -51,7 +51,7 @@ class AndroidDriver extends DeviceDriverBase {
     this.aapt = deps.aapt;
     this.apkValidator = deps.apkValidator;
     this.invocationManager = deps.invocationManager;
-    this.fileXfer = deps.fileXfer;
+    this.fileTransfer = deps.fileTransfer;
     this.appInstallHelper = deps.appInstallHelper;
     this.appUninstallHelper = deps.appUninstallHelper;
     this.devicePathBuilder = deps.devicePathBuilder;
@@ -331,8 +331,8 @@ class AndroidDriver extends DeviceDriverBase {
   }
 
   async _sendNotificationDataToDevice(dataFileLocalPath, adbName) {
-    await this.fileXfer.prepareDestinationDir(adbName);
-    return await this.fileXfer.send(adbName, dataFileLocalPath, 'notification.json');
+    await this.fileTransfer.prepareDestinationDir(adbName);
+    return await this.fileTransfer.send(adbName, dataFileLocalPath, 'notification.json');
   }
 
   _startActivityWithUrl(url) {
diff --git a/detox/src/devices/runtime/factories/android.js b/detox/src/devices/runtime/factories/android.js
index e025ecf4c..2dc478ee3 100644
--- a/detox/src/devices/runtime/factories/android.js
+++ b/detox/src/devices/runtime/factories/android.js
@@ -6,7 +6,7 @@ class RuntimeDriverFactoryAndroid extends RuntimeDeviceFactory {
     const adb = serviceLocator.adb;
     const aapt = serviceLocator.aapt;
     const apkValidator = serviceLocator.apkValidator;
-    const fileXfer = serviceLocator.fileXfer;
+    const fileTransfer = serviceLocator.fileTransfer;
     const devicePathBuilder = serviceLocator.devicePathBuilder;
 
     const AppInstallHelper = require('../../common/drivers/android/tools/AppInstallHelper');
@@ -18,9 +18,9 @@ class RuntimeDriverFactoryAndroid extends RuntimeDeviceFactory {
       adb,
       aapt,
       apkValidator,
-      fileXfer,
+      fileTransfer,
       devicePathBuilder,
-      appInstallHelper: new AppInstallHelper(adb, fileXfer),
+      appInstallHelper: new AppInstallHelper(adb, fileTransfer),
       appUninstallHelper: new AppUninstallHelper(adb),
       instrumentation: new MonitoredInstrumentation(adb),
     };
diff --git a/detox/src/servicelocator/android/index.js b/detox/src/servicelocator/android/index.js
index c1b7a9599..0bf53c95d 100644
--- a/detox/src/servicelocator/android/index.js
+++ b/detox/src/servicelocator/android/index.js
@@ -3,7 +3,7 @@ const DeviceRegistry = require('../../devices/DeviceRegistry');
 const AAPT = require('../../devices/common/drivers/android/exec/AAPT');
 const ADB = require('../../devices/common/drivers/android/exec/ADB');
 const ApkValidator = require('../../devices/common/drivers/android/tools/ApkValidator');
-const TempFileXfer = require('../../devices/common/drivers/android/tools/TempFileXfer');
+const { TempFileTransfer } = require('../../devices/common/drivers/android/tools/TempFileTransfer');
 
 const AndroidServiceLocator = {
   get emulator() {
@@ -18,7 +18,7 @@ const AndroidServiceLocator = {
 AndroidServiceLocator.adb = new ADB();
 AndroidServiceLocator.aapt = new AAPT();
 AndroidServiceLocator.apkValidator = new ApkValidator(AndroidServiceLocator.aapt);
-AndroidServiceLocator.fileXfer = new TempFileXfer(AndroidServiceLocator.adb);
+AndroidServiceLocator.fileTransfer = new TempFileTransfer(AndroidServiceLocator.adb);
 AndroidServiceLocator.deviceRegistry = DeviceRegistry.forAndroid();
 AndroidServiceLocator.devicePathBuilder = new AndroidDevicePathBuilder();
 


Test patch:
diff --git a/detox/src/devices/common/drivers/android/tools/AppInstallHelper.test.js b/detox/src/devices/common/drivers/android/tools/AppInstallHelper.test.js
index 9a1acb003..aedb4586a 100644
--- a/detox/src/devices/common/drivers/android/tools/AppInstallHelper.test.js
+++ b/detox/src/devices/common/drivers/android/tools/AppInstallHelper.test.js
@@ -10,23 +10,23 @@ describe('Android app installation helper', () => {
     adb = new ADBMock();
   });
 
-  let fileXfer;
+  let fileTransfer;
   let uut;
   beforeEach(() => {
-    const TempFileXfer = jest.genMockFromModule('./TempFileXfer');
-    fileXfer = new TempFileXfer();
+    const FileTransfer = jest.genMockFromModule('./FileTransfer');
+    fileTransfer = new FileTransfer(adb, '/mock-destination-dir');
 
     const AppInstallHelper = require('./AppInstallHelper');
-    uut = new AppInstallHelper(adb, fileXfer);
+    uut = new AppInstallHelper(adb, fileTransfer);
   });
 
   it('should recreate the transient dir on the device', async () => {
     await uut.install(deviceId, appBinaryPath, testBinaryPath);
-    expect(fileXfer.prepareDestinationDir).toHaveBeenCalledWith(deviceId);
+    expect(fileTransfer.prepareDestinationDir).toHaveBeenCalledWith(deviceId);
   });
 
   it('should throw if transient dir prep fails', async () => {
-    fileXfer.prepareDestinationDir.mockRejectedValue(new Error('mocked error in adb-shell'));
+    fileTransfer.prepareDestinationDir.mockRejectedValue(new Error('mocked error in adb-shell'));
 
     try {
       await uut.install(deviceId, appBinaryPath, testBinaryPath);
@@ -36,16 +36,16 @@ describe('Android app installation helper', () => {
 
   it('should push app-binary file to the device', async () => {
     await uut.install(deviceId, appBinaryPath, testBinaryPath);
-    expect(fileXfer.send).toHaveBeenCalledWith(deviceId, appBinaryPath, 'Application.apk');
+    expect(fileTransfer.send).toHaveBeenCalledWith(deviceId, appBinaryPath, 'Application.apk');
   });
 
   it('should push test-binary file to the device', async () => {
     await uut.install(deviceId, appBinaryPath, testBinaryPath);
-    expect(fileXfer.send).toHaveBeenCalledWith(deviceId, testBinaryPath, 'Test.apk');
+    expect(fileTransfer.send).toHaveBeenCalledWith(deviceId, testBinaryPath, 'Test.apk');
   });
 
   it('should break if file push fails', async () => {
-    fileXfer.send.mockRejectedValue(new Error('mocked error in adb-push'));
+    fileTransfer.send.mockRejectedValue(new Error('mocked error in adb-push'));
 
     try {
       await uut.install(deviceId, appBinaryPath, testBinaryPath);
@@ -54,7 +54,7 @@ describe('Android app installation helper', () => {
   });
 
   it('should remote-install both binaries via shell', async () => {
-    fileXfer.send
+    fileTransfer.send
       .mockReturnValueOnce('/mocked-final-dir/first.apk')
       .mockReturnValueOnce('/mocked-final-dir/second.apk');
 
@@ -73,12 +73,12 @@ describe('Android app installation helper', () => {
   });
 
   it('should allow for an install with no test binary', async () => {
-    fileXfer.send
+    fileTransfer.send
       .mockReturnValueOnce('/mocked-final-dir/first.apk')
       .mockReturnValueOnce('/mocked-final-dir/second.apk');
 
     await uut.install(deviceId, appBinaryPath, undefined);
-    expect(fileXfer.send).toHaveBeenCalledTimes(1);
+    expect(fileTransfer.send).toHaveBeenCalledTimes(1);
     expect(adb.remoteInstall).toHaveBeenCalledWith(deviceId, '/mocked-final-dir/first.apk');
     expect(adb.remoteInstall).toHaveBeenCalledTimes(1);
   });
diff --git a/detox/src/devices/common/drivers/android/tools/FileXfer.test.js b/detox/src/devices/common/drivers/android/tools/FileTransfer.test.js
similarity index 91%
rename from detox/src/devices/common/drivers/android/tools/FileXfer.test.js
rename to detox/src/devices/common/drivers/android/tools/FileTransfer.test.js
index 287dec3ff..1a7186457 100644
--- a/detox/src/devices/common/drivers/android/tools/FileXfer.test.js
+++ b/detox/src/devices/common/drivers/android/tools/FileTransfer.test.js
@@ -9,8 +9,8 @@ describe('File-transfer util', () => {
     const ADBMock = jest.genMockFromModule('../exec/ADB');
     adb = new ADBMock();
 
-    const FileXfer = require('./FileXfer');
-    uut = new FileXfer(adb, deviceDestinationDir);
+    const FileTransfer = require('./FileTransfer');
+    uut = new FileTransfer(adb, deviceDestinationDir);
   });
 
   it('should create the destination directory on the device', async () => {
diff --git a/detox/src/devices/common/drivers/android/tools/TempFileXfer.test.js b/detox/src/devices/common/drivers/android/tools/TempFileTransfer.test.js
similarity index 78%
rename from detox/src/devices/common/drivers/android/tools/TempFileXfer.test.js
rename to detox/src/devices/common/drivers/android/tools/TempFileTransfer.test.js
index 0fa3fd709..c63df0817 100644
--- a/detox/src/devices/common/drivers/android/tools/TempFileXfer.test.js
+++ b/detox/src/devices/common/drivers/android/tools/TempFileTransfer.test.js
@@ -5,8 +5,8 @@ describe('Temp file transfer', () => {
     const ADB = jest.genMockFromModule('../exec/ADB');
     adb = new ADB();
 
-    const TempFileXfer = require('./TempFileXfer');
-    uut = new TempFileXfer(adb);
+    const { TempFileTransfer } = require('./TempFileTransfer');
+    uut = new TempFileTransfer(adb);
   });
 
   it('should use the default temp-dir', async () => {
diff --git a/detox/src/devices/runtime/drivers/android/AndroidDriver.test.js b/detox/src/devices/runtime/drivers/android/AndroidDriver.test.js
index a37dcf5cb..52334ad48 100644
--- a/detox/src/devices/runtime/drivers/android/AndroidDriver.test.js
+++ b/detox/src/devices/runtime/drivers/android/AndroidDriver.test.js
@@ -17,7 +17,7 @@ describe('Android driver', () => {
   let adb;
   let aapt;
   let apkValidator;
-  let fileXfer;
+  let fileTransfer;
   let appInstallHelper;
   let appUninstallHelper;
   let instrumentation;
@@ -36,7 +36,7 @@ describe('Android driver', () => {
       adb,
       aapt,
       apkValidator,
-      fileXfer,
+      fileTransfer,
       appInstallHelper,
       appUninstallHelper,
       instrumentation,
@@ -213,21 +213,21 @@ describe('Android driver', () => {
     describe('in app launch (with dedicated arg)', () => {
       it('should prepare the device for receiving notification data file', async () => {
         await uut.launchApp(bundleId, notificationArgs, '');
-        expect(fileXfer.prepareDestinationDir).toHaveBeenCalledWith(adbName);
+        expect(fileTransfer.prepareDestinationDir).toHaveBeenCalledWith(adbName);
       });
 
       it('should transfer the notification data file to the device', async () => {
         await uut.launchApp(bundleId, notificationArgs, '');
-        expect(fileXfer.send).toHaveBeenCalledWith(adbName, notificationArgs.detoxUserNotificationDataURL, 'notification.json');
+        expect(fileTransfer.send).toHaveBeenCalledWith(adbName, notificationArgs.detoxUserNotificationDataURL, 'notification.json');
       });
 
       it('should not send the data if device prep fails', async () => {
-        fileXfer.prepareDestinationDir.mockRejectedValue(new Error());
+        fileTransfer.prepareDestinationDir.mockRejectedValue(new Error());
         await expect(uut.launchApp(bundleId, notificationArgs, '')).rejects.toThrowError();
       });
 
       it('should launch instrumentation with a modified notification data URL arg', async () => {
-        fileXfer.send.mockReturnValue(mockNotificationDataTargetPath);
+        fileTransfer.send.mockReturnValue(mockNotificationDataTargetPath);
 
         await uut.launchApp(bundleId, notificationArgs, '');
 
@@ -252,8 +252,8 @@ describe('Android driver', () => {
         it('should pre-transfer notification data to device', async () => {
           await spec.applyFn();
 
-          expect(fileXfer.prepareDestinationDir).toHaveBeenCalledWith(adbName);
-          expect(fileXfer.send).toHaveBeenCalledWith(adbName, notificationArgs.detoxUserNotificationDataURL, 'notification.json');
+          expect(fileTransfer.prepareDestinationDir).toHaveBeenCalledWith(adbName);
+          expect(fileTransfer.send).toHaveBeenCalledWith(adbName, notificationArgs.detoxUserNotificationDataURL, 'notification.json');
         });
 
         it('should start the app with notification data using invocation-manager', async () => {
@@ -277,7 +277,7 @@ describe('Android driver', () => {
         await uut.launchApp(bundleId, {}, '');
         await uut.deliverPayload(notificationArgsDelayed);
 
-        expect(fileXfer.send).not.toHaveBeenCalled();
+        expect(fileTransfer.send).not.toHaveBeenCalled();
       });
 
       it('should not start the app using invocation-manager', async () => {
@@ -580,10 +580,10 @@ describe('Android driver', () => {
     const ApkValidator = require('../../../common/drivers/android/tools/ApkValidator');
     apkValidator = new ApkValidator();
 
-    jest.mock('../../../common/drivers/android/tools/TempFileXfer');
-    const FileXfer = jest.requireMock('../../../common/drivers/android/tools/TempFileXfer');
-    fileXfer = new FileXfer();
-    fileXfer.send.mockResolvedValue(mockNotificationDataTargetPath);
+    jest.mock('../../../common/drivers/android/tools/FileTransfer');
+    const FileTransfer = jest.requireMock('../../../common/drivers/android/tools/FileTransfer');
+    fileTransfer = new FileTransfer();
+    fileTransfer.send.mockResolvedValue(mockNotificationDataTargetPath);
 
     jest.mock('../../../common/drivers/android/tools/AppInstallHelper');
     const AppInstallHelper = require('../../../common/drivers/android/tools/AppInstallHelper');