Skip to content

Fixes to exit tests. #615

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

Merged
merged 4 commits into from
Aug 21, 2024
Merged

Fixes to exit tests. #615

merged 4 commits into from
Aug 21, 2024

Conversation

grynspan
Copy link
Contributor

@grynspan grynspan commented 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 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:

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:

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

@grynspan grynspan added bug 🪲 Something isn't working documentation 📚 Improvements or additions to documentation enhancement New feature or request linux 🐧 Linux support (all distros) darwin 🍎 macOS, iOS, watchOS, tvOS, and visionOS support public-api Affects public API swift-6.1 exit-tests ☠️ Work related to exit tests labels Aug 15, 2024
@grynspan grynspan self-assigned this Aug 15, 2024
@grynspan
Copy link
Contributor Author

@swift-ci please test

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 force-pushed the jgrynspan/exit-tests-2024-08-15 branch from 391de4a to e48d355 Compare August 15, 2024 23:15
@grynspan
Copy link
Contributor Author

@swift-ci please test

@grynspan
Copy link
Contributor Author

@swift-ci please test Windows

@grynspan
Copy link
Contributor Author

@swift-ci please test

@grynspan
Copy link
Contributor Author

@swift-ci please test

@grynspan
Copy link
Contributor Author

@swift-ci please test

@grynspan grynspan changed the base branch from main to main-next August 21, 2024 00:15
@grynspan grynspan merged commit a46df3f into main-next Aug 21, 2024
3 checks passed
@grynspan grynspan deleted the jgrynspan/exit-tests-2024-08-15 branch August 21, 2024 00:16
@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 darwin 🍎 macOS, iOS, watchOS, tvOS, and visionOS support documentation 📚 Improvements or additions to documentation enhancement New feature or request exit-tests ☠️ Work related to exit tests linux 🐧 Linux support (all distros) public-api Affects public API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants