Skip to content

Commit 98d6e93

Browse files
committed
refactor(tests/commands/bump): use tmp_dir to replace self implemented tmp dir behavior
1 parent 09f7e59 commit 98d6e93

File tree

1 file changed

+14
-43
lines changed

1 file changed

+14
-43
lines changed

tests/commands/test_bump_command.py

Lines changed: 14 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import errno
2-
import os
3-
import shutil
4-
import stat
51
import sys
62
import uuid
73
from pathlib import Path
@@ -12,32 +8,15 @@
128
from commitizen import cli, cmd, git
139

1410

15-
class ReadOnlyException(Exception):
16-
pass
17-
18-
19-
# https://stackoverflow.com/questions/1213706/what-user-do-python-scripts-run-as-in-windows
20-
def handle_remove_read_only(func, path, exc):
21-
excvalue = exc[1]
22-
if func in (os.rmdir, os.remove, shutil.rmtree) and excvalue.errno == errno.EACCESS:
23-
os.chmod(path, stat.S_IRWXU | stat.S_IRWXG | stat.IRWXO) # 744
24-
func(path)
25-
else:
26-
raise ReadOnlyException
27-
11+
@pytest.fixture(scope="function")
12+
def tmp_git_project(tmpdir):
13+
with tmpdir.as_cwd():
14+
with open("pyproject.toml", "w") as f:
15+
f.write("[tool.commitizen]\n" 'version="0.1.0"')
2816

29-
@pytest.fixture
30-
def create_project():
31-
current_directory = os.getcwd()
32-
tmp_proj_path = "tests/tmp-proj"
33-
full_tmp_path = os.path.join(current_directory, tmp_proj_path)
34-
if not os.path.exists(full_tmp_path):
35-
os.makedirs(full_tmp_path)
17+
cmd.run("git init")
3618

37-
os.chdir(full_tmp_path)
38-
yield
39-
os.chdir(current_directory)
40-
shutil.rmtree(full_tmp_path, handle_remove_read_only)
19+
yield
4120

4221

4322
def create_file_and_commit(message: str, filename: Optional[str] = None):
@@ -49,12 +28,8 @@ def create_file_and_commit(message: str, filename: Optional[str] = None):
4928
git.commit(message)
5029

5130

52-
def test_bump_command(mocker, create_project):
53-
with open("./pyproject.toml", "w") as f:
54-
f.write("[tool.commitizen]\n" 'version="0.1.0"')
55-
56-
cmd.run("git init")
57-
31+
@pytest.mark.usefixtures("tmp_git_project")
32+
def test_bump_command(mocker):
5833
# MINOR
5934
create_file_and_commit("feat: new file")
6035

@@ -143,17 +118,13 @@ def test_bump_is_not_specify(mocker, capsys, tmpdir):
143118
assert expected_error_message in err
144119

145120

146-
def test_bump_when_not_new_commit(mocker, capsys, tmpdir):
147-
with tmpdir.as_cwd():
148-
with open("./pyproject.toml", "w") as f:
149-
f.write("[tool.commitizen]\n" 'version="0.1.0"')
150-
cmd.run("git init")
121+
def test_bump_when_not_new_commit(mocker, capsys, tmp_git_project):
122+
testargs = ["cz", "bump", "--yes"]
123+
mocker.patch.object(sys, "argv", testargs)
151124

152-
testargs = ["cz", "bump", "--yes"]
153-
mocker.patch.object(sys, "argv", testargs)
125+
with pytest.raises(SystemExit):
126+
cli.main()
154127

155-
with pytest.raises(SystemExit):
156-
cli.main()
157128
expected_error_message = "[NO_COMMITS_FOUND]\n" "No new commits found."
158129
_, err = capsys.readouterr()
159130
assert expected_error_message in err

0 commit comments

Comments
 (0)