Skip to content

Allow throwing an error from an exit test's body. #614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

grynspan
Copy link
Contributor

@grynspan grynspan commented Aug 15, 2024

This PR amends the signatures of the exit test macros (#expect(exitsWith:) {} and try #require(exitsWith:) {}) to allow bodies to throw errors. If they do, they are treated as uncaught errors and the child process terminates abnormally (in the same way it does if an error is thrown from the main function of a Swift program.)

Checklist:

  • Code and documentation should follow the style of the Style Guide.
  • If public symbols are renamed or modified, DocC references should be updated.

This PR amends the signatures of the exit test macros (`#expect(exitsWith:) {}`
and `try #require(exitsWith:) {}`) to allow bodies to throw errors. If they do,
they are treated as uncaught errors and the child process terminates abnormally
(in the same way it does if an error is thrown from the main function of a Swift
program.)
@grynspan grynspan added bug 🪲 Something isn't working public-api Affects public API swift-6.1 labels Aug 15, 2024
@grynspan grynspan self-assigned this Aug 15, 2024
@grynspan
Copy link
Contributor Author

@swift-ci please test

@grynspan grynspan added the exit-tests ☠️ Work related to exit tests label Aug 15, 2024
@grynspan
Copy link
Contributor Author

@swift-ci please test

grynspan added a commit that referenced this pull request Aug 15, 2024
This PR supersedes #603, #613, and #614. Exit tests remain an experimental feature.

 ## Clarify that 8-bit exit codes aren't a problem on macOS and Windows. (#603)

The documentation for the experimental exit tests feature currently says that on POSIX-like systems, only the low 8 bits of a process' exit code are preserved. This would be true if we used `wait()`, `wait4()`, etc. and `WEXITSTATUS()`, but we use `waitid()` instead which is [supposed to](https://pubs.opengroup.org/onlinepubs/9699919799/functions/exit.html) preserve the full exit code. It does so on Darwin, but not on Linux; Windows doesn't use `waitid()` but does report the full exit code.

Now, we're not currently building for any other POSIX-like systems that support processes (WASI/Wasm doesn't count here), so I've left in some weasel words and added a canary unit test. It will let us know if/when we add a platform that where `waitid()` doesn't preserve all the bits of the exit code, and we can amend the documentation in that case.

 ## Implement an equality operator for ExitCondition. (#613)

This PR implements `==` and `===` for `ExitCondition`, part of the experimental exit tests feature. These operators are necessary in order to allow for exit tests to support more complex matching by trailing closure (e.g. to support inspecting `stdout`.) Because `.failure` is a fuzzy case, `==` fuzzy-matches while `===` exactly matches. `Hashable` conformance is unavailable.

Example usage:

```swift
let lhs: ExitCondition = .failure
let rhs: ExitCondition = .signal(SIGTERM)
print(lhs == rhs) // prints "true"
print(lhs === rhs) // prints "false"
```

 ## Allow throwing an error from an exit test's body. (#614)

This PR amends the signatures of the exit test macros (`#expect(exitsWith:) {}` and `try #require(exitsWith:) {}`) to allow bodies to throw errors. If they do, they are treated as uncaught errors and the child process terminates abnormally (in the same way it does if an error is thrown from the main function of a Swift program.)
@grynspan grynspan mentioned this pull request Aug 15, 2024
2 tasks
@grynspan
Copy link
Contributor Author

Superseded by #615.

@grynspan grynspan closed this Aug 15, 2024
grynspan added a commit that referenced this pull request Aug 15, 2024
This PR supersedes #603, #613, and #614. Exit tests remain an experimental feature.

 ## Clarify that 8-bit exit codes aren't a problem on macOS and Windows. (#603)

The documentation for the experimental exit tests feature currently says that on POSIX-like systems, only the low 8 bits of a process' exit code are preserved. This would be true if we used `wait()`, `wait4()`, etc. and `WEXITSTATUS()`, but we use `waitid()` instead which is [supposed to](https://pubs.opengroup.org/onlinepubs/9699919799/functions/exit.html) preserve the full exit code. It does so on Darwin, but not on Linux; Windows doesn't use `waitid()` but does report the full exit code.

Now, we're not currently building for any other POSIX-like systems that support processes (WASI/Wasm doesn't count here), so I've left in some weasel words and added a canary unit test. It will let us know if/when we add a platform that where `waitid()` doesn't preserve all the bits of the exit code, and we can amend the documentation in that case.

 ## Implement an equality operator for ExitCondition. (#613)

This PR implements `==` and `===` for `ExitCondition`, part of the experimental exit tests feature. These operators are necessary in order to allow for exit tests to support more complex matching by trailing closure (e.g. to support inspecting `stdout`.) Because `.failure` is a fuzzy case, `==` fuzzy-matches while `===` exactly matches. `Hashable` conformance is unavailable.

Example usage:

```swift
let lhs: ExitCondition = .failure
let rhs: ExitCondition = .signal(SIGTERM)
print(lhs == rhs) // prints "true"
print(lhs === rhs) // prints "false"
```

 ## Allow throwing an error from an exit test's body. (#614)

This PR amends the signatures of the exit test macros (`#expect(exitsWith:) {}` and `try #require(exitsWith:) {}`) to allow bodies to throw errors. If they do, they are treated as uncaught errors and the child process terminates abnormally (in the same way it does if an error is thrown from the main function of a Swift program.)
grynspan added a commit that referenced this pull request Aug 21, 2024
This PR supersedes #603, #613, and #614. Exit tests remain an
experimental feature.

## Clarify that 8-bit exit codes aren't a problem on macOS and Windows.
(#603)

The documentation for the experimental exit tests feature currently says
that on POSIX-like systems, only the low 8 bits of a process' exit code
are preserved. This would be true if we used `wait()`, `wait4()`, etc.
and `WEXITSTATUS()`, but we use `waitid()` instead which is [supposed
to](https://pubs.opengroup.org/onlinepubs/9699919799/functions/exit.html)
preserve the full exit code. It does so on Darwin, but not on Linux;
Windows doesn't use `waitid()` but does report the full exit code.

Now, we're not currently building for any other POSIX-like systems that
support processes (WASI/Wasm doesn't count here), so I've left in some
weasel words and added a canary unit test. It will let us know if/when
we add a platform that where `waitid()` doesn't preserve all the bits of
the exit code, and we can amend the documentation in that case.

 ## Implement an equality operator for ExitCondition. (#613)

This PR implements `==` and `===` for `ExitCondition`, part of the
experimental exit tests feature. These operators are necessary in order
to allow for exit tests to support more complex matching by trailing
closure (e.g. to support inspecting `stdout`.) Because `.failure` is a
fuzzy case, `==` fuzzy-matches while `===` exactly matches. `Hashable`
conformance is unavailable.

Example usage:

```swift
let lhs: ExitCondition = .failure
let rhs: ExitCondition = .signal(SIGTERM)
print(lhs == rhs) // prints "true"
print(lhs === rhs) // prints "false"
```

 ## Allow throwing an error from an exit test's body. (#614)

This PR amends the signatures of the exit test macros
(`#expect(exitsWith:) {}` and `try #require(exitsWith:) {}`) to allow
bodies to throw errors. If they do, they are treated as uncaught errors
and the child process terminates abnormally (in the same way it does if
an error is thrown from the main function of a Swift program.)

### Checklist:

- [x] Code and documentation should follow the style of the [Style
Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md).
- [x] If public symbols are renamed or modified, DocC references should
be updated.
@grynspan grynspan added this to the Swift 6.1 milestone Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🪲 Something isn't working exit-tests ☠️ Work related to exit tests public-api Affects public API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant