From dce5d5af4a7eda008f56135b0ddbc97fe24698aa Mon Sep 17 00:00:00 2001 From: alfeilex <101652401+alfeilex@users.noreply.github.com> Date: Tue, 17 Dec 2024 14:49:55 +0100 Subject: [PATCH] add ProcessResult to ProcessContextGitMock and remove redundant --- .../tools/ide/process/ProcessContextImpl.java | 1 + .../ide/context/ProcessContextGitMock.java | 47 ++++++++----------- .../devonfw/tools/ide/git/GitContextTest.java | 13 +++-- 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/cli/src/main/java/com/devonfw/tools/ide/process/ProcessContextImpl.java b/cli/src/main/java/com/devonfw/tools/ide/process/ProcessContextImpl.java index 5556236fd..6b31213de 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/process/ProcessContextImpl.java +++ b/cli/src/main/java/com/devonfw/tools/ide/process/ProcessContextImpl.java @@ -219,6 +219,7 @@ public ProcessResult run(ProcessMode processMode) { * "https://stackoverflow.com/questions/14165517/processbuilder-forwarding-stdout-and-stderr-of-started-processes-without-blocki/57483714#57483714">StackOverflow * * @param is {@link InputStream}. + * @param errorStream to identify if the output came from stdout or stderr * @return {@link CompletableFuture}. */ private static CompletableFuture> readInputStream(InputStream is, boolean errorStream, ConcurrentLinkedQueue outputMessages) { diff --git a/cli/src/test/java/com/devonfw/tools/ide/context/ProcessContextGitMock.java b/cli/src/test/java/com/devonfw/tools/ide/context/ProcessContextGitMock.java index 7caa5caea..405e834cf 100644 --- a/cli/src/test/java/com/devonfw/tools/ide/context/ProcessContextGitMock.java +++ b/cli/src/test/java/com/devonfw/tools/ide/context/ProcessContextGitMock.java @@ -7,7 +7,6 @@ import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; import com.devonfw.tools.ide.process.OutputMessage; import com.devonfw.tools.ide.process.ProcessContext; @@ -23,67 +22,58 @@ public class ProcessContextGitMock implements ProcessContext { private final List arguments; - private final List outputMessages; - private final LocalDateTime now; private int exitCode; private final Path directory; + private ProcessResult processResult; + /** * @param directory the {@link Path} to the git repository. */ public ProcessContextGitMock(Path directory) { this.arguments = new ArrayList<>(); - this.outputMessages = new ArrayList<>(); + this.processResult = new ProcessResultImpl("git", "", 0, new ArrayList<>()); this.exitCode = ProcessResult.SUCCESS; this.directory = directory; this.now = LocalDateTime.now(); } /** - * @return the mocked {@link ProcessResult#getExitCode() exit code}. + * @return the mocked {@link ProcessResult} */ - public int getExitCode() { + public ProcessResult getProcessResult() { - return this.exitCode; + return this.processResult; } /** * @param exitCode the {@link #getExitCode() exit code}. + * @param output the list of {@link OutputMessage}} + * @return the mocked {@link ProcessResult} */ - public void setExitCode(int exitCode) { + public void setProcessResult(int exitCode, List output) { - this.exitCode = exitCode; + this.processResult = new ProcessResultImpl("git", "", exitCode, output); } /** - * @return the {@link List} of mocked stderr messages. - */ - public List getErrors() { - - return this.outputMessages.stream().filter(OutputMessage::error).map(OutputMessage::message).collect(Collectors.toList()); - - } - - /** - * @return the {@link List} of mocked stdout messages. + * @return the mocked {@link ProcessResult#getExitCode() exit code}. */ - public List getOuts() { - - return this.outputMessages.stream().filter(output -> !output.error()).map(OutputMessage::message).collect(Collectors.toList()); + public int getExitCode() { + return this.exitCode; } /** - * @return the {@link List} of mocked {@link OutputMessage}. + * @param exitCode the {@link #getExitCode() exit code}. */ - public List getOutputMessages() { - - return this.outputMessages; + public void setExitCode(int exitCode) { + this.exitCode = exitCode; } @Override @@ -150,7 +140,7 @@ public ProcessResult run(ProcessMode processMode) { if (this.arguments.contains("ls-files")) { if (Files.exists(this.directory.resolve("new-folder"))) { OutputMessage outputMessage = new OutputMessage(false, "new-folder"); - this.outputMessages.add(outputMessage); + processResult.getOutputMessages().add(outputMessage); this.exitCode = 0; } } @@ -192,7 +182,8 @@ public ProcessResult run(ProcessMode processMode) { } } this.arguments.clear(); - return new ProcessResultImpl("git", command.toString(), this.exitCode, this.outputMessages); + setProcessResult(getExitCode(), this.processResult.getOutputMessages()); + return this.processResult; } } diff --git a/cli/src/test/java/com/devonfw/tools/ide/git/GitContextTest.java b/cli/src/test/java/com/devonfw/tools/ide/git/GitContextTest.java index e82e8789c..2ab98427d 100644 --- a/cli/src/test/java/com/devonfw/tools/ide/git/GitContextTest.java +++ b/cli/src/test/java/com/devonfw/tools/ide/git/GitContextTest.java @@ -35,6 +35,8 @@ private IdeTestContext newGitContext(Path dir) { this.processContext = new ProcessContextGitMock(dir); context.setProcessContext(processContext); context.setGitContext(new GitContextImpl(context)); + // reset ProcessResult instance + this.processContext.setProcessResult(0, new ArrayList<>()); return context; } @@ -72,7 +74,8 @@ public void testRunGitClone(@TempDir Path tempDir) { // arrange String gitRepoUrl = "https://github.com/test"; IdeTestContext context = newGitContext(tempDir); - this.processContext.getOuts().add("test-remote"); + OutputMessage outputMessage = new OutputMessage(false, "test-remote"); + this.processContext.getProcessResult().getOutputMessages().add(outputMessage); // act context.getGitContext().pullOrClone(GitUrl.of(gitRepoUrl), tempDir); // assert @@ -91,7 +94,7 @@ public void testRunGitPullWithoutForce(@TempDir Path tempDir) { String gitRepoUrl = "https://github.com/test"; IdeTestContext context = newGitContext(tempDir); OutputMessage outputMessage = new OutputMessage(false, "test-remote"); - this.processContext.getOutputMessages().add(outputMessage); + this.processContext.getProcessResult().getOutputMessages().add(outputMessage); FileAccess fileAccess = new FileAccessImpl(context); Path gitFolderPath = tempDir.resolve(".git"); fileAccess.mkdirs(gitFolderPath); @@ -131,7 +134,8 @@ public void testRunGitPullWithForceStartsReset(@TempDir Path tempDir) { throw new RuntimeException(e); } IdeTestContext context = newGitContext(tempDir); - this.processContext.getOuts().add("test-remote"); + OutputMessage outputMessage = new OutputMessage(false, "test-remote"); + this.processContext.getProcessResult().getOutputMessages().add(outputMessage); // act context.getGitContext().pullOrCloneAndResetIfNeeded(new GitUrl(gitRepoUrl, "master"), tempDir, "origin"); // assert @@ -149,7 +153,8 @@ public void testRunGitPullWithForceStartsCleanup(@TempDir Path tempDir) { // arrange String gitRepoUrl = "https://github.com/test"; IdeTestContext context = newGitContext(tempDir); - this.processContext.getOuts().add("test-remote"); + OutputMessage outputMessage = new OutputMessage(false, "test-remote"); + this.processContext.getProcessResult().getOutputMessages().add(outputMessage); GitContext gitContext = context.getGitContext(); FileAccess fileAccess = context.getFileAccess(); Path gitFolderPath = tempDir.resolve(".git");