Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Commit

Permalink
Use getMountPoint to construct a path to Fork directory instead of ha…
Browse files Browse the repository at this point in the history
…rdcoding it
  • Loading branch information
AndreiZaitcev committed Aug 25, 2022
1 parent c7e8a98 commit ee92adf
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void saveScreenRecording(TestIdentifier test, File output) {
try {
recorderTask.awaitCompletion();

String remoteFilePath = remoteVideoForTest(test);
String remoteFilePath = remoteVideoForTest(device.getDeviceInterface(), test);
logger.debug("Save screen recording {} to {}", remoteFilePath, output);
pullTestVideo(remoteFilePath, output);
} catch (Exception e) {
Expand All @@ -126,7 +126,7 @@ public void removeScreenRecording(TestIdentifier test) {
try {
recorderTask.awaitCompletion();

String remoteFilePath = remoteVideoForTest(test);
String remoteFilePath = remoteVideoForTest(device.getDeviceInterface(), test);
logger.debug("Remove screen recording {}", remoteFilePath);
removeTestVideo(remoteFilePath);

Expand Down Expand Up @@ -169,7 +169,7 @@ public void awaitCompletion() throws InterruptedException {
@Override
public void run() {
try {
String remoteFilePath = remoteVideoForTest(test);
String remoteFilePath = remoteVideoForTest(deviceInterface, test);
logger.debug("Started recording video {}", remoteFilePath);

startRecordingTestVideo(remoteFilePath);
Expand Down
5 changes: 3 additions & 2 deletions fork-runner/src/main/java/com/shazam/fork/runner/TestRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.shazam.fork.model.TestCaseEvent;
import com.shazam.fork.system.PermissionGrantingManager;
import com.shazam.fork.system.io.RemoteFileManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -69,7 +68,9 @@ public void execute() {

if (testRunParameters.isCoverageEnabled()) {
runner.setCoverage(true);
runner.addInstrumentationArg("coverageFile", RemoteFileManager.getCoverageFileName(new TestIdentifier(testClassName, testMethodName)));
String coverageFileName =
RemoteFileManager.getCoverageFileName(device, new TestIdentifier(testClassName, testMethodName));
runner.addInstrumentationArg("coverageFile", coverageFileName);
}
String excludedAnnotation = testRunParameters.getExcludedAnnotation();
if (!Strings.isNullOrEmpty(excludedAnnotation)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import com.android.ddmlib.testrunner.ITestRunListener;
import com.android.ddmlib.testrunner.TestIdentifier;
import com.shazam.fork.model.*;
import com.shazam.fork.model.Device;
import com.shazam.fork.model.Pool;
import com.shazam.fork.model.TestCaseEvent;
import com.shazam.fork.system.io.FileManager;
import com.shazam.fork.system.io.RemoteFileManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -64,7 +65,7 @@ public void testRunStopped(long elapsedTime) {
@Override
public void testRunEnded(long elapsedTime, Map<String, String> runMetrics) {
TestIdentifier testIdentifier = new TestIdentifier(testCase.getTestClass(), testCase.getTestMethod());
final String remoteFile = RemoteFileManager.getCoverageFileName(testIdentifier);
final String remoteFile = RemoteFileManager.getCoverageFileName(device.getDeviceInterface(), testIdentifier);
final File file = fileManager.createFile(COVERAGE, pool, device, testIdentifier);
try {
device.getDeviceInterface().pullFile(remoteFile, file.getAbsolutePath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,78 @@
import com.android.ddmlib.IDevice;
import com.android.ddmlib.NullOutputReceiver;
import com.android.ddmlib.ShellCommandUnresponsiveException;
import com.android.ddmlib.SyncException;
import com.android.ddmlib.TimeoutException;
import com.android.ddmlib.testrunner.TestIdentifier;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class RemoteFileManager {

private static final Logger logger = LoggerFactory.getLogger(RemoteFileManager.class);
private static final String FORK_DIRECTORY = "/sdcard/fork";
private static final NullOutputReceiver NO_OP_RECEIVER = new NullOutputReceiver();
private static final String COVERAGE_DIRECTORY = FORK_DIRECTORY + "/coverage";

private RemoteFileManager() {}
private RemoteFileManager() {
}

public static void removeRemotePath(IDevice device, String remotePath) {
executeCommand(device, "rm " + remotePath, "Could not delete remote file(s): " + remotePath);
}

public static void createCoverageDirectory(IDevice device) {
executeCommand(device, "mkdir " + COVERAGE_DIRECTORY,
"Could not create remote directory: " + COVERAGE_DIRECTORY);
String coverageDirectory = getCoverageDirectory(device);
executeCommand(device, "mkdir " + coverageDirectory,
"Could not create remote directory: " + coverageDirectory);
}

public static String getCoverageFileName(TestIdentifier testIdentifier) {
return COVERAGE_DIRECTORY + "/" +testIdentifier.toString() + ".ec";
public static String getCoverageFileName(IDevice device, TestIdentifier testIdentifier) {
return getCoverageDirectory(device) + "/" + testIdentifier.toString() + ".ec";
}

public static void createRemoteDirectory(IDevice device) {
executeCommand(device, "mkdir " + FORK_DIRECTORY, "Could not create remote directory: " + FORK_DIRECTORY);
String forkDirectory = getForkDirectory(device);
executeCommand(device, "mkdir " + forkDirectory, "Could not create remote directory: " + forkDirectory);
}

public static void removeRemoteDirectory(IDevice device) {
executeCommand(device, "rm -r " + FORK_DIRECTORY, "Could not delete remote directory: " + FORK_DIRECTORY);
String forkDirectory = getForkDirectory(device);
executeCommand(device, "rm -r " + forkDirectory, "Could not delete remote directory: " + forkDirectory);
}

private static void executeCommand(IDevice device, String command, String errorMessage) {
try {
device.executeShellCommand(command, NO_OP_RECEIVER);
} catch (TimeoutException | AdbCommandRejectedException | ShellCommandUnresponsiveException | IOException e) {
} catch (TimeoutException | AdbCommandRejectedException |
ShellCommandUnresponsiveException | IOException e) {
logger.error(errorMessage, e);
}
}

public static String remoteVideoForTest(TestIdentifier test) {
return remoteFileForTest(videoFileName(test));
@NotNull
public static String remoteVideoForTest(IDevice device, TestIdentifier test) {
return remoteFileForTest(device, videoFileName(test));
}

private static String remoteFileForTest(String filename) {
return FORK_DIRECTORY + "/" + filename;
@NotNull
private static String remoteFileForTest(IDevice device, String filename) {
return getForkDirectory(device) + "/" + filename;
}

@NotNull
private static String getCoverageDirectory(IDevice device) {
return getForkDirectory(device) + "/coverage";
}

@NotNull
private static String getForkDirectory(IDevice device) {
String externalStorage = device.getMountPoint(IDevice.MNT_EXTERNAL_STORAGE);
if (externalStorage != null) {
return externalStorage + "/fork";
} else {
return "/sdcard/fork";
}
}

private static String videoFileName(TestIdentifier test) {
Expand Down

0 comments on commit ee92adf

Please sign in to comment.