From 730acdcd245d323849c18a1e6102bb2ff04fc846 Mon Sep 17 00:00:00 2001 From: dragonmacher <48328597+dragonmacher@users.noreply.github.com> Date: Thu, 18 Jul 2019 18:45:58 -0400 Subject: [PATCH] GT-3003 - Testing - updated framework test code to work in user installation with Eclipse --- ...AbstractGhidraHeadlessIntegrationTest.java | 4 +- .../framework/TestApplicationUtils.java | 113 +++++++++++++++--- 2 files changed, 98 insertions(+), 19 deletions(-) diff --git a/Ghidra/Features/Base/src/main/java/ghidra/test/AbstractGhidraHeadlessIntegrationTest.java b/Ghidra/Features/Base/src/main/java/ghidra/test/AbstractGhidraHeadlessIntegrationTest.java index 3a73e21c7a..aee8eb938b 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/test/AbstractGhidraHeadlessIntegrationTest.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/test/AbstractGhidraHeadlessIntegrationTest.java @@ -15,7 +15,7 @@ */ package ghidra.test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertNotNull; import java.io.File; import java.io.IOException; @@ -55,7 +55,7 @@ public abstract class AbstractGhidraHeadlessIntegrationTest extends AbstractDock public static final String PROJECT_NAME = createProjectName(); private static String createProjectName() { - File repoDirectory = TestApplicationUtils.getRepoContainerDirectory(); + File repoDirectory = TestApplicationUtils.getInstallationDirectory(); return repoDirectory.getName() + PROJECT_NAME_SUFFIX; } diff --git a/Ghidra/Framework/Generic/src/main/java/ghidra/framework/TestApplicationUtils.java b/Ghidra/Framework/Generic/src/main/java/ghidra/framework/TestApplicationUtils.java index 13410872f6..755f5b5b07 100644 --- a/Ghidra/Framework/Generic/src/main/java/ghidra/framework/TestApplicationUtils.java +++ b/Ghidra/Framework/Generic/src/main/java/ghidra/framework/TestApplicationUtils.java @@ -16,18 +16,23 @@ package ghidra.framework; import java.io.File; +import java.util.List; +import org.apache.commons.lang3.StringUtils; + +import ghidra.util.Msg; +import ghidra.util.SystemUtilities; +import ghidra.util.exception.AssertException; +import utilities.util.FileUtilities; import utility.module.ModuleUtilities; public class TestApplicationUtils { - public static File getTestApplicationRootDirectory() { - // returns the application root used for testing; called before Application.initialize() - File repo = getCurrentRepoDirectory(); - return new File(repo, "Ghidra"); - } - - public static File getCurrentRepoDirectory() { + /** + * Returns the directory that contains the source code repository + * @return the directory that contains the source code repository + */ + private static File getCurrentRepoDirectory() { // Assumption: the user is running tests from within a repo sub-project // // At the time writing "user.dir" is the "Ghidra" directory. @@ -39,31 +44,105 @@ public class TestApplicationUtils { } /** - * Returns a directory that contains all repos for a given git clone. This - * directory name is unique to the active clone collection, which makes it - * useful for creating unique temporary directories to allow multiple - * simultaneous test runs. + * Returns a directory that contains all repos for a given git clone. This directory name + * is unique to the active clone collection, which makes it useful for creating unique + * temporary directories to allow multiple simultaneous test runs. * * @return the parent dir of the current repo */ - public static File getRepoContainerDirectory() { + private static File getRepoContainerDirectory() { File repo = getCurrentRepoDirectory(); + if (repo == null) { + return null; + } File repoContainer = repo.getParentFile(); return repoContainer; } /** - * Creates a folder that is unique for the current repo. This allows clients - * to have multiple clones on their machine, running tests from each repo - * simultaneously. + * Returns the directory containing the installation of this application. The value returned + * here will either be an actual installation directory or the parent directory of a cloned + * repository. This method will work in the various modes of operation, including: + * + * + * + * @return the installation directory + */ + public static File getInstallationDirectory() { + + File repo = getCurrentRepoDirectory(); + if (repo != null) { + // development mode: either user-level or test machine + return repo; + } + + // Assumption - in an installation the current user dir is /...//Ghidra + + String currentDir = System.getProperty("user.dir"); + Msg.debug(null, "user.dir: " + currentDir); + + // Assume that core library files are bundled in a jar file. Find the installation + // directory by using the distributed jar file. + File jarFile = SystemUtilities.getSourceLocationForClass(SystemUtilities.class); + if (jarFile == null || !jarFile.getName().endsWith(".jar")) { + throw new AssertException("Unable to determine the installation directory"); + } + + // Assumption - jar file location follows this form: + // /App Name/Module Group/Module Name/lib/file.jar + List parts = FileUtilities.pathToParts(jarFile.getAbsolutePath()); + int last = parts.size() - 1; + int installDir = last - 5; // 5 folders above the filename (see above) + + String path = StringUtils.join(parts.subList(0, installDir + 1), File.separator); + return new File(path); + } + + /** + * Creates a folder that is unique for the current installation. This allows clients to + * have multiple clones (for development mode) or multiple installations (for release mode) + * on their machine, running tests from each repo simultaneously. + * + * @return a folder that is unique for the current installation */ public static File getUniqueTempFolder() { + // // Create a unique name based upon the repo from which we are running. // - File reposContainer = TestApplicationUtils.getRepoContainerDirectory(); + File reposContainer = getRepoContainerDirectory(); + if (reposContainer == null) { + File installDir = getInstallationDirectory(); + reposContainer = installDir; + } File tmpDir = new File(System.getProperty("java.io.tmpdir")); String tempName = tmpDir.getName();