Skip to content
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

doctest outputs ANSI escapes to non-color TTYs, resulting in visible junk #832

Open
snej opened this issue Nov 28, 2023 · 1 comment
Open

Comments

@snej
Copy link

snej commented Nov 28, 2023

Description

doctest’s check for whether to enable color output is too simplistic: it just tests isatty(STDOUT_FILENO) (e.g. in the color_to_stream function.) But not all TTYs support ANSI color; one common exception is Xcode’s console pane.

This causes junk to appear in the output for just about anyone developing native Mac or iOS apps unless they disable color on the command line.

Steps to reproduce

Run doctest tests in an Xcode project, with output going to the console pane as usual.

  • Expected: Plain output
  • Actual: Lots of “[0;36m and the like in the console output

Extra information

On Unix systems you can test the TERM environment variable, but that doesn’t work on Windows. Here’s a reliable function that I’ve used in other projects:

static bool isColor(int fd) {
    if (!isatty(fd))
        return false;

    if (const char *term = getenv("TERM")) {
        if (strstr(term,"ANSI") || strstr(term,"ansi") || strstr(term,"color"))
            return true;
    }

#ifdef _MSC_VER
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING  0x0004
#endif
    if (GetRealOSVersion().dwMajorVersion >= 10) {
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        DWORD consoleMode;
        if(GetConsoleMode(hConsole, &consoleMode)) {
            SetConsoleMode(hConsole, consoleMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
        }
        return true;
    }
#endif

    return false;
}
@onqtam
Copy link
Member

onqtam commented Nov 29, 2023

Thanks! And for now you could use DOCTEST_CONFIG_COLORS_NONE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants