|
6 | 6 |
|
7 | 7 | importglob
|
8 | 8 | importio
|
| 9 | +importos |
| 10 | +fromunittestimportmock |
9 | 11 |
|
10 | 12 | fromgitimport (
|
11 | 13 | GitConfigParser
|
@@ -238,6 +240,128 @@ def check_test_value(cr, value):
|
238 | 240 | withGitConfigParser(fpa,read_only=True)ascr:
|
239 | 241 | check_test_value(cr,tv)
|
240 | 242 |
|
| 243 | +@with_rw_directory |
| 244 | +deftest_conditional_includes_from_git_dir(self,rw_dir): |
| 245 | +# Initiate repository path |
| 246 | +git_dir=osp.join(rw_dir,"target1","repo1") |
| 247 | +os.makedirs(git_dir) |
| 248 | + |
| 249 | +# Initiate mocked repository |
| 250 | +repo=mock.Mock(git_dir=git_dir) |
| 251 | + |
| 252 | +# Initiate config files. |
| 253 | +path1=osp.join(rw_dir,"config1") |
| 254 | +path2=osp.join(rw_dir,"config2") |
| 255 | +template="[includeIf\"{}:{}\"]\n path={}\n" |
| 256 | + |
| 257 | +withopen(path1,"w")asstream: |
| 258 | +stream.write(template.format("gitdir",git_dir,path2)) |
| 259 | + |
| 260 | +# Ensure that config is ignored if no repo is set. |
| 261 | +withGitConfigParser(path1)asconfig: |
| 262 | +assertnotconfig._has_includes() |
| 263 | +assertconfig._included_paths()== [] |
| 264 | + |
| 265 | +# Ensure that config is included if path is matching git_dir. |
| 266 | +withGitConfigParser(path1,repo=repo)asconfig: |
| 267 | +assertconfig._has_includes() |
| 268 | +assertconfig._included_paths()== [("path",path2)] |
| 269 | + |
| 270 | +# Ensure that config is ignored if case is incorrect. |
| 271 | +withopen(path1,"w")asstream: |
| 272 | +stream.write(template.format("gitdir",git_dir.upper(),path2)) |
| 273 | + |
| 274 | +withGitConfigParser(path1,repo=repo)asconfig: |
| 275 | +assertnotconfig._has_includes() |
| 276 | +assertconfig._included_paths()== [] |
| 277 | + |
| 278 | +# Ensure that config is included if case is ignored. |
| 279 | +withopen(path1,"w")asstream: |
| 280 | +stream.write(template.format("gitdir/i",git_dir.upper(),path2)) |
| 281 | + |
| 282 | +withGitConfigParser(path1,repo=repo)asconfig: |
| 283 | +assertconfig._has_includes() |
| 284 | +assertconfig._included_paths()== [("path",path2)] |
| 285 | + |
| 286 | +# Ensure that config is included with path using glob pattern. |
| 287 | +withopen(path1,"w")asstream: |
| 288 | +stream.write(template.format("gitdir","**/repo1",path2)) |
| 289 | + |
| 290 | +withGitConfigParser(path1,repo=repo)asconfig: |
| 291 | +assertconfig._has_includes() |
| 292 | +assertconfig._included_paths()== [("path",path2)] |
| 293 | + |
| 294 | +# Ensure that config is ignored if path is not matching git_dir. |
| 295 | +withopen(path1,"w")asstream: |
| 296 | +stream.write(template.format("gitdir","incorrect",path2)) |
| 297 | + |
| 298 | +withGitConfigParser(path1,repo=repo)asconfig: |
| 299 | +assertnotconfig._has_includes() |
| 300 | +assertconfig._included_paths()== [] |
| 301 | + |
| 302 | +# Ensure that config is included if path in hierarchy. |
| 303 | +withopen(path1,"w")asstream: |
| 304 | +stream.write(template.format("gitdir","target1/",path2)) |
| 305 | + |
| 306 | +withGitConfigParser(path1,repo=repo)asconfig: |
| 307 | +assertconfig._has_includes() |
| 308 | +assertconfig._included_paths()== [("path",path2)] |
| 309 | + |
| 310 | +@with_rw_directory |
| 311 | +deftest_conditional_includes_from_branch_name(self,rw_dir): |
| 312 | +# Initiate mocked branch |
| 313 | +branch=mock.Mock() |
| 314 | + type(branch).name=mock.PropertyMock(return_value="/foo/branch") |
| 315 | + |
| 316 | +# Initiate mocked repository |
| 317 | +repo=mock.Mock(active_branch=branch) |
| 318 | + |
| 319 | +# Initiate config files. |
| 320 | +path1=osp.join(rw_dir,"config1") |
| 321 | +path2=osp.join(rw_dir,"config2") |
| 322 | +template="[includeIf\"onbranch:{}\"]\n path={}\n" |
| 323 | + |
| 324 | +# Ensure that config is included is branch is correct. |
| 325 | +withopen(path1,"w")asstream: |
| 326 | +stream.write(template.format("/foo/branch",path2)) |
| 327 | + |
| 328 | +withGitConfigParser(path1,repo=repo)asconfig: |
| 329 | +assertconfig._has_includes() |
| 330 | +assertconfig._included_paths()== [("path",path2)] |
| 331 | + |
| 332 | +# Ensure that config is included is branch is incorrect. |
| 333 | +withopen(path1,"w")asstream: |
| 334 | +stream.write(template.format("incorrect",path2)) |
| 335 | + |
| 336 | +withGitConfigParser(path1,repo=repo)asconfig: |
| 337 | +assertnotconfig._has_includes() |
| 338 | +assertconfig._included_paths()== [] |
| 339 | + |
| 340 | +# Ensure that config is included with branch using glob pattern. |
| 341 | +withopen(path1,"w")asstream: |
| 342 | +stream.write(template.format("/foo/**",path2)) |
| 343 | + |
| 344 | +withGitConfigParser(path1,repo=repo)asconfig: |
| 345 | +assertconfig._has_includes() |
| 346 | +assertconfig._included_paths()== [("path",path2)] |
| 347 | + |
| 348 | +@with_rw_directory |
| 349 | +deftest_conditional_includes_from_branch_name_error(self,rw_dir): |
| 350 | +# Initiate mocked repository to raise an error if HEAD is detached. |
| 351 | +repo=mock.Mock() |
| 352 | + type(repo).active_branch=mock.PropertyMock(side_effect=TypeError) |
| 353 | + |
| 354 | +# Initiate config file. |
| 355 | +path1=osp.join(rw_dir,"config1") |
| 356 | + |
| 357 | +# Ensure that config is ignored when active branch cannot be found. |
| 358 | +withopen(path1,"w")asstream: |
| 359 | +stream.write("[includeIf\"onbranch:foo\"]\n path=/path\n") |
| 360 | + |
| 361 | +withGitConfigParser(path1,repo=repo)asconfig: |
| 362 | +assertnotconfig._has_includes() |
| 363 | +assertconfig._included_paths()== [] |
| 364 | + |
241 | 365 | deftest_rename(self):
|
242 | 366 | file_obj=self._to_memcache(fixture_path('git_config'))
|
243 | 367 | withGitConfigParser(file_obj,read_only=False,merge_includes=False)ascw:
|
|