-
Notifications
You must be signed in to change notification settings - Fork 156
fix(install): surface custom port in generic host clone/ls-remote error #804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
danielmeppiel
merged 4 commits into
microsoft:main
from
edenfunf:fix/clone-error-port-display
Apr 23, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
28b7a30
fix(install): surface custom port in generic host clone/ls-remote error
edenfunf f9dd0f5
Merge branch 'main' into fix/clone-error-port-display
danielmeppiel 4a88c8e
fix(install): address review feedback on #798 generic-host port fix +…
edenfunf afbc529
Merge branch 'main' into fix/clone-error-port-display
danielmeppiel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| """Regression tests for issue #798 -- is_generic error paths must render host:port. | ||
|
|
||
| When a non-GitHub/non-ADO host fails during ``_clone_with_fallback`` or | ||
| ``list_remote_refs``, the diagnostic hint ``"For private repositories on | ||
| {host}, ..."`` previously rendered the bare host and dropped the port. | ||
| Users on Bitbucket Datacenter (custom port 7999) or any self-hosted host | ||
| on a non-default port saw a message that hid the very detail they | ||
| needed to verify their credentials against. | ||
|
|
||
| The fix routes both call sites through | ||
| ``AuthResolver.classify_host(...).display_name``, so the generic branch | ||
| shares port rendering with the adjacent ADO / auth branches (which | ||
| already used ``build_error_context`` -> ``host_info.display_name``). | ||
| """ | ||
|
|
||
| import os | ||
| import shutil | ||
| import tempfile | ||
| from pathlib import Path | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest | ||
| from git.exc import GitCommandError | ||
|
|
||
| from apm_cli.deps.github_downloader import GitHubPackageDownloader | ||
| from apm_cli.models.apm_package import DependencyReference | ||
|
|
||
|
|
||
| def _make_downloader(): | ||
| """Build a GitHubPackageDownloader with a real AuthResolver. | ||
|
|
||
| The real resolver is required here: the regression is specifically | ||
| about ``classify_host`` feeding ``HostInfo.display_name`` into the | ||
| rendered error message. A mocked resolver would bypass the exact | ||
| integration under test. | ||
| """ | ||
| with patch.dict(os.environ, {}, clear=True), patch( | ||
| "apm_cli.core.token_manager.GitHubTokenManager.resolve_credential_from_git", | ||
| return_value=None, | ||
| ): | ||
| dl = GitHubPackageDownloader() | ||
| dl.auth_resolver._cache.clear() | ||
| dl._resolve_dep_token = MagicMock(return_value=None) | ||
| return dl | ||
|
|
||
|
|
||
| def _diagnostic_prefix(msg: str) -> str: | ||
| """Strip the `` Last error: ...`` tail so assertions only see our hint. | ||
|
|
||
| The sanitized git-command text can echo arbitrary hostnames; we | ||
| don't want a bare-host regression hiding behind that noise. | ||
| """ | ||
| return msg.split(" Last error:", 1)[0] | ||
|
|
||
|
|
||
| class TestGenericHostCloneErrorPort: | ||
| """``_clone_with_fallback`` generic branch (github_downloader.py ~L864).""" | ||
|
|
||
| def _clone_error(self, dep) -> str: | ||
| dl = _make_downloader() | ||
|
edenfunf marked this conversation as resolved.
|
||
|
|
||
| def _fake_clone(*_args, **_kwargs): | ||
| raise GitCommandError("clone", 128) | ||
|
|
||
| with patch.dict(os.environ, {}, clear=True), patch( | ||
| "apm_cli.core.token_manager.GitHubTokenManager.resolve_credential_from_git", | ||
| return_value=None, | ||
| ), patch("apm_cli.deps.github_downloader.Repo") as MockRepo: | ||
| MockRepo.clone_from.side_effect = _fake_clone | ||
| target = Path(tempfile.mkdtemp()) | ||
| try: | ||
| with pytest.raises(RuntimeError) as exc_info: | ||
| dl._clone_with_fallback(dep.repo_url, target, dep_ref=dep) | ||
| return str(exc_info.value) | ||
| finally: | ||
| shutil.rmtree(target, ignore_errors=True) | ||
|
|
||
| def test_ssh_custom_port_surfaces_in_error(self): | ||
| """Bitbucket-DC-style ssh://host:7999/... -> hint names host:7999.""" | ||
| dep = DependencyReference.parse( | ||
| "ssh://git@bitbucket.example.com:7999/project/repo.git" | ||
| ) | ||
| assert dep.port == 7999 | ||
|
|
||
| prefix = _diagnostic_prefix(self._clone_error(dep)) | ||
| assert "For private repositories on bitbucket.example.com:7999," in prefix | ||
|
|
||
| def test_https_custom_port_surfaces_in_error(self): | ||
| """https://host:7990/... -> hint names host:7990.""" | ||
| dep = DependencyReference.parse( | ||
| "https://bitbucket.example.com:7990/project/repo.git" | ||
| ) | ||
| assert dep.port == 7990 | ||
|
|
||
| prefix = _diagnostic_prefix(self._clone_error(dep)) | ||
| assert "For private repositories on bitbucket.example.com:7990," in prefix | ||
|
|
||
| def test_no_port_renders_bare_host(self): | ||
| """Default-port dep has no port suffix -- no regression for common case.""" | ||
| dep = DependencyReference.parse( | ||
| "https://gitlab.example.com/team/repo.git" | ||
| ) | ||
| assert dep.port is None | ||
|
|
||
| prefix = _diagnostic_prefix(self._clone_error(dep)) | ||
| assert "For private repositories on gitlab.example.com," in prefix | ||
| assert "gitlab.example.com:" not in prefix, ( | ||
| f"bare-host case must not synthesise a stray ':' suffix: {prefix!r}" | ||
| ) | ||
|
|
||
|
|
||
| class TestGenericHostLsRemoteErrorPort: | ||
| """``list_remote_refs`` generic branch (github_downloader.py ~L1035).""" | ||
|
|
||
| def _ls_remote_error(self, dep) -> str: | ||
| dl = _make_downloader() | ||
| with patch.dict(os.environ, {}, clear=True), patch( | ||
| "apm_cli.core.token_manager.GitHubTokenManager.resolve_credential_from_git", | ||
| return_value=None, | ||
| ), patch( | ||
| "apm_cli.deps.github_downloader.git.cmd.Git" | ||
| ) as MockGitCmd: | ||
| mock_git = MockGitCmd.return_value | ||
| mock_git.ls_remote.side_effect = GitCommandError("ls-remote", 128) | ||
| with pytest.raises(RuntimeError) as exc_info: | ||
| dl.list_remote_refs(dep) | ||
| return str(exc_info.value) | ||
|
|
||
| def test_ssh_custom_port_surfaces_in_error(self): | ||
| dep = DependencyReference.parse( | ||
| "ssh://git@bitbucket.example.com:7999/project/repo.git" | ||
| ) | ||
| assert dep.port == 7999 | ||
|
|
||
| prefix = _diagnostic_prefix(self._ls_remote_error(dep)) | ||
| assert "For private repositories on bitbucket.example.com:7999," in prefix | ||
|
|
||
| def test_https_custom_port_surfaces_in_error(self): | ||
| dep = DependencyReference.parse( | ||
| "https://bitbucket.example.com:7990/project/repo.git" | ||
| ) | ||
| assert dep.port == 7990 | ||
|
|
||
| prefix = _diagnostic_prefix(self._ls_remote_error(dep)) | ||
| assert "For private repositories on bitbucket.example.com:7990," in prefix | ||
|
|
||
| def test_no_port_renders_bare_host(self): | ||
| dep = DependencyReference.parse( | ||
| "https://gitlab.example.com/team/repo.git" | ||
| ) | ||
| assert dep.port is None | ||
|
|
||
| prefix = _diagnostic_prefix(self._ls_remote_error(dep)) | ||
| assert "For private repositories on gitlab.example.com," in prefix | ||
| assert "gitlab.example.com:" not in prefix, ( | ||
| f"bare-host case must not synthesise a stray ':' suffix: {prefix!r}" | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.