Skip to content

Commit

Permalink
#24 Fix parameter quoting for Linux based OS
Browse files Browse the repository at this point in the history
  • Loading branch information
frimtec committed Dec 18, 2020
1 parent bcac4c2 commit aeea231
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ abstract class AbstractPowerShellExecutor implements PowerShellExecutor {
private static final int INITIAL_STREAM_BUFFER_SIZE = 1000;
private static final String JPSE_GLOBBER_THREAD_NAME = "JPSE-Gobbler";

private final String executionCommand;
private final Path tempPath;
private final String executionCommand;
private final String paramQuote;

AbstractPowerShellExecutor(Path tempPath, String executionCommand) {
AbstractPowerShellExecutor(Path tempPath, String executionCommand, String paramQuote) {
this.tempPath = tempPath;
this.executionCommand = executionCommand;
this.paramQuote = paramQuote;
if (tempPath != null && !Files.exists(tempPath)) {
try {
Files.createDirectories(tempPath);
Expand All @@ -44,7 +46,7 @@ public ExecutionResult execute(Path script, Map<String, String> arguments) {
List<String> commandLine = new ArrayList<>(Arrays.asList(this.executionCommand, "-File", script.toString()));
commandLine.addAll(arguments.entrySet()
.stream()
.flatMap(entry -> Stream.of("-" + entry.getKey(), "\"" + entry.getValue() + "\""))
.flatMap(entry -> Stream.of("-" + entry.getKey(), this.paramQuote + entry.getValue() + this.paramQuote))
.collect(Collectors.toList()));
return execute(createProcessBuilder(commandLine));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
class LinuxPowerShellExecutor extends AbstractPowerShellExecutor {

private static final String POWER_SHELL_CMD = "pwsh";
private static final String PARAM_QUOTE = "";

LinuxPowerShellExecutor(Path tempPath) {
super(tempPath, POWER_SHELL_CMD);
super(tempPath, POWER_SHELL_CMD, PARAM_QUOTE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
class WindowsPowerShellExecutor extends AbstractPowerShellExecutor {

private static final String POWER_SHELL_CMD = "powershell.exe";
private static final String PARAM_QUOTE = "\"";

WindowsPowerShellExecutor(Path tempPath) {
super(tempPath, POWER_SHELL_CMD);
super(tempPath, POWER_SHELL_CMD, PARAM_QUOTE);
}

WindowsPowerShellExecutor(Path tempPath, String executionCommand) {
super(tempPath, executionCommand);
super(tempPath, executionCommand, PARAM_QUOTE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ void testExecuteForScriptFromFile() {
@Test
void testExecuteForScriptFromClasspath() {
// arrange
Map<String, String> arguments = Collections.singletonMap("name", "PowerShell");
Map<String, String> arguments = Collections.singletonMap("name", "Power - 'Shell'");

// act
ExecutionResult executionResult = this.executor.execute(AbstractPowerShellExecutorTest.class.getResourceAsStream("/test.ps1"), arguments);

// assert
assertThat(executionResult.isSuccess()).isTrue();
assertThat(executionResult.getStandardOutput()).isEqualTo("Hello PowerShell!");
assertThat(executionResult.getStandardOutput()).isEqualTo("Hello Power - 'Shell'!");
}

@Test
Expand Down

0 comments on commit aeea231

Please sign in to comment.