From ec878dc3937e5c8e630a3fe0db192cdd7734dce2 Mon Sep 17 00:00:00 2001 From: Joe Rickerby Date: Sun, 4 Dec 2022 19:08:47 +0000 Subject: [PATCH] Add a unit test for the workaround, and fix a bug --- cibuildwheel/util.py | 4 ++-- unit_test/utils_test.py | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/cibuildwheel/util.py b/cibuildwheel/util.py index ca83d964..e0b2ae12 100644 --- a/cibuildwheel/util.py +++ b/cibuildwheel/util.py @@ -683,9 +683,9 @@ def fix_ansi_codes_for_github_actions(text: str) -> str: ansi_codes: list[str] = [] output = "" - for line in text.split("\n"): + for line in text.splitlines(keepends=True): # add the current ANSI codes to the beginning of the line - output += "".join(ansi_codes) + line + "\n" + output += "".join(ansi_codes) + line # split the line at each ANSI code parts = ansi_code_regex.split(line) diff --git a/unit_test/utils_test.py b/unit_test/utils_test.py index adf21435..4628ff6d 100644 --- a/unit_test/utils_test.py +++ b/unit_test/utils_test.py @@ -1,10 +1,16 @@ from __future__ import annotations +import textwrap from pathlib import PurePath import pytest -from cibuildwheel.util import find_compatible_wheel, format_safe, prepare_command +from cibuildwheel.util import ( + find_compatible_wheel, + fix_ansi_codes_for_github_actions, + format_safe, + prepare_command, +) def test_format_safe(): @@ -90,3 +96,31 @@ def test_find_compatible_wheel_found(wheel: str, identifier: str): ) def test_find_compatible_wheel_not_found(wheel: str, identifier: str): assert find_compatible_wheel([PurePath(wheel)], identifier) is None + + +def test_fix_ansi_codes_for_github_actions(): + input = textwrap.dedent( + """ + This line is normal + \033[1mThis line is bold + This line is also bold + \033[31m this line is red and bold + This line is red and bold, too\033[0m + This line is normal again + """ + ) + + expected = textwrap.dedent( + """ + This line is normal + \033[1mThis line is bold + \033[1mThis line is also bold + \033[1m\033[31m this line is red and bold + \033[1m\033[31mThis line is red and bold, too\033[0m + This line is normal again + """ + ) + + output = fix_ansi_codes_for_github_actions(input) + + assert output == expected