1
- import errno
2
- import os
3
- import shutil
4
- import stat
5
1
import sys
6
2
import uuid
7
3
from pathlib import Path
12
8
from commitizen import cli , cmd , git
13
9
14
10
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"' )
28
16
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" )
36
18
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
41
20
42
21
43
22
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):
49
28
git .commit (message )
50
29
51
30
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 ):
58
33
# MINOR
59
34
create_file_and_commit ("feat: new file" )
60
35
@@ -143,17 +118,13 @@ def test_bump_is_not_specify(mocker, capsys, tmpdir):
143
118
assert expected_error_message in err
144
119
145
120
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 )
151
124
152
- testargs = [ "cz" , "bump" , "--yes" ]
153
- mocker . patch . object ( sys , "argv" , testargs )
125
+ with pytest . raises ( SystemExit ):
126
+ cli . main ( )
154
127
155
- with pytest .raises (SystemExit ):
156
- cli .main ()
157
128
expected_error_message = "[NO_COMMITS_FOUND]\n " "No new commits found."
158
129
_ , err = capsys .readouterr ()
159
130
assert expected_error_message in err
0 commit comments