|
| 1 | +// Copyright 2024 The Bazel Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License.package com.google.testing.junit.runner.internal; |
| 14 | + |
| 15 | +// This code has been lifted from: |
| 16 | +// https://github.com/bazelbuild/bazel/blob/a74b12a652ba9b28a078e4270bc144973e26b2a1/src/java_tools/junitrunner/java/com/google/testing/junit/runner/internal/SystemExitDetectingShutdownHook.java#L27 |
| 17 | + |
| 18 | +package com.github.bazel_contrib.contrib_rules_jvm.junit5; |
| 19 | + |
| 20 | +import java.io.PrintStream; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +public class SystemExitDetectingShutdownHook { |
| 25 | + |
| 26 | + private SystemExitDetectingShutdownHook() {} |
| 27 | + |
| 28 | + public static Thread newShutdownHook(PrintStream testRunnerOut) { |
| 29 | + Runnable hook = |
| 30 | + () -> { |
| 31 | + boolean foundRuntimeExit = false; |
| 32 | + for (StackTraceElement[] stack : Thread.getAllStackTraces().values()) { |
| 33 | + List<String> framesStartingWithRuntimeExit = new ArrayList<>(); |
| 34 | + boolean foundRuntimeExitInThisThread = false; |
| 35 | + for (StackTraceElement frame : stack) { |
| 36 | + if (!foundRuntimeExitInThisThread |
| 37 | + && frame.getClassName().equals("java.lang.Runtime") |
| 38 | + && frame.getMethodName().equals("exit")) { |
| 39 | + foundRuntimeExitInThisThread = true; |
| 40 | + } |
| 41 | + if (foundRuntimeExitInThisThread) { |
| 42 | + framesStartingWithRuntimeExit.add(frameString(frame)); |
| 43 | + } |
| 44 | + } |
| 45 | + if (foundRuntimeExitInThisThread) { |
| 46 | + foundRuntimeExit = true; |
| 47 | + testRunnerOut.println("\nSystem.exit or Runtime.exit was called!"); |
| 48 | + testRunnerOut.println(String.join("\n", framesStartingWithRuntimeExit)); |
| 49 | + } |
| 50 | + } |
| 51 | + if (foundRuntimeExit) { |
| 52 | + // We must call halt rather than exit, because exit would lead to a deadlock. We use a |
| 53 | + // hopefully unique exit code to make it easier to identify this case. |
| 54 | + Runtime.getRuntime().halt(121); |
| 55 | + } |
| 56 | + }; |
| 57 | + return new Thread(hook, "SystemExitDetectingShutdownHook"); |
| 58 | + } |
| 59 | + |
| 60 | + private static String frameString(StackTraceElement frame) { |
| 61 | + return String.format( |
| 62 | + " at %s.%s(%s:%d)", |
| 63 | + frame.getClassName(), frame.getMethodName(), frame.getFileName(), frame.getLineNumber()); |
| 64 | + } |
| 65 | +} |
0 commit comments