diff options
author | Kostis Anagnostopoulos <ankostis@gmail.com> | 2016-10-01 18:20:13 +0200 |
---|---|---|
committer | Kostis Anagnostopoulos <ankostis@gmail.com> | 2016-10-01 18:25:57 +0200 |
commit | 9a521681ff8614beb8e2c566cf3c475baca22169 (patch) | |
tree | 77365cb808a255eb53889725bfce775b5090330e /git/test | |
parent | bdf1e68f6bec679edc3feb455596e18c387879c4 (diff) | |
download | gitpython-9a521681ff8614beb8e2c566cf3c475baca22169.tar.gz |
io, #519: ALL open() --> with open()
+ Some cases had restructuring of code.
Diffstat (limited to 'git/test')
-rw-r--r-- | git/test/fixtures/cat_file.py | 7 | ||||
-rw-r--r-- | git/test/lib/helper.py | 8 | ||||
-rw-r--r-- | git/test/test_base.py | 13 | ||||
-rw-r--r-- | git/test/test_commit.py | 6 | ||||
-rw-r--r-- | git/test/test_docs.py | 3 | ||||
-rw-r--r-- | git/test/test_git.py | 14 | ||||
-rw-r--r-- | git/test/test_remote.py | 4 | ||||
-rw-r--r-- | git/test/test_repo.py | 6 |
8 files changed, 32 insertions, 29 deletions
diff --git a/git/test/fixtures/cat_file.py b/git/test/fixtures/cat_file.py index 2f1b915a..5480e628 100644 --- a/git/test/fixtures/cat_file.py +++ b/git/test/fixtures/cat_file.py @@ -1,5 +1,6 @@ import sys -for line in open(sys.argv[1]).readlines(): - sys.stdout.write(line) - sys.stderr.write(line) +with open(sys.argv[1]) as fd: + for line in fd.readlines(): + sys.stdout.write(line) + sys.stderr.write(line) diff --git a/git/test/lib/helper.py b/git/test/lib/helper.py index 90d2b1e9..a85ac2fd 100644 --- a/git/test/lib/helper.py +++ b/git/test/lib/helper.py @@ -39,7 +39,8 @@ def fixture_path(name): def fixture(name): - return open(fixture_path(name), 'rb').read() + with open(fixture_path(name), 'rb') as fd: + return fd.read() def absolute_project_path(): @@ -373,7 +374,6 @@ class TestBase(TestCase): """ repo = repo or self.rorepo abs_path = os.path.join(repo.working_tree_dir, rela_path) - fp = open(abs_path, "w") - fp.write(data) - fp.close() + with open(abs_path, "w") as fp: + fp.write(data) return abs_path diff --git a/git/test/test_base.py b/git/test/test_base.py index fa0bebca..e5e8f173 100644 --- a/git/test/test_base.py +++ b/git/test/test_base.py @@ -77,13 +77,11 @@ class TestBase(TestBase): assert data tmpfilename = tempfile.mktemp(suffix='test-stream') - tmpfile = open(tmpfilename, 'wb+') - assert item == item.stream_data(tmpfile) - tmpfile.seek(0) - assert tmpfile.read() == data - tmpfile.close() + with open(tmpfilename, 'wb+') as tmpfile: + assert item == item.stream_data(tmpfile) + tmpfile.seek(0) + assert tmpfile.read() == data os.remove(tmpfilename) - # END stream to file directly # END for each object type to create # each has a unique sha @@ -133,7 +131,8 @@ class TestBase(TestBase): from nose import SkipTest raise SkipTest("Environment doesn't support unicode filenames") - open(file_path, "wb").write(b'something') + with open(file_path, "wb") as fp: + fp.write(b'something') if is_win: # on windows, there is no way this works, see images on diff --git a/git/test/test_commit.py b/git/test/test_commit.py index 33f8081c..66d988a3 100644 --- a/git/test/test_commit.py +++ b/git/test/test_commit.py @@ -313,14 +313,16 @@ class TestCommit(TestBase): def test_invalid_commit(self): cmt = self.rorepo.commit() - cmt._deserialize(open(fixture_path('commit_invalid_data'), 'rb')) + with open(fixture_path('commit_invalid_data'), 'rb') as fd: + cmt._deserialize(fd) self.assertEqual(cmt.author.name, u'E.Azer Ko�o�o�oculu', cmt.author.name) self.assertEqual(cmt.author.email, 'azer@kodfabrik.com', cmt.author.email) def test_gpgsig(self): cmt = self.rorepo.commit() - cmt._deserialize(open(fixture_path('commit_with_gpgsig'), 'rb')) + with open(fixture_path('commit_with_gpgsig'), 'rb') as fd: + cmt._deserialize(fd) fixture_sig = """-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) diff --git a/git/test/test_docs.py b/git/test/test_docs.py index a6e92543..8a2dff0f 100644 --- a/git/test/test_docs.py +++ b/git/test/test_docs.py @@ -53,7 +53,8 @@ class Tutorials(TestBase): # ![5-test_init_repo_object] # [6-test_init_repo_object] - repo.archive(open(join(rw_dir, 'repo.tar'), 'wb')) + with open(join(rw_dir, 'repo.tar'), 'wb') as fp: + repo.archive(fp) # ![6-test_init_repo_object] # repository paths diff --git a/git/test/test_git.py b/git/test/test_git.py index 8a0242e6..94614cd1 100644 --- a/git/test/test_git.py +++ b/git/test/test_git.py @@ -93,10 +93,9 @@ class TestGit(TestBase): def test_it_accepts_stdin(self): filename = fixture_path("cat_file_blob") - fh = open(filename, 'r') - assert_equal("70c379b63ffa0795fdbfbc128e5a2818397b7ef8", - self.git.hash_object(istream=fh, stdin=True)) - fh.close() + with open(filename, 'r') as fh: + assert_equal("70c379b63ffa0795fdbfbc128e5a2818397b7ef8", + self.git.hash_object(istream=fh, stdin=True)) @patch.object(Git, 'execute') def test_it_ignores_false_kwargs(self, git): @@ -200,10 +199,9 @@ class TestGit(TestBase): self.assertEqual(self.git.environment(), {}) path = os.path.join(rw_dir, 'failing-script.sh') - stream = open(path, 'wt') - stream.write("#!/usr/bin/env sh\n" + - "echo FOO\n") - stream.close() + with open(path, 'wt') as stream: + stream.write("#!/usr/bin/env sh\n" + "echo FOO\n") os.chmod(path, 0o777) rw_repo = Repo.init(os.path.join(rw_dir, 'repo')) diff --git a/git/test/test_remote.py b/git/test/test_remote.py index 05de4ae2..b99e49cf 100644 --- a/git/test/test_remote.py +++ b/git/test/test_remote.py @@ -105,8 +105,8 @@ class TestRemote(TestBase): gc.collect() def _print_fetchhead(self, repo): - fp = open(os.path.join(repo.git_dir, "FETCH_HEAD")) - fp.close() + with open(os.path.join(repo.git_dir, "FETCH_HEAD")): + pass def _do_test_fetch_result(self, results, remote): # self._print_fetchhead(remote.repo) diff --git a/git/test/test_repo.py b/git/test/test_repo.py index a37c9be9..349d955e 100644 --- a/git/test/test_repo.py +++ b/git/test/test_repo.py @@ -781,14 +781,16 @@ class TestRepo(TestBase): real_path_abs = os.path.abspath(join_path_native(rwrepo.working_tree_dir, '.real')) os.rename(rwrepo.git_dir, real_path_abs) git_file_path = join_path_native(rwrepo.working_tree_dir, '.git') - open(git_file_path, 'wb').write(fixture('git_file')) + with open(git_file_path, 'wb') as fp: + fp.write(fixture('git_file')) # Create a repo and make sure it's pointing to the relocated .git directory. git_file_repo = Repo(rwrepo.working_tree_dir) self.assertEqual(os.path.abspath(git_file_repo.git_dir), real_path_abs) # Test using an absolute gitdir path in the .git file. - open(git_file_path, 'wb').write(('gitdir: %s\n' % real_path_abs).encode('ascii')) + with open(git_file_path, 'wb') as fp: + fp.write(('gitdir: %s\n' % real_path_abs).encode('ascii')) git_file_repo = Repo(rwrepo.working_tree_dir) self.assertEqual(os.path.abspath(git_file_repo.git_dir), real_path_abs) |