Skip to content

Commit 04b695b

Browse files
Merge pull request #2 from AnetteTaivere/analysisTests
Analysis tests
2 parents 94417f3 + ea26a6a commit 04b695b

File tree

2 files changed

+173
-61
lines changed

2 files changed

+173
-61
lines changed

src/main/java/goblintserver/GoblintServer.java

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,22 @@ public void abortAnalysis() throws IOException {
7171
*/
7272
public void preAnalyse() {
7373
String[] preAnalyzeCommand = configuration.getPreAnalyzeCommand();
74-
75-
//Miks seda ei ilmu kuhugile??
76-
log.info(preAnalyzeCommand);
77-
System.out.println(preAnalyzeCommand);
78-
System.out.println();
79-
80-
if (preAnalyzeCommand != null) {
74+
if (preAnalyzeCommand != null && preAnalyzeCommand.length != 0) {
8175
try {
82-
log.info("Preanalyze command ran: \"" + Arrays.toString(preAnalyzeCommand) + "\"");
83-
runCommand(new File(System.getProperty("user.dir")), preAnalyzeCommand);
84-
log.info("Preanalyze command finished.");
76+
String preAnalyzeCommandString = Arrays.toString(preAnalyzeCommand);
77+
log.info("PreAnalysis command ran: '" + preAnalyzeCommandString + "'");
78+
ProcessListener processListener = new ProcessListener() {};
79+
StartedProcess preAnalysisProcess = runCommand(new File(System.getProperty("user.dir")), preAnalyzeCommand, processListener);
80+
switch (preAnalysisProcess.getProcess().waitFor()) {
81+
case 0 -> log.info("PreAnalysis command finished.");
82+
default -> {
83+
log.warn("Running preAnalysis command failed. (code: " + preAnalysisProcess.getProcess().exitValue() + ")");
84+
magpieServer.forwardMessageToClient(new MessageParams(MessageType.Warning, "Running preAnalysis command failed."));
85+
}
86+
}
8587
} catch (IOException | InvalidExitValueException | InterruptedException | TimeoutException e) {
86-
this.magpieServer.forwardMessageToClient(
87-
new MessageParams(MessageType.Warning, "Running preanalysis command failed. " + e.getMessage()));
88+
log.warn("Running preAnalysis command failed. " + e.getMessage());
89+
this.magpieServer.forwardMessageToClient(new MessageParams(MessageType.Warning, "Running preAnalysis command failed. " + e.getMessage()));
8890
}
8991
}
9092
}
@@ -129,9 +131,26 @@ private String[] constructGoblintVersionCheckCommand() {
129131
*/
130132
public void startGoblintServer() {
131133
try {
132-
// run command to start goblint
134+
// run command to start Goblint
135+
ProcessListener listener = new ProcessListener() {
136+
public void afterStop(Process process) {
137+
switch (process.exitValue()) {
138+
case 0 -> log.info("Goblint server has stopped.");
139+
case 143 -> {
140+
log.info("Goblint server has been killed.");
141+
magpieServer.forwardMessageToClient(new MessageParams(MessageType.Error, "Goblint server has been killed. Please check the output terminal of GobPie extension for more information."));
142+
}
143+
default -> {
144+
log.error("Goblint server exited due to an error (code: " + process.exitValue() + "). Please fix the issue reported above and rerun the analysis to restart the extension.");
145+
magpieServer.forwardMessageToClient(new MessageParams(MessageType.Error, "Goblint server exited due to an error. Please check the output terminal of GobPie extension for more information."));
146+
}
147+
}
148+
magpieServer.cleanUp();
149+
// TODO: throw an exception? where (and how) can it be caught to be handled though?
150+
}
151+
};
133152
log.info("Goblint run with command: " + String.join(" ", goblintRunCommand));
134-
goblintRunProcess = runCommand(new File(System.getProperty("user.dir")), goblintRunCommand);
153+
goblintRunProcess = runCommand(new File(System.getProperty("user.dir")), goblintRunCommand, listener);
135154
} catch (IOException | InvalidExitValueException | InterruptedException | TimeoutException e) {
136155
throw new GobPieException("Running Goblint failed.", e, GOBLINT_EXCEPTION);
137156
}
@@ -176,23 +195,7 @@ public String checkGoblintVersion() {
176195
* @param command The command to run.
177196
* @return An object that represents a process that has started. It may or may not have finished.
178197
*/
179-
private StartedProcess runCommand(File dirPath, String[] command) throws IOException, InterruptedException, TimeoutException {
180-
ProcessListener listener = new ProcessListener() {
181-
182-
public void afterStop(Process process) {
183-
if (process.exitValue() == 0) {
184-
log.info("Goblint server has stopped.");
185-
} else if (process.exitValue() == 143) {
186-
log.info("Goblint server has been killed.");
187-
} else {
188-
magpieServer.forwardMessageToClient(new MessageParams(MessageType.Error, "Goblint server exited due to an error. Please check the output terminal of GobPie extension for more information."));
189-
log.error("Goblint server exited due to an error (code: " + process.exitValue() + "). Please fix the issue reported above and rerun the analysis to restart the extension.");
190-
}
191-
magpieServer.cleanUp();
192-
// TODO: throw an exception? where (and how) can it be caught to be handled though?
193-
}
194-
};
195-
198+
private StartedProcess runCommand(File dirPath, String[] command, ProcessListener listener) throws IOException, InterruptedException, TimeoutException {
196199
log.debug("Waiting for command: " + Arrays.toString(command) + " to run...");
197200
return new ProcessExecutor()
198201
.directory(dirPath)

0 commit comments

Comments
 (0)