Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,13 @@ private static SystemExitToggle getSystemExitToggle() {
System.err.println("Failed to load Java 17 system exit override: " + e.getMessage());
}

// Install a shutdown hook so people can track down what went wrong
// if a test calls `System.exit`
Thread shutdownHook = SystemExitDetectingShutdownHook.newShutdownHook(System.err);
Runtime.getRuntime().addShutdownHook(shutdownHook);

// Fall through
}
System.err.println(
"Unable to create a mechanism to prevent `System.exit` being called. "
+ "Tests may cause `bazel test` to exit prematurely.");

System.err.println(
"Unable to create a mechanism to prevent `System.exit` being called. "
+ "Tests may cause `bazel test` to exit prematurely.");

return new NullSystemExitToggle();
return new NullSystemExitToggle();
}
}

private static void detectJUnit5Classes() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
package com.github.bazel_contrib.contrib_rules_jvm.junit5;

import java.util.Optional;

public class NullSystemExitToggle implements SystemExitToggle {

private Optional<Thread> shutdownHook;

public NullSystemExitToggle() {
this.shutdownHook = Optional.empty();
}

@Override
public void prevent() {
// No-op
if (!shutdownHook.isEmpty()) {
throw new RuntimeException("SystemExitDetectingShutdownHook already added");
}
// Install a shutdown hook so people can track down what went wrong
// if a test calls `System.exit`
Thread shutdownHook = SystemExitDetectingShutdownHook.newShutdownHook(System.err);
this.shutdownHook = Optional.of(shutdownHook);
Runtime.getRuntime().addShutdownHook(shutdownHook);
}

@Override
public void allow() {
// No-op
if (shutdownHook.isEmpty()) {
throw new RuntimeException("SystemExitDetectingShutdownHook never added");
}
Runtime.getRuntime().removeShutdownHook(shutdownHook.get());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.github.bazel_contrib.contrib_rules_jvm.junit5;

import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

public class NullSystemExitToggleTest {

@Test
public void prevent_alreadyPrevented_throwsRuntimeException() {
// Arrange.
NullSystemExitToggle toggle = new NullSystemExitToggle();

// Act and Assert.
toggle.prevent();
assertThrows(RuntimeException.class, () -> toggle.prevent());

// Cleanup.
toggle.allow();
}

@Test
public void allow_alreadyAllowed_throwsRuntimeException() {
// Arrange.
NullSystemExitToggle toggle = new NullSystemExitToggle();

// Act and Assert.
assertThrows(RuntimeException.class, () -> toggle.allow());
}

@Test
public void normalFlow_success() {
// Arrange.
NullSystemExitToggle toggle = new NullSystemExitToggle();

// Act and (implicit) Assert.
try {
toggle.prevent();
} finally {
toggle.allow();
}
}
}
Loading