From 839ff9aef2a01719a922d53deb7c039b5b9a3d3f Mon Sep 17 00:00:00 2001 From: Rohan Kumar Date: Wed, 26 Jul 2023 10:19:48 +0530 Subject: [PATCH] feat (jkube-kit/common) : Add workDir configuration option in ExternalCommand 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 --- .../jkube/kit/common/ExternalCommand.java | 9 +- .../jkube/kit/common/ExternalCommandTest.java | 89 +++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/ExternalCommandTest.java diff --git a/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/ExternalCommand.java b/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/ExternalCommand.java index 26f3716ce8..8bb2033c09 100644 --- a/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/ExternalCommand.java +++ b/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/ExternalCommand.java @@ -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; @@ -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 { @@ -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(), diff --git a/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/ExternalCommandTest.java b/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/ExternalCommandTest.java new file mode 100644 index 0000000000..2405ee3a08 --- /dev/null +++ b/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/ExternalCommandTest.java @@ -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; + } + } +}