[StdlibUnittest] Install our own handler for uncaught ObjC exceptions.

We don't want to be at the whims of the system on what to do with an
uncaught exception; we need to make sure its message gets printed to
stderr so that the parent process can check it.

(There's a bit of trickery here to see if the class looks like an
NSException; otherwise we lose the name of the exception and just get
the reason.)
This commit is contained in:
Jordan Rose
2016-03-18 11:22:25 -07:00
parent e2c43cfe14
commit e6a519fbda
3 changed files with 85 additions and 1 deletions

View File

@@ -3,7 +3,8 @@ set(swift_stdlib_unittest_module_depends
SwiftPrivate SwiftPrivatePthreadExtras SwiftPrivateLibcExtras) SwiftPrivate SwiftPrivatePthreadExtras SwiftPrivateLibcExtras)
set(swift_stdlib_unittest_framework_depends) set(swift_stdlib_unittest_framework_depends)
set(swift_stdlib_unittest_private_link_libraries) set(swift_stdlib_unittest_private_link_libraries)
set(swift_stdlib_unittest_compile_flags) set(swift_stdlib_unittest_compile_flags
"-Xfrontend" "-disable-objc-attr-requires-foundation-module")
if(SWIFT_HOST_VARIANT MATCHES "${SWIFT_DARWIN_VARIANTS}") if(SWIFT_HOST_VARIANT MATCHES "${SWIFT_DARWIN_VARIANTS}")
list(APPEND swift_stdlib_unittest_platform_sources list(APPEND swift_stdlib_unittest_platform_sources

View File

@@ -448,8 +448,24 @@ let _crashedPrefix = "CRASHED:"
@_silgen_name("swift_stdlib_installTrapInterceptor") @_silgen_name("swift_stdlib_installTrapInterceptor")
func _stdlib_installTrapInterceptor() func _stdlib_installTrapInterceptor()
@objc protocol _StdlibUnittestNSException {
optional var name: String { get }
}
func _childProcess() { func _childProcess() {
_stdlib_installTrapInterceptor() _stdlib_installTrapInterceptor()
objc_setUncaughtExceptionHandler {
var stderr = _Stderr()
let maybeNSException = unsafeBitCast($0, to:_StdlibUnittestNSException.self)
if let name = maybeNSException.name {
print("*** [StdlibUnittest] Terminating due to uncaught exception " +
"\(name): \($0)",
to: &stderr)
} else {
print("*** [StdlibUnittest] Terminating due to uncaught exception: \($0)",
to: &stderr)
}
}
while let line = _stdlib_getline() { while let line = _stdlib_getline() {
let parts = line._split(separator: ";") let parts = line._split(separator: ";")
let testSuiteName = parts[0] let testSuiteName = parts[0]

View File

@@ -0,0 +1,67 @@
// RUN: %target-run-simple-swift 2>&1 | FileCheck %s
// REQUIRES: executable_test
// REQUIRES: objc_interop
import StdlibUnittest
import ObjectiveC
import Foundation
// Don't actually exit with a non-zero status, just say we're going to do it.
_setTestSuiteFailedCallback() { print("abort()") }
func raiseNSException() {
NSException(name: "Trogdor", reason: "Burnination", userInfo: nil).raise()
}
var TestSuiteCrashes = TestSuite("NSExceptionCrashes")
TestSuiteCrashes.test("uncaught") {
print("uncaught")
raiseNSException()
}
// CHECK-LABEL: stdout>>> uncaught
// CHECK: stderr>>> *** [StdlibUnittest] Terminating due to uncaught exception Trogdor: Burnination
// CHECK: stderr>>> CRASHED: SIG
// CHECK: the test crashed unexpectedly
// CHECK: [ FAIL ] NSExceptionCrashes.uncaught
TestSuiteCrashes.test("crashesAsExpected") {
print("crashesAsExpected")
expectCrashLater()
raiseNSException()
}
// CHECK-LABEL: stdout>>> crashesAsExpected
// CHECK: stderr>>> *** [StdlibUnittest] Terminating due to uncaught exception Trogdor: Burnination
// CHECK: stderr>>> OK: saw expected "crashed: sig
// CHECK: [ OK ] NSExceptionCrashes.crashesAsExpected
TestSuiteCrashes.test("crashesWithMessage")
.crashOutputMatches("libUnittest]")
.crashOutputMatches("Trogdor")
.crashOutputMatches("Burnination").code {
print("crashesWithMessage")
expectCrashLater()
raiseNSException()
}
// CHECK-LABEL: stdout>>> crashesWithMessage
// CHECK: stderr>>> *** [StdlibUnittest] Terminating due to uncaught exception Trogdor: Burnination
// CHECK: stderr>>> OK: saw expected "crashed: sig
// CHECK: [ OK ] NSExceptionCrashes.crashesWithMessage
TestSuiteCrashes.test("nonNSException")
.crashOutputMatches("countryside").code {
print("nonNSException")
expectCrashLater()
objc_exception_throw("countryside")
}
// CHECK-LABEL: stdout>>> nonNSException
// CHECK: stderr>>> *** [StdlibUnittest] Terminating due to uncaught exception: countryside
// CHECK: stderr>>> OK: saw expected "crashed: sig
// CHECK: [ OK ] NSExceptionCrashes.nonNSException
// CHECK: NSExceptionCrashes: Some tests failed, aborting
// CHECK: abort()
runAllTests()