41
41
UnsafeProtocolError ,
42
42
)
43
43
from git .repo .fun import touch
44
- from git .util import bin_to_hex , cygpath , join_path_native , rmfile , rmtree
44
+ from git .util import bin_to_hex , cwd , cygpath , join_path_native , rmfile , rmtree
45
45
from test .lib import TestBase , fixture , with_rw_directory , with_rw_repo
46
46
47
47
@@ -71,15 +71,30 @@ def tearDown(self):
71
71
for lfp in glob .glob (_tc_lock_fpaths ):
72
72
if osp .isfile (lfp ):
73
73
raise AssertionError ("Previous TC left hanging git-lock file: {}" .format (lfp ))
74
+
74
75
import gc
75
76
76
77
gc .collect ()
77
78
78
79
def test_new_should_raise_on_invalid_repo_location (self ):
79
- self .assertRaises (InvalidGitRepositoryError , Repo , tempfile .gettempdir ())
80
+ # Ideally this tests a directory that is outside of any repository. In the rare
81
+ # case tempfile.gettempdir() is inside a repo, this still passes, but tests the
82
+ # same scenario as test_new_should_raise_on_invalid_repo_location_within_repo.
83
+ with tempfile .TemporaryDirectory () as tdir :
84
+ self .assertRaises (InvalidGitRepositoryError , Repo , tdir )
85
+
86
+ @with_rw_directory
87
+ def test_new_should_raise_on_invalid_repo_location_within_repo (self , rw_dir ):
88
+ repo_dir = osp .join (rw_dir , "repo" )
89
+ Repo .init (repo_dir )
90
+ subdir = osp .join (repo_dir , "subdir" )
91
+ os .mkdir (subdir )
92
+ self .assertRaises (InvalidGitRepositoryError , Repo , subdir )
80
93
81
94
def test_new_should_raise_on_non_existent_path (self ):
82
- self .assertRaises (NoSuchPathError , Repo , "repos/foobar" )
95
+ with tempfile .TemporaryDirectory () as tdir :
96
+ nonexistent = osp .join (tdir , "foobar" )
97
+ self .assertRaises (NoSuchPathError , Repo , nonexistent )
83
98
84
99
@with_rw_repo ("0.3.2.1" )
85
100
def test_repo_creation_from_different_paths (self , rw_repo ):
@@ -118,7 +133,7 @@ def test_tree_from_revision(self):
118
133
self .assertEqual (tree .type , "tree" )
119
134
self .assertEqual (self .rorepo .tree (tree ), tree )
120
135
121
- # try from invalid revision that does not exist
136
+ # Try from an invalid revision that does not exist.
122
137
self .assertRaises (BadName , self .rorepo .tree , "hello world" )
123
138
124
139
def test_pickleable (self ):
@@ -507,13 +522,11 @@ def write(self, b):
507
522
repo .git .log (n = 100 , output_stream = TestOutputStream (io .DEFAULT_BUFFER_SIZE ))
508
523
509
524
def test_init (self ):
510
- prev_cwd = os .getcwd ()
511
- os .chdir (tempfile .gettempdir ())
512
- git_dir_rela = "repos/foo/bar.git"
513
- del_dir_abs = osp .abspath ("repos" )
514
- git_dir_abs = osp .abspath (git_dir_rela )
515
- try :
516
- # with specific path
525
+ with tempfile .TemporaryDirectory () as tdir , cwd (tdir ):
526
+ git_dir_rela = "repos/foo/bar.git"
527
+ git_dir_abs = osp .abspath (git_dir_rela )
528
+
529
+ # With specific path
517
530
for path in (git_dir_rela , git_dir_abs ):
518
531
r = Repo .init (path = path , bare = True )
519
532
self .assertIsInstance (r , Repo )
@@ -523,7 +536,7 @@ def test_init(self):
523
536
524
537
self ._assert_empty_repo (r )
525
538
526
- # test clone
539
+ # Test clone
527
540
clone_path = path + "_clone"
528
541
rc = r .clone (clone_path )
529
542
self ._assert_empty_repo (rc )
@@ -558,13 +571,6 @@ def test_init(self):
558
571
assert not r .has_separate_working_tree ()
559
572
560
573
self ._assert_empty_repo (r )
561
- finally :
562
- try :
563
- rmtree (del_dir_abs )
564
- except OSError :
565
- pass
566
- os .chdir (prev_cwd )
567
- # END restore previous state
568
574
569
575
def test_bare_property (self ):
570
576
self .rorepo .bare
0 commit comments