diff options
author | Kostis Anagnostopoulos <ankostis@gmail.com> | 2016-10-02 14:26:15 +0200 |
---|---|---|
committer | Kostis Anagnostopoulos <ankostis@gmail.com> | 2016-10-04 02:11:31 +0200 |
commit | 8a2f7dce43617b773a6be425ea155812396d3856 (patch) | |
tree | 17ad88b4f1e7185b55f53074c39945015bc5611b /git/test | |
parent | a469af892b3e929cbe9d29e414b6fcd59bec246e (diff) | |
download | gitpython-8a2f7dce43617b773a6be425ea155812396d3856.tar.gz |
io: Wrap (probably) allconfig_writers in `with` blocks
Diffstat (limited to 'git/test')
-rw-r--r-- | git/test/lib/helper.py | 20 | ||||
-rw-r--r-- | git/test/test_docs.py | 9 | ||||
-rw-r--r-- | git/test/test_index.py | 7 | ||||
-rw-r--r-- | git/test/test_refs.py | 16 | ||||
-rw-r--r-- | git/test/test_remote.py | 16 | ||||
-rw-r--r-- | git/test/test_submodule.py | 85 |
6 files changed, 70 insertions, 83 deletions
diff --git a/git/test/lib/helper.py b/git/test/lib/helper.py index 3c9374e7..e92ce8b4 100644 --- a/git/test/lib/helper.py +++ b/git/test/lib/helper.py @@ -237,16 +237,13 @@ def with_rw_and_rw_remote_repo(working_tree_ref): rw_remote_repo.daemon_export = True # this thing is just annoying ! - crw = rw_remote_repo.config_writer() - section = "daemon" - try: - crw.add_section(section) - except Exception: - pass - crw.set(section, "receivepack", True) - # release lock - crw.release() - del(crw) + with rw_remote_repo.config_writer() as crw: + section = "daemon" + try: + crw.add_section(section) + except Exception: + pass + crw.set(section, "receivepack", True) # initialize the remote - first do it as local remote and pull, then # we change the url to point to the daemon. The daemon should be started @@ -255,7 +252,8 @@ def with_rw_and_rw_remote_repo(working_tree_ref): d_remote.fetch() remote_repo_url = "git://localhost:%s%s" % (GIT_DAEMON_PORT, remote_repo_dir) - d_remote.config_writer.set('url', remote_repo_url) + with d_remote.config_writer as cw: + cw.set('url', remote_repo_url) temp_dir = osp(_mktemp()) gd = launch_git_daemon(temp_dir, '127.0.0.1', GIT_DAEMON_PORT) diff --git a/git/test/test_docs.py b/git/test/test_docs.py index 5c7ae7f0..e2bfcb21 100644 --- a/git/test/test_docs.py +++ b/git/test/test_docs.py @@ -39,8 +39,8 @@ class Tutorials(TestBase): # [3-test_init_repo_object] repo.config_reader() # get a config reader for read-only access - cw = repo.config_writer() # get a config writer to change configuration - cw.release() # call release() to be sure changes are written and locks are released + with repo.config_writer(): # get a config writer to change configuration + pass # call release() to be sure changes are written and locks are released # ![3-test_init_repo_object] # [4-test_init_repo_object] @@ -398,9 +398,8 @@ class Tutorials(TestBase): # [26-test_references_and_objects] assert origin.url == repo.remotes.origin.url - cw = origin.config_writer - cw.set("pushurl", "other_url") - cw.release() + with origin.config_writer as cw: + cw.set("pushurl", "other_url") # Please note that in python 2, writing origin.config_writer.set(...) is totally safe. # In py3 __del__ calls can be delayed, thus not writing changes in time. diff --git a/git/test/test_index.py b/git/test/test_index.py index 01506c6f..34014064 100644 --- a/git/test/test_index.py +++ b/git/test/test_index.py @@ -412,10 +412,9 @@ class TestIndex(TestBase): uname = u"Thomas Müller" umail = "sd@company.com" - writer = rw_repo.config_writer() - writer.set_value("user", "name", uname) - writer.set_value("user", "email", umail) - writer.release() + with rw_repo.config_writer() as writer: + writer.set_value("user", "name", uname) + writer.set_value("user", "email", umail) self.assertEqual(writer.get_value("user", "name"), uname) # remove all of the files, provide a wild mix of paths, BaseIndexEntries, diff --git a/git/test/test_refs.py b/git/test/test_refs.py index 00b5232a..43f1dcc7 100644 --- a/git/test/test_refs.py +++ b/git/test/test_refs.py @@ -101,15 +101,13 @@ class TestRefs(TestBase): assert prev_object == cur_object # represent the same git object assert prev_object is not cur_object # but are different instances - writer = head.config_writer() - tv = "testopt" - writer.set_value(tv, 1) - assert writer.get_value(tv) == 1 - writer.release() + with head.config_writer() as writer: + tv = "testopt" + writer.set_value(tv, 1) + assert writer.get_value(tv) == 1 assert head.config_reader().get_value(tv) == 1 - writer = head.config_writer() - writer.remove_option(tv) - writer.release() + with head.config_writer() as writer: + writer.remove_option(tv) # after the clone, we might still have a tracking branch setup head.set_tracking_branch(None) @@ -175,7 +173,7 @@ class TestRefs(TestBase): def test_orig_head(self): assert type(self.rorepo.head.orig_head()) == SymbolicReference - + @with_rw_repo('0.1.6') def test_head_checkout_detached_head(self, rw_repo): res = rw_repo.remotes.origin.refs.master.checkout() diff --git a/git/test/test_remote.py b/git/test/test_remote.py index 3fd71a1f..7b52ccce 100644 --- a/git/test/test_remote.py +++ b/git/test/test_remote.py @@ -267,7 +267,8 @@ class TestRemote(TestBase): # put origin to git-url other_origin = other_repo.remotes.origin - other_origin.config_writer.set("url", remote_repo_url) + with other_origin.config_writer as cw: + cw.set("url", remote_repo_url) # it automatically creates alternates as remote_repo is shared as well. # It will use the transport though and ignore alternates when fetching # assert not other_repo.alternates # this would fail @@ -416,13 +417,12 @@ class TestRemote(TestBase): self.failUnlessRaises(IOError, reader.set, opt, "test") # change value - writer = remote.config_writer - new_val = "myval" - writer.set(opt, new_val) - assert writer.get(opt) == new_val - writer.set(opt, val) - assert writer.get(opt) == val - del(writer) + with remote.config_writer as writer: + new_val = "myval" + writer.set(opt, new_val) + assert writer.get(opt) == new_val + writer.set(opt, val) + assert writer.get(opt) == val assert getattr(remote, opt) == val # END for each default option key diff --git a/git/test/test_submodule.py b/git/test/test_submodule.py index be388e5d..46928f51 100644 --- a/git/test/test_submodule.py +++ b/git/test/test_submodule.py @@ -105,21 +105,25 @@ class TestSubmodule(TestBase): new_smclone_path = None # keep custom paths for later new_csmclone_path = None # if rwrepo.bare: - self.failUnlessRaises(InvalidGitRepositoryError, sm.config_writer) + with self.assertRaises(InvalidGitRepositoryError): + with sm.config_writer() as cw: + pass else: - writer = sm.config_writer() - # for faster checkout, set the url to the local path - new_smclone_path = to_native_path_linux(join_path_native(self.rorepo.working_tree_dir, sm.path)) - writer.set_value('url', new_smclone_path) - writer.release() - assert sm.config_reader().get_value('url') == new_smclone_path - assert sm.url == new_smclone_path + with sm.config_writer() as writer: + # for faster checkout, set the url to the local path + new_smclone_path = to_native_path_linux(join_path_native(self.rorepo.working_tree_dir, sm.path)) + writer.set_value('url', new_smclone_path) + writer.release() + assert sm.config_reader().get_value('url') == new_smclone_path + assert sm.url == new_smclone_path # END handle bare repo smold.config_reader() # cannot get a writer on historical submodules if not rwrepo.bare: - self.failUnlessRaises(ValueError, smold.config_writer) + with self.assertRaises(ValueError): + with smold.config_writer(): + pass # END handle bare repo # make the old into a new - this doesn't work as the name changed @@ -210,9 +214,8 @@ class TestSubmodule(TestBase): # adjust the path of the submodules module to point to the local destination new_csmclone_path = to_native_path_linux(join_path_native(self.rorepo.working_tree_dir, sm.path, csm.path)) - writer = csm.config_writer() - writer.set_value('url', new_csmclone_path) - writer.release() + with csm.config_writer() as writer: + writer.set_value('url', new_csmclone_path) assert csm.url == new_csmclone_path # dry-run does nothing @@ -274,9 +277,8 @@ class TestSubmodule(TestBase): # module() is supposed to point to gitdb, which has a child-submodule whose URL is still pointing # to github. To save time, we will change it to csm.set_parent_commit(csm.repo.head.commit) - cw = csm.config_writer() - cw.set_value('url', self._small_repo_url()) - cw.release() + with csm.config_writer() as cw: + cw.set_value('url', self._small_repo_url()) csm.repo.index.commit("adjusted URL to point to local source, instead of the internet") # We have modified the configuration, hence the index is dirty, and the @@ -284,12 +286,10 @@ class TestSubmodule(TestBase): # NOTE: As we did a few updates in the meanwhile, the indices were reset # Hence we create some changes csm.set_parent_commit(csm.repo.head.commit) - writer = sm.config_writer() - writer.set_value("somekey", "somevalue") - writer.release() - writer = csm.config_writer() - writer.set_value("okey", "ovalue") - writer.release() + with sm.config_writer() as writer: + writer.set_value("somekey", "somevalue") + with csm.config_writer() as writer: + writer.set_value("okey", "ovalue") self.failUnlessRaises(InvalidGitRepositoryError, sm.remove) # if we remove the dirty index, it would work sm.module().index.reset() @@ -452,8 +452,8 @@ class TestSubmodule(TestBase): assert len(rm.list_items(rm.module())) == 1 rm.config_reader() - w = rm.config_writer() - w.release() + with rm.config_writer(): + pass # deep traversal gitdb / async rsmsp = [sm.path for sm in rm.traverse()] @@ -478,9 +478,8 @@ class TestSubmodule(TestBase): assert not sm.module_exists() # was never updated after rwrepo's clone # assure we clone from a local source - writer = sm.config_writer() - writer.set_value('url', to_native_path_linux(join_path_native(self.rorepo.working_tree_dir, sm.path))) - writer.release() + with sm.config_writer() as writer: + writer.set_value('url', to_native_path_linux(join_path_native(self.rorepo.working_tree_dir, sm.path))) # dry-run does nothing sm.update(recursive=False, dry_run=True, progress=prog) @@ -488,9 +487,8 @@ class TestSubmodule(TestBase): sm.update(recursive=False) assert sm.module_exists() - writer = sm.config_writer() - writer.set_value('path', fp) # change path to something with prefix AFTER url change - writer.release() + with sm.config_writer() as writer: + writer.set_value('path', fp) # change path to something with prefix AFTER url change # update fails as list_items in such a situations cannot work, as it cannot # find the entry at the changed path @@ -577,9 +575,8 @@ class TestSubmodule(TestBase): # repository at the different url nsm.set_parent_commit(csmremoved) nsmurl = to_native_path_linux(join_path_native(self.rorepo.working_tree_dir, rsmsp[0])) - writer = nsm.config_writer() - writer.set_value('url', nsmurl) - writer.release() + with nsm.config_writer() as writer: + writer.set_value('url', nsmurl) csmpathchange = rwrepo.index.commit("changed url") nsm.set_parent_commit(csmpathchange) @@ -609,9 +606,8 @@ class TestSubmodule(TestBase): nsmm = nsm.module() prev_commit = nsmm.head.commit for branch in ("some_virtual_branch", cur_branch.name): - writer = nsm.config_writer() - writer.set_value(Submodule.k_head_option, git.Head.to_full_path(branch)) - writer.release() + with nsm.config_writer() as writer: + writer.set_value(Submodule.k_head_option, git.Head.to_full_path(branch)) csmbranchchange = rwrepo.index.commit("changed branch to %s" % branch) nsm.set_parent_commit(csmbranchchange) # END for each branch to change @@ -639,9 +635,8 @@ class TestSubmodule(TestBase): assert nsm.exists() and nsm.module_exists() and len(nsm.children()) >= 1 # assure we pull locally only nsmc = nsm.children()[0] - writer = nsmc.config_writer() - writer.set_value('url', subrepo_url) - writer.release() + with nsmc.config_writer() as writer: + writer.set_value('url', subrepo_url) rm.update(recursive=True, progress=prog, dry_run=True) # just to run the code rm.update(recursive=True, progress=prog) @@ -793,8 +788,8 @@ class TestSubmodule(TestBase): rsm = parent.submodule_update() assert_exists(sm) assert_exists(csm) - csm_writer = csm.config_writer().set_value('url', 'bar') - csm_writer.release() + with csm.config_writer().set_value('url', 'bar'): + pass csm.repo.index.commit("Have to commit submodule change for algorithm to pick it up") assert csm.url == 'bar' @@ -872,9 +867,8 @@ class TestSubmodule(TestBase): sm.repo.index.commit("added new file") # change designated submodule checkout branch to the new upstream feature branch - smcw = sm.config_writer() - smcw.set_value('branch', sm_fb.name) - smcw.release() + with sm.config_writer() as smcw: + smcw.set_value('branch', sm_fb.name) assert sm.repo.is_dirty(index=True, working_tree=False) sm.repo.index.commit("changed submodule branch to '%s'" % sm_fb) @@ -898,9 +892,8 @@ class TestSubmodule(TestBase): sm_source_repo.index.commit("new file added, to past of '%r'" % sm_fb) # Change designated submodule checkout branch to a new commit in its own past - smcw = sm.config_writer() - smcw.set_value('branch', sm_pfb.path) - smcw.release() + with sm.config_writer() as smcw: + smcw.set_value('branch', sm_pfb.path) sm.repo.index.commit("changed submodule branch to '%s'" % sm_pfb) # Test submodule updates - must fail if submodule is dirty |