Skip to content

Commit f73d1bb

Browse files
bastien31Lee-W
authored andcommitted
fix(test): set terminal width for cli tests
Force consistent terminal width for tests to avoid wrapping differences between single and multi-worker pytest modes Signed-off-by: Sebastien Fusilier <sebastien.fusilier@gmail.com>
1 parent bc34af0 commit f73d1bb

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

commitizen/cz/conventional_commits/conventional_commits.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,17 @@ def message(self, answers: ConventionalCommitsAnswers) -> str: # type: ignore[o
154154
footer = answers["footer"]
155155
is_breaking_change = answers["is_breaking_change"]
156156

157-
scope = f"({scope})" if scope else ""
158-
body = f"\n\n{body}" if body else ""
159-
title = f"{prefix}{scope}"
157+
formatted_scope = f"({scope})" if scope else ""
158+
title = f"{prefix}{formatted_scope}"
160159
if is_breaking_change:
161160
if self.config.settings.get("breaking_change_exclamation_in_title", False):
162161
title = f"{title}!"
163162
footer = f"BREAKING CHANGE: {footer}"
164163

165-
return f"{title}: {subject}{body}{footer}"
164+
formatted_body = f"\n\n{body}" if body else ""
165+
formatted_footter = f"\n\n{footer}" if footer else ""
166+
167+
return f"{title}: {subject}{formatted_body}{formatted_footter}"
166168

167169
def example(self) -> str:
168170
return (

tests/commands/test_version_command.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import platform
23
import sys
34

@@ -112,13 +113,25 @@ def test_version_use_version_provider(
112113
def test_version_command_shows_description_when_use_help_option(
113114
mocker: MockerFixture, capsys, file_regression
114115
):
115-
testargs = ["cz", "version", "--help"]
116-
mocker.patch.object(sys, "argv", testargs)
117-
with pytest.raises(SystemExit):
118-
cli.main()
119-
120-
out, _ = capsys.readouterr()
121-
file_regression.check(out, extension=".txt")
116+
# Force consistent terminal width for tests to avoid wrapping differences
117+
# between single and multi-worker pytest modes
118+
original_columns = os.environ.get("COLUMNS")
119+
os.environ["COLUMNS"] = "80"
120+
121+
try:
122+
testargs = ["cz", "version", "--help"]
123+
mocker.patch.object(sys, "argv", testargs)
124+
with pytest.raises(SystemExit):
125+
cli.main()
126+
127+
out, _ = capsys.readouterr()
128+
file_regression.check(out, extension=".txt")
129+
finally:
130+
# Restore original COLUMNS
131+
if original_columns is not None:
132+
os.environ["COLUMNS"] = original_columns
133+
else:
134+
os.environ.pop("COLUMNS", None)
122135

123136

124137
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)