-
Notifications
You must be signed in to change notification settings - Fork 528
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (jkube-kit/common) : Add workDir configuration option in Externa…
…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
1 parent
1ffc921
commit 839ff9a
Showing
2 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/ExternalCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |