1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
|
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//
@testable @_spi(Experimental) @_spi(ForToolsIntegrationOnly) import Testing
private import _TestingInternals
#if !SWT_NO_EXIT_TESTS
@Suite("Exit test tests") struct ExitTestTests {
@Test("Exit tests (passing)") func passing() async {
await #expect(processExitsWith: .failure) {
exit(EXIT_FAILURE)
}
if EXIT_SUCCESS != EXIT_FAILURE + 1 {
await #expect(processExitsWith: .failure) {
exit(EXIT_FAILURE + 1)
}
}
await #expect(processExitsWith: .success) {}
await #expect(processExitsWith: .success) {
exit(EXIT_SUCCESS)
}
await #expect(processExitsWith: .exitCode(123)) {
exit(123)
}
await #expect(processExitsWith: .exitCode(123)) {
await Task.yield()
exit(123)
}
await #expect(processExitsWith: .signal(SIGSEGV)) {
_ = raise(SIGSEGV)
// Allow up to 1s for the signal to be delivered. On some platforms,
// raise() delivers signals fully asynchronously and may not terminate the
// child process before this closure returns.
if #available(_clockAPI, *) {
try await Test.Clock.sleep(for: .seconds(1))
} else {
try await Task.sleep(nanoseconds: 1_000_000_000)
}
}
await #expect(processExitsWith: .signal(SIGABRT)) {
abort()
}
#if !SWT_NO_UNSTRUCTURED_TASKS
#if false
// Test the detached (no task-local configuration) path. Disabled because,
// like other tests using Task.detached, it can interfere with other tests
// running concurrently.
#expect(Test.current != nil)
await Task.detached {
#expect(Test.current == nil)
await #expect(processExitsWith: .failure) {
fatalError()
}
}.value
#endif
#endif
}
@Test("Exit tests (failing)") func failing() async {
await confirmation("Exit tests failed", expectedCount: 9) { failed in
var configuration = Configuration()
configuration.eventHandler = { event, _ in
if case .issueRecorded = event.kind {
failed()
}
}
configuration.exitTestHandler = ExitTest.handlerForEntryPoint()
await runTest(for: FailingExitTests.self, configuration: configuration)
}
}
@Test("Mock exit test handlers (passing)") func passingMockHandler() async {
await confirmation("System issue recorded", expectedCount: 0) { issueRecorded in
var configuration = Configuration()
configuration.eventHandler = { event, _ in
if case .issueRecorded = event.kind {
issueRecorded()
}
}
// Mock an exit test where the process exits successfully.
configuration.exitTestHandler = { _ in
return ExitTest.Result(exitStatus: .exitCode(EXIT_SUCCESS))
}
await Test {
await #expect(processExitsWith: .success) {}
}.run(configuration: configuration)
// Mock an exit test where the process exits with a particular error code.
configuration.exitTestHandler = { _ in
return ExitTest.Result(exitStatus: .exitCode(123))
}
await Test {
await #expect(processExitsWith: .failure) {}
}.run(configuration: configuration)
// Mock an exit test where the process exits with a signal.
configuration.exitTestHandler = { _ in
return ExitTest.Result(exitStatus: .signal(SIGABRT))
}
await Test {
await #expect(processExitsWith: .signal(SIGABRT)) {}
}.run(configuration: configuration)
await Test {
await #expect(processExitsWith: .failure) {}
}.run(configuration: configuration)
}
}
@Test("Mock exit test handlers (failing)") func failingMockHandlers() async {
await confirmation("Issue recorded", expectedCount: 6) { issueRecorded in
var configuration = Configuration()
configuration.eventHandler = { event, _ in
if case .issueRecorded = event.kind {
issueRecorded()
}
}
// Mock exit tests that were expected to fail but passed.
configuration.exitTestHandler = { _ in
return ExitTest.Result(exitStatus: .exitCode(EXIT_SUCCESS))
}
await Test {
await #expect(processExitsWith: .failure) {}
}.run(configuration: configuration)
await Test {
await #expect(processExitsWith: .exitCode(EXIT_FAILURE)) {}
}.run(configuration: configuration)
await Test {
await #expect(processExitsWith: .signal(SIGABRT)) {}
}.run(configuration: configuration)
// Mock exit tests that unexpectedly signalled.
configuration.exitTestHandler = { _ in
return ExitTest.Result(exitStatus: .signal(SIGABRT))
}
await Test {
await #expect(processExitsWith: .exitCode(EXIT_SUCCESS)) {}
}.run(configuration: configuration)
await Test {
await #expect(processExitsWith: .exitCode(EXIT_FAILURE)) {}
}.run(configuration: configuration)
await Test {
await #expect(processExitsWith: .success) {}
}.run(configuration: configuration)
}
}
@Test("Exit test without configured exit test handler") func noHandler() async {
await confirmation("System issue recorded") { issueRecorded in
var configuration = Configuration()
configuration.eventHandler = { event, _ in
if case let .issueRecorded(issue) = event.kind, case .system = issue.kind {
issueRecorded()
}
}
await Test {
await #expect(processExitsWith: .success) {}
}.run(configuration: configuration)
}
}
@Test("Exit test forwards issues") func forwardsIssues() async {
await confirmation("Issue recorded") { issueRecorded in
await confirmation("Error caught") { errorCaught in
var configuration = Configuration()
configuration.eventHandler = { event, _ in
guard case let .issueRecorded(issue) = event.kind else {
return
}
if case .unconditional = issue.kind, issue.comments.contains("Something went wrong!") {
issueRecorded()
} else if issue.error != nil {
errorCaught()
}
}
configuration.exitTestHandler = ExitTest.handlerForEntryPoint()
await Test {
await #expect(processExitsWith: .success) {
#expect(Bool(false), "Something went wrong!")
exit(0)
}
await #expect(processExitsWith: .failure) {
Issue.record(MyError())
}
}.run(configuration: configuration)
}
}
}
#if !os(Linux)
@Test("Exit test reports > 8 bits of the exit code")
func fullWidthExitCode() async {
// On POSIX-like platforms, we use waitid() which per POSIX should report
// the full exit code, not just the low 8 bits. This behaviour is not
// well-documented and not all platforms (as of this writing) report the
// full value:
//
// | Platform | Bits Reported |
// |----------------------|----------------|
// | Darwin | 32 |
// | Linux | 8 |
// | Windows | 32 (see below) |
// | FreeBSD | 32 |
//
// Other platforms may also have issues reporting the full value. This test
// serves as a canary when adding new platforms that we need to document the
// difference.
//
// Windows does not have the 8-bit exit code restriction and always reports
// the full CInt value back to the testing library.
await #expect(processExitsWith: .exitCode(512)) {
exit(512)
}
}
#endif
@MainActor static func someMainActorFunction() {
MainActor.assertIsolated()
}
@Test("Exit test can be main-actor-isolated")
@MainActor
func mainActorIsolation() async {
await #expect(processExitsWith: .success) {
await Self.someMainActorFunction()
_ = 0
exit(EXIT_SUCCESS)
}
}
@Test("Result is set correctly on success")
func successfulArtifacts() async throws {
// Test that basic passing exit tests produce the correct results (#expect)
var result = await #expect(processExitsWith: .success) {
exit(EXIT_SUCCESS)
}
#expect(result?.exitStatus == .exitCode(EXIT_SUCCESS))
result = await #expect(processExitsWith: .exitCode(123)) {
exit(123)
}
#expect(result?.exitStatus == .exitCode(123))
// Test that basic passing exit tests produce the correct results (#require)
result = try await #require(processExitsWith: .success) {
exit(EXIT_SUCCESS)
}
#expect(result?.exitStatus == .exitCode(EXIT_SUCCESS))
result = try await #require(processExitsWith: .exitCode(123)) {
exit(123)
}
#expect(result?.exitStatus == .exitCode(123))
}
@Test("Result is nil on failure")
func nilArtifactsOnFailure() async {
// Test that an exit test that produces the wrong exit condition reports it
// as an expectation failure, but also returns the exit condition (#expect)
await confirmation("Expectation failed") { expectationFailed in
var configuration = Configuration()
configuration.eventHandler = { event, _ in
if case let .issueRecorded(issue) = event.kind {
if case .expectationFailed = issue.kind {
expectationFailed()
} else {
issue.record()
}
}
}
configuration.exitTestHandler = { _ in
ExitTest.Result(exitStatus: .exitCode(123))
}
await Test {
let result = await #expect(processExitsWith: .success) {}
#expect(result == nil)
}.run(configuration: configuration)
}
// Test that an exit test that produces the wrong exit condition throws an
// ExpectationFailedError (#require)
await confirmation("Expectation failed") { expectationFailed in
var configuration = Configuration()
configuration.eventHandler = { event, _ in
if case let .issueRecorded(issue) = event.kind {
if case .expectationFailed = issue.kind {
expectationFailed()
} else {
issue.record()
}
}
}
configuration.exitTestHandler = { _ in
ExitTest.Result(exitStatus: .exitCode(EXIT_FAILURE))
}
await Test {
try await #require(processExitsWith: .success) {}
Issue.record("#require(processExitsWith:) should have thrown an error")
}.run(configuration: configuration)
}
}
@Test("Result is nil on system failure")
func nilArtifactsOnSystemFailure() async {
// Test that an exit test that fails to start due to a system error produces
// a .system issue and reports .failure as its exit condition.
await confirmation("System issue recorded") { systemIssueRecorded in
await confirmation("Expectation failed") { expectationFailed in
var configuration = Configuration()
configuration.eventHandler = { event, _ in
if case let .issueRecorded(issue) = event.kind {
if case .system = issue.kind {
systemIssueRecorded()
} else if case .expectationFailed = issue.kind {
expectationFailed()
} else {
issue.record()
}
}
}
configuration.exitTestHandler = { _ in
throw MyError()
}
await Test {
let result = await #expect(processExitsWith: .success) {}
#expect(result == nil)
}.run(configuration: configuration)
}
}
}
@Test("Result contains stdout/stderr")
func exitTestResultContainsStandardStreams() async throws {
var result = try await #require(processExitsWith: .success, observing: [\.standardOutputContent]) {
try FileHandle.stdout.write("STANDARD OUTPUT")
try FileHandle.stderr.write(String("STANDARD ERROR".reversed()))
exit(EXIT_SUCCESS)
}
#expect(result.exitStatus == .exitCode(EXIT_SUCCESS))
#expect(result.standardOutputContent.contains("STANDARD OUTPUT".utf8))
#expect(!result.standardOutputContent.contains(ExitTest.barrierValue))
#expect(result.standardErrorContent.isEmpty)
result = try await #require(processExitsWith: .success, observing: [\.standardErrorContent]) {
try FileHandle.stdout.write("STANDARD OUTPUT")
try FileHandle.stderr.write(String("STANDARD ERROR".reversed()))
exit(EXIT_SUCCESS)
}
#expect(result.exitStatus == .exitCode(EXIT_SUCCESS))
#expect(result.standardOutputContent.isEmpty)
#expect(result.standardErrorContent.contains("STANDARD ERROR".utf8.reversed()))
#expect(!result.standardErrorContent.contains(ExitTest.barrierValue))
}
@Test("Arguments to the macro are not captured during expansion (do not need to be literals/const)")
func argumentsAreNotCapturedDuringMacroExpansion() async throws {
let unrelatedSourceLocation = #_sourceLocation
func nonConstExitCondition() async throws -> ExitTest.Condition {
.failure
}
await #expect(processExitsWith: try await nonConstExitCondition(), sourceLocation: unrelatedSourceLocation) {
fatalError()
}
}
@Test("ExitTest.current property")
func currentProperty() async {
#expect((ExitTest.current == nil) as Bool)
await #expect(processExitsWith: .success) {
#expect((ExitTest.current != nil) as Bool)
}
}
#if ExperimentalExitTestValueCapture
@Test("Capture list")
func captureList() async {
let i = 123
let s = "abc" as Any
await #expect(processExitsWith: .success) { [i = i as Int, s = s as! String, t = (s as Any) as? String?] in
#expect(i == 123)
#expect(s == "abc")
#expect(t == "abc")
}
}
@Test("Capture list (very long encoded form)")
func longCaptureList() async {
let count = 1 * 1024 * 1024
let buffer = Array(repeatElement(0 as UInt8, count: count))
await #expect(processExitsWith: .success) { [count = count as Int, buffer = buffer as [UInt8]] in
#expect(buffer.count == count)
}
}
struct CapturableSuite: Codable {
var property = 456
@Test("self in capture list")
func captureListWithSelf() async {
await #expect(processExitsWith: .success) { [self, x = self, y = self as Self] in
#expect(self.property == 456)
#expect(x.property == 456)
#expect(y.property == 456)
}
}
}
class CapturableBaseClass: @unchecked Sendable, Codable {
init() {}
required init(from decoder: any Decoder) throws {}
func encode(to encoder: any Encoder) throws {}
}
final class CapturableDerivedClass: CapturableBaseClass, @unchecked Sendable {
let x: Int
init(x: Int) {
self.x = x
super.init()
}
required init(from decoder: any Decoder) throws {
let container = try decoder.singleValueContainer()
self.x = try container.decode(Int.self)
super.init()
}
override func encode(to encoder: any Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(x)
}
}
@Test("Capturing an instance of a subclass")
func captureSubclass() async {
let instance = CapturableDerivedClass(x: 123)
await #expect(processExitsWith: .success) { [instance = instance as CapturableBaseClass] in
#expect((instance as AnyObject) is CapturableBaseClass)
// However, because the static type of `instance` is not Derived, we won't
// be able to cast it to Derived.
#expect(!((instance as AnyObject) is CapturableDerivedClass))
}
await #expect(processExitsWith: .success) { [instance = instance as CapturableDerivedClass] in
#expect((instance as AnyObject) is CapturableBaseClass)
#expect((instance as AnyObject) is CapturableDerivedClass)
#expect(instance.x == 123)
}
}
@Test("Capturing a parameter to the test function")
func captureListWithParameter() async {
let i = Int.random(in: 0 ..< 1000)
func f(j: Int) async {
await #expect(processExitsWith: .success) { [i = i as Int, j] in
#expect(i == j)
#expect(j >= 0)
#expect(j < 1000)
}
}
await f(j: i)
await { (j: Int) in
_ = await #expect(processExitsWith: .success) { [i = i as Int, j] in
#expect(i == j)
#expect(j >= 0)
#expect(j < 1000)
}
}(i)
#if false // intentionally fails to compile
// FAILS TO COMPILE: shadowing `i` with a variable of a different type will
// prevent correct expansion (we need an equivalent of decltype() for that.)
func g(i: Int) async {
let i = String(i)
await #expect(processExitsWith: .success) { [i] in
#expect(!i.isEmpty)
}
}
#endif
}
@Test("Capturing a literal expression")
func captureListWithLiterals() async {
await #expect(processExitsWith: .success) { [i = 0, f = 1.0, s = "", b = true] in
#expect(i == 0)
#expect(f == 1.0)
#expect(s == "")
#expect(b == true)
}
}
@Test("Capturing #_sourceLocation")
func captureListPreservesSourceLocationMacro() async {
func sl(_ sl: SourceLocation = #_sourceLocation) -> SourceLocation {
sl
}
await #expect(processExitsWith: .success) { [sl = sl() as SourceLocation] in
#expect(sl.fileID == #fileID)
}
}
@Test("Capturing an optional value")
func captureListWithOptionalValue() async throws {
await #expect(processExitsWith: .success) { [x = nil as Int?] in
#expect(x != 1)
}
await #expect(processExitsWith: .success) { [x = (0 as Any) as? String] in
#expect(x == nil)
}
}
@Test("Capturing an effectful expression")
func captureListWithEffectfulExpression() async throws {
func f() async throws -> Int { 0 }
try await #require(processExitsWith: .success) { [f = try await f() as Int] in
#expect(f == 0)
}
try await #expect(processExitsWith: .success) { [f = f() as Int] in
#expect(f == 0)
}
}
#if false // intentionally fails to compile
@Test("Capturing a tuple")
func captureListWithTuple() async throws {
// A tuple whose elements conform to Codable does not itself conform to
// Codable, so we cannot actually express this capture list in a way that
// works with #expect().
await #expect(processExitsWith: .success) { [x = (0 as Int, 1 as Double, "2" as String)] in
#expect(x.0 == 0)
#expect(x.1 == 1)
#expect(x.2 == "2")
}
}
#endif
#if false // intentionally fails to compile
struct NonCodableValue {}
// We can't capture a value that isn't Codable. A unit test is not possible
// for this case as the type checker needs to get involved.
@Test("Capturing a move-only value")
func captureListWithMoveOnlyValue() async {
let x = NonCodableValue()
await #expect(processExitsWith: .success) { [x = x as NonCodableValue] in
_ = x
}
}
#endif
#endif
}
// MARK: - Fixtures
@Suite(.hidden) struct FailingExitTests {
@Test(.hidden) func failingExitTests() async {
await #expect(processExitsWith: .failure) {}
await #expect(processExitsWith: .exitCode(123)) {}
await #expect(processExitsWith: .failure) {
exit(EXIT_SUCCESS)
}
await #expect(processExitsWith: .success) {
exit(EXIT_FAILURE)
}
await #expect(processExitsWith: .exitCode(123)) {
exit(0)
}
await #expect(processExitsWith: .exitCode(SIGABRT)) {
// abort() raises on Windows, but we don't handle that yet and it is
// reported as .failure (which will fuzzy-match with SIGABRT.)
abort()
}
await #expect(processExitsWith: .signal(123)) {}
await #expect(processExitsWith: .signal(123)) {
exit(123)
}
await #expect(processExitsWith: .signal(SIGSEGV)) {
abort() // sends SIGABRT, not SIGSEGV
}
}
}
#if false // intentionally fails to compile
@Test(.hidden, arguments: 100 ..< 200)
func sellIceCreamCones(count: Int) async throws {
try await #require(processExitsWith: .failure) {
precondition(count < 10, "Too many ice cream cones")
}
}
#endif
#endif
|