Skip to content

Commit

Permalink
CommandExecutor - Support executable path with spaces (#677)
Browse files Browse the repository at this point in the history
  • Loading branch information
yahavi authored Nov 27, 2022
1 parent ef79ccc commit 9d4aeb8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class CommandExecutor implements Serializable {
* @param env - Environment variables to use during execution.
*/
public CommandExecutor(String executablePath, Map<String, String> env) {
this.executablePath = executablePath;
this.executablePath = escapeSpacesInPath(executablePath);
Map<String, String> finalEnvMap = new HashMap<>(System.getenv());
if (env != null) {
Map<String, String> fixedEnvMap = new HashMap<>(env);
Expand Down Expand Up @@ -61,6 +61,19 @@ private void fixPathEnv(Map<String, String> env) {
env.replace("PATH", path);
}

/**
* Escape spaces in the input executable path and trim leading and trailing whitespaces.
*
* @param executablePath - the executable path to process
* @return escaped and trimmed executable path.
*/
private static String escapeSpacesInPath(String executablePath) {
if (executablePath == null) {
return null;
}
return executablePath.trim().replaceAll(" ", SystemUtils.IS_OS_WINDOWS ? "^ " : "\\\\ ");
}

/**
* Fix the PATH value to be valid for execution on a Windows machine.
* Take care of a case when either non-Windows or Windows environment-variables are received.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package org.jfrog.build.extractor.executor;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.jfrog.build.api.util.NullLog;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.collections.Lists;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -61,4 +66,23 @@ public void testExeCommand() {
fail(ExceptionUtils.getRootCauseMessage(e));
}
}

@Test
public void testExeCommandWithSpaces() throws IOException, InterruptedException {
Path tmpDir = Files.createTempDirectory("testEscapeSpacesInPath");
try {
Path testDir = Files.createDirectories(tmpDir.resolve("Kuiil space"));
File executableFile = Files.createFile(testDir.resolve("ship.bat")).toFile();
assertTrue(executableFile.setExecutable(true));
FileUtils.writeStringToFile(executableFile, "echo \"I have spoken\"", StandardCharsets.UTF_8);

// Run executable in "Kuiil space/ship.bat" with no arguments and make sure it prints "I have spoken"
CommandExecutor commandExecutor = new CommandExecutor(executableFile.getAbsolutePath(), null);
CommandResults results = commandExecutor.exeCommand(null, new ArrayList<>(), new ArrayList<>(), null);
assertTrue(results.isOk(), results.getErr());
assertTrue(results.getRes().contains("I have spoken"));
} finally {
FileUtils.forceDelete(tmpDir.toFile());
}
}
}

0 comments on commit 9d4aeb8

Please sign in to comment.