Skip to content

Commit e761bd7

Browse files
authored
Check for exit calls in StageTest
1 parent c198e3c commit e761bd7

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/main/java/org/hyperskill/hstest/stage/StageTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import lombok.Getter;
44
import org.hyperskill.hstest.checker.CheckLibraryVersion;
5+
import org.hyperskill.hstest.common.ExitCallDetector;
56
import org.hyperskill.hstest.common.FileUtils;
67
import org.hyperskill.hstest.common.ReflectionUtils;
78
import org.hyperskill.hstest.dynamic.ClassSearcher;
@@ -22,7 +23,9 @@
2223
import org.junit.runner.JUnitCore;
2324
import org.junit.runner.Result;
2425

26+
import java.io.IOException;
2527
import java.lang.reflect.Modifier;
28+
import java.nio.file.Path;
2629
import java.util.ArrayList;
2730
import java.util.List;
2831
import java.util.stream.Collectors;
@@ -137,6 +140,28 @@ private void printTestNum(int num) {
137140
OutputHandler.print(RED_BOLD + "\nStart test " + num + totalTests + RESET);
138141
}
139142

143+
/**
144+
* Checks user code for forbidden exit calls before running tests
145+
*/
146+
private void checkForExitCalls() {
147+
// Only check Java files (other languages handled differently in Docker)
148+
if (!hasJavaSolution(FileUtils.cwd())) {
149+
return;
150+
}
151+
152+
try {
153+
Path currentDir = FileUtils.cwd().toPath();
154+
ExitCallDetector.DetectionResult result = ExitCallDetector.analyzeDirectory(currentDir);
155+
156+
if (result.hasExitCalls()) {
157+
throw new WrongAnswer(result.getFormattedMessage());
158+
}
159+
} catch (IOException e) {
160+
// If we can't read files, just continue (fail safely)
161+
// The SecurityManager will catch it at runtime if needed
162+
}
163+
}
164+
140165
@Test
141166
public final void start() {
142167
int currTest = 0;
@@ -149,6 +174,9 @@ public final void start() {
149174
ReflectionUtils.setupCwd(this);
150175
}
151176

177+
// Check for exit calls before running any tests
178+
checkForExitCalls();
179+
152180
List<TestRun> testRuns = initTests();
153181

154182
for (TestRun testRun : testRuns) {

0 commit comments

Comments
 (0)