diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2016-10-22 11:11:25 +0200 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2016-10-22 11:11:25 +0200 |
commit | caa0ea7a0893fe90ea043843d4e6ad407126d7b8 (patch) | |
tree | 2f688eb182f2e76091134c47c4a327681c12e15b /git/test | |
parent | afcd64ebbb770908bd2a751279ff070dea5bb97c (diff) | |
parent | cc77e6b2862733a211c55cf29cc7a83c36c27919 (diff) | |
download | gitpython-caa0ea7a0893fe90ea043843d4e6ad407126d7b8.tar.gz |
Merge branch 'cygwin' of https://github.com/ankostis/GitPython into ankostis-cygwin
Diffstat (limited to 'git/test')
-rw-r--r-- | git/test/lib/helper.py | 222 | ||||
-rw-r--r-- | git/test/performance/lib.py | 17 | ||||
-rw-r--r-- | git/test/performance/test_streams.py | 23 | ||||
-rw-r--r-- | git/test/test_base.py | 28 | ||||
-rw-r--r-- | git/test/test_commit.py | 42 | ||||
-rw-r--r-- | git/test/test_config.py | 13 | ||||
-rw-r--r-- | git/test/test_db.py | 9 | ||||
-rw-r--r-- | git/test/test_diff.py | 25 | ||||
-rw-r--r-- | git/test/test_docs.py | 26 | ||||
-rw-r--r-- | git/test/test_fun.py | 33 | ||||
-rw-r--r-- | git/test/test_git.py | 28 | ||||
-rw-r--r-- | git/test/test_index.py | 62 | ||||
-rw-r--r-- | git/test/test_reflog.py | 21 | ||||
-rw-r--r-- | git/test/test_refs.py | 28 | ||||
-rw-r--r-- | git/test/test_repo.py | 42 | ||||
-rw-r--r-- | git/test/test_submodule.py | 34 | ||||
-rw-r--r-- | git/test/test_tree.py | 9 | ||||
-rw-r--r-- | git/test/test_util.py | 90 |
18 files changed, 433 insertions, 319 deletions
diff --git a/git/test/lib/helper.py b/git/test/lib/helper.py index b009a9cd..1515f2a1 100644 --- a/git/test/lib/helper.py +++ b/git/test/lib/helper.py @@ -5,6 +5,7 @@ # the BSD License: http://www.opensource.org/licenses/bsd-license.php from __future__ import print_function +import contextlib from functools import wraps import io import logging @@ -16,7 +17,7 @@ from unittest import TestCase import unittest from git.compat import string_types, is_win, PY3 -from git.util import rmtree +from git.util import rmtree, cwd import os.path as osp @@ -32,7 +33,7 @@ __all__ = ( 'GIT_REPO', 'GIT_DAEMON_PORT' ) -log = logging.getLogger('git.util') +log = logging.getLogger(__name__) #{ Routines @@ -118,7 +119,7 @@ def with_rw_repo(working_tree_ref, bare=False): if bare: prefix = '' # END handle prefix - repo_dir = tempfile.mktemp("%sbare_%s" % (prefix, func.__name__)) + repo_dir = tempfile.mktemp(prefix="%sbare_%s" % (prefix, func.__name__)) rw_repo = self.rorepo.clone(repo_dir, shared=True, bare=bare, n=True) rw_repo.head.commit = rw_repo.commit(working_tree_ref) @@ -151,32 +152,66 @@ def with_rw_repo(working_tree_ref, bare=False): return argument_passer -def launch_git_daemon(base_path, ip, port): - from git import Git - if is_win: - ## On MINGW-git, daemon exists in .\Git\mingw64\libexec\git-core\, - # but if invoked as 'git daemon', it detaches from parent `git` cmd, - # and then CANNOT DIE! - # So, invoke it as a single command. - ## Cygwin-git has no daemon. But it can use MINGW's. - # - daemon_cmd = ['git-daemon', - '--enable=receive-pack', - '--listen=%s' % ip, - '--port=%s' % port, - '--base-path=%s' % base_path, - base_path] - gd = Git().execute(daemon_cmd, as_process=True) +@contextlib.contextmanager +def git_daemon_launched(base_path, ip, port): + from git import Git # Avoid circular deps. + + gd = None + try: + if is_win: + ## On MINGW-git, daemon exists in .\Git\mingw64\libexec\git-core\, + # but if invoked as 'git daemon', it detaches from parent `git` cmd, + # and then CANNOT DIE! + # So, invoke it as a single command. + ## Cygwin-git has no daemon. But it can use MINGW's. + # + daemon_cmd = ['git-daemon', + '--enable=receive-pack', + '--listen=%s' % ip, + '--port=%s' % port, + '--base-path=%s' % base_path, + base_path] + gd = Git().execute(daemon_cmd, as_process=True) + else: + gd = Git().daemon(base_path, + enable='receive-pack', + listen=ip, + port=port, + base_path=base_path, + as_process=True) + # yes, I know ... fortunately, this is always going to work if sleep time is just large enough + time.sleep(0.5 * (1 + is_win)) + except Exception as ex: + msg = textwrap.dedent(""" + Launching git-daemon failed due to: %s + Probably test will fail subsequently. + + BUT you may start *git-daemon* manually with this command:" + git daemon --enable=receive-pack --listen=%s --port=%s --base-path=%s %s + You may also run the daemon on a different port by passing --port=<port>" + and setting the environment variable GIT_PYTHON_TEST_GIT_DAEMON_PORT to <port> + """) + if is_win: + msg += textwrap.dedent(""" + + On Windows, + the `git-daemon.exe` must be in PATH. + For MINGW, look into .\Git\mingw64\libexec\git-core\), but problems with paths might appear. + CYGWIN has no daemon, but if one exists, it gets along fine (but has also paths problems).""") + log.warning(msg, ex, ip, port, base_path, base_path, exc_info=1) + + yield # OK, assume daemon started manually. + else: - gd = Git().daemon(base_path, - enable='receive-pack', - listen=ip, - port=port, - base_path=base_path, - as_process=True) - # yes, I know ... fortunately, this is always going to work if sleep time is just large enough - time.sleep(0.5) - return gd + yield # Yield outside try, to avoid catching + finally: + if gd: + try: + log.debug("Killing git-daemon...") + gd.proc.kill() + except Exception as ex: + ## Either it has died (and we're here), or it won't die, again here... + log.debug("Hidden error while Killing git-daemon: %s", ex, exc_info=1) def with_rw_and_rw_remote_repo(working_tree_ref): @@ -196,7 +231,7 @@ def with_rw_and_rw_remote_repo(working_tree_ref): rorepo ---<bare clone>---> rw_remote_repo ---<clone>---> rw_repo The test case needs to support the following signature:: - def case(self, rw_repo, rw_remote_repo) + def case(self, rw_repo, rw_daemon_repo) This setup allows you to test push and pull scenarios and hooks nicely. @@ -211,94 +246,65 @@ def with_rw_and_rw_remote_repo(working_tree_ref): @wraps(func) def remote_repo_creator(self): - remote_repo_dir = tempfile.mktemp("remote_repo_%s" % func.__name__) - repo_dir = tempfile.mktemp("remote_clone_non_bare_repo") + rw_daemon_repo_dir = tempfile.mktemp(prefix="daemon_repo-%s-" % func.__name__) + rw_repo_dir = tempfile.mktemp(prefix="daemon_cloned_repo-%s-" % func.__name__) - rw_remote_repo = self.rorepo.clone(remote_repo_dir, shared=True, bare=True) + rw_daemon_repo = self.rorepo.clone(rw_daemon_repo_dir, shared=True, bare=True) # recursive alternates info ? - rw_repo = rw_remote_repo.clone(repo_dir, shared=True, bare=False, n=True) - rw_repo.head.commit = working_tree_ref - rw_repo.head.reference.checkout() - - # prepare for git-daemon - rw_remote_repo.daemon_export = True - - # this thing is just annoying ! - 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. - d_remote = Remote.create(rw_repo, "daemon_origin", remote_repo_dir) - d_remote.fetch() - - base_path, rel_repo_dir = osp.split(remote_repo_dir) - - remote_repo_url = Git.polish_url("git://localhost:%s/%s" % (GIT_DAEMON_PORT, rel_repo_dir)) - with d_remote.config_writer as cw: - cw.set('url', remote_repo_url) - + rw_repo = rw_daemon_repo.clone(rw_repo_dir, shared=True, bare=False, n=True) try: - gd = launch_git_daemon(Git.polish_url(base_path), '127.0.0.1', GIT_DAEMON_PORT) - except Exception as ex: - if is_win: - msg = textwrap.dedent(""" - The `git-daemon.exe` must be in PATH. - For MINGW, look into .\Git\mingw64\libexec\git-core\), but problems with paths might appear. - CYGWIN has no daemon, but if one exists, it gets along fine (has also paths problems) - Anyhow, alternatively try starting `git-daemon` manually:""") - else: - msg = "Please try starting `git-daemon` manually:" - msg += textwrap.dedent(""" - git daemon --enable=receive-pack --base-path=%s %s - You can also run the daemon on a different port by passing --port=<port>" - and setting the environment variable GIT_PYTHON_TEST_GIT_DAEMON_PORT to <port> - """ % (base_path, base_path)) - raise AssertionError(ex, msg) - # END make assertion - else: - # Try listing remotes, to diagnose whether the daemon is up. - rw_repo.git.ls_remote(d_remote) - - # adjust working dir - prev_cwd = os.getcwd() - os.chdir(rw_repo.working_dir) + rw_repo.head.commit = working_tree_ref + rw_repo.head.reference.checkout() - try: - return func(self, rw_repo, rw_remote_repo) - except: - log.info("Keeping repos after failure: repo_dir = %s, remote_repo_dir = %s", - repo_dir, remote_repo_dir) - repo_dir = remote_repo_dir = None - raise - finally: - os.chdir(prev_cwd) + # prepare for git-daemon + rw_daemon_repo.daemon_export = True + + # this thing is just annoying ! + with rw_daemon_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. + d_remote = Remote.create(rw_repo, "daemon_origin", rw_daemon_repo_dir) + d_remote.fetch() + + base_daemon_path, rel_repo_dir = osp.split(rw_daemon_repo_dir) + + remote_repo_url = Git.polish_url("git://localhost:%s/%s" % (GIT_DAEMON_PORT, rel_repo_dir)) + with d_remote.config_writer as cw: + cw.set('url', remote_repo_url) + + with git_daemon_launched(Git.polish_url(base_daemon_path, is_cygwin=False), # No daemon in Cygwin. + '127.0.0.1', + GIT_DAEMON_PORT): + # Try listing remotes, to diagnose whether the daemon is up. + rw_repo.git.ls_remote(d_remote) + + with cwd(rw_repo.working_dir): + try: + return func(self, rw_repo, rw_daemon_repo) + except: + log.info("Keeping repos after failure: \n rw_repo_dir: %s \n rw_daemon_repo_dir: %s", + rw_repo_dir, rw_daemon_repo_dir) + rw_repo_dir = rw_daemon_repo_dir = None + raise finally: - try: - log.debug("Killing git-daemon...") - gd.proc.kill() - except: - ## Either it has died (and we're here), or it won't die, again here... - pass - rw_repo.git.clear_cache() - rw_remote_repo.git.clear_cache() - rw_repo = rw_remote_repo = None + rw_daemon_repo.git.clear_cache() + del rw_repo + del rw_daemon_repo import gc gc.collect() - if repo_dir: - rmtree(repo_dir) - if remote_repo_dir: - rmtree(remote_repo_dir) - - if gd is not None: - gd.proc.wait() + if rw_repo_dir: + rmtree(rw_repo_dir) + if rw_daemon_repo_dir: + rmtree(rw_daemon_repo_dir) # END cleanup # END bare repo creator return remote_repo_creator diff --git a/git/test/performance/lib.py b/git/test/performance/lib.py index ed82e4dd..7edffa78 100644 --- a/git/test/performance/lib.py +++ b/git/test/performance/lib.py @@ -1,22 +1,23 @@ """Contains library functions""" +import logging import os -from git.test.lib import ( - TestBase -) import tempfile -import logging +from git import ( + Repo +) from git.db import ( GitCmdObjectDB, GitDB ) - -from git import ( - Repo +from git.test.lib import ( + TestBase ) from git.util import rmtree +import os.path as osp #{ Invariants + k_env_git_repo = "GIT_PYTHON_TEST_GIT_REPO_BASE" #} END invariants @@ -52,7 +53,7 @@ class TestBigRepoR(TestBase): logging.info( ("You can set the %s environment variable to a .git repository of" % k_env_git_repo) + "your choice - defaulting to the gitpython repository") - repo_path = os.path.dirname(__file__) + repo_path = osp.dirname(__file__) # end set some repo path self.gitrorepo = Repo(repo_path, odbt=GitCmdObjectDB, search_parent_directories=True) self.puregitrorepo = Repo(repo_path, odbt=GitDB, search_parent_directories=True) diff --git a/git/test/performance/test_streams.py b/git/test/performance/test_streams.py index 42cbade5..3909d8ff 100644 --- a/git/test/performance/test_streams.py +++ b/git/test/performance/test_streams.py @@ -1,26 +1,27 @@ """Performance data streaming performance""" from __future__ import print_function -from time import time import os -import sys import subprocess +import sys +from time import time from git.test.lib import ( with_rw_repo ) -from gitdb.util import bin_to_hex +from git.util import bin_to_hex +from gitdb import ( + LooseObjectDB, + IStream +) from gitdb.test.lib import make_memory_file +import os.path as osp + from .lib import ( TestBigRepoR ) -from gitdb import ( - LooseObjectDB, - IStream -) - class TestObjDBPerformance(TestBigRepoR): @@ -31,7 +32,7 @@ class TestObjDBPerformance(TestBigRepoR): def test_large_data_streaming(self, rwrepo): # TODO: This part overlaps with the same file in gitdb.test.performance.test_stream # It should be shared if possible - ldb = LooseObjectDB(os.path.join(rwrepo.git_dir, 'objects')) + ldb = LooseObjectDB(osp.join(rwrepo.git_dir, 'objects')) for randomize in range(2): desc = (randomize and 'random ') or '' @@ -47,7 +48,7 @@ class TestObjDBPerformance(TestBigRepoR): elapsed_add = time() - st assert ldb.has_object(binsha) db_file = ldb.readable_db_object_path(bin_to_hex(binsha)) - fsize_kib = os.path.getsize(db_file) / 1000 + fsize_kib = osp.getsize(db_file) / 1000 size_kib = size / 1000 msg = "Added %i KiB (filesize = %i KiB) of %s data to loose odb in %f s ( %f Write KiB / s)" @@ -109,7 +110,7 @@ class TestObjDBPerformance(TestBigRepoR): assert gitsha == bin_to_hex(binsha) # we do it the same way, right ? # as its the same sha, we reuse our path - fsize_kib = os.path.getsize(db_file) / 1000 + fsize_kib = osp.getsize(db_file) / 1000 msg = "Added %i KiB (filesize = %i KiB) of %s data to using git-hash-object in %f s ( %f Write KiB / s)" msg %= (size_kib, fsize_kib, desc, gelapsed_add, size_kib / gelapsed_add) print(msg, file=sys.stderr) diff --git a/git/test/test_base.py b/git/test/test_base.py index 7fc3096f..cec40de8 100644 --- a/git/test/test_base.py +++ b/git/test/test_base.py @@ -9,22 +9,24 @@ import sys import tempfile from unittest import skipIf -import git.objects.base as base -from git.test.lib import ( - TestBase, - assert_raises, - with_rw_repo, - with_rw_and_rw_remote_repo -) from git import ( Blob, Tree, Commit, TagObject ) -from git.objects.util import get_object_type_by_name -from gitdb.util import hex_to_bin from git.compat import is_win +from git.objects.util import get_object_type_by_name +from git.test.lib import ( + TestBase, + assert_raises, + with_rw_repo, + with_rw_and_rw_remote_repo +) +from git.util import hex_to_bin + +import git.objects.base as base +import os.path as osp class TestBase(TestBase): @@ -103,19 +105,19 @@ class TestBase(TestBase): @with_rw_repo('HEAD', bare=True) def test_with_bare_rw_repo(self, bare_rw_repo): assert bare_rw_repo.config_reader("repository").getboolean("core", "bare") - assert os.path.isfile(os.path.join(bare_rw_repo.git_dir, 'HEAD')) + assert osp.isfile(osp.join(bare_rw_repo.git_dir, 'HEAD')) @with_rw_repo('0.1.6') def test_with_rw_repo(self, rw_repo): assert not rw_repo.config_reader("repository").getboolean("core", "bare") - assert os.path.isdir(os.path.join(rw_repo.working_tree_dir, 'lib')) + assert osp.isdir(osp.join(rw_repo.working_tree_dir, 'lib')) #@skipIf(HIDE_WINDOWS_FREEZE_ERRORS, "FIXME: Freezes! sometimes...") @with_rw_and_rw_remote_repo('0.1.6') def test_with_rw_remote_and_rw_repo(self, rw_repo, rw_remote_repo): assert not rw_repo.config_reader("repository").getboolean("core", "bare") assert rw_remote_repo.config_reader("repository").getboolean("core", "bare") - assert os.path.isdir(os.path.join(rw_repo.working_tree_dir, 'lib')) + assert osp.isdir(osp.join(rw_repo.working_tree_dir, 'lib')) @skipIf(sys.version_info < (3,) and is_win, "Unicode woes, see https://github.com/gitpython-developers/GitPython/pull/519") @@ -123,7 +125,7 @@ class TestBase(TestBase): def test_add_unicode(self, rw_repo): filename = u"שלום.txt" - file_path = os.path.join(rw_repo.working_dir, filename) + file_path = osp.join(rw_repo.working_dir, filename) # verify first that we could encode file name in this environment try: diff --git a/git/test/test_commit.py b/git/test/test_commit.py index fd9777fb..fbb1c244 100644 --- a/git/test/test_commit.py +++ b/git/test/test_commit.py @@ -6,34 +6,36 @@ # the BSD License: http://www.opensource.org/licenses/bsd-license.php from __future__ import print_function -from git.test.lib import ( - TestBase, - assert_equal, - assert_not_equal, - with_rw_repo, - fixture_path, - StringProcessAdapter -) +from datetime import datetime +from io import BytesIO +import re +import sys +import time + from git import ( Commit, Actor, ) -from gitdb import IStream -from git.test.lib import with_rw_directory +from git import Repo from git.compat import ( string_types, text_type ) -from git import Repo +from git.objects.util import tzoffset, utc from git.repo.fun import touch +from git.test.lib import ( + TestBase, + assert_equal, + assert_not_equal, + with_rw_repo, + fixture_path, + StringProcessAdapter +) +from git.test.lib import with_rw_directory +from gitdb import IStream + +import os.path as osp -from io import BytesIO -import time -import sys -import re -import os -from datetime import datetime -from git.objects.util import tzoffset, utc try: from unittest.mock import Mock @@ -232,8 +234,8 @@ class TestCommit(TestBase): @with_rw_directory def test_ambiguous_arg_iteration(self, rw_dir): - rw_repo = Repo.init(os.path.join(rw_dir, 'test_ambiguous_arg')) - path = os.path.join(rw_repo.working_tree_dir, 'master') + rw_repo = Repo.init(osp.join(rw_dir, 'test_ambiguous_arg')) + path = osp.join(rw_repo.working_tree_dir, 'master') touch(path) rw_repo.index.add([path]) rw_repo.index.commit('initial commit') diff --git a/git/test/test_config.py b/git/test/test_config.py index 32873f24..0dfadda6 100644 --- a/git/test/test_config.py +++ b/git/test/test_config.py @@ -6,7 +6,6 @@ import glob import io -import os from git import ( GitConfigParser @@ -91,7 +90,7 @@ class TestBase(TestCase): @with_rw_directory def test_lock_reentry(self, rw_dir): - fpl = os.path.join(rw_dir, 'l') + fpl = osp.join(rw_dir, 'l') gcp = GitConfigParser(fpl, read_only=False) with gcp as cw: cw.set_value('include', 'some_value', 'a') @@ -103,7 +102,7 @@ class TestBase(TestCase): GitConfigParser(fpl, read_only=False) # but work when the lock is removed with GitConfigParser(fpl, read_only=False): - assert os.path.exists(fpl) + assert osp.exists(fpl) # reentering with an existing lock must fail due to exclusive access with self.assertRaises(IOError): gcp.__enter__() @@ -178,17 +177,17 @@ class TestBase(TestCase): # end # PREPARE CONFIG FILE A - fpa = os.path.join(rw_dir, 'a') + fpa = osp.join(rw_dir, 'a') with GitConfigParser(fpa, read_only=False) as cw: write_test_value(cw, 'a') - fpb = os.path.join(rw_dir, 'b') - fpc = os.path.join(rw_dir, 'c') + fpb = osp.join(rw_dir, 'b') + fpc = osp.join(rw_dir, 'c') cw.set_value('include', 'relative_path_b', 'b') cw.set_value('include', 'doesntexist', 'foobar') cw.set_value('include', 'relative_cycle_a_a', 'a') cw.set_value('include', 'absolute_cycle_a_a', fpa) - assert os.path.exists(fpa) + assert osp.exists(fpa) # PREPARE CONFIG FILE B with GitConfigParser(fpb, read_only=False) as cw: diff --git a/git/test/test_db.py b/git/test/test_db.py index 5dcf592a..8f67dd48 100644 --- a/git/test/test_db.py +++ b/git/test/test_db.py @@ -3,17 +3,18 @@ # # This module is part of GitPython and is released under # the BSD License: http://www.opensource.org/licenses/bsd-license.php -from git.test.lib import TestBase from git.db import GitCmdObjectDB -from gitdb.util import bin_to_hex from git.exc import BadObject -import os +from git.test.lib import TestBase +from git.util import bin_to_hex + +import os.path as osp class TestDB(TestBase): def test_base(self): - gdb = GitCmdObjectDB(os.path.join(self.rorepo.git_dir, 'objects'), self.rorepo.git) + gdb = GitCmdObjectDB(osp.join(self.rorepo.git_dir, 'objects'), self.rorepo.git) # partial to complete - works with everything hexsha = bin_to_hex(gdb.partial_to_complete_sha_hex("0.1.6")) diff --git a/git/test/test_diff.py b/git/test/test_diff.py index d34d84e3..48a5a641 100644 --- a/git/test/test_diff.py +++ b/git/test/test_diff.py @@ -4,8 +4,15 @@ # # This module is part of GitPython and is released under # the BSD License: http://www.opensource.org/licenses/bsd-license.php -import os - +import ddt +from git import ( + Repo, + GitCommandError, + Diff, + DiffIndex, + NULL_TREE, +) +from git.cmd import Git from git.test.lib import ( TestBase, StringProcessAdapter, @@ -14,17 +21,9 @@ from git.test.lib import ( assert_true, ) - from git.test.lib import with_rw_directory -from git import ( - Repo, - GitCommandError, - Diff, - DiffIndex, - NULL_TREE, -) -import ddt +import os.path as osp @ddt.ddt @@ -53,10 +52,10 @@ class TestDiff(TestBase): def test_diff_with_staged_file(self, rw_dir): # SETUP INDEX WITH MULTIPLE STAGES r = Repo.init(rw_dir) - fp = os.path.join(rw_dir, 'hello.txt') + fp = osp.join(rw_dir, 'hello.txt') with open(fp, 'w') as fs: fs.write("hello world") - r.git.add(fp) + r.git.add(Git.polish_url(fp)) r.git.commit(message="init") with open(fp, 'w') as fs: diff --git a/git/test/test_docs.py b/git/test/test_docs.py index f3c75f79..bb937d93 100644 --- a/git/test/test_docs.py +++ b/git/test/test_docs.py @@ -9,6 +9,8 @@ import os from git.test.lib import TestBase from git.test.lib.helper import with_rw_directory +import os.path as osp + class Tutorials(TestBase): @@ -23,7 +25,7 @@ class Tutorials(TestBase): def test_init_repo_object(self, rw_dir): # [1-test_init_repo_object] from git import Repo - join = os.path.join + join = osp.join # rorepo is a Repo instance pointing to the git-python repository. # For all you know, the first argument to Repo is a path to the repository @@ -62,7 +64,7 @@ class Tutorials(TestBase): # repository paths # [7-test_init_repo_object] - assert os.path.isdir(cloned_repo.working_tree_dir) # directory with your work files + assert osp.isdir(cloned_repo.working_tree_dir) # directory with your work files assert cloned_repo.git_dir.startswith(cloned_repo.working_tree_dir) # directory containing the git repository assert bare_repo.working_tree_dir is None # bare repositories have no working tree # ![7-test_init_repo_object] @@ -146,7 +148,7 @@ class Tutorials(TestBase): self.assertEqual(new_branch.checkout(), cloned_repo.active_branch) # checking out branch adjusts the wtree self.assertEqual(new_branch.commit, past.commit) # Now the past is checked out - new_file_path = os.path.join(cloned_repo.working_tree_dir, 'my-new-file') + new_file_path = osp.join(cloned_repo.working_tree_dir, 'my-new-file') open(new_file_path, 'wb').close() # create new file in working tree cloned_repo.index.add([new_file_path]) # add it to the index # Commit the changes to deviate masters history @@ -162,7 +164,7 @@ class Tutorials(TestBase): # now new_branch is ahead of master, which probably should be checked out and reset softly. # note that all these operations didn't touch the working tree, as we managed it ourselves. # This definitely requires you to know what you are doing :) ! - assert os.path.basename(new_file_path) in new_branch.commit.tree # new file is now in tree + assert osp.basename(new_file_path) in new_branch.commit.tree # new file is now in tree master.commit = new_branch.commit # let master point to most recent commit cloned_repo.head.reference = master # we adjusted just the reference, not the working tree or index # ![13-test_init_repo_object] @@ -192,7 +194,7 @@ class Tutorials(TestBase): def test_references_and_objects(self, rw_dir): # [1-test_references_and_objects] import git - repo = git.Repo.clone_from(self._small_repo_url(), os.path.join(rw_dir, 'repo'), branch='master') + repo = git.Repo.clone_from(self._small_repo_url(), osp.join(rw_dir, 'repo'), branch='master') heads = repo.heads master = heads.master # lists can be accessed by name for convenience @@ -264,7 +266,7 @@ class Tutorials(TestBase): # [11-test_references_and_objects] hct.blobs[0].data_stream.read() # stream object to read data from - hct.blobs[0].stream_data(open(os.path.join(rw_dir, 'blob_data'), 'wb')) # write data to given stream + hct.blobs[0].stream_data(open(osp.join(rw_dir, 'blob_data'), 'wb')) # write data to given stream # ![11-test_references_and_objects] # [12-test_references_and_objects] @@ -350,11 +352,11 @@ class Tutorials(TestBase): # Access blob objects for (path, stage), entry in index.entries.items(): # @UnusedVariable pass - new_file_path = os.path.join(repo.working_tree_dir, 'new-file-name') + new_file_path = osp.join(repo.working_tree_dir, 'new-file-name') open(new_file_path, 'w').close() index.add([new_file_path]) # add a new file to the index index.remove(['LICENSE']) # remove an existing one - assert os.path.isfile(os.path.join(repo.working_tree_dir, 'LICENSE')) # working tree is untouched + assert osp.isfile(osp.join(repo.working_tree_dir, 'LICENSE')) # working tree is untouched self.assertEqual(index.commit("my commit message").type, 'commit') # commit changed index repo.active_branch.commit = repo.commit('HEAD~1') # forget last commit @@ -373,11 +375,11 @@ class Tutorials(TestBase): # merge two trees three-way into memory merge_index = IndexFile.from_tree(repo, 'HEAD~10', 'HEAD', repo.merge_base('HEAD~10', 'HEAD')) # and persist it - merge_index.write(os.path.join(rw_dir, 'merged_index')) + merge_index.write(osp.join(rw_dir, 'merged_index')) # ![24-test_references_and_objects] # [25-test_references_and_objects] - empty_repo = git.Repo.init(os.path.join(rw_dir, 'empty')) + empty_repo = git.Repo.init(osp.join(rw_dir, 'empty')) origin = empty_repo.create_remote('origin', repo.remotes.origin.url) assert origin.exists() assert origin == empty_repo.remotes.origin == empty_repo.remotes['origin'] @@ -480,8 +482,8 @@ class Tutorials(TestBase): def test_add_file_and_commit(self, rw_dir): import git - repo_dir = os.path.join(rw_dir, 'my-new-repo') - file_name = os.path.join(repo_dir, 'new-file') + repo_dir = osp.join(rw_dir, 'my-new-repo') + file_name = osp.join(repo_dir, 'new-file') r = git.Repo.init(repo_dir) # This function just creates an empty file ... diff --git a/git/test/test_fun.py b/git/test/test_fun.py index 02f338dd..9d436653 100644 --- a/git/test/test_fun.py +++ b/git/test/test_fun.py @@ -1,6 +1,11 @@ -from git.test.lib import ( - TestBase, - with_rw_repo +from io import BytesIO +from stat import S_IFDIR, S_IFREG, S_IFLNK +from unittest.case import skipIf + +from git.compat import PY3 +from git.index import IndexFile +from git.index.fun import ( + aggressive_tree_merge ) from git.objects.fun import ( traverse_tree_recursive, @@ -8,25 +13,13 @@ from git.objects.fun import ( tree_to_stream, tree_entries_from_data ) - -from git.index.fun import ( - aggressive_tree_merge +from git.test.lib import ( + TestBase, + with_rw_repo ) - -from gitdb.util import bin_to_hex +from git.util import bin_to_hex from gitdb.base import IStream from gitdb.typ import str_tree_type -from git.compat import PY3 - -from unittest.case import skipIf -from stat import ( - S_IFDIR, - S_IFREG, - S_IFLNK -) - -from git.index import IndexFile -from io import BytesIO class TestFun(TestBase): @@ -262,7 +255,7 @@ class TestFun(TestBase): def test_tree_entries_from_data_with_failing_name_decode_py2(self): r = tree_entries_from_data(b'100644 \x9f\0aaa') assert r == [('aaa', 33188, u'\udc9f')], r - + @skipIf(not PY3, 'odd types returned ... maybe figure it out one day') def test_tree_entries_from_data_with_failing_name_decode_py3(self): r = tree_entries_from_data(b'100644 \x9f\0aaa') diff --git a/git/test/test_git.py b/git/test/test_git.py index 14c70e18..f97f8130 100644 --- a/git/test/test_git.py +++ b/git/test/test_git.py @@ -5,9 +5,17 @@ # This module is part of GitPython and is released under # the BSD License: http://www.opensource.org/licenses/bsd-license.php import os -import sys import subprocess +import sys +from git import ( + Git, + GitCommandError, + GitCommandNotFound, + Repo, + cmd +) +from git.compat import PY3, is_darwin from git.test.lib import ( TestBase, patch, @@ -17,18 +25,12 @@ from git.test.lib import ( assert_match, fixture_path ) -from git import ( - Git, - GitCommandError, - GitCommandNotFound, - Repo, - cmd -) from git.test.lib import with_rw_directory - -from git.compat import PY3, is_darwin from git.util import finalize_process +import os.path as osp + + try: from unittest import mock except ImportError: @@ -147,7 +149,7 @@ class TestGit(TestBase): exc = GitCommandNotFound try: # set it to something that doens't exist, assure it raises - type(self.git).GIT_PYTHON_GIT_EXECUTABLE = os.path.join( + type(self.git).GIT_PYTHON_GIT_EXECUTABLE = osp.join( "some", "path", "which", "doesn't", "exist", "gitbinary") self.failUnlessRaises(exc, self.git.version) finally: @@ -212,13 +214,13 @@ class TestGit(TestBase): self.assertEqual(new_env, {'VARKEY': 'VARVALUE'}) self.assertEqual(self.git.environment(), {}) - path = os.path.join(rw_dir, 'failing-script.sh') + path = osp.join(rw_dir, 'failing-script.sh') 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')) + rw_repo = Repo.init(osp.join(rw_dir, 'repo')) remote = rw_repo.create_remote('ssh-origin', "ssh://git@server/foo") with rw_repo.git.custom_environment(GIT_SSH=path): diff --git a/git/test/test_index.py b/git/test/test_index.py index d851743e..1abe22f4 100644 --- a/git/test/test_index.py +++ b/git/test/test_index.py @@ -26,7 +26,7 @@ from git import ( GitCommandError, CheckoutError, ) -from git.compat import string_types, is_win +from git.compat import string_types, is_win, PY3 from git.exc import ( HookExecutionError, InvalidGitRepositoryError @@ -43,11 +43,13 @@ from git.test.lib import ( fixture, with_rw_repo ) -from git.util import HIDE_WINDOWS_KNOWN_ERRORS from git.test.lib import with_rw_directory from git.util import Actor, rmtree +from git.util import HIDE_WINDOWS_KNOWN_ERRORS, hex_to_bin from gitdb.base import IStream -from gitdb.util import hex_to_bin + +import os.path as osp +from git.cmd import Git class TestIndex(TestBase): @@ -85,7 +87,7 @@ class TestIndex(TestBase): def _assert_entries(self, entries): for entry in entries: assert isinstance(entry, BaseIndexEntry) - assert not os.path.isabs(entry.path) + assert not osp.isabs(entry.path) assert "\\" not in entry.path # END for each entry @@ -329,7 +331,7 @@ class TestIndex(TestBase): # reset the working copy as well to current head,to pull 'back' as well new_data = b"will be reverted" - file_path = os.path.join(rw_repo.working_tree_dir, "CHANGES") + file_path = osp.join(rw_repo.working_tree_dir, "CHANGES") with open(file_path, "wb") as fp: fp.write(new_data) index.reset(rev_head_parent, working_tree=True) @@ -340,26 +342,26 @@ class TestIndex(TestBase): assert fp.read() != new_data # test full checkout - test_file = os.path.join(rw_repo.working_tree_dir, "CHANGES") + test_file = osp.join(rw_repo.working_tree_dir, "CHANGES") with open(test_file, 'ab') as fd: fd.write(b"some data") rval = index.checkout(None, force=True, fprogress=self._fprogress) assert 'CHANGES' in list(rval) self._assert_fprogress([None]) - assert os.path.isfile(test_file) + assert osp.isfile(test_file) os.remove(test_file) rval = index.checkout(None, force=False, fprogress=self._fprogress) assert 'CHANGES' in list(rval) self._assert_fprogress([None]) - assert os.path.isfile(test_file) + assert osp.isfile(test_file) # individual file os.remove(test_file) rval = index.checkout(test_file, fprogress=self._fprogress) self.assertEqual(list(rval)[0], 'CHANGES') self._assert_fprogress([test_file]) - assert os.path.exists(test_file) + assert osp.exists(test_file) # checking out non-existing file throws self.failUnlessRaises(CheckoutError, index.checkout, "doesnt_exist_ever.txt.that") @@ -373,7 +375,7 @@ class TestIndex(TestBase): index.checkout(test_file) except CheckoutError as e: self.assertEqual(len(e.failed_files), 1) - self.assertEqual(e.failed_files[0], os.path.basename(test_file)) + self.assertEqual(e.failed_files[0], osp.basename(test_file)) self.assertEqual(len(e.failed_files), len(e.failed_reasons)) self.assertIsInstance(e.failed_reasons[0], string_types) self.assertEqual(len(e.valid_files), 0) @@ -388,7 +390,7 @@ class TestIndex(TestBase): assert not open(test_file, 'rb').read().endswith(append_data) # checkout directory - rmtree(os.path.join(rw_repo.working_tree_dir, "lib")) + rmtree(osp.join(rw_repo.working_tree_dir, "lib")) rval = index.checkout('lib') assert len(list(rval)) > 1 @@ -399,11 +401,17 @@ class TestIndex(TestBase): existing = 0 basedir = repo.working_tree_dir for f in files: - existing += os.path.isfile(os.path.join(basedir, f)) + existing += osp.isfile(osp.join(basedir, f)) # END for each deleted file return existing # END num existing helper + @skipIf(HIDE_WINDOWS_KNOWN_ERRORS and Git.is_cygwin(), + """FIXME: File "C:\projects\gitpython\git\test\test_index.py", line 642, in test_index_mutation + self.assertEqual(fd.read(), link_target) + AssertionError: '!<symlink>\xff\xfe/\x00e\x00t\x00c\x00/\x00t\x00h\x00a\x00t\x00\x00\x00' + != '/etc/that' + """) @with_rw_repo('0.1.6') def test_index_mutation(self, rw_repo): index = rw_repo.index @@ -458,7 +466,7 @@ class TestIndex(TestBase): self.failUnlessRaises(TypeError, index.remove, [1]) # absolute path - deleted_files = index.remove([os.path.join(rw_repo.working_tree_dir, "lib")], r=True) + deleted_files = index.remove([osp.join(rw_repo.working_tree_dir, "lib")], r=True) assert len(deleted_files) > 1 self.failUnlessRaises(ValueError, index.remove, ["/doesnt/exists"]) @@ -525,9 +533,9 @@ class TestIndex(TestBase): # re-add all files in lib # get the lib folder back on disk, but get an index without it index.reset(new_commit.parents[0], working_tree=True).reset(new_commit, working_tree=False) - lib_file_path = os.path.join("lib", "git", "__init__.py") + lib_file_path = osp.join("lib", "git", "__init__.py") assert (lib_file_path, 0) not in index.entries - assert os.path.isfile(os.path.join(rw_repo.working_tree_dir, lib_file_path)) + assert osp.isfile(osp.join(rw_repo.working_tree_dir, lib_file_path)) # directory entries = index.add(['lib'], fprogress=self._fprogress_add) @@ -536,14 +544,14 @@ class TestIndex(TestBase): assert len(entries) > 1 # glob - entries = index.reset(new_commit).add([os.path.join('lib', 'git', '*.py')], fprogress=self._fprogress_add) + entries = index.reset(new_commit).add([osp.join('lib', 'git', '*.py')], fprogress=self._fprogress_add) self._assert_entries(entries) self._assert_fprogress(entries) self.assertEqual(len(entries), 14) # same file entries = index.reset(new_commit).add( - [os.path.join(rw_repo.working_tree_dir, 'lib', 'git', 'head.py')] * 2, fprogress=self._fprogress_add) + [osp.join(rw_repo.working_tree_dir, 'lib', 'git', 'head.py')] * 2, fprogress=self._fprogress_add) self._assert_entries(entries) self.assertEqual(entries[0].mode & 0o644, 0o644) # would fail, test is too primitive to handle this case @@ -583,7 +591,7 @@ class TestIndex(TestBase): for target in ('/etc/nonexisting', '/etc/passwd', '/etc'): basename = "my_real_symlink" - link_file = os.path.join(rw_repo.working_tree_dir, basename) + link_file = osp.join(rw_repo.working_tree_dir, basename) os.symlink(target, link_file) entries = index.reset(new_commit).add([link_file], fprogress=self._fprogress_add) self._assert_entries(entries) @@ -645,7 +653,7 @@ class TestIndex(TestBase): # TEST RENAMING def assert_mv_rval(rval): for source, dest in rval: - assert not os.path.exists(source) and os.path.exists(dest) + assert not osp.exists(source) and osp.exists(dest) # END for each renamed item # END move assertion utility @@ -661,7 +669,7 @@ class TestIndex(TestBase): paths = ['LICENSE', 'VERSION', 'doc'] rval = index.move(paths, dry_run=True) self.assertEqual(len(rval), 2) - assert os.path.exists(paths[0]) + assert osp.exists(paths[0]) # again, no dry run rval = index.move(paths) @@ -719,8 +727,8 @@ class TestIndex(TestBase): index.add(files, write=True) if is_win: hp = hook_path('pre-commit', index.repo.git_dir) - hpd = os.path.dirname(hp) - if not os.path.isdir(hpd): + hpd = osp.dirname(hp) + if not osp.isdir(hpd): os.mkdir(hpd) with open(hp, "wt") as fp: fp.write("#!/usr/bin/env sh\necho stdout; echo stderr 1>&2; exit 1") @@ -766,7 +774,7 @@ class TestIndex(TestBase): for fkey in keys: assert fkey in index.entries for absfile in absfiles: - assert os.path.isfile(absfile) + assert osp.isfile(absfile) @with_rw_repo('HEAD') def test_compare_write_tree(self, rw_repo): @@ -815,21 +823,21 @@ class TestIndex(TestBase): # Adding using a path should still require a non-bare repository. asserted = False - path = os.path.join('git', 'test', 'test_index.py') + path = osp.join('git', 'test', 'test_index.py') try: rw_bare_repo.index.add([path]) except InvalidGitRepositoryError: asserted = True assert asserted, "Adding using a filename is not correctly asserted." - @skipIf(HIDE_WINDOWS_KNOWN_ERRORS and sys.version_info[:2] == (2, 7), r""" + @skipIf(HIDE_WINDOWS_KNOWN_ERRORS and not PY3, r""" FIXME: File "C:\projects\gitpython\git\util.py", line 125, in to_native_path_linux return path.replace('\\', '/') UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)""") @with_rw_directory def test_add_utf8P_path(self, rw_dir): # NOTE: fp is not a Unicode object in python 2 (which is the source of the problem) - fp = os.path.join(rw_dir, 'ø.txt') + fp = osp.join(rw_dir, 'ø.txt') with open(fp, 'wb') as fs: fs.write(u'content of ø'.encode('utf-8')) @@ -840,7 +848,7 @@ class TestIndex(TestBase): @with_rw_directory def test_add_a_file_with_wildcard_chars(self, rw_dir): # see issue #407 - fp = os.path.join(rw_dir, '[.exe') + fp = osp.join(rw_dir, '[.exe') with open(fp, "wb") as f: f.write(b'something') diff --git a/git/test/test_reflog.py b/git/test/test_reflog.py index dffedf3b..20495be1 100644 --- a/git/test/test_reflog.py +++ b/git/test/test_reflog.py @@ -1,17 +1,18 @@ -from git.test.lib import ( - TestBase, - fixture_path -) +import os +import tempfile + from git.objects import IndexObject from git.refs import ( RefLogEntry, RefLog ) -from git.util import Actor, rmtree -from gitdb.util import hex_to_bin +from git.test.lib import ( + TestBase, + fixture_path +) +from git.util import Actor, rmtree, hex_to_bin -import tempfile -import os +import os.path as osp class TestRefLog(TestBase): @@ -42,7 +43,7 @@ class TestRefLog(TestBase): os.mkdir(tdir) rlp_master_ro = RefLog.path(self.rorepo.head) - assert os.path.isfile(rlp_master_ro) + assert osp.isfile(rlp_master_ro) # simple read reflog = RefLog.from_file(rlp_master_ro) @@ -70,7 +71,7 @@ class TestRefLog(TestBase): cr = self.rorepo.config_reader() for rlp in (rlp_head, rlp_master): reflog = RefLog.from_file(rlp) - tfile = os.path.join(tdir, os.path.basename(rlp)) + tfile = osp.join(tdir, osp.basename(rlp)) reflog.to_file(tfile) assert reflog.write() is reflog diff --git a/git/test/test_refs.py b/git/test/test_refs.py index 43f1dcc7..fd0be108 100644 --- a/git/test/test_refs.py +++ b/git/test/test_refs.py @@ -4,10 +4,8 @@ # This module is part of GitPython and is released under # the BSD License: http://www.opensource.org/licenses/bsd-license.php -from git.test.lib import ( - TestBase, - with_rw_repo -) +from itertools import chain + from git import ( Reference, Head, @@ -18,11 +16,15 @@ from git import ( GitCommandError, RefLog ) -import git.refs as refs -from git.util import Actor from git.objects.tag import TagObject -from itertools import chain -import os +from git.test.lib import ( + TestBase, + with_rw_repo +) +from git.util import Actor + +import git.refs as refs +import os.path as osp class TestRefs(TestBase): @@ -268,10 +270,10 @@ class TestRefs(TestBase): assert tmp_head == new_head and tmp_head.object == new_head.object logfile = RefLog.path(tmp_head) - assert os.path.isfile(logfile) + assert osp.isfile(logfile) Head.delete(rw_repo, tmp_head) # deletion removes the log as well - assert not os.path.isfile(logfile) + assert not osp.isfile(logfile) heads = rw_repo.heads assert tmp_head not in heads and new_head not in heads # force on deletion testing would be missing here, code looks okay though ;) @@ -450,12 +452,12 @@ class TestRefs(TestBase): symbol_ref_path = "refs/symbol_ref" symref = SymbolicReference(rw_repo, symbol_ref_path) assert symref.path == symbol_ref_path - symbol_ref_abspath = os.path.join(rw_repo.git_dir, symref.path) + symbol_ref_abspath = osp.join(rw_repo.git_dir, symref.path) # set it symref.reference = new_head assert symref.reference == new_head - assert os.path.isfile(symbol_ref_abspath) + assert osp.isfile(symbol_ref_abspath) assert symref.commit == new_head.commit for name in ('absname', 'folder/rela_name'): @@ -507,7 +509,7 @@ class TestRefs(TestBase): rw_repo.head.reference = Head.create(rw_repo, "master") # At least the head should still exist - assert os.path.isfile(os.path.join(rw_repo.git_dir, 'HEAD')) + assert osp.isfile(osp.join(rw_repo.git_dir, 'HEAD')) refs = list(SymbolicReference.iter_items(rw_repo)) assert len(refs) == 1 diff --git a/git/test/test_repo.py b/git/test/test_repo.py index a0a6a5b0..8b644f7f 100644 --- a/git/test/test_repo.py +++ b/git/test/test_repo.py @@ -50,10 +50,9 @@ from git.test.lib import ( assert_true, raises ) -from git.util import HIDE_WINDOWS_KNOWN_ERRORS +from git.util import HIDE_WINDOWS_KNOWN_ERRORS, cygpath from git.test.lib import with_rw_directory -from git.util import join_path_native, rmtree, rmfile -from gitdb.util import bin_to_hex +from git.util import join_path_native, rmtree, rmfile, bin_to_hex from unittest import SkipTest import functools as fnt @@ -203,8 +202,8 @@ class TestRepo(TestBase): prev_cwd = os.getcwd() os.chdir(tempfile.gettempdir()) git_dir_rela = "repos/foo/bar.git" - del_dir_abs = os.path.abspath("repos") - git_dir_abs = os.path.abspath(git_dir_rela) + del_dir_abs = osp.abspath("repos") + git_dir_abs = osp.abspath(git_dir_rela) try: # with specific path for path in (git_dir_rela, git_dir_abs): @@ -212,7 +211,7 @@ class TestRepo(TestBase): self.assertIsInstance(r, Repo) assert r.bare is True assert not r.has_separate_working_tree() - assert os.path.isdir(r.git_dir) + assert osp.isdir(r.git_dir) self._assert_empty_repo(r) @@ -306,16 +305,16 @@ class TestRepo(TestBase): def test_is_dirty_with_path(self, rwrepo): assert rwrepo.is_dirty(path="git") is False - with open(os.path.join(rwrepo.working_dir, "git", "util.py"), "at") as f: + with open(osp.join(rwrepo.working_dir, "git", "util.py"), "at") as f: f.write("junk") assert rwrepo.is_dirty(path="git") is True assert rwrepo.is_dirty(path="doc") is False - rwrepo.git.add(os.path.join("git", "util.py")) + rwrepo.git.add(Git.polish_url(osp.join("git", "util.py"))) assert rwrepo.is_dirty(index=False, path="git") is False assert rwrepo.is_dirty(path="git") is True - with open(os.path.join(rwrepo.working_dir, "doc", "no-such-file.txt"), "wt") as f: + with open(osp.join(rwrepo.working_dir, "doc", "no-such-file.txt"), "wt") as f: f.write("junk") assert rwrepo.is_dirty(path="doc") is False assert rwrepo.is_dirty(untracked_files=True, path="doc") is True @@ -412,6 +411,14 @@ class TestRepo(TestBase): self.assertEqual(len(res), 1) self.assertEqual(len(res[0][1]), 83, "Unexpected amount of parsed blame lines") + @skipIf(HIDE_WINDOWS_KNOWN_ERRORS and Git.is_cygwin(), + """FIXME: File "C:\projects\gitpython\git\cmd.py", line 671, in execute + raise GitCommandError(command, status, stderr_value, stdout_value) + GitCommandError: Cmd('git') failed due to: exit code(128) + cmdline: git add 1__��ava verb��ten 1_test _myfile 1_test_other_file + 1_��ava-----verb��ten + stderr: 'fatal: pathspec '"1__çava verböten"' did not match any files' + """) @with_rw_repo('HEAD', bare=False) def test_untracked_files(self, rwrepo): for run, (repo_add, is_invoking_git) in enumerate(( @@ -494,10 +501,10 @@ class TestRepo(TestBase): ph = os.environ.get('HOME') try: os.environ['HOME'] = rw_dir - Repo.init(os.path.join('~', 'test.git'), bare=True) + Repo.init(osp.join('~', 'test.git'), bare=True) os.environ['FOO'] = rw_dir - Repo.init(os.path.join('$FOO', 'test.git'), bare=True) + Repo.init(osp.join('$FOO', 'test.git'), bare=True) finally: if ph: os.environ['HOME'] = ph @@ -785,7 +792,7 @@ class TestRepo(TestBase): @with_rw_repo('HEAD') def test_git_file(self, rwrepo): # Move the .git directory to another location and create the .git file. - real_path_abs = os.path.abspath(join_path_native(rwrepo.working_tree_dir, '.real')) + real_path_abs = osp.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') with open(git_file_path, 'wb') as fp: @@ -793,13 +800,13 @@ class TestRepo(TestBase): # 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) + self.assertEqual(osp.abspath(git_file_repo.git_dir), real_path_abs) # Test using an absolute gitdir path in the .git file. 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) + self.assertEqual(osp.abspath(git_file_repo.git_dir), real_path_abs) @skipIf(HIDE_WINDOWS_KNOWN_ERRORS and PY3, "FIXME: smmp fails with: TypeError: Can't convert 'bytes' object to str implicitly") @@ -840,7 +847,7 @@ class TestRepo(TestBase): # It's expected to not be able to access a tree self.failUnlessRaises(ValueError, r.tree) - new_file_path = os.path.join(rw_dir, "new_file.ext") + new_file_path = osp.join(rw_dir, "new_file.ext") touch(new_file_path) r.index.add([new_file_path]) r.index.commit("initial commit\nBAD MESSAGE 1\n") @@ -901,9 +908,6 @@ class TestRepo(TestBase): for i, j in itertools.permutations([c1, 'ffffff', ''], r=2): self.assertRaises(GitCommandError, repo.is_ancestor, i, j) - # @skipIf(HIDE_WINDOWS_KNOWN_ERRORS, - # "FIXME: helper.wrapper fails with: PermissionError: [WinError 5] Access is denied: " - # "'C:\\Users\\appveyor\\AppData\\Local\\Temp\\1\\test_work_tree_unsupportedryfa60di\\master_repo\\.git\\objects\\pack\\pack-bc9e0787aef9f69e1591ef38ea0a6f566ec66fe3.idx") # noqa E501 @with_rw_directory def test_work_tree_unsupported(self, rw_dir): git = Git(rw_dir) @@ -913,6 +917,8 @@ class TestRepo(TestBase): rw_master = self.rorepo.clone(join_path_native(rw_dir, 'master_repo')) rw_master.git.checkout('HEAD~10') worktree_path = join_path_native(rw_dir, 'worktree_repo') + if Git.is_cygwin(): + worktree_path = cygpath(worktree_path) try: rw_master.git.worktree('add', worktree_path, 'master') except Exception as ex: diff --git a/git/test/test_submodule.py b/git/test/test_submodule.py index 9db4f9c9..fcaad04b 100644 --- a/git/test/test_submodule.py +++ b/git/test/test_submodule.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # This module is part of GitPython and is released under # the BSD License: http://www.opensource.org/licenses/bsd-license.php import os @@ -6,24 +7,34 @@ from unittest.case import skipIf import git from git.cmd import Git -from git.compat import string_types, is_win +from git.compat import ( + string_types, + is_win, +) from git.exc import ( InvalidGitRepositoryError, RepositoryDirtyError ) from git.objects.submodule.base import Submodule -from git.objects.submodule.root import RootModule, RootUpdateProgress +from git.objects.submodule.root import ( + RootModule, + RootUpdateProgress, +) from git.repo.fun import ( find_git_dir, - touch + touch, ) from git.test.lib import ( TestBase, - with_rw_repo + with_rw_repo, ) from git.test.lib import with_rw_directory -from git.util import HIDE_WINDOWS_KNOWN_ERRORS -from git.util import to_native_path_linux, join_path_native +from git.util import ( + to_native_path_linux, + join_path_native, + HIDE_WINDOWS_KNOWN_ERRORS, +) + import os.path as osp @@ -673,6 +684,13 @@ class TestSubmodule(TestBase): url=empty_repo_dir, no_checkout=checkout_mode and True or False) # end for each checkout mode + @skipIf(HIDE_WINDOWS_KNOWN_ERRORS and Git.is_cygwin(), + """FIXME: ile "C:\projects\gitpython\git\cmd.py", line 671, in execute + raise GitCommandError(command, status, stderr_value, stdout_value) + GitCommandError: Cmd('git') failed due to: exit code(128) + cmdline: git add 1__Xava verbXXten 1_test _myfile 1_test_other_file 1_XXava-----verbXXten + stderr: 'fatal: pathspec '"1__çava verböten"' did not match any files' + """) @with_rw_directory def test_git_submodules_and_add_sm_with_new_commit(self, rwdir): parent = git.Repo.init(osp.join(rwdir, 'parent')) @@ -705,7 +723,7 @@ class TestSubmodule(TestBase): fp = osp.join(smm.working_tree_dir, 'empty-file') with open(fp, 'w'): pass - smm.git.add(fp) + smm.git.add(Git.polish_url(fp)) smm.git.commit(m="new file added") # submodules are retrieved from the current commit's tree, therefore we can't really get a new submodule @@ -730,7 +748,7 @@ class TestSubmodule(TestBase): assert commit_sm.binsha == sm_too.binsha assert sm_too.binsha != sm.binsha - # @skipIf(HIDE_WINDOWS_KNOWN_ERRORS, + # @skipIf(HIDE_WINDOWS_KNOWN_ERRORS, ## ACTUALLY skipped by `git.submodule.base#L869`. # "FIXME: helper.wrapper fails with: PermissionError: [WinError 5] Access is denied: " # "'C:\\Users\\appveyor\\AppData\\Local\\Temp\\1\\test_work_tree_unsupportedryfa60di\\master_repo\\.git\\objects\\pack\\pack-bc9e0787aef9f69e1591ef38ea0a6f566ec66fe3.idx") # noqa E501 @with_rw_directory diff --git a/git/test/test_tree.py b/git/test/test_tree.py index f36c4337..f9259874 100644 --- a/git/test/test_tree.py +++ b/git/test/test_tree.py @@ -5,7 +5,6 @@ # the BSD License: http://www.opensource.org/licenses/bsd-license.php from io import BytesIO -import os import sys from unittest.case import skipIf @@ -13,8 +12,10 @@ from git import ( Tree, Blob ) -from git.util import HIDE_WINDOWS_KNOWN_ERRORS from git.test.lib import TestBase +from git.util import HIDE_WINDOWS_KNOWN_ERRORS + +import os.path as osp class TestTree(TestBase): @@ -90,12 +91,12 @@ class TestTree(TestBase): assert len(set(b for b in root if isinstance(b, Blob)) | set(root.blobs)) == len(root.blobs) subitem = trees[0][0] assert "/" in subitem.path - assert subitem.name == os.path.basename(subitem.path) + assert subitem.name == osp.basename(subitem.path) # assure that at some point the traversed paths have a slash in them found_slash = False for item in root.traverse(): - assert os.path.isabs(item.abspath) + assert osp.isabs(item.abspath) if '/' in item.path: found_slash = True # END check for slash diff --git a/git/test/test_util.py b/git/test/test_util.py index e07417b4..8f8d2272 100644 --- a/git/test/test_util.py +++ b/git/test/test_util.py @@ -5,7 +5,19 @@ # the BSD License: http://www.opensource.org/licenses/bsd-license.php import tempfile +import time +from unittest.case import skipIf + +import ddt +from git.cmd import dashify +from git.compat import string_types, is_win +from git.objects.util import ( + altz_to_utctz_str, + utctz_to_altz, + verify_utctz, + parse_date, +) from git.test.lib import ( TestBase, assert_equal @@ -15,19 +27,37 @@ from git.util import ( BlockingLockFile, get_user_id, Actor, - IterableList + IterableList, + cygpath, + decygpath ) -from git.objects.util import ( - altz_to_utctz_str, - utctz_to_altz, - verify_utctz, - parse_date, + + +_norm_cygpath_pairs = ( + (r'foo\bar', 'foo/bar'), + (r'foo/bar', 'foo/bar'), + + (r'C:\Users', '/cygdrive/c/Users'), + (r'C:\d/e', '/cygdrive/c/d/e'), + + ('C:\\', '/cygdrive/c/'), + + (r'\\server\C$\Users', '//server/C$/Users'), + (r'\\server\C$', '//server/C$'), + ('\\\\server\\c$\\', '//server/c$/'), + (r'\\server\BAR/', '//server/BAR/'), + + (r'D:/Apps', '/cygdrive/d/Apps'), + (r'D:/Apps\fOO', '/cygdrive/d/Apps/fOO'), + (r'D:\Apps/123', '/cygdrive/d/Apps/123'), ) -from git.cmd import dashify -from git.compat import string_types, is_win -import time -import ddt +_unc_cygpath_pairs = ( + (r'\\?\a:\com', '/cygdrive/a/com'), + (r'\\?\a:/com', '/cygdrive/a/com'), + + (r'\\?\UNC\server\D$\Apps', '//server/D$/Apps'), +) class TestIterableMember(object): @@ -52,6 +82,46 @@ class TestUtils(TestBase): "array": [42], } + @skipIf(not is_win, "Paths specifically for Windows.") + @ddt.idata(_norm_cygpath_pairs + _unc_cygpath_pairs) + def test_cygpath_ok(self, case): + wpath, cpath = case + cwpath = cygpath(wpath) + self.assertEqual(cwpath, cpath, wpath) + + @skipIf(not is_win, "Paths specifically for Windows.") + @ddt.data( + (r'./bar', 'bar'), + (r'.\bar', 'bar'), + (r'../bar', '../bar'), + (r'..\bar', '../bar'), + (r'../bar/.\foo/../chu', '../bar/chu'), + ) + def test_cygpath_norm_ok(self, case): + wpath, cpath = case + cwpath = cygpath(wpath) + self.assertEqual(cwpath, cpath or wpath, wpath) + + @skipIf(not is_win, "Paths specifically for Windows.") + @ddt.data( + r'C:', + r'C:Relative', + r'D:Apps\123', + r'D:Apps/123', + r'\\?\a:rel', + r'\\share\a:rel', + ) + def test_cygpath_invalids(self, wpath): + cwpath = cygpath(wpath) + self.assertEqual(cwpath, wpath.replace('\\', '/'), wpath) + + @skipIf(not is_win, "Paths specifically for Windows.") + @ddt.idata(_norm_cygpath_pairs) + def test_decygpath(self, case): + wpath, cpath = case + wcpath = decygpath(cpath) + self.assertEqual(wcpath, wpath.replace('/', '\\'), cpath) + def test_it_should_dashify(self): assert_equal('this-is-my-argument', dashify('this_is_my_argument')) assert_equal('foo', dashify('foo')) |