@@ -61,7 +61,7 @@ def unsafe_ask(self):
6161
6262
6363def test_init_without_setup_pre_commit_hook (
64- tmpdir , mocker : MockFixture , config : BaseConfig
64+ tmp_path , monkeypatch , mocker : MockFixture , config : BaseConfig
6565):
6666 mocker .patch (
6767 "questionary.select" ,
@@ -76,13 +76,13 @@ def test_init_without_setup_pre_commit_hook(
7676 mocker .patch ("questionary.text" , return_value = FakeQuestion ("$version" ))
7777 # Return None to skip hook installation
7878 mocker .patch ("questionary.checkbox" , return_value = FakeQuestion (None ))
79- with tmpdir . as_cwd ():
80- commands .Init (config )()
79+ monkeypatch . chdir ( tmp_path )
80+ commands .Init (config )()
8181
82- config_data = Path ("pyproject.toml" ).read_text (encoding = "utf-8" )
83- assert config_data == expected_config
82+ config_data = Path ("pyproject.toml" ).read_text (encoding = "utf-8" )
83+ assert config_data == expected_config
8484
85- assert not Path (pre_commit_config_filename ).exists ()
85+ assert not Path (pre_commit_config_filename ).exists ()
8686
8787
8888def test_init_when_config_already_exists (config : BaseConfig , capsys ):
@@ -95,7 +95,9 @@ def test_init_when_config_already_exists(config: BaseConfig, capsys):
9595 assert captured .out == f"Config file { path } already exists\n "
9696
9797
98- def test_init_without_choosing_tag (config : BaseConfig , mocker : MockFixture , tmpdir ):
98+ def test_init_without_choosing_tag (
99+ config : BaseConfig , mocker : MockFixture , tmp_path , monkeypatch
100+ ):
99101 mocker .patch (
100102 "commitizen.commands.init.get_tag_names" , return_value = ["0.0.2" , "0.0.1" ]
101103 )
@@ -112,9 +114,9 @@ def test_init_without_choosing_tag(config: BaseConfig, mocker: MockFixture, tmpd
112114 mocker .patch ("questionary.confirm" , return_value = FakeQuestion (False ))
113115 mocker .patch ("questionary.text" , return_value = FakeQuestion ("y" ))
114116
115- with tmpdir . as_cwd ():
116- with pytest .raises (NoAnswersError ):
117- commands .Init (config )()
117+ monkeypatch . chdir ( tmp_path )
118+ with pytest .raises (NoAnswersError ):
119+ commands .Init (config )()
118120
119121
120122@pytest .fixture
@@ -177,67 +179,67 @@ def check_pre_commit_config(expected: list[dict[str, Any]]):
177179@pytest .mark .usefixtures ("pre_commit_installed" )
178180class TestPreCommitCases :
179181 def test_no_existing_pre_commit_config (
180- self , default_choice : str , tmpdir , config : BaseConfig
182+ self , default_choice : str , tmp_path , monkeypatch , config : BaseConfig
181183 ):
182- with tmpdir . as_cwd ():
183- commands .Init (config )()
184- check_cz_config (default_choice )
185- check_pre_commit_config ([cz_hook_config ])
184+ monkeypatch . chdir ( tmp_path )
185+ commands .Init (config )()
186+ check_cz_config (default_choice )
187+ check_pre_commit_config ([cz_hook_config ])
186188
187189 def test_empty_pre_commit_config (
188- self , default_choice : str , tmpdir , config : BaseConfig
190+ self , default_choice : str , tmp_path , monkeypatch , config : BaseConfig
189191 ):
190- with tmpdir . as_cwd ():
191- p = tmpdir . join ( pre_commit_config_filename )
192- p . write ("" )
192+ monkeypatch . chdir ( tmp_path )
193+ p = tmp_path / pre_commit_config_filename
194+ p . write_text ("" )
193195
194- commands .Init (config )()
195- check_cz_config (default_choice )
196- check_pre_commit_config ([cz_hook_config ])
196+ commands .Init (config )()
197+ check_cz_config (default_choice )
198+ check_pre_commit_config ([cz_hook_config ])
197199
198200 def test_pre_commit_config_without_cz_hook (
199- self , default_choice : str , tmpdir , config : BaseConfig
201+ self , default_choice : str , tmp_path , monkeypatch , config : BaseConfig
200202 ):
201203 existing_hook_config = {
202204 "repo" : "https://github.com/pre-commit/pre-commit-hooks" ,
203205 "rev" : "v1.2.3" ,
204206 "hooks" : [{"id" , "trailing-whitespace" }],
205207 }
206208
207- with tmpdir . as_cwd ():
208- p = tmpdir . join ( pre_commit_config_filename )
209- p . write (yaml .safe_dump ({"repos" : [existing_hook_config ]}))
209+ monkeypatch . chdir ( tmp_path )
210+ p = tmp_path / pre_commit_config_filename
211+ p . write_text (yaml .safe_dump ({"repos" : [existing_hook_config ]}))
210212
211- commands .Init (config )()
212- check_cz_config (default_choice )
213- check_pre_commit_config ([existing_hook_config , cz_hook_config ])
213+ commands .Init (config )()
214+ check_cz_config (default_choice )
215+ check_pre_commit_config ([existing_hook_config , cz_hook_config ])
214216
215217 def test_cz_hook_exists_in_pre_commit_config (
216- self , default_choice : str , tmpdir , config : BaseConfig
218+ self , default_choice : str , tmp_path , monkeypatch , config : BaseConfig
217219 ):
218- with tmpdir . as_cwd ():
219- p = tmpdir . join ( pre_commit_config_filename )
220- p . write (yaml .safe_dump ({"repos" : [cz_hook_config ]}))
220+ monkeypatch . chdir ( tmp_path )
221+ p = tmp_path / pre_commit_config_filename
222+ p . write_text (yaml .safe_dump ({"repos" : [cz_hook_config ]}))
221223
222- commands .Init (config )()
223- check_cz_config (default_choice )
224- # check that config is not duplicated
225- check_pre_commit_config ([cz_hook_config ])
224+ commands .Init (config )()
225+ check_cz_config (default_choice )
226+ # check that config is not duplicated
227+ check_pre_commit_config ([cz_hook_config ])
226228
227229
228230class TestNoPreCommitInstalled :
229231 @pytest .mark .usefixtures ("default_choice" )
230232 def test_pre_commit_not_installed (
231- self , mocker : MockFixture , config : BaseConfig , tmpdir
233+ self , mocker : MockFixture , config : BaseConfig , tmp_path , monkeypatch
232234 ):
233235 # Assume `pre-commit` is not installed
234236 mocker .patch (
235237 "commitizen.project_info.is_pre_commit_installed" ,
236238 return_value = False ,
237239 )
238- with tmpdir . as_cwd ():
239- with pytest .raises (InitFailedError ):
240- commands .Init (config )()
240+ monkeypatch . chdir ( tmp_path )
241+ with pytest .raises (InitFailedError ):
242+ commands .Init (config )()
241243
242244
243245class TestAskTagFormat :
@@ -273,7 +275,7 @@ def test_empty_input_returns_default(self, mocker: MockFixture, config: BaseConf
273275
274276
275277def test_init_with_confirmed_tag_format (
276- config : BaseConfig , mocker : MockFixture , tmpdir
278+ config : BaseConfig , mocker : MockFixture , tmp_path , monkeypatch
277279):
278280 mocker .patch (
279281 "commitizen.commands.init.get_tag_names" , return_value = ["v0.0.2" , "v0.0.1" ]
@@ -292,14 +294,16 @@ def test_init_with_confirmed_tag_format(
292294 mocker .patch ("questionary.text" , return_value = FakeQuestion ("$version" ))
293295 mocker .patch ("questionary.checkbox" , return_value = FakeQuestion (None ))
294296
295- with tmpdir . as_cwd ():
296- commands .Init (config )()
297- assert 'tag_format = "v$version"' in Path ("pyproject.toml" ).read_text (
298- encoding = "utf-8"
299- )
297+ monkeypatch . chdir ( tmp_path )
298+ commands .Init (config )()
299+ assert 'tag_format = "v$version"' in Path ("pyproject.toml" ).read_text (
300+ encoding = "utf-8"
301+ )
300302
301303
302- def test_init_with_no_existing_tags (config : BaseConfig , mocker : MockFixture , tmpdir ):
304+ def test_init_with_no_existing_tags (
305+ config : BaseConfig , mocker : MockFixture , tmp_path , monkeypatch
306+ ):
303307 mocker .patch ("commitizen.commands.init.get_tag_names" , return_value = [])
304308 mocker .patch ("commitizen.commands.init.get_latest_tag_name" , return_value = "v1.0.0" )
305309 mocker .patch (
@@ -315,13 +319,13 @@ def test_init_with_no_existing_tags(config: BaseConfig, mocker: MockFixture, tmp
315319 mocker .patch ("questionary.text" , return_value = FakeQuestion ("$version" ))
316320 mocker .patch ("questionary.checkbox" , return_value = FakeQuestion (None ))
317321
318- with tmpdir . as_cwd ():
319- commands .Init (config )()
320- assert 'version = "0.0.1"' in Path ("pyproject.toml" ).read_text (encoding = "utf-8" )
322+ monkeypatch . chdir ( tmp_path )
323+ commands .Init (config )()
324+ assert 'version = "0.0.1"' in Path ("pyproject.toml" ).read_text (encoding = "utf-8" )
321325
322326
323327def test_init_with_no_existing_latest_tag (
324- config : BaseConfig , mocker : MockFixture , tmpdir
328+ config : BaseConfig , mocker : MockFixture , tmp_path , monkeypatch
325329):
326330 mocker .patch ("commitizen.commands.init.get_latest_tag_name" , return_value = None )
327331 mocker .patch (
@@ -337,12 +341,14 @@ def test_init_with_no_existing_latest_tag(
337341 mocker .patch ("questionary.text" , return_value = FakeQuestion ("$version" ))
338342 mocker .patch ("questionary.checkbox" , return_value = FakeQuestion (None ))
339343
340- with tmpdir . as_cwd ():
341- commands .Init (config )()
342- assert 'version = "0.0.1"' in Path ("pyproject.toml" ).read_text (encoding = "utf-8" )
344+ monkeypatch . chdir ( tmp_path )
345+ commands .Init (config )()
346+ assert 'version = "0.0.1"' in Path ("pyproject.toml" ).read_text (encoding = "utf-8" )
343347
344348
345- def test_init_with_existing_tags (config : BaseConfig , mocker : MockFixture , tmpdir ):
349+ def test_init_with_existing_tags (
350+ config : BaseConfig , mocker : MockFixture , tmp_path , monkeypatch
351+ ):
346352 expected_tags = ["v1.0.0" , "v0.9.0" , "v0.8.0" ]
347353 mocker .patch ("commitizen.commands.init.get_tag_names" , return_value = expected_tags )
348354 mocker .patch ("commitizen.commands.init.get_latest_tag_name" , return_value = "v1.0.0" )
@@ -360,12 +366,14 @@ def test_init_with_existing_tags(config: BaseConfig, mocker: MockFixture, tmpdir
360366 mocker .patch ("questionary.text" , return_value = FakeQuestion ("$version" ))
361367 mocker .patch ("questionary.checkbox" , return_value = FakeQuestion (None ))
362368
363- with tmpdir . as_cwd ():
364- commands .Init (config )()
365- assert 'version = "1.0.0"' in Path ("pyproject.toml" ).read_text (encoding = "utf-8" )
369+ monkeypatch . chdir ( tmp_path )
370+ commands .Init (config )()
371+ assert 'version = "1.0.0"' in Path ("pyproject.toml" ).read_text (encoding = "utf-8" )
366372
367373
368- def test_init_with_valid_tag_selection (config : BaseConfig , mocker : MockFixture , tmpdir ):
374+ def test_init_with_valid_tag_selection (
375+ config : BaseConfig , mocker : MockFixture , tmp_path , monkeypatch
376+ ):
369377 expected_tags = ["v1.0.0" , "v0.9.0" , "v0.8.0" ]
370378 mocker .patch ("commitizen.commands.init.get_tag_names" , return_value = expected_tags )
371379 mocker .patch ("commitizen.commands.init.get_latest_tag_name" , return_value = "v1.0.0" )
@@ -388,14 +396,16 @@ def test_init_with_valid_tag_selection(config: BaseConfig, mocker: MockFixture,
388396 mocker .patch ("questionary.text" , return_value = FakeQuestion ("$version" ))
389397 mocker .patch ("questionary.checkbox" , return_value = FakeQuestion (None ))
390398
391- with tmpdir . as_cwd ():
392- commands .Init (config )()
393- content = Path ("pyproject.toml" ).read_text (encoding = "utf-8" )
394- assert 'version = "0.9.0"' in content
395- assert 'version_scheme = "semver"' in content
399+ monkeypatch . chdir ( tmp_path )
400+ commands .Init (config )()
401+ content = Path ("pyproject.toml" ).read_text (encoding = "utf-8" )
402+ assert 'version = "0.9.0"' in content
403+ assert 'version_scheme = "semver"' in content
396404
397405
398- def test_init_configuration_settings (tmpdir , mocker : MockFixture , config : BaseConfig ):
406+ def test_init_configuration_settings (
407+ tmp_path , monkeypatch , mocker : MockFixture , config : BaseConfig
408+ ):
399409 """Test that all configuration settings are properly initialized."""
400410 mocker .patch (
401411 "questionary.select" ,
@@ -410,22 +420,22 @@ def test_init_configuration_settings(tmpdir, mocker: MockFixture, config: BaseCo
410420 mocker .patch ("questionary.text" , return_value = FakeQuestion ("$version" ))
411421 mocker .patch ("questionary.checkbox" , return_value = FakeQuestion (None ))
412422
413- with tmpdir . as_cwd ():
414- commands .Init (config )()
423+ monkeypatch . chdir ( tmp_path )
424+ commands .Init (config )()
415425
416- config_data = Path ("pyproject.toml" ).read_text (encoding = "utf-8" )
426+ config_data = Path ("pyproject.toml" ).read_text (encoding = "utf-8" )
417427
418- # Verify all expected settings are present
419- assert 'name = "cz_conventional_commits"' in config_data
420- assert 'tag_format = "$version"' in config_data
421- assert 'version_scheme = "semver"' in config_data
422- assert 'version = "0.0.1"' in config_data
423- assert "update_changelog_on_bump = true" in config_data
424- assert "major_version_zero = true" in config_data
428+ # Verify all expected settings are present
429+ assert 'name = "cz_conventional_commits"' in config_data
430+ assert 'tag_format = "$version"' in config_data
431+ assert 'version_scheme = "semver"' in config_data
432+ assert 'version = "0.0.1"' in config_data
433+ assert "update_changelog_on_bump = true" in config_data
434+ assert "major_version_zero = true" in config_data
425435
426436
427437def test_init_configuration_with_version_provider (
428- tmpdir , mocker : MockFixture , config : BaseConfig
438+ tmp_path , monkeypatch , mocker : MockFixture , config : BaseConfig
429439):
430440 """Test configuration initialization with a different version provider."""
431441 mocker .patch (
@@ -441,21 +451,21 @@ def test_init_configuration_with_version_provider(
441451 mocker .patch ("questionary.text" , return_value = FakeQuestion ("$version" ))
442452 mocker .patch ("questionary.checkbox" , return_value = FakeQuestion (None ))
443453
444- with tmpdir . as_cwd ():
445- commands .Init (config )()
454+ monkeypatch . chdir ( tmp_path )
455+ commands .Init (config )()
446456
447- config_data = Path ("pyproject.toml" ).read_text (encoding = "utf-8" )
448-
449- # Verify version provider is set instead of version
450- assert 'name = "cz_conventional_commits"' in config_data
451- assert 'tag_format = "$version"' in config_data
452- assert 'version_scheme = "semver"' in config_data
453- assert 'version_provider = "pep621"' in config_data
454- assert "update_changelog_on_bump = true" in config_data
455- assert "major_version_zero = true" in config_data
456- assert (
457- "version = " not in config_data
458- ) # Version should not be set when using version_provider
457+ config_data = Path ("pyproject.toml" ).read_text (encoding = "utf-8" )
458+
459+ # Verify version provider is set instead of version
460+ assert 'name = "cz_conventional_commits"' in config_data
461+ assert 'tag_format = "$version"' in config_data
462+ assert 'version_scheme = "semver"' in config_data
463+ assert 'version_provider = "pep621"' in config_data
464+ assert "update_changelog_on_bump = true" in config_data
465+ assert "major_version_zero = true" in config_data
466+ assert (
467+ "version = " not in config_data
468+ ) # Version should not be set when using version_provider
459469
460470
461471def test_construct_name_choice_from_registry (config : BaseConfig ):
0 commit comments