Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2b69bac

Browse files
authored
Merge pull request#1759 from EliahKagan/subdir
Avoid brittle assumptions about preexisting temporary files in tests
2 parentsd7bf231 +c09ac1a commit2b69bac

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

‎test/test_repo.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
UnsafeProtocolError,
4242
)
4343
fromgit.repo.funimporttouch
44-
fromgit.utilimportbin_to_hex,cygpath,join_path_native,rmfile,rmtree
44+
fromgit.utilimportbin_to_hex,cwd,cygpath,join_path_native,rmfile,rmtree
4545
fromtest.libimportTestBase,fixture,with_rw_directory,with_rw_repo
4646

4747

@@ -71,15 +71,30 @@ def tearDown(self):
7171
forlfpinglob.glob(_tc_lock_fpaths):
7272
ifosp.isfile(lfp):
7373
raiseAssertionError("Previous TC left hanging git-lock file: {}".format(lfp))
74+
7475
importgc
7576

7677
gc.collect()
7778

7879
deftest_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+
withtempfile.TemporaryDirectory()astdir:
84+
self.assertRaises(InvalidGitRepositoryError,Repo,tdir)
85+
86+
@with_rw_directory
87+
deftest_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
deftest_new_should_raise_on_non_existent_path(self):
82-
self.assertRaises(NoSuchPathError,Repo,"repos/foobar")
95+
withtempfile.TemporaryDirectory()astdir:
96+
nonexistent=osp.join(tdir,"foobar")
97+
self.assertRaises(NoSuchPathError,Repo,nonexistent)
8398

8499
@with_rw_repo("0.3.2.1")
85100
deftest_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 fromaninvalid revision that does not exist.
122137
self.assertRaises(BadName,self.rorepo.tree,"hello world")
123138

124139
deftest_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
deftest_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+
withtempfile.TemporaryDirectory()astdir,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
forpathin (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
assertnotr.has_separate_working_tree()
559572

560573
self._assert_empty_repo(r)
561-
finally:
562-
try:
563-
rmtree(del_dir_abs)
564-
exceptOSError:
565-
pass
566-
os.chdir(prev_cwd)
567-
# END restore previous state
568574

569575
deftest_bare_property(self):
570576
self.rorepo.bare

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp