Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork966
Expand file tree
/
Copy pathtest_config.py
More file actions
251 lines (209 loc) · 10 KB
/
test_config.py
File metadata and controls
251 lines (209 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# test_config.py
# Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
#
# This module is part of GitPython and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
importglob
importio
fromgitimport (
GitConfigParser
)
fromgit.compatimportstring_types
fromgit.configimportcp
fromgit.test.libimport (
TestCase,
fixture_path,
)
fromgit.test.libimportwith_rw_directory
importos.pathasosp
fromgit.utilimportrmfile
_tc_lock_fpaths=osp.join(osp.dirname(__file__),'fixtures/*.lock')
def_rm_lock_files():
forlfpinglob.glob(_tc_lock_fpaths):
rmfile(lfp)
classTestBase(TestCase):
defsetUp(self):
_rm_lock_files()
deftearDown(self):
forlfpinglob.glob(_tc_lock_fpaths):
ifosp.isfile(lfp):
raiseAssertionError('Previous TC left hanging git-lock file: %s',lfp)
def_to_memcache(self,file_path):
withopen(file_path,"rb")asfp:
sio=io.BytesIO(fp.read())
sio.name=file_path
returnsio
deftest_read_write(self):
# writer must create the exact same file as the one read before
forfilenamein ("git_config","git_config_global"):
file_obj=self._to_memcache(fixture_path(filename))
withGitConfigParser(file_obj,read_only=False)asw_config:
w_config.read()# enforce reading
assertw_config._sections
w_config.write()# enforce writing
# we stripped lines when reading, so the results differ
assertfile_obj.getvalue()
self.assertEqual(file_obj.getvalue(),self._to_memcache(fixture_path(filename)).getvalue())
# creating an additional config writer must fail due to exclusive access
withself.assertRaises(IOError):
GitConfigParser(file_obj,read_only=False)
# should still have a lock and be able to make changes
assertw_config._lock._has_lock()
# changes should be written right away
sname="my_section"
oname="mykey"
val="myvalue"
w_config.add_section(sname)
assertw_config.has_section(sname)
w_config.set(sname,oname,val)
assertw_config.has_option(sname,oname)
assertw_config.get(sname,oname)==val
sname_new="new_section"
oname_new="new_key"
ival=10
w_config.set_value(sname_new,oname_new,ival)
assertw_config.get_value(sname_new,oname_new)==ival
file_obj.seek(0)
r_config=GitConfigParser(file_obj,read_only=True)
assertr_config.has_section(sname)
assertr_config.has_option(sname,oname)
assertr_config.get(sname,oname)==val
# END for each filename
@with_rw_directory
deftest_lock_reentry(self,rw_dir):
fpl=osp.join(rw_dir,'l')
gcp=GitConfigParser(fpl,read_only=False)
withgcpascw:
cw.set_value('include','some_value','a')
# entering again locks the file again...
withgcpascw:
cw.set_value('include','some_other_value','b')
# ...so creating an additional config writer must fail due to exclusive access
withself.assertRaises(IOError):
GitConfigParser(fpl,read_only=False)
# but work when the lock is removed
withGitConfigParser(fpl,read_only=False):
assertosp.exists(fpl)
# reentering with an existing lock must fail due to exclusive access
withself.assertRaises(IOError):
gcp.__enter__()
deftest_multi_line_config(self):
file_obj=self._to_memcache(fixture_path("git_config_with_comments"))
withGitConfigParser(file_obj,read_only=False)asconfig:
ev="ruby -e '\n"
ev+="system %(git), %(merge-file), %(--marker-size=%L), %(%A), %(%O), %(%B)\n"
ev+="b = File.read(%(%A))\n"
ev+="b.sub!(/^<+ .*\\nActiveRecord::Schema\\.define.:version => (\\d+). do\\n=+\\nActiveRecord::Schema\\."# noqa E501
ev+="define.:version => (\\d+). do\\n>+ .*/) do\n"
ev+=" %(ActiveRecord::Schema.define(:version => #{[$1, $2].max}) do)\n"
ev+="end\n"
ev+="File.open(%(%A), %(w)) {|f| f.write(b)}\n"
ev+="exit 1 if b.include?(%(<)*%L)'"
self.assertEqual(config.get('merge "railsschema"','driver'),ev)
self.assertEqual(config.get('alias','lg'),
"log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset'"
" --abbrev-commit --date=relative")
self.assertEqual(len(config.sections()),23)
deftest_base(self):
path_repo=fixture_path("git_config")
path_global=fixture_path("git_config_global")
r_config=GitConfigParser([path_repo,path_global],read_only=True)
assertr_config.read_only
num_sections=0
num_options=0
# test reader methods
assertr_config._is_initializedisFalse
forsectioninr_config.sections():
num_sections+=1
foroptioninr_config.options(section):
num_options+=1
val=r_config.get(section,option)
val_typed=r_config.get_value(section,option)
assertisinstance(val_typed, (bool,int,float, )+string_types)
assertval
assert"\n"notinoption
assert"\n"notinval
# writing must fail
withself.assertRaises(IOError):
r_config.set(section,option,None)
withself.assertRaises(IOError):
r_config.remove_option(section,option)
# END for each option
withself.assertRaises(IOError):
r_config.remove_section(section)
# END for each section
assertnum_sectionsandnum_options
assertr_config._is_initializedisTrue
# get value which doesnt exist, with default
default="my default value"
assertr_config.get_value("doesnt","exist",default)==default
# it raises if there is no default though
withself.assertRaises(cp.NoSectionError):
r_config.get_value("doesnt","exist")
@with_rw_directory
deftest_config_include(self,rw_dir):
defwrite_test_value(cw,value):
cw.set_value(value,'value',value)
# end
defcheck_test_value(cr,value):
assertcr.get_value(value,'value')==value
# end
# PREPARE CONFIG FILE A
fpa=osp.join(rw_dir,'a')
withGitConfigParser(fpa,read_only=False)ascw:
write_test_value(cw,'a')
fpb=osp.join(rw_dir,'b')
fpc=osp.join(rw_dir,'c')
cw.set_value('include','relative_path_b','b')
cw.set_value('include','doesntexist','foobar')
cw.set_value('include','relative_cycle_a_a','a')
cw.set_value('include','absolute_cycle_a_a',fpa)
assertosp.exists(fpa)
# PREPARE CONFIG FILE B
withGitConfigParser(fpb,read_only=False)ascw:
write_test_value(cw,'b')
cw.set_value('include','relative_cycle_b_a','a')
cw.set_value('include','absolute_cycle_b_a',fpa)
cw.set_value('include','relative_path_c','c')
cw.set_value('include','absolute_path_c',fpc)
# PREPARE CONFIG FILE C
withGitConfigParser(fpc,read_only=False)ascw:
write_test_value(cw,'c')
withGitConfigParser(fpa,read_only=True)ascr:
fortvin ('a','b','c'):
check_test_value(cr,tv)
# end for each test to verify
assertlen(cr.items('include'))==8,"Expected all include sections to be merged"
# test writable config writers - assure write-back doesn't involve includes
withGitConfigParser(fpa,read_only=False,merge_includes=True)ascw:
tv='x'
write_test_value(cw,tv)
withGitConfigParser(fpa,read_only=True)ascr:
withself.assertRaises(cp.NoSectionError):
check_test_value(cr,tv)
# But can make it skip includes alltogether, and thus allow write-backs
withGitConfigParser(fpa,read_only=False,merge_includes=False)ascw:
write_test_value(cw,tv)
withGitConfigParser(fpa,read_only=True)ascr:
check_test_value(cr,tv)
deftest_rename(self):
file_obj=self._to_memcache(fixture_path('git_config'))
withGitConfigParser(file_obj,read_only=False,merge_includes=False)ascw:
withself.assertRaises(ValueError):
cw.rename_section("doesntexist","foo")
withself.assertRaises(ValueError):
cw.rename_section("core","include")
nn="bee"
assertcw.rename_section('core',nn)iscw
assertnotcw.has_section('core')
assertlen(cw.items(nn))==4
deftest_complex_aliases(self):
file_obj=self._to_memcache(fixture_path('.gitconfig'))
withGitConfigParser(file_obj,read_only=False)asw_config:
self.assertEqual(w_config.get('alias','rbi'),'"!g() { git rebase -i origin/${1:-master} ; } ; g"')
self.assertEqual(file_obj.getvalue(),self._to_memcache(fixture_path('.gitconfig')).getvalue())
deftest_empty_config_value(self):
cr=GitConfigParser(fixture_path('git_config_with_empty_value'),read_only=True)
assertcr.get_value('core','filemode'),"Should read keys with values"
withself.assertRaises(cp.NoOptionError):
cr.get_value('color','ui')