Skip to content

Commit 1709095

Browse files
bearomorphismLee-W
authored andcommitted
test(Init): cover _ask_tag test
1 parent 77143db commit 1709095

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

tests/commands/test_init_command.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,125 @@ def test_init_command_shows_description_when_use_help_option(
298298

299299
out, _ = capsys.readouterr()
300300
file_regression.check(out, extension=".txt")
301+
302+
303+
def test_init_with_confirmed_tag_format(config, mocker: MockFixture, tmpdir):
304+
mocker.patch(
305+
"commitizen.commands.init.get_tag_names", return_value=["v0.0.2", "v0.0.1"]
306+
)
307+
mocker.patch("commitizen.commands.init.get_latest_tag_name", return_value="v0.0.2")
308+
mocker.patch(
309+
"questionary.select",
310+
side_effect=[
311+
FakeQuestion("pyproject.toml"),
312+
FakeQuestion("cz_conventional_commits"),
313+
FakeQuestion("commitizen"),
314+
FakeQuestion("semver"),
315+
],
316+
)
317+
mocker.patch("questionary.confirm", return_value=FakeQuestion(True))
318+
mocker.patch("questionary.text", return_value=FakeQuestion("$version"))
319+
mocker.patch("questionary.checkbox", return_value=FakeQuestion(None))
320+
321+
with tmpdir.as_cwd():
322+
commands.Init(config)()
323+
with open("pyproject.toml", encoding="utf-8") as toml_file:
324+
assert 'tag_format = "v$version"' in toml_file.read()
325+
326+
327+
def test_init_with_no_existing_tags(config, mocker: MockFixture, tmpdir):
328+
mocker.patch("commitizen.commands.init.get_tag_names", return_value=[])
329+
mocker.patch("commitizen.commands.init.get_latest_tag_name", return_value="v1.0.0")
330+
mocker.patch(
331+
"questionary.select",
332+
side_effect=[
333+
FakeQuestion("pyproject.toml"),
334+
FakeQuestion("cz_conventional_commits"),
335+
FakeQuestion("commitizen"),
336+
FakeQuestion("semver"),
337+
],
338+
)
339+
mocker.patch("questionary.confirm", return_value=FakeQuestion(False))
340+
mocker.patch("questionary.text", return_value=FakeQuestion("$version"))
341+
mocker.patch("questionary.checkbox", return_value=FakeQuestion(None))
342+
343+
with tmpdir.as_cwd():
344+
commands.Init(config)()
345+
with open("pyproject.toml", encoding="utf-8") as toml_file:
346+
assert 'version = "0.0.1"' in toml_file.read()
347+
348+
349+
def test_init_with_no_existing_latest_tag(config, mocker: MockFixture, tmpdir):
350+
mocker.patch("commitizen.commands.init.get_latest_tag_name", return_value=None)
351+
mocker.patch(
352+
"questionary.select",
353+
side_effect=[
354+
FakeQuestion("pyproject.toml"),
355+
FakeQuestion("cz_conventional_commits"),
356+
FakeQuestion("commitizen"),
357+
FakeQuestion("semver"),
358+
],
359+
)
360+
mocker.patch("questionary.confirm", return_value=FakeQuestion(True))
361+
mocker.patch("questionary.text", return_value=FakeQuestion("$version"))
362+
mocker.patch("questionary.checkbox", return_value=FakeQuestion(None))
363+
364+
with tmpdir.as_cwd():
365+
commands.Init(config)()
366+
with open("pyproject.toml", encoding="utf-8") as toml_file:
367+
assert 'version = "0.0.1"' in toml_file.read()
368+
369+
370+
def test_init_with_existing_tags(config, mocker: MockFixture, tmpdir):
371+
expected_tags = ["v1.0.0", "v0.9.0", "v0.8.0"]
372+
mocker.patch("commitizen.commands.init.get_tag_names", return_value=expected_tags)
373+
mocker.patch("commitizen.commands.init.get_latest_tag_name", return_value="v1.0.0")
374+
mocker.patch(
375+
"questionary.select",
376+
side_effect=[
377+
FakeQuestion("pyproject.toml"),
378+
FakeQuestion("cz_conventional_commits"),
379+
FakeQuestion("commitizen"),
380+
FakeQuestion("semver"), # Select version scheme first
381+
FakeQuestion("v1.0.0"), # Then select the latest tag
382+
],
383+
)
384+
mocker.patch("questionary.confirm", return_value=FakeQuestion(True))
385+
mocker.patch("questionary.text", return_value=FakeQuestion("$version"))
386+
mocker.patch("questionary.checkbox", return_value=FakeQuestion(None))
387+
388+
with tmpdir.as_cwd():
389+
commands.Init(config)()
390+
with open("pyproject.toml", encoding="utf-8") as toml_file:
391+
assert 'version = "1.0.0"' in toml_file.read()
392+
393+
394+
def test_init_with_valid_tag_selection(config, mocker: MockFixture, tmpdir):
395+
expected_tags = ["v1.0.0", "v0.9.0", "v0.8.0"]
396+
mocker.patch("commitizen.commands.init.get_tag_names", return_value=expected_tags)
397+
mocker.patch("commitizen.commands.init.get_latest_tag_name", return_value="v1.0.0")
398+
399+
# Mock all questionary.select calls in the exact order they appear in Init.__call__
400+
mocker.patch(
401+
"questionary.select",
402+
side_effect=[
403+
FakeQuestion("pyproject.toml"), # _ask_config_path
404+
FakeQuestion("cz_conventional_commits"), # _ask_name
405+
FakeQuestion("commitizen"), # _ask_version_provider
406+
FakeQuestion("v0.9.0"), # _ask_tag (after confirm=False)
407+
FakeQuestion("semver"), # _ask_version_scheme
408+
],
409+
)
410+
411+
mocker.patch(
412+
"questionary.confirm", return_value=FakeQuestion(False)
413+
) # Don't confirm latest tag
414+
mocker.patch("questionary.text", return_value=FakeQuestion("$version"))
415+
mocker.patch("questionary.checkbox", return_value=FakeQuestion(None))
416+
417+
with tmpdir.as_cwd():
418+
commands.Init(config)()
419+
with open("pyproject.toml", encoding="utf-8") as toml_file:
420+
content = toml_file.read()
421+
assert 'version = "0.9.0"' in content
422+
assert 'version_scheme = "semver"' in content

0 commit comments

Comments
 (0)