Skip to content

Commit 90ac66a

Browse files
committed
test: replace legacy tmpdir with tmp_path
1 parent 2847b8c commit 90ac66a

File tree

9 files changed

+418
-388
lines changed

9 files changed

+418
-388
lines changed

tests/commands/test_bump_command.py

Lines changed: 106 additions & 109 deletions
Large diffs are not rendered by default.

tests/commands/test_changelog_command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ def test_changelog_incremental_newline_separates_new_content_from_old(
437437

438438

439439
def test_changelog_without_revision(tmp_commitizen_project, util: UtilFixture):
440-
tmp_commitizen_project.join("CHANGELOG.md").write(
440+
(tmp_commitizen_project / "CHANGELOG.md").write_text(
441441
"""
442442
# Unreleased
443443

tests/commands/test_check_command.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ def test_check_conventional_commit_succeeds(
119119
),
120120
],
121121
)
122-
def test_check_no_conventional_commit(commit_msg, config, tmpdir):
123-
tempfile = tmpdir.join("temp_commit_file")
124-
tempfile.write(commit_msg)
122+
def test_check_no_conventional_commit(commit_msg, config, tmp_path):
123+
tempfile = tmp_path / "temp_commit_file"
124+
tempfile.write_text(commit_msg)
125125

126126
with pytest.raises(InvalidCommitMessageError):
127127
commands.Check(config=config, arguments={"commit_msg_file": tempfile})()
@@ -136,9 +136,11 @@ def test_check_no_conventional_commit(commit_msg, config, tmpdir):
136136
"bump: 0.0.1 -> 1.0.0",
137137
],
138138
)
139-
def test_check_conventional_commit(commit_msg, config, success_mock: MockType, tmpdir):
140-
tempfile = tmpdir.join("temp_commit_file")
141-
tempfile.write(commit_msg)
139+
def test_check_conventional_commit(
140+
commit_msg, config, success_mock: MockType, tmp_path
141+
):
142+
tempfile = tmp_path / "temp_commit_file"
143+
tempfile.write_text(commit_msg)
142144
commands.Check(config=config, arguments={"commit_msg_file": tempfile})()
143145
success_mock.assert_called_once()
144146

tests/commands/test_commit_command.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@ def staging_is_clean(mocker: MockFixture, tmp_git_project):
5151
@pytest.fixture
5252
def backup_file(tmp_git_project, monkeypatch):
5353
"""Write backup message so Commit finds it when run from tmp_git_project."""
54-
with tmp_git_project.as_cwd():
55-
path = get_backup_file_path()
56-
path.write_text("backup commit", encoding="utf-8")
5754
monkeypatch.chdir(tmp_git_project)
55+
path = get_backup_file_path()
56+
path.write_text("backup commit", encoding="utf-8")
5857

5958

6059
@pytest.mark.usefixtures("staging_is_clean", "commit_mock", "prompt_mock_feat")
@@ -263,10 +262,10 @@ def test_commit_when_no_user_answer(config, mocker: MockFixture):
263262
commands.Commit(config, {})()
264263

265264

266-
def test_commit_in_non_git_project(tmpdir, config):
267-
with tmpdir.as_cwd():
268-
with pytest.raises(NotAGitProjectError):
269-
commands.Commit(config, {})
265+
def test_commit_in_non_git_project(tmp_path, monkeypatch, config):
266+
monkeypatch.chdir(tmp_path)
267+
with pytest.raises(NotAGitProjectError):
268+
commands.Commit(config, {})
270269

271270

272271
@pytest.mark.usefixtures("staging_is_clean", "commit_mock", "prompt_mock_feat")

tests/commands/test_init_command.py

Lines changed: 101 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def unsafe_ask(self):
6161

6262

6363
def 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

8888
def 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")
178180
class 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

228230
class 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

243245
class TestAskTagFormat:
@@ -273,7 +275,7 @@ def test_empty_input_returns_default(self, mocker: MockFixture, config: BaseConf
273275

274276

275277
def 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

323327
def 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

427437
def 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

461471
def test_construct_name_choice_from_registry(config: BaseConfig):

0 commit comments

Comments
 (0)