4141 UnsafeProtocolError ,
4242)
4343from 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
4545from test .lib import TestBase , fixture , with_rw_directory , with_rw_repo
4646
4747
@@ -71,15 +71,30 @@ def tearDown(self):
7171 for lfp in glob .glob (_tc_lock_fpaths ):
7272 if osp .isfile (lfp ):
7373 raise AssertionError ("Previous TC left hanging git-lock file: {}" .format (lfp ))
74+
7475 import gc
7576
7677 gc .collect ()
7778
7879 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 )
8093
8194 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 )
8398
8499 @with_rw_repo ("0.3.2.1" )
85100 def test_repo_creation_from_different_paths (self , rw_repo ):
@@ -118,7 +133,7 @@ def test_tree_from_revision(self):
118133 self .assertEqual (tree .type , "tree" )
119134 self .assertEqual (self .rorepo .tree (tree ), tree )
120135
121- # try from invalid revision that does not exist
136+ # Try from an invalid revision that does not exist.
122137 self .assertRaises (BadName , self .rorepo .tree , "hello world" )
123138
124139 def test_pickleable (self ):
@@ -507,13 +522,11 @@ def write(self, b):
507522 repo .git .log (n = 100 , output_stream = TestOutputStream (io .DEFAULT_BUFFER_SIZE ))
508523
509524 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
517530 for path in (git_dir_rela , git_dir_abs ):
518531 r = Repo .init (path = path , bare = True )
519532 self .assertIsInstance (r , Repo )
@@ -523,7 +536,7 @@ def test_init(self):
523536
524537 self ._assert_empty_repo (r )
525538
526- # test clone
539+ # Test clone
527540 clone_path = path + "_clone"
528541 rc = r .clone (clone_path )
529542 self ._assert_empty_repo (rc )
@@ -558,13 +571,6 @@ def test_init(self):
558571 assert not r .has_separate_working_tree ()
559572
560573 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
568574
569575 def test_bare_property (self ):
570576 self .rorepo .bare
0 commit comments