Skip to content

Commit d184abb

Browse files
Kimmo Linnavuod471061c
authored andcommitted
Change optionals to exceptions
1 parent 6320365 commit d184abb

File tree

1 file changed

+46
-57
lines changed

1 file changed

+46
-57
lines changed

tmc-langs-qmake/src/main/java/fi/helsinki/cs/tmc/langs/qmake/QmakePlugin.java

Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public boolean isExerciseTypeCorrect(Path path) {
117117
* The project root path must be specified for the {@link StudentFilePolicy}
118118
* to read any configuration files such as <tt>.tmcproject.yml</tt>.
119119
* </p>
120+
*
120121
* @param projectPath The project's root path
121122
*/
122123
@Override
@@ -126,9 +127,26 @@ protected StudentFilePolicy getStudentFilePolicy(Path projectPath) {
126127

127128
@Override
128129
public RunResult runTests(Path path) {
129-
Optional<RunResult> result = build(path);
130-
if (result.isPresent()) {
131-
return result.get();
130+
try {
131+
ProcessResult qmakeBuild = buildWithQmake(path);
132+
if (qmakeBuild.statusCode != 0) {
133+
log.error("Building project with qmake failed: {}", qmakeBuild.errorOutput);
134+
return filledFailure(qmakeBuild.errorOutput);
135+
}
136+
} catch (Exception e) {
137+
log.error("Building project with qmake failed", e);
138+
throw new RuntimeException(e);
139+
}
140+
141+
try {
142+
ProcessResult makeBuild = buildWithMake(path);
143+
if (makeBuild.statusCode != 0) {
144+
log.error("Building project with make failed: {}", makeBuild.errorOutput);
145+
return filledFailure(makeBuild.errorOutput);
146+
}
147+
} catch (Exception e) {
148+
log.error("Building project with make failed", e);
149+
throw new RuntimeException(e);
132150
}
133151

134152
Path testResults = path.resolve(TMC_TEST_RESULTS);
@@ -139,34 +157,21 @@ public RunResult runTests(Path path) {
139157

140158
log.info("Testing project with command {}", Arrays.toString(makeCommand));
141159

142-
Optional<ProcessResult> test = run(makeCommand, path);
143-
if (test.isPresent()) {
160+
try {
161+
ProcessResult testRun = run(makeCommand, path);
162+
144163
if (!Files.exists(testResults)) {
145164
log.error("Failed to get test output at {}", testResults);
146-
return filledFailure(test.get());
165+
return filledFailure(testRun.output);
147166
}
148-
QTestResultParser parser = new QTestResultParser();
149-
parser.loadTests(testResults);
150-
return parser.result();
151-
}
152-
153-
return EMPTY_FAILURE;
154-
}
155-
156-
private Optional<RunResult> build(Path path) {
157-
Optional<RunResult> error = buildWithQmake(path);
158-
if (error.isPresent()) {
159-
log.warn("Failed to compile project with qmake");
160-
return error;
167+
} catch (Exception e) {
168+
log.error("Testing with make check failed", e);
169+
throw new RuntimeException(e);
161170
}
162171

163-
error = buildWithMake(path);
164-
if (error.isPresent()) {
165-
log.warn("Failed to compile project with make");
166-
return error;
167-
}
168-
169-
return Optional.absent();
172+
QTestResultParser parser = new QTestResultParser();
173+
parser.loadTests(testResults);
174+
return parser.result();
170175
}
171176

172177
@Override
@@ -184,58 +189,42 @@ public Map<File, List<ValidationError>> getValidationErrors() {
184189
};
185190
}
186191

187-
private Optional<RunResult> buildWithQmake(Path dir) {
192+
private ProcessResult buildWithQmake(Path dir) throws Exception {
188193
String qmakeArguments = "CONFIG+=test";
189194
Path pro = getProFile(dir);
190195
String[] qmakeCommand = {"qmake", qmakeArguments, pro.toString()};
191196

192197
log.info("Building project with command {}", Arrays.deepToString(qmakeCommand));
193-
Optional<ProcessResult> result = run(qmakeCommand, dir);
194-
return checkBuildResult(result);
198+
return run(qmakeCommand, dir);
195199
}
196200

197-
private Optional<RunResult> buildWithMake(Path dir) {
201+
private ProcessResult buildWithMake(Path dir) throws Exception {
198202
String[] makeCommand = {"make"};
199203
log.info("Building project with command {}", Arrays.deepToString(makeCommand));
200-
Optional<ProcessResult> result = run(makeCommand, dir);
201-
return checkBuildResult(result);
202-
}
203-
204-
private Optional<RunResult> checkBuildResult(Optional<ProcessResult> error) {
205-
if (error.isPresent()) {
206-
if (error.get().statusCode == 0) {
207-
return Optional.absent();
208-
}
209-
return Optional.of(filledFailure(error.get()));
210-
}
211-
return Optional.of(EMPTY_FAILURE);
204+
return run(makeCommand, dir);
212205
}
213206

214207
@Override
215208
public void clean(Path path) {
216209
String[] command = {"make", "clean"};
217-
if (run(command, path).isPresent()) {
210+
try {
211+
ProcessResult result = run(command, path);
212+
if (result.statusCode != 0) {
213+
log.error("Cleaning project was not successful", result.errorOutput);
214+
}
218215
log.info("Cleaned project");
219-
} else {
220-
log.warn("Cleaning project was not successful");
216+
} catch (Exception e) {
217+
log.error("Cleaning project was not successful", e);
221218
}
222219
}
223220

224-
private Optional<ProcessResult> run(String[] command, Path dir) {
221+
private ProcessResult run(String[] command, Path dir) throws Exception {
225222
ProcessRunner runner = new ProcessRunner(command, dir);
226-
227-
try {
228-
return Optional.of(runner.call());
229-
} catch (IOException | InterruptedException e) {
230-
log.error("Running command {} failed {}", Arrays.deepToString(command), e);
231-
return Optional.absent();
232-
}
233-
234-
223+
return runner.call();
235224
}
236225

237-
private RunResult filledFailure(ProcessResult processResult) {
238-
byte[] errorOutput = processResult.errorOutput.getBytes(StandardCharsets.UTF_8);
226+
private RunResult filledFailure(String output) {
227+
byte[] errorOutput = output.getBytes(StandardCharsets.UTF_8);
239228
ImmutableMap<String, byte[]> logs
240229
= new ImmutableMap.Builder()
241230
.put(SpecialLogs.COMPILER_OUTPUT, errorOutput)

0 commit comments

Comments
 (0)