Skip to content

Commit

Permalink
add --debug flag to enable debug mode #577
Browse files Browse the repository at this point in the history
  • Loading branch information
Luro02 committed Jul 23, 2024
1 parent 084f319 commit d9c18c0
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import de.firemage.autograder.api.Translatable;
import de.firemage.autograder.api.loader.AutograderLoader;
import de.firemage.autograder.cmd.output.Annotation;
import de.firemage.autograder.core.integrated.CoreUtil;
import de.firemage.autograder.span.Formatter;
import de.firemage.autograder.span.Highlight;
import de.firemage.autograder.span.Position;
Expand Down Expand Up @@ -81,6 +82,9 @@ public class Application implements Callable<Integer> {
@Option(names = {"--max-problems"}, description = "The maximum number of problems to report per check", defaultValue = "10")
private int maxProblemsPerCheck;

@Option(names = {"--debug"}, description = "Enables debug mode, note that this slows down execution", defaultValue = "false")
private boolean isInDebugMode;

@Spec
private CommandSpec spec;

Expand Down Expand Up @@ -195,6 +199,10 @@ public Integer call() {
}
}

if (this.isInDebugMode) {
CoreUtil.setDebugMode();
}

if (!outputJson) {
System.out.println("Student source code directory is " + file);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,48 @@
package de.firemage.autograder.core.integrated;

import java.util.Arrays;
import java.util.Optional;

// TODO: rename?
/**
* Utility class for functionality that does not fit in any other utility class.
*/
public final class CoreUtil {
private static Optional<Boolean> AUTOGRADER_DEBUG_ENVIRONMENT = parseOptionalFlag(System.getenv("AUTOGRADER_DEBUG"));
private static final boolean IS_IN_JUNIT_TEST = Arrays.stream(Thread.currentThread().getStackTrace())
.anyMatch(element -> element.getClassName().startsWith("org.junit."));

private CoreUtil() {
}

public static boolean isInJunitTest() {
return IS_IN_JUNIT_TEST;
private static Optional<Boolean> parseOptionalFlag(String flag) {
if (flag == null) {
return Optional.empty();
}

try {
return Optional.of(Integer.parseInt(flag) != 0);
} catch (NumberFormatException exception) {
return Optional.of(Boolean.parseBoolean(flag));
}
}

/**
* Enables debug mode for the autograder.
* <br>
* Note that this slows down the execution by a lot and should therefore only be used for debugging purposes.
*/
public static void setDebugMode() {
AUTOGRADER_DEBUG_ENVIRONMENT = Optional.of(true);
}

/**
* Checks if the code is currently running in debug mode.
* <br>
* This is the case if the code is executed in a junit test or if the debug mode is explicitly enabled.
*
* @return {@code true} if the code is currently running in debug mode
*/
public static boolean isInDebugMode() {
return IS_IN_JUNIT_TEST || AUTOGRADER_DEBUG_ENVIRONMENT.orElse(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.util.stream.Collectors;

public class IntegratedAnalysis implements CodeLinter<IntegratedCheck> {
private static final boolean IS_IN_DEBUG_MODE = CoreUtil.isInJunitTest();
private static final boolean IS_IN_DEBUG_MODE = CoreUtil.isInDebugMode();
private static final String INITIAL_INTEGRITY_CHECK_NAME = "StaticAnalysis-Constructor";
private static final boolean ENSURE_NO_ORPHANS = false;
private static final boolean ENSURE_NO_MODEL_CHANGES = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public static boolean isSubtypeOf(CtType<?> potentialSubtype, CtType<?> parentTy
boolean result = TypeUtil.streamAllSuperTypes(potentialSubtype).anyMatch(type -> parentType == type);

// this is just a sanity check to ensure that our implementation is correct
if (CoreUtil.isInJunitTest() && result != potentialSubtype.isSubtypeOf(parentType.getReference())) {
if (CoreUtil.isInDebugMode() && result != potentialSubtype.isSubtypeOf(parentType.getReference())) {
throw new IllegalStateException("Inconsistent subtype information for %s and %s".formatted(
potentialSubtype.getQualifiedName(),
parentType.getQualifiedName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.util.function.UnaryOperator;

public final class StructuralEqualsVisitor extends EqualsVisitor {
private static final boolean IS_IN_DEBUG_MODE = CoreUtil.isInJunitTest();
private static final boolean IS_IN_DEBUG_MODE = CoreUtil.isInDebugMode();

private static final Set<CtRole> ALLOWED_MISMATCHING_ROLES = Set.of(
// allow mismatching comments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
ProblemType.COMPOSITION_OVER_INHERITANCE
})
public class InheritanceBadPractices extends IntegratedCheck {
private static final boolean IS_IN_DEBUG_MODE = CoreUtil.isInJunitTest();
private static final boolean IS_IN_DEBUG_MODE = CoreUtil.isInDebugMode();

@Override
protected void check(StaticAnalysis staticAnalysis) {
Expand Down

0 comments on commit d9c18c0

Please sign in to comment.