Skip to content

Commit 9b6f74b

Browse files
test: normalize argparse choice snapshots
1 parent a523e55 commit 9b6f74b

1 file changed

Lines changed: 27 additions & 23 deletions

File tree

tests/test_cli.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import re
23
import subprocess
34
import sys
45
import types
@@ -17,6 +18,29 @@
1718
)
1819
from tests.utils import UtilFixture
1920

21+
ARGPARSE_CHOICES_PATTERN = re.compile(r"\(choose from (?P<choices>[^)]*)\)")
22+
ARGPARSE_QUOTED_CHOICE_PATTERN = re.compile(r"'([^']*)'")
23+
24+
25+
def normalize_argparse_choice_quotes(text: str) -> str:
26+
def normalize_match(match: re.Match[str]) -> str:
27+
choices = ARGPARSE_QUOTED_CHOICE_PATTERN.sub(r"\1", match.group("choices"))
28+
return f"(choose from {choices})"
29+
30+
return ARGPARSE_CHOICES_PATTERN.sub(normalize_match, text)
31+
32+
33+
def test_normalize_argparse_choice_quotes():
34+
text = (
35+
"cz: error: argument {init,commit}: invalid choice: 'invalidCommand' "
36+
"(choose from 'init', 'commit')"
37+
)
38+
39+
assert normalize_argparse_choice_quotes(text) == (
40+
"cz: error: argument {init,commit}: invalid choice: 'invalidCommand' "
41+
"(choose from init, commit)"
42+
)
43+
2044

2145
@pytest.mark.usefixtures("python_version", "consistent_terminal_output")
2246
def test_no_argv(util: UtilFixture, capsys, file_regression):
@@ -29,36 +53,16 @@ def test_no_argv(util: UtilFixture, capsys, file_regression):
2953

3054
@pytest.mark.parametrize(
3155
"arg",
32-
[
33-
"--invalid-arg",
34-
pytest.param(
35-
"invalidCommand",
36-
marks=pytest.mark.skipif(
37-
(3, 14, 5) <= sys.version_info < (3, 15),
38-
reason=(
39-
"Python 3.14.5 restored argparse choice quoting (CPython "
40-
"gh-130750); the checked-in fixture matches the 3.14.0-4 "
41-
"unquoted format. See #1990."
42-
),
43-
),
44-
),
45-
],
46-
)
47-
@pytest.mark.skipif(
48-
sys.version_info[:2] == (3, 12) and sys.version_info < (3, 12, 7),
49-
reason=(
50-
"argparse stopped quoting choices in 3.13 (CPython gh-129019), "
51-
"backported to 3.12.7. The reference snapshot reflects the "
52-
"no-quote format, so older 3.12.x patches (3.12.0-3.12.6) print "
53-
"quoted choices and fail. See commitizen-tools/commitizen#1864."
54-
),
56+
["--invalid-arg", "invalidCommand"],
5557
)
5658
@pytest.mark.usefixtures("python_version", "consistent_terminal_output")
5759
def test_invalid_command(util: UtilFixture, capsys, file_regression, arg):
5860
with pytest.raises(NoCommandFoundError):
5961
util.run_cli(arg)
6062
out, err = capsys.readouterr()
6163
assert out == ""
64+
if arg == "invalidCommand":
65+
err = normalize_argparse_choice_quotes(err)
6266
file_regression.check(err, extension=".txt")
6367

6468

0 commit comments

Comments
 (0)