@pytest.mark.parametrize('tty', [False, True])
def test_disable_colors_piped(tty):
    """Verify colors enabled by default when piped to TTY and disabled when not.

    :param bool tty: Pipe to TTY/terminal?
    """
    assert_statement = 'assert __import__("colorclass").codes.ANSICodeMapping.DISABLE_COLORS is {bool}'
    command_colors_enabled = [sys.executable, '-c', assert_statement.format(bool='False')]
    command_colors_disabled = [sys.executable, '-c', assert_statement.format(bool='True')]

    # Run piped to this pytest process.
    if not tty:  # Outputs piped to non-terminal/non-tty. Colors disabled by default.
        proc = subprocess.Popen(command_colors_disabled, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
        output = proc.communicate()
        assert not output[0]
        assert not output[1]
        assert proc.poll() == 0
        return

    # Run through a new console window (Windows).
    if IS_WINDOWS:
        c_flags = subprocess.CREATE_NEW_CONSOLE
        proc = subprocess.Popen(command_colors_enabled, close_fds=True, creationflags=c_flags)
        proc.communicate()  # Pipes directed towards new console window. Not worth doing screenshot image processing.
        assert proc.poll() == 0
        return

    # Run through pseudo tty (Linux/OSX).
    master, slave = __import__('pty').openpty()
    proc = subprocess.Popen(command_colors_enabled, stderr=subprocess.STDOUT, stdout=slave, close_fds=True)
    os.close(slave)

    # Read output.
    output = ''
    while True:
        try:
            data = os.read(master, 1024).decode()
        except __HOLE__ as exc:
            if exc.errno != errno.EIO:  # EIO means EOF on some systems.
                raise
            data = None
        if data:
            output += data
        elif proc.poll() is None:
            time.sleep(0.01)
        else:
            break
    os.close(master)
    assert not output
    assert proc.poll() == 0
dataset/ETHPy150Open Robpol86/colorclass/tests/test_codes.py/test_disable_colors_piped