Skip to content

ANSI codes not stripped from click.confirm after upgrading to click 8.4 #3572

@alasdairnicol

Description

@alasdairnicol

I had failing unit tests after upgrading to click 8.4.1. I was able to address some
by using runner.invoke(cmd, color=False), but that didn't help for commands
that use click.confirm()

This test script test_repro.py replicates the issue.

import click
from click.testing import CliRunner


@click.command()
def cmd():
    click.echo(click.style('Hello World!', fg='green'))


@click.command()
def cmd_confirm():
    click.confirm(click.style('Hello World!', fg='green'), abort=True)


def test_echo_strips_ansi_with_color_false():
    runner = CliRunner()
    result = runner.invoke(cmd, color=False)
    assert result.output == "Hello World!\n"


def test_confirm_strips_ansi_with_color_false():
    runner = CliRunner()
    result = runner.invoke(cmd_confirm, input="Y", color=False)
    assert result.output == "Hello World! [y/N]: Y\n"  # FAILS: ANSI codes present

With click 8.3.3 both tests pass. With click 8.4.0 and 8.4.1, the test_confirm_strips_ansi_with_color_false test fails:

uvx --with click==8.4 pytest test_repro.py
...
================================================ FAILURES ================================================
_______________________________ test_confirm_strips_ansi_with_color_false ________________________________

    def test_confirm_strips_ansi_with_color_false():
        runner = CliRunner()
        result = runner.invoke(cmd_confirm, input="Y", color=False)
>       assert result.output == "Hello World! [y/N]: Y\n"  # FAILS: ANSI codes present

The ansi codes should be stripped when running click.confirm, as they are for click.echo.

Environment:

  • Python version: 3.13
  • Click version: 8.4.1
  • MacOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions