def test_git_check_rev_options_ref_not_found(get_sha_mock): get_sha_mock.return_value = None git = Git() rev_options = git.make_rev_options('develop') new_options = git.check_rev_options('.', rev_options) assert new_options.rev == 'develop'
def test_resolve_commit_not_on_branch(script: PipTestEnvironment, tmp_path: pathlib.Path) -> None: repo_path = tmp_path / "repo" repo_file = repo_path / "file.txt" clone_path = repo_path / "clone" repo_path.mkdir() script.run("git", "init", cwd=str(repo_path)) repo_file.write_text(".") script.run("git", "add", "file.txt", cwd=str(repo_path)) script.run("git", "commit", "-m", "initial commit", cwd=str(repo_path)) script.run("git", "checkout", "-b", "abranch", cwd=str(repo_path)) # create a commit repo_file.write_text("..") script.run("git", "commit", "-a", "-m", "commit 1", cwd=str(repo_path)) commit = script.run("git", "rev-parse", "HEAD", cwd=str(repo_path)).stdout.strip() # make sure our commit is not on a branch script.run("git", "checkout", "master", cwd=str(repo_path)) script.run("git", "branch", "-D", "abranch", cwd=str(repo_path)) # create a ref that points to our commit (repo_path / ".git" / "refs" / "myrefs").mkdir(parents=True) (repo_path / ".git" / "refs" / "myrefs" / "myref").write_text(commit) # check we can fetch our commit rev_options = Git.make_rev_options(commit) Git().fetch_new( str(clone_path), HiddenText(repo_path.as_uri(), redacted="*"), rev_options, verbosity=0, )
def test_clone_without_partial_clone_support(script: PipTestEnvironment, tmp_path: pathlib.Path) -> None: """Older git clients don't support partial clone. Test the fallback path""" repo_path = tmp_path / "repo" repo_file = _initialize_clonetest_server(repo_path, script, enable_partial_clone=True) clone_path = repo_path / "clone1" # Check that we can clone w/ old version of git w/o --filter with patch("pip._internal.vcs.git.Git.get_git_version", return_value=(2, 16)): Git().fetch_new( str(clone_path), HiddenText(repo_path.as_uri(), redacted="*"), Git.make_rev_options(), verbosity=0, ) repo_file.write_text("...") script.run("git", "commit", "-am", "third commit", cwd=str(repo_path)) # Should work fine w/o attempting to use `--filter` args assert ("warning: filtering not recognized by server, ignoring" not in script.run("git", "pull", cwd=clone_path).stderr)
def test_git_check_rev_options_ref_exists(get_sha_mock): get_sha_mock.return_value = '123456' git = Git() rev_options = git.make_rev_options('develop') new_options = git.check_rev_options('.', rev_options) assert new_options.rev == '123456'
def test_check_rev_options_ref_exists(get_sha_mock): get_sha_mock.return_value = '123456' git = Git() rev_options = git.make_rev_options('develop') new_options = git.check_rev_options('.', rev_options) assert new_options.rev == '123456'
def test_check_rev_options_ref_not_found(get_sha_mock): get_sha_mock.return_value = None git = Git() rev_options = git.make_rev_options('develop') new_options = git.check_rev_options('.', rev_options) assert new_options.rev == 'develop'
def test_check_rev_options_should_handle_tag_name(get_refs_mock): get_refs_mock.return_value = {'master': '123456', '0.1': 'abc123'} git = Git() rev_options = git.make_rev_options('0.1') new_options = git.check_rev_options('.', rev_options) assert new_options.rev == 'abc123'
def test_check_rev_options_should_handle_ambiguous_commit(get_refs_mock): get_refs_mock.return_value = {'master': '123456', '0.1': '123456'} git = Git() rev_options = git.make_rev_options('0.1') new_options = git.check_rev_options('.', rev_options) assert new_options.rev == '123456'
def test_git_resolve_revision_rev_exists(get_sha_mock): get_sha_mock.return_value = ('123456', False) url = 'git+https://git.example.com' rev_options = Git.make_rev_options('develop') new_options = Git.resolve_revision('.', url, rev_options) assert new_options.rev == '123456'
def test_git_resolve_revision_rev_not_found(get_sha_mock): get_sha_mock.return_value = (None, False) url = 'git+https://git.example.com' rev_options = Git.make_rev_options('develop') new_options = Git.resolve_revision('.', url, rev_options) assert new_options.rev == 'develop'
def git(): git_url = 'https://github.com/pypa/pip-test-package' sha = '5547fa909e83df8bd743d3978d6667497983a4b7' git = Git() git.get_remote_url = Mock(return_value=git_url) git.get_revision = Mock(return_value=sha) return git
def git(): git_url = 'https://github.com/pypa/pip-test-package' sha = '5547fa909e83df8bd743d3978d6667497983a4b7' git = Git() git.get_url = Mock(return_value=git_url) git.get_revision = Mock(return_value=sha) return git
def test_git_resolve_revision_rev_not_found(get_sha_mock: mock.Mock) -> None: get_sha_mock.return_value = (None, False) url = HiddenText("git+https://git.example.com", redacted="*") rev_options = Git.make_rev_options("develop") new_options = Git.resolve_revision(".", url, rev_options) assert new_options.rev == "develop"
def test_git_resolve_revision_rev_exists(get_sha_mock): get_sha_mock.return_value = ('123456', False) git = Git() rev_options = git.make_rev_options('develop') url = 'git+https://git.example.com' new_options = git.resolve_revision('.', url, rev_options) assert new_options.rev == '123456'
def test_git_resolve_revision_rev_not_found(get_sha_mock): get_sha_mock.return_value = (None, False) git = Git() rev_options = git.make_rev_options('develop') url = 'git+https://git.example.com' new_options = git.resolve_revision('.', url, rev_options) assert new_options.rev == 'develop'
def test_paths_are_not_mistaken_for_scp_shorthand(url, platform): # File paths should not be mistaken for SCP shorthand. If they do then # 'c:/piffle/wiffle' would end up as 'ssh://c/piffle/wiffle'. from pip._internal.vcs.git import SCP_REGEX assert not SCP_REGEX.match(url) if platform == os.name: with pytest.raises(RemoteNotValidError): Git._git_remote_to_pip_url(url)
def test_is_immutable_rev_checkout(script: PipTestEnvironment) -> None: version_pkg_path = os.fspath(_create_test_package(script.scratch_path)) commit = script.run("git", "rev-parse", "HEAD", cwd=version_pkg_path).stdout.strip() assert Git().is_immutable_rev_checkout("git+https://g.c/o/r@" + commit, version_pkg_path) assert not Git().is_immutable_rev_checkout("git+https://g.c/o/r", version_pkg_path) assert not Git().is_immutable_rev_checkout("git+https://g.c/o/r@master", version_pkg_path)
def test_get_repository_root(script): version_pkg_path = _create_test_package(script) tests_path = version_pkg_path.joinpath("tests") tests_path.mkdir() root1 = Git.get_repository_root(version_pkg_path) assert os.path.normcase(root1) == os.path.normcase(version_pkg_path) root2 = Git.get_repository_root(version_pkg_path.joinpath("tests")) assert os.path.normcase(root2) == os.path.normcase(version_pkg_path)
def test_is_immutable_rev_checkout(script): version_pkg_path = _create_test_package(script) commit = script.run('git', 'rev-parse', 'HEAD', cwd=version_pkg_path).stdout.strip() assert Git().is_immutable_rev_checkout("git+https://g.c/o/r@" + commit, version_pkg_path) assert not Git().is_immutable_rev_checkout("git+https://g.c/o/r", version_pkg_path) assert not Git().is_immutable_rev_checkout("git+https://g.c/o/r@master", version_pkg_path)
def test_get_short_refs_should_return_branch_name_and_commit_pair(script): version_pkg_path = _create_test_package(script) script.run('git', 'branch', 'branch0.1', cwd=version_pkg_path) commit = script.run( 'git', 'rev-parse', 'HEAD', cwd=version_pkg_path ).stdout.strip() git = Git() result = git.get_short_refs(version_pkg_path) assert result['master'] == commit, result assert result['branch0.1'] == commit, result
def test_git_dir_ignored(): """ Test that a GIT_DIR environment variable is ignored. """ git = Git() with TempDirectory() as temp: temp_dir = temp.path env = {'GIT_DIR': 'foo'} # If GIT_DIR is not ignored, then os.listdir() will return ['foo']. git.run_command(['init', temp_dir], cwd=temp_dir, extra_environ=env) assert os.listdir(temp_dir) == ['.git']
def test_get_repository_root(script: PipTestEnvironment) -> None: version_pkg_path = _create_test_package(script.scratch_path) tests_path = version_pkg_path.joinpath("tests") tests_path.mkdir() root1 = Git.get_repository_root(os.fspath(version_pkg_path)) assert root1 is not None assert os.path.normcase(root1) == os.path.normcase(version_pkg_path) root2 = Git.get_repository_root(os.fspath(tests_path)) assert root2 is not None assert os.path.normcase(root2) == os.path.normcase(version_pkg_path)
def test_get_remote_url__no_remote(script: PipTestEnvironment, tmpdir: Path) -> None: """ Test a repo with no remote. """ repo_path = tmpdir / "temp-repo" repo_path.mkdir() repo_dir = str(repo_path) script.run("git", "init", cwd=repo_dir) with pytest.raises(RemoteNotFoundError): Git.get_remote_url(repo_dir)
def test_git_dir_ignored(tmpdir): """ Test that a GIT_DIR environment variable is ignored. """ repo_path = tmpdir / 'test-repo' repo_path.mkdir() repo_dir = str(repo_path) env = {'GIT_DIR': 'foo'} # If GIT_DIR is not ignored, then os.listdir() will return ['foo']. Git.run_command(['init', repo_dir], cwd=repo_dir, extra_environ=env) assert os.listdir(repo_dir) == ['.git']
def test_check_version(script): version_pkg_path = _create_test_package(script) script.run('git', 'branch', 'branch0.1', cwd=version_pkg_path) commit = script.run( 'git', 'rev-parse', 'HEAD', cwd=version_pkg_path ).stdout.strip() git = Git() assert git.check_version(version_pkg_path, [commit]) assert git.check_version(version_pkg_path, [commit[:7]]) assert not git.check_version(version_pkg_path, ['branch0.1']) assert not git.check_version(version_pkg_path, ['abc123'])
def test_get_remote_url__no_remote(script, tmpdir): """ Test a repo with no remote. """ repo_dir = tmpdir / 'temp-repo' repo_dir.mkdir() repo_dir = str(repo_dir) script.run('git', 'init', cwd=repo_dir) with pytest.raises(RemoteNotFoundError): Git.get_remote_url(repo_dir)
def test_git_dir_ignored(tmpdir: pathlib.Path) -> None: """ Test that a GIT_DIR environment variable is ignored. """ repo_path = tmpdir / "test-repo" repo_path.mkdir() repo_dir = str(repo_path) env = {"GIT_DIR": "foo"} # If GIT_DIR is not ignored, then os.listdir() will return ['foo']. Git.run_command(["init", repo_dir], cwd=repo_dir, extra_environ=env) assert os.listdir(repo_dir) == [".git"]
def test_git_work_tree_ignored(): """ Test that a GIT_WORK_TREE environment variable is ignored. """ git = Git() with TempDirectory() as temp: temp_dir = temp.path git.run_command(['init', temp_dir], cwd=temp_dir) # Choose a directory relative to the cwd that does not exist. # If GIT_WORK_TREE is not ignored, then the command will error out # with: "fatal: This operation must be run in a work tree". env = {'GIT_WORK_TREE': 'foo'} git.run_command(['status', temp_dir], extra_environ=env, cwd=temp_dir)
def test_git__get_url_rev__idempotent() -> None: """ Check that Git.get_url_rev_and_auth() is idempotent for what the code calls "stub URLs" (i.e. URLs that don't contain "://"). Also check that it doesn't change self.url. """ url = "[email protected]:MyProject#egg=MyProject" result1 = Git.get_url_rev_and_auth(url) result2 = Git.get_url_rev_and_auth(url) expected = ("[email protected]:MyProject", None, (None, None)) assert result1 == expected assert result2 == expected
def test_git__get_url_rev__idempotent(): """ Check that Git.get_url_rev_and_auth() is idempotent for what the code calls "stub URLs" (i.e. URLs that don't contain "://"). Also check that it doesn't change self.url. """ url = '[email protected]:MyProject#egg=MyProject' result1 = Git.get_url_rev_and_auth(url) result2 = Git.get_url_rev_and_auth(url) expected = ('[email protected]:MyProject', None, (None, None)) assert result1 == expected assert result2 == expected
def test_git__get_url_rev__idempotent(): """ Check that Git.get_url_rev() is idempotent for what the code calls "stub URLs" (i.e. URLs that don't contain "://"). Also check that it doesn't change self.url. """ url = '[email protected]:MyProject#egg=MyProject' vcs = Git(url) result1 = vcs.get_url_rev(url) assert vcs.url == url result2 = vcs.get_url_rev(url) assert result1 == ('[email protected]:MyProject', None) assert result2 == ('[email protected]:MyProject', None)
def test_git_work_tree_ignored(tmpdir): """ Test that a GIT_WORK_TREE environment variable is ignored. """ repo_path = tmpdir / 'test-repo' repo_path.mkdir() repo_dir = str(repo_path) Git.run_command(['init', repo_dir], cwd=repo_dir) # Choose a directory relative to the cwd that does not exist. # If GIT_WORK_TREE is not ignored, then the command will error out # with: "fatal: This operation must be run in a work tree". env = {'GIT_WORK_TREE': 'foo'} Git.run_command(['status', repo_dir], extra_environ=env, cwd=repo_dir)
def test_is_commit_id_equal(script: PipTestEnvironment) -> None: """ Test Git.is_commit_id_equal(). """ version_pkg_path = _create_test_package(script) script.run("git", "branch", "branch0.1", cwd=version_pkg_path) commit = script.run("git", "rev-parse", "HEAD", cwd=version_pkg_path).stdout.strip() assert Git.is_commit_id_equal(version_pkg_path, commit) assert not Git.is_commit_id_equal(version_pkg_path, commit[:7]) assert not Git.is_commit_id_equal(version_pkg_path, "branch0.1") assert not Git.is_commit_id_equal(version_pkg_path, "abc123") # Also check passing a None value. assert not Git.is_commit_id_equal(version_pkg_path, None)
def test_git_work_tree_ignored(tmpdir: pathlib.Path) -> None: """ Test that a GIT_WORK_TREE environment variable is ignored. """ repo_path = tmpdir / "test-repo" repo_path.mkdir() repo_dir = str(repo_path) Git.run_command(["init", repo_dir], cwd=repo_dir) # Choose a directory relative to the cwd that does not exist. # If GIT_WORK_TREE is not ignored, then the command will error out # with: "fatal: This operation must be run in a work tree". env = {"GIT_WORK_TREE": "foo"} Git.run_command(["status", repo_dir], extra_environ=env, cwd=repo_dir)
def test_git_get_src_requirements( mock_get_subdirectory, mock_get_revision, mock_get_remote_url, git_url, target_url_prefix, ): sha = '5547fa909e83df8bd743d3978d6667497983a4b7' mock_get_remote_url.return_value = Git._git_remote_to_pip_url(git_url) mock_get_revision.return_value = sha mock_get_subdirectory.return_value = None ret = Git.get_src_requirement('.', 'pip-test-package') target = f"{target_url_prefix}@{sha}#egg=pip_test_package" assert ret == target
def test_is_commit_id_equal(script): """ Test Git.is_commit_id_equal(). """ version_pkg_path = _create_test_package(script) script.run('git', 'branch', 'branch0.1', cwd=version_pkg_path) commit = script.run('git', 'rev-parse', 'HEAD', cwd=version_pkg_path).stdout.strip() assert Git.is_commit_id_equal(version_pkg_path, commit) assert not Git.is_commit_id_equal(version_pkg_path, commit[:7]) assert not Git.is_commit_id_equal(version_pkg_path, 'branch0.1') assert not Git.is_commit_id_equal(version_pkg_path, 'abc123') # Also check passing a None value. assert not Git.is_commit_id_equal(version_pkg_path, None)
def test_get_current_branch(script): repo_dir = str(script.scratch_path) script.run('git', 'init', cwd=repo_dir) sha = do_commit(script, repo_dir) assert Git.get_current_branch(repo_dir) == 'master' # Switch to a branch with the same SHA as "master" but whose name # is alphabetically after. checkout_new_branch(script, repo_dir, 'release') assert Git.get_current_branch(repo_dir) == 'release' # Also test the detached HEAD case. checkout_ref(script, repo_dir, sha) assert Git.get_current_branch(repo_dir) is None
def test_git__get_netloc_and_auth(args, expected): """ Test VersionControl.get_netloc_and_auth(). """ netloc, scheme = args actual = Git.get_netloc_and_auth(netloc, scheme) assert actual == expected
def test_git_is_commit_id_equal(mock_get_revision: mock.Mock, rev_name: Optional[str], result: bool) -> None: """ Test Git.is_commit_id_equal(). """ mock_get_revision.return_value = "5547fa909e83df8bd743d3978d6667497983a4b7" assert Git.is_commit_id_equal("/path", rev_name) is result
def test_is_commit_id_equal(script): """ Test Git.is_commit_id_equal(). """ version_pkg_path = _create_test_package(script) script.run('git', 'branch', 'branch0.1', cwd=version_pkg_path) commit = script.run( 'git', 'rev-parse', 'HEAD', cwd=version_pkg_path ).stdout.strip() assert Git.is_commit_id_equal(version_pkg_path, commit) assert not Git.is_commit_id_equal(version_pkg_path, commit[:7]) assert not Git.is_commit_id_equal(version_pkg_path, 'branch0.1') assert not Git.is_commit_id_equal(version_pkg_path, 'abc123') # Also check passing a None value. assert not Git.is_commit_id_equal(version_pkg_path, None)
def test_get_current_branch__branch_and_tag_same_name(script, tmpdir): """ Check calling get_current_branch() from a branch or tag when the branch and tag have the same name. """ repo_dir = str(tmpdir) script.run('git', 'init', cwd=repo_dir) do_commit(script, repo_dir) checkout_new_branch(script, repo_dir, 'dev') # Create a tag with the same name as the branch. script.run('git', 'tag', 'dev', cwd=repo_dir) assert Git.get_current_branch(repo_dir) == 'dev' # Now try with the tag checked out. checkout_ref(script, repo_dir, 'refs/tags/dev') assert Git.get_current_branch(repo_dir) is None
def test_get_short_refs_should_ignore_no_branch(script): version_pkg_path = _create_test_package(script) script.run('git', 'branch', 'branch0.1', cwd=version_pkg_path) commit = script.run( 'git', 'rev-parse', 'HEAD', cwd=version_pkg_path ).stdout.strip() # current branch here is "* (nobranch)" script.run( 'git', 'checkout', commit, cwd=version_pkg_path, expect_stderr=True, ) git = Git() result = git.get_short_refs(version_pkg_path) assert result['master'] == commit, result assert result['branch0.1'] == commit, result
def test_get_branch(script, tmpdir): repo_dir = str(tmpdir) script.run('git', 'init', cwd=repo_dir) sha = do_commit(script, repo_dir) git = Git() assert git.get_branch(repo_dir) == 'master' # Switch to a branch with the same SHA as "master" but whose name # is alphabetically after. script.run( 'git', 'checkout', '-b', 'release', cwd=repo_dir, expect_stderr=True, ) assert git.get_branch(repo_dir) == 'release' # Also test the detached HEAD case. script.run('git', 'checkout', sha, cwd=repo_dir, expect_stderr=True) assert git.get_branch(repo_dir) is None
def test_check_rev_options_not_found_warning(get_sha_mock, caplog): get_sha_mock.return_value = None git = Git() sha = 40 * 'a' rev_options = git.make_rev_options(sha) new_options = git.check_rev_options('.', rev_options) assert new_options.rev == sha rev_options = git.make_rev_options(sha[:6]) new_options = git.check_rev_options('.', rev_options) assert new_options.rev == 'aaaaaa' # Check that a warning got logged only for the abbreviated hash. messages = [r.getMessage() for r in caplog.records] messages = [msg for msg in messages if msg.startswith('Did not find ')] assert messages == [ "Did not find branch or tag 'aaaaaa', assuming revision or ref." ]
def test_git_resolve_revision_not_found_warning(get_sha_mock, caplog): get_sha_mock.return_value = (None, False) url = 'git+https://git.example.com' sha = 40 * 'a' rev_options = Git.make_rev_options(sha) new_options = Git.resolve_revision('.', url, rev_options) assert new_options.rev == sha rev_options = Git.make_rev_options(sha[:6]) new_options = Git.resolve_revision('.', url, rev_options) assert new_options.rev == 'aaaaaa' # Check that a warning got logged only for the abbreviated hash. messages = [r.getMessage() for r in caplog.records] messages = [msg for msg in messages if msg.startswith('Did not find ')] assert messages == [ "Did not find branch or tag 'aaaaaa', assuming revision or ref." ]
def git(): git_url = 'http://github.com/pypa/pip-test-package' refs = { '0.1': 'a8992fc7ee17e5b9ece022417b64594423caca7c', '0.1.1': '7d654e66c8fa7149c165ddeffa5b56bc06619458', '0.1.2': 'f1c1020ebac81f9aeb5c766ff7a772f709e696ee', 'foo': '5547fa909e83df8bd743d3978d6667497983a4b7', 'bar': '5547fa909e83df8bd743d3978d6667497983a4b7', 'master': '5547fa909e83df8bd743d3978d6667497983a4b7', 'origin/master': '5547fa909e83df8bd743d3978d6667497983a4b7', 'origin/HEAD': '5547fa909e83df8bd743d3978d6667497983a4b7', } sha = refs['foo'] git = Git() git.get_url = Mock(return_value=git_url) git.get_revision = Mock(return_value=sha) git.get_short_refs = Mock(return_value=refs) return git
def test_git_get_src_requirements(mock_get_remote_url, mock_get_revision): git_url = 'https://github.com/pypa/pip-test-package' sha = '5547fa909e83df8bd743d3978d6667497983a4b7' mock_get_remote_url.return_value = git_url mock_get_revision.return_value = sha ret = Git.get_src_requirement('.', 'pip-test-package') assert ret == ( 'git+https://github.com/pypa/pip-test-package' '@5547fa909e83df8bd743d3978d6667497983a4b7#egg=pip_test_package' )
def test_get_remote_url(script, tmpdir): source_dir = tmpdir / 'source' source_dir.mkdir() source_url = _test_path_to_file_url(source_dir) source_dir = str(source_dir) script.run('git', 'init', cwd=source_dir) do_commit(script, source_dir) repo_dir = str(tmpdir / 'repo') script.run('git', 'clone', source_url, repo_dir, expect_stderr=True) remote_url = Git.get_remote_url(repo_dir) assert remote_url == source_url
def check_rev(repo_dir, rev, expected): assert Git.get_revision_sha(repo_dir, rev) == expected
def check_rev(repo_dir, rev, expected): git = Git() assert git.get_revision_sha(repo_dir, rev) == expected
def test_git_is_commit_id_equal(mock_get_revision, rev_name, result): """ Test Git.is_commit_id_equal(). """ mock_get_revision.return_value = '5547fa909e83df8bd743d3978d6667497983a4b7' assert Git.is_commit_id_equal('/path', rev_name) is result
def test_git__make_rev_args(username, password, expected): """ Test VersionControl.make_rev_args(). """ actual = Git.make_rev_args(username, password) assert actual == expected
def test_check_rev_options_should_handle_branch_name(get_refs_mock): get_refs_mock.return_value = {'master': '123456', '0.1': '123456'} git = Git() result = git.check_rev_options('master', '.', []) assert result == ['123456']
def test_check_rev_options_should_handle_ambiguous_commit(get_refs_mock): get_refs_mock.return_value = {'master': '123456', '0.1': '123456'} git = Git() result = git.check_rev_options('0.1', '.', []) assert result == ['123456'], result