Skip to content

Support forcing color output #411

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 1 commit into from

Conversation

plemarquand
Copy link
Contributor

Add support for the FORCE_COLOR environment variable. It is possible to run in an environment that does not look like either a TTY or a named pipe and still want ANSI escape codes emitted. The NO_COLOR environment variable takes precedence.

Checklist:

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

Add support for the FORCE_COLOR environment variable. It is possible to
run in an environment that does not look like either a TTY or a named
pipe and still want ANSI escape codes emitted. The NO_COLOR environment
variable takes precedence.

- [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
Copy link
Contributor

grynspan commented May 9, 2024

Please update the markup for XCTestScaffold.runAllTests(hostedBy:_:) too?

@grynspan grynspan assigned grynspan and plemarquand and unassigned grynspan May 9, 2024
@grynspan grynspan added enhancement New feature or request swiftpm-integration 📦 Swift Package Manager integration tools integration 🛠️ Integration of swift-testing into tools/IDEs labels May 9, 2024
if result.useANSIEscapeCodes {

// Respect the FORCE_COLOR environment variable. SEE: https://www.force-color.org
if result.useANSIEscapeCodes || Environment.variable(named: "FORCE_COLOR")?.isEmpty == false {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the intended interaction between FORCE_COLOR and NO_COLOR if both are specified?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The NO_COLOR environment variable takes precedence.

Copy link
Contributor

@grynspan grynspan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate the intent here, but NO_COLOR is supported specifically for the benefit of SwiftPM which uses pipes rather than TTYs to pass data along to/from swift test. FORCE_COLOR is not widely supported and the interactions with NO_COLOR don't seem to be defined.

Rather than adding another environment variable check, this seems like something we could pass as a command-line argument instead, e.g. --color or --enable-color or some such.

@plemarquand
Copy link
Contributor Author

My thoughts are supporting only NO_COLOR is only half of the implementation of controlling terminal colours. Forcing it off when you do have a TTY is just as valid as forcing it on when you don't.

Adding another CLI flag would mean plumbing it through SwiftPM to the swift-testing binary. An environment variable lets anyone opt in all the way down the chain.

There is some debate around whether to use FORCE_COLOR or CLICOLOR_FORCE. No preference there.

@grynspan
Copy link
Contributor

grynspan commented May 9, 2024

Neither CLICOLOR_FORCE nor FORCE_COLOR is widely used. If you'd like to pursue a solution here, let's use a command-line argument. Thanks!

@plemarquand plemarquand closed this May 9, 2024
@plemarquand plemarquand deleted the force-color branch May 9, 2024 20:24
grynspan added a commit that referenced this pull request Jun 10, 2025
On
[FreeBSD](https://man.freebsd.org/cgi/man.cgi?query=posix_spawn_file_actions_adddup2),
[OpenBSD](https://github.com/openbsd/src/blob/master/lib/libc/gen/posix_spawn.c#L155),
[Android](https://android.googlesource.com/platform/bionic/+/master/libc/bionic/spawn.cpp#103),
and [Glibc ≥
2.29](https://sourceware.org/bugzilla/show_bug.cgi?id=23640),
`posix_spawn_file_actions_adddup2()` automatically clears `FD_CLOEXEC`
if the file descriptors passed to it are equal. Relying on this
behaviour eliminates a race condition when spawning child processes.
This functionality is standardized in
[POSIX.1-2024](https://pubs.opengroup.org/onlinepubs/9799919799/) thanks
to [Austin Group Defect
#411](https://www.austingroupbugs.net/view.php?id=411).

Some older Linuxes (Amazon Linux 2 in particular) don't have this
functionality, so we do a runtime check of the Glibc version.

This PR is a follow-up to #1145.
Resolves #1140.

### 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request swiftpm-integration 📦 Swift Package Manager integration tools integration 🛠️ Integration of swift-testing into tools/IDEs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants