4
4
#
5
5
# This module is part of GitPython and is released under
6
6
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
7
+ import glob
8
+ from io import BytesIO
9
+ import itertools
10
+ import os
7
11
import pickle
12
+ import sys
13
+ import tempfile
8
14
9
- from git .test .lib import (
10
- patch ,
11
- TestBase ,
12
- with_rw_repo ,
13
- fixture ,
14
- assert_false ,
15
- assert_equal ,
16
- assert_true ,
17
- raises
18
- )
19
15
from git import (
20
16
InvalidGitRepositoryError ,
21
17
Repo ,
33
29
BadName ,
34
30
GitCommandError
35
31
)
36
- from git .repo .fun import touch
37
- from git .util import join_path_native , rmtree
32
+ from git .compat import string_types
38
33
from git .exc import (
39
34
BadObject ,
40
35
)
41
- from gitdb .util import bin_to_hex
42
- from git .compat import string_types
36
+ from git .repo .fun import touch
37
+ from git .test .lib import (
38
+ patch ,
39
+ TestBase ,
40
+ with_rw_repo ,
41
+ fixture ,
42
+ assert_false ,
43
+ assert_equal ,
44
+ assert_true ,
45
+ raises
46
+ )
43
47
from git .test .lib import with_rw_directory
44
-
45
- import os
46
- import sys
47
- import tempfile
48
- import itertools
49
- from io import BytesIO
50
-
48
+ from git .util import join_path_native , rmtree , rmfile
49
+ from gitdb .util import bin_to_hex
51
50
from nose import SkipTest
52
51
52
+ import os .path as osp
53
+
53
54
54
55
def iter_flatten (lol ):
55
56
for items in lol :
@@ -61,9 +62,23 @@ def flatten(lol):
61
62
return list (iter_flatten (lol ))
62
63
63
64
65
+ _tc_lock_fpaths = osp .join (osp .dirname (__file__ ), '../../.git/*.lock' )
66
+
67
+
68
+ def _rm_lock_files ():
69
+ for lfp in glob .glob (_tc_lock_fpaths ):
70
+ rmfile (lfp )
71
+
72
+
64
73
class TestRepo (TestBase ):
65
74
75
+ def setUp (self ):
76
+ _rm_lock_files ()
77
+
66
78
def tearDown (self ):
79
+ for lfp in glob .glob (_tc_lock_fpaths ):
80
+ if osp .isfile (lfp ):
81
+ raise AssertionError ('Previous TC left hanging git-lock file: %s' , lfp )
67
82
import gc
68
83
gc .collect ()
69
84
@@ -309,10 +324,9 @@ def test_tag(self):
309
324
310
325
def test_archive (self ):
311
326
tmpfile = tempfile .mktemp (suffix = 'archive-test' )
312
- stream = open (tmpfile , 'wb' )
313
- self .rorepo .archive (stream , '0.1.6' , path = 'doc' )
314
- assert stream .tell ()
315
- stream .close ()
327
+ with open (tmpfile , 'wb' ) as stream :
328
+ self .rorepo .archive (stream , '0.1.6' , path = 'doc' )
329
+ assert stream .tell ()
316
330
os .remove (tmpfile )
317
331
318
332
@patch .object (Git , '_call_process' )
@@ -401,9 +415,8 @@ def test_untracked_files(self, rwrepo):
401
415
402
416
num_recently_untracked = 0
403
417
for fpath in files :
404
- fd = open (fpath , "wb" )
405
- fd .close ()
406
- # END for each filename
418
+ with open (fpath , "wb" ):
419
+ pass
407
420
untracked_files = rwrepo .untracked_files
408
421
num_recently_untracked = len (untracked_files )
409
422
@@ -426,19 +439,16 @@ def test_config_reader(self):
426
439
def test_config_writer (self ):
427
440
for config_level in self .rorepo .config_level :
428
441
try :
429
- writer = self .rorepo .config_writer (config_level )
430
- assert not writer .read_only
431
- writer .release ()
442
+ with self .rorepo .config_writer (config_level ) as writer :
443
+ self .assertFalse (writer .read_only )
432
444
except IOError :
433
445
# its okay not to get a writer for some configuration files if we
434
446
# have no permissions
435
447
pass
436
- # END for each config level
437
448
438
449
def test_config_level_paths (self ):
439
450
for config_level in self .rorepo .config_level :
440
451
assert self .rorepo ._get_config_path (config_level )
441
- # end for each config level
442
452
443
453
def test_creation_deletion (self ):
444
454
# just a very quick test to assure it generally works. There are
@@ -448,8 +458,8 @@ def test_creation_deletion(self):
448
458
449
459
tag = self .rorepo .create_tag ("new_tag" , "HEAD~2" )
450
460
self .rorepo .delete_tag (tag )
451
- writer = self .rorepo .config_writer ()
452
- writer . release ()
461
+ with self .rorepo .config_writer ():
462
+ pass
453
463
remote = self .rorepo .create_remote ("new_remote" , "git@server:repo.git" )
454
464
self .rorepo .delete_remote (remote )
455
465
0 commit comments