Skip to content

Commit

Permalink
feat (jkube-kit/common) : Add workDir configuration option in Externa…
Browse files Browse the repository at this point in the history
…lCommand

While executing a command, add a workDir configuration option so that
command can be executed in directory other than current directory.

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia authored and manusa committed Jul 31, 2023
1 parent 1ffc921 commit 839ff9a
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand All @@ -35,13 +36,19 @@
*/
public abstract class ExternalCommand {
protected final KitLogger log;
private final File workDir;

private final ExecutorService executor = Executors.newFixedThreadPool(2);

private int statusCode;

protected ExternalCommand(KitLogger log) {
this(log, null);
}

protected ExternalCommand(KitLogger log, File dir) {
this.log = log;
this.workDir = dir;
}

public void execute() throws IOException {
Expand Down Expand Up @@ -109,7 +116,7 @@ private void inputStreamPump(OutputStream outputStream, String processInput) {

private Process startProcess() throws IOException {
try {
return Runtime.getRuntime().exec(getArgs());
return Runtime.getRuntime().exec(getArgs(), null, workDir);
} catch (IOException e) {
throw new IOException(String.format("Failed to start '%s' : %s",
getCommandAsString(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.kit.common;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIOException;

class ExternalCommandTest {
private final KitLogger kitLogger = new KitLogger.SilentLogger();

@Test
void execute_whenCommandCompletedSuccessfully_thenPrintResult() throws IOException {
// Given
TestCommand testCommand = new TestCommand(kitLogger, new String[] {"echo", "hello"});

// When
testCommand.execute();

// Then
assertThat(testCommand.getResult()).isEqualTo("hello");
}

@Test
void execute_whenCommandFailed_thenThrowException() {
// Given
TestCommand testCommand = new TestCommand(kitLogger, new String[] {"ls", "idontexist"});

// When + Then
assertThatIOException()
.isThrownBy(testCommand::execute)
.withMessage("Process 'ls idontexist' exited with status 2");
}

@Test
void execute_whenWorkDirProvided_thenUseWorkDir(@TempDir File temporaryFolder) throws IOException {
// Given
TestCommand testCommand = new TestCommand(kitLogger, new String[] {"touch", "foo"}, temporaryFolder);

// When
testCommand.execute();

// Then
assertThat(new File(temporaryFolder, "foo")).exists();
}


private static class TestCommand extends ExternalCommand {
private String result;
private final String[] args;
public TestCommand(KitLogger kitLogger, String[] args) {
this(kitLogger, args, null);
}

public TestCommand(KitLogger kitLogger, String[] args, File dir) {
super(kitLogger, dir);
this.args = args;
}

@Override
protected String[] getArgs() {
return args;
}

@Override
protected void processLine(String line) {
result = line;
}

public String getResult() {
return result;
}
}
}

0 comments on commit 839ff9a

Please sign in to comment.