Skip to content

Commit

Permalink
add ProcessResult to ProcessContextGitMock and remove redundant
Browse files Browse the repository at this point in the history
  • Loading branch information
alfeilex committed Dec 17, 2024
1 parent 4c5f98a commit dce5d5a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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</a>
*
* @param is {@link InputStream}.
* @param errorStream to identify if the output came from stdout or stderr
* @return {@link CompletableFuture}.
*/
private static CompletableFuture<List<String>> readInputStream(InputStream is, boolean errorStream, ConcurrentLinkedQueue<OutputMessage> outputMessages) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,67 +22,58 @@ public class ProcessContextGitMock implements ProcessContext {

private final List<String> arguments;

private final List<OutputMessage> 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<OutputMessage> output) {

this.exitCode = exitCode;
this.processResult = new ProcessResultImpl("git", "", exitCode, output);
}

/**
* @return the {@link List} of mocked stderr messages.
*/
public List<String> 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<String> 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<OutputMessage> getOutputMessages() {

return this.outputMessages;
public void setExitCode(int exitCode) {

this.exitCode = exitCode;
}

@Override
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}

}
13 changes: 9 additions & 4 deletions cli/src/test/java/com/devonfw/tools/ide/git/GitContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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");
Expand Down

0 comments on commit dce5d5a

Please sign in to comment.