Skip to content

Commit

Permalink
add general List of output messages and rename logevent to outputmess…
Browse files Browse the repository at this point in the history
…age and refactor ProcessContextGitMock
  • Loading branch information
alfeilex committed Dec 10, 2024
1 parent 41a42ce commit 3da75c7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
* @param error A boolean flag that indicates whether the log event represents and error or standard output
* @param message A string containing the log message
*/
public record LogEvent(boolean error, String message) {
public record OutputMessage(boolean error, String message) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,16 @@ public ProcessResult run(ProcessMode processMode) {

this.processBuilder.command(args);

List<LogEvent> logs = new ArrayList<>();
List<String> out = null;
List<String> err = null;
List<OutputMessage> output = new ArrayList<>();

Process process = this.processBuilder.start();

try {
if (processMode == ProcessMode.DEFAULT_CAPTURE) {
CompletableFuture<List<String>> outFut = readInputStream(process.getInputStream());
CompletableFuture<List<String>> errFut = readInputStream(process.getErrorStream());
out = outFut.get();
err = errFut.get();
CompletableFuture<List<String>> outFut = readInputStream(process.getInputStream(), false, output);
CompletableFuture<List<String>> errFut = readInputStream(process.getErrorStream(), true, output);
outFut.get();
errFut.get();
}

int exitCode;
Expand All @@ -190,7 +188,7 @@ public ProcessResult run(ProcessMode processMode) {
exitCode = process.waitFor();
}

ProcessResult result = new ProcessResultImpl(this.executable.getFileName().toString(), command, exitCode, out, err);
ProcessResult result = new ProcessResultImpl(this.executable.getFileName().toString(), command, exitCode, output);

performLogging(result, exitCode, interpreter);

Expand Down Expand Up @@ -221,7 +219,7 @@ public ProcessResult run(ProcessMode processMode) {
* @param is {@link InputStream}.
* @return {@link CompletableFuture}.
*/
private static CompletableFuture<List<String>> readInputStream(InputStream is, boolean errorStream, List<LogEvent> logs) {
private static CompletableFuture<List<String>> readInputStream(InputStream is, boolean errorStream, List<OutputMessage> logs) {

return CompletableFuture.supplyAsync(() -> {

Expand All @@ -230,8 +228,8 @@ private static CompletableFuture<List<String>> readInputStream(InputStream is, b
String line;
while ((line = br.readLine()) != null) {
synchronized (logs) {
LogEvent logEvent = new LogEvent(errorStream, line);
logs.add(logEvent);
OutputMessage outputMessage = new OutputMessage(errorStream, line);
logs.add(outputMessage);
}
}
return br.lines().toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.devonfw.tools.ide.cli.CliProcessException;
import com.devonfw.tools.ide.context.IdeContext;
Expand All @@ -23,27 +24,25 @@ public class ProcessResultImpl implements ProcessResult {

private final List<String> err;

private final List<LogEvent> logEvents;
private final List<OutputMessage> outputMessages;

/**
* The constructor.
*
* @param executable the {@link #getExecutable() executable}.
* @param command the {@link #getCommand() command}.
* @param exitCode the {@link #getExitCode() exit code}.
* @param out the {@link #getOut() out}.
* @param err the {@link #getErr() err}.
* @param logEvents the {@link #getLogEvents()} () logEvents}.
* @param output {@link #getOutputMessages() output Messages}.
*/
public ProcessResultImpl(String executable, String command, int exitCode, List<String> out, List<String> err) {
public ProcessResultImpl(String executable, String command, int exitCode, List<OutputMessage> output) {

super();
this.executable = executable;
this.command = command;
this.exitCode = exitCode;
this.out = Objects.requireNonNullElse(out, Collections.emptyList());
this.err = Objects.requireNonNullElse(err, Collections.emptyList());
this.logEvents = Objects.requireNonNullElse(logEvents, Collections.emptyList());
this.outputMessages = Objects.requireNonNullElse(output, Collections.emptyList());
this.out = this.outputMessages.stream().filter(outputMessage -> !outputMessage.error()).map(OutputMessage::message).collect(Collectors.toList());
this.err = this.outputMessages.stream().filter(OutputMessage::error).map(OutputMessage::message).collect(Collectors.toList());
}

@Override
Expand All @@ -67,18 +66,18 @@ public int getExitCode() {
@Override
public List<String> getOut() {

return this.out;
return outputMessages.stream().filter(output -> !output.error()).map(OutputMessage::message).collect(Collectors.toList());
}

@Override
public List<String> getErr() {

return this.err;
return outputMessages.stream().filter(OutputMessage::error).map(OutputMessage::message).collect(Collectors.toList());
}

public List<LogEvent> getLogEvents() {
public List<OutputMessage> getOutputMessages() {

return this.logEvents;
return this.outputMessages;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
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;
import com.devonfw.tools.ide.process.ProcessErrorHandling;
import com.devonfw.tools.ide.process.ProcessMode;
Expand All @@ -21,9 +23,7 @@ public class ProcessContextGitMock implements ProcessContext {

private final List<String> arguments;

private final List<String> errors;

private final List<String> outs;
private final List<OutputMessage> outputMessages;

private final LocalDateTime now;

Expand All @@ -37,8 +37,7 @@ public class ProcessContextGitMock implements ProcessContext {
public ProcessContextGitMock(Path directory) {

this.arguments = new ArrayList<>();
this.errors = new ArrayList<>();
this.outs = new ArrayList<>();
this.outputMessages = new ArrayList<>();
this.exitCode = ProcessResult.SUCCESS;
this.directory = directory;
this.now = LocalDateTime.now();
Expand All @@ -65,15 +64,17 @@ public void setExitCode(int exitCode) {
*/
public List<String> getErrors() {

return errors;
return this.outputMessages.stream().filter(OutputMessage::error).map(OutputMessage::message).collect(Collectors.toList());

}

/**
* @return the {@link List} of mocked out messages.
*/
public List<String> getOuts() {

return outs;
return this.outputMessages.stream().filter(outputMessage -> !outputMessage.error()).map(OutputMessage::message).collect(Collectors.toList());

}

@Override
Expand Down Expand Up @@ -139,7 +140,8 @@ public ProcessResult run(ProcessMode processMode) {
// part of git cleanup checks if a new directory 'new-folder' exists
if (this.arguments.contains("ls-files")) {
if (Files.exists(this.directory.resolve("new-folder"))) {
this.outs.add("new-folder");
OutputMessage outputMessage = new OutputMessage(false, "new-folder");
this.outputMessages.add(outputMessage);
this.exitCode = 0;
}
}
Expand Down Expand Up @@ -181,7 +183,7 @@ public ProcessResult run(ProcessMode processMode) {
}
}
this.arguments.clear();
return new ProcessResultImpl("git", command.toString(), this.exitCode, this.outs, this.errors);
return new ProcessResultImpl("git", command.toString(), this.exitCode, this.outputMessages);
}

}

0 comments on commit 3da75c7

Please sign in to comment.