@@ -266,3 +266,125 @@ def test_init_command_shows_description_when_use_help_option(
266266
267267 out , _ = capsys .readouterr ()
268268 file_regression .check (out , extension = ".txt" )
269+
270+
271+ def test_init_with_confirmed_tag_format (config , mocker : MockFixture , tmpdir ):
272+ mocker .patch (
273+ "commitizen.commands.init.get_tag_names" , return_value = ["v0.0.2" , "v0.0.1" ]
274+ )
275+ mocker .patch ("commitizen.commands.init.get_latest_tag_name" , return_value = "v0.0.2" )
276+ mocker .patch (
277+ "questionary.select" ,
278+ side_effect = [
279+ FakeQuestion ("pyproject.toml" ),
280+ FakeQuestion ("cz_conventional_commits" ),
281+ FakeQuestion ("commitizen" ),
282+ FakeQuestion ("semver" ),
283+ ],
284+ )
285+ mocker .patch ("questionary.confirm" , return_value = FakeQuestion (True ))
286+ mocker .patch ("questionary.text" , return_value = FakeQuestion ("$version" ))
287+ mocker .patch ("questionary.checkbox" , return_value = FakeQuestion (None ))
288+
289+ with tmpdir .as_cwd ():
290+ commands .Init (config )()
291+ with open ("pyproject.toml" , encoding = "utf-8" ) as toml_file :
292+ assert 'tag_format = "v$version"' in toml_file .read ()
293+
294+
295+ def test_init_with_no_existing_tags (config , mocker : MockFixture , tmpdir ):
296+ mocker .patch ("commitizen.commands.init.get_tag_names" , return_value = [])
297+ mocker .patch ("commitizen.commands.init.get_latest_tag_name" , return_value = "v1.0.0" )
298+ mocker .patch (
299+ "questionary.select" ,
300+ side_effect = [
301+ FakeQuestion ("pyproject.toml" ),
302+ FakeQuestion ("cz_conventional_commits" ),
303+ FakeQuestion ("commitizen" ),
304+ FakeQuestion ("semver" ),
305+ ],
306+ )
307+ mocker .patch ("questionary.confirm" , return_value = FakeQuestion (False ))
308+ mocker .patch ("questionary.text" , return_value = FakeQuestion ("$version" ))
309+ mocker .patch ("questionary.checkbox" , return_value = FakeQuestion (None ))
310+
311+ with tmpdir .as_cwd ():
312+ commands .Init (config )()
313+ with open ("pyproject.toml" , encoding = "utf-8" ) as toml_file :
314+ assert 'version = "0.0.1"' in toml_file .read ()
315+
316+
317+ def test_init_with_no_existing_latest_tag (config , mocker : MockFixture , tmpdir ):
318+ mocker .patch ("commitizen.commands.init.get_latest_tag_name" , return_value = None )
319+ mocker .patch (
320+ "questionary.select" ,
321+ side_effect = [
322+ FakeQuestion ("pyproject.toml" ),
323+ FakeQuestion ("cz_conventional_commits" ),
324+ FakeQuestion ("commitizen" ),
325+ FakeQuestion ("semver" ),
326+ ],
327+ )
328+ mocker .patch ("questionary.confirm" , return_value = FakeQuestion (True ))
329+ mocker .patch ("questionary.text" , return_value = FakeQuestion ("$version" ))
330+ mocker .patch ("questionary.checkbox" , return_value = FakeQuestion (None ))
331+
332+ with tmpdir .as_cwd ():
333+ commands .Init (config )()
334+ with open ("pyproject.toml" , encoding = "utf-8" ) as toml_file :
335+ assert 'version = "0.0.1"' in toml_file .read ()
336+
337+
338+ def test_init_with_existing_tags (config , mocker : MockFixture , tmpdir ):
339+ expected_tags = ["v1.0.0" , "v0.9.0" , "v0.8.0" ]
340+ mocker .patch ("commitizen.commands.init.get_tag_names" , return_value = expected_tags )
341+ mocker .patch ("commitizen.commands.init.get_latest_tag_name" , return_value = "v1.0.0" )
342+ mocker .patch (
343+ "questionary.select" ,
344+ side_effect = [
345+ FakeQuestion ("pyproject.toml" ),
346+ FakeQuestion ("cz_conventional_commits" ),
347+ FakeQuestion ("commitizen" ),
348+ FakeQuestion ("semver" ), # Select version scheme first
349+ FakeQuestion ("v1.0.0" ), # Then select the latest tag
350+ ],
351+ )
352+ mocker .patch ("questionary.confirm" , return_value = FakeQuestion (True ))
353+ mocker .patch ("questionary.text" , return_value = FakeQuestion ("$version" ))
354+ mocker .patch ("questionary.checkbox" , return_value = FakeQuestion (None ))
355+
356+ with tmpdir .as_cwd ():
357+ commands .Init (config )()
358+ with open ("pyproject.toml" , encoding = "utf-8" ) as toml_file :
359+ assert 'version = "1.0.0"' in toml_file .read ()
360+
361+
362+ def test_init_with_valid_tag_selection (config , mocker : MockFixture , tmpdir ):
363+ expected_tags = ["v1.0.0" , "v0.9.0" , "v0.8.0" ]
364+ mocker .patch ("commitizen.commands.init.get_tag_names" , return_value = expected_tags )
365+ mocker .patch ("commitizen.commands.init.get_latest_tag_name" , return_value = "v1.0.0" )
366+
367+ # Mock all questionary.select calls in the exact order they appear in Init.__call__
368+ mocker .patch (
369+ "questionary.select" ,
370+ side_effect = [
371+ FakeQuestion ("pyproject.toml" ), # _ask_config_path
372+ FakeQuestion ("cz_conventional_commits" ), # _ask_name
373+ FakeQuestion ("commitizen" ), # _ask_version_provider
374+ FakeQuestion ("v0.9.0" ), # _ask_tag (after confirm=False)
375+ FakeQuestion ("semver" ), # _ask_version_scheme
376+ ],
377+ )
378+
379+ mocker .patch (
380+ "questionary.confirm" , return_value = FakeQuestion (False )
381+ ) # Don't confirm latest tag
382+ mocker .patch ("questionary.text" , return_value = FakeQuestion ("$version" ))
383+ mocker .patch ("questionary.checkbox" , return_value = FakeQuestion (None ))
384+
385+ with tmpdir .as_cwd ():
386+ commands .Init (config )()
387+ with open ("pyproject.toml" , encoding = "utf-8" ) as toml_file :
388+ content = toml_file .read ()
389+ assert 'version = "0.9.0"' in content
390+ assert 'version_scheme = "semver"' in content
0 commit comments