4
4
# This module is part of GitPython and is released under
5
5
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
6
6
7
- from git .test .lib import (
8
- TestCase ,
9
- fixture_path ,
10
- assert_equal ,
11
- )
12
- from git .test .lib import with_rw_directory
7
+ import glob
8
+ import io
9
+ import os
10
+
13
11
from git import (
14
12
GitConfigParser
15
13
)
16
14
from git .compat import (
17
15
string_types ,
18
- )
19
- import io
20
- import os
16
+ is_win ,)
21
17
from git .config import cp
18
+ from git .test .lib import (
19
+ TestCase ,
20
+ fixture_path ,
21
+ )
22
+ from git .test .lib import with_rw_directory
23
+
24
+ import os .path as osp
25
+
26
+
27
+ _tc_lock_fpaths = osp .join (osp .dirname (__file__ ), 'fixtures/*.lock' )
28
+
29
+
30
+ def _rm_lock_files ():
31
+ for lfp in glob .glob (_tc_lock_fpaths ):
32
+ if is_win and osp .isfile (lfp ):
33
+ os .chmod (lfp , 0o777 )
34
+ os .remove (lfp )
22
35
23
36
24
37
class TestBase (TestCase ):
38
+ def setUp (self ):
39
+ _rm_lock_files ()
40
+
41
+ def tearDown (self ):
42
+ for lfp in glob .glob (_tc_lock_fpaths ):
43
+ if osp .isfile (lfp ):
44
+ raise AssertionError ('Previous TC left hanging git-lock file: %s' , lfp )
25
45
26
46
def _to_memcache (self , file_path ):
27
- fp = open (file_path , "rb" )
28
- sio = io .BytesIO (fp .read ())
47
+ with open (file_path , "rb" ) as fp :
48
+ sio = io .BytesIO (fp .read ())
29
49
sio .name = file_path
30
50
return sio
31
51
32
52
def test_read_write (self ):
33
53
# writer must create the exact same file as the one read before
34
54
for filename in ("git_config" , "git_config_global" ):
35
55
file_obj = self ._to_memcache (fixture_path (filename ))
36
- w_config = GitConfigParser (file_obj , read_only = False )
37
- w_config .read () # enforce reading
38
- assert w_config ._sections
39
- w_config .write () # enforce writing
40
-
41
- # we stripped lines when reading, so the results differ
42
- assert file_obj .getvalue ()
43
- self .assertEqual (file_obj .getvalue (), self ._to_memcache (fixture_path (filename )).getvalue ())
44
-
45
- # creating an additional config writer must fail due to exclusive access
46
- self .failUnlessRaises (IOError , GitConfigParser , file_obj , read_only = False )
47
-
48
- # should still have a lock and be able to make changes
49
- assert w_config ._lock ._has_lock ()
50
-
51
- # changes should be written right away
52
- sname = "my_section"
53
- oname = "mykey"
54
- val = "myvalue"
55
- w_config .add_section (sname )
56
- assert w_config .has_section (sname )
57
- w_config .set (sname , oname , val )
58
- assert w_config .has_option (sname , oname )
59
- assert w_config .get (sname , oname ) == val
60
-
61
- sname_new = "new_section"
62
- oname_new = "new_key"
63
- ival = 10
64
- w_config .set_value (sname_new , oname_new , ival )
65
- assert w_config .get_value (sname_new , oname_new ) == ival
66
-
67
- file_obj .seek (0 )
68
- r_config = GitConfigParser (file_obj , read_only = True )
69
- assert r_config .has_section (sname )
70
- assert r_config .has_option (sname , oname )
71
- assert r_config .get (sname , oname ) == val
72
- w_config .release ()
56
+ with GitConfigParser (file_obj , read_only = False ) as w_config :
57
+ w_config .read () # enforce reading
58
+ assert w_config ._sections
59
+ w_config .write () # enforce writing
60
+
61
+ # we stripped lines when reading, so the results differ
62
+ assert file_obj .getvalue ()
63
+ self .assertEqual (file_obj .getvalue (), self ._to_memcache (fixture_path (filename )).getvalue ())
64
+
65
+ # creating an additional config writer must fail due to exclusive access
66
+ self .failUnlessRaises (IOError , GitConfigParser , file_obj , read_only = False )
67
+
68
+ # should still have a lock and be able to make changes
69
+ assert w_config ._lock ._has_lock ()
70
+
71
+ # changes should be written right away
72
+ sname = "my_section"
73
+ oname = "mykey"
74
+ val = "myvalue"
75
+ w_config .add_section (sname )
76
+ assert w_config .has_section (sname )
77
+ w_config .set (sname , oname , val )
78
+ assert w_config .has_option (sname , oname )
79
+ assert w_config .get (sname , oname ) == val
80
+
81
+ sname_new = "new_section"
82
+ oname_new = "new_key"
83
+ ival = 10
84
+ w_config .set_value (sname_new , oname_new , ival )
85
+ assert w_config .get_value (sname_new , oname_new ) == ival
86
+
87
+ file_obj .seek (0 )
88
+ r_config = GitConfigParser (file_obj , read_only = True )
89
+ assert r_config .has_section (sname )
90
+ assert r_config .has_option (sname , oname )
91
+ assert r_config .get (sname , oname ) == val
73
92
# END for each filename
74
93
75
94
@with_rw_directory
76
95
def test_lock_reentry (self , rw_dir ):
77
96
fpl = os .path .join (rw_dir , 'l' )
78
- gcp = GitConfigParser (fpl , read_only = False )
79
- with gcp as cw :
80
- cw .set_value ('include' , 'some_value' , 'a' )
97
+ with GitConfigParser (fpl , read_only = False ) as gcp :
98
+ gcp .set_value ('include' , 'some_value' , 'a' )
81
99
# entering again locks the file again...
82
100
with gcp as cw :
83
101
cw .set_value ('include' , 'some_other_value' , 'b' )
@@ -91,21 +109,21 @@ def test_lock_reentry(self, rw_dir):
91
109
92
110
def test_multi_line_config (self ):
93
111
file_obj = self ._to_memcache (fixture_path ("git_config_with_comments" ))
94
- config = GitConfigParser (file_obj , read_only = False )
95
- ev = "ruby -e '\n "
96
- ev += " system %(git), %(merge-file), %(--marker-size=%L), %(%A), %(%O), %(%B)\n "
97
- ev += " b = File.read(%(%A))\n "
98
- ev += " b.sub!(/^<+ .*\\ nActiveRecord::Schema\\ .define.:version => (\\ d+). do\\ n=+\\ nActiveRecord::Schema\\ ."
99
- ev += "define.:version => (\\ d+). do\\ n>+ .*/) do\n "
100
- ev += " %(ActiveRecord::Schema.define(:version => #{[$1, $2].max}) do)\n "
101
- ev += " end\n "
102
- ev += " File.open(%(%A), %(w)) {|f| f.write(b)}\n "
103
- ev += " exit 1 if b.include?(%(<)*%L)'"
104
- assert_equal (config .get ('merge "railsschema"' , 'driver' ), ev )
105
- assert_equal (config .get ('alias' , 'lg' ),
106
- "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset'"
107
- " --abbrev-commit --date=relative" )
108
- assert len (config .sections ()) == 23
112
+ with GitConfigParser (file_obj , read_only = False ) as config :
113
+ ev = "ruby -e '\n "
114
+ ev += " system %(git), %(merge-file), %(--marker-size=%L), %(%A), %(%O), %(%B)\n "
115
+ ev += " b = File.read(%(%A))\n "
116
+ ev += " b.sub!(/^<+ .*\\ nActiveRecord::Schema\\ .define.:version => (\\ d+). do\\ n=+\\ nActiveRecord::Schema\\ ." # noqa E501
117
+ ev += "define.:version => (\\ d+). do\\ n>+ .*/) do\n "
118
+ ev += " %(ActiveRecord::Schema.define(:version => #{[$1, $2].max}) do)\n "
119
+ ev += " end\n "
120
+ ev += " File.open(%(%A), %(w)) {|f| f.write(b)}\n "
121
+ ev += " exit 1 if b.include?(%(<)*%L)'"
122
+ self . assertEqual (config .get ('merge "railsschema"' , 'driver' ), ev )
123
+ self . assertEqual (config .get ('alias' , 'lg' ),
124
+ "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset'"
125
+ " --abbrev-commit --date=relative" )
126
+ self . assertEqual ( len (config .sections ()), 23 )
109
127
110
128
def test_base (self ):
111
129
path_repo = fixture_path ("git_config" )
@@ -202,22 +220,19 @@ def check_test_value(cr, value):
202
220
203
221
def test_rename (self ):
204
222
file_obj = self ._to_memcache (fixture_path ('git_config' ))
205
- cw = GitConfigParser (file_obj , read_only = False , merge_includes = False )
206
-
207
- self .failUnlessRaises (ValueError , cw .rename_section , "doesntexist" , "foo" )
208
- self .failUnlessRaises (ValueError , cw .rename_section , "core" , "include" )
223
+ with GitConfigParser (file_obj , read_only = False , merge_includes = False ) as cw :
224
+ self .failUnlessRaises (ValueError , cw .rename_section , "doesntexist" , "foo" )
225
+ self .failUnlessRaises (ValueError , cw .rename_section , "core" , "include" )
209
226
210
- nn = "bee"
211
- assert cw .rename_section ('core' , nn ) is cw
212
- assert not cw .has_section ('core' )
213
- assert len (cw .items (nn )) == 4
214
- cw .release ()
227
+ nn = "bee"
228
+ assert cw .rename_section ('core' , nn ) is cw
229
+ assert not cw .has_section ('core' )
230
+ assert len (cw .items (nn )) == 4
215
231
216
232
def test_complex_aliases (self ):
217
233
file_obj = self ._to_memcache (fixture_path ('.gitconfig' ))
218
- w_config = GitConfigParser (file_obj , read_only = False )
219
- self .assertEqual (w_config .get ('alias' , 'rbi' ), '"!g() { git rebase -i origin/${1:-master} ; } ; g"' )
220
- w_config .release ()
234
+ with GitConfigParser (file_obj , read_only = False ) as w_config :
235
+ self .assertEqual (w_config .get ('alias' , 'rbi' ), '"!g() { git rebase -i origin/${1:-master} ; } ; g"' )
221
236
self .assertEqual (file_obj .getvalue (), self ._to_memcache (fixture_path ('.gitconfig' )).getvalue ())
222
237
223
238
def test_empty_config_value (self ):
0 commit comments