From d5697fc862e52e82dff5bef55eda0f65cec4872d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Lipski?= Date: Mon, 27 Feb 2023 13:35:17 +0100 Subject: [PATCH] Fix `TestGitHub` failing in envs with `gh` present (#832) --- .../enforce-consistent-style-for-github.sh | 2 +- git_machete/client.py | 2 +- tests/test_github.py | 24 ++++++++++++------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/ci/checks/enforce-consistent-style-for-github.sh b/ci/checks/enforce-consistent-style-for-github.sh index 62fb8dc88..b1220dd9b 100755 --- a/ci/checks/enforce-consistent-style-for-github.sh +++ b/ci/checks/enforce-consistent-style-for-github.sh @@ -6,6 +6,6 @@ self_dir=$(cd "$(dirname "$0")" &>/dev/null; pwd -P) source "$self_dir"/utils.sh self_name=$(basename "$0") -if git grep -w Github -- :!**/$self_name; then +if git grep Github -- :!**/$self_name; then die "Please use 'GitHub' instead of 'Github'." fi diff --git a/git_machete/client.py b/git_machete/client.py index 5a8dc7a3c..7bcaf09c6 100644 --- a/git_machete/client.py +++ b/git_machete/client.py @@ -1502,7 +1502,7 @@ def __sync_annotations_to_definition_file(self, if pr.base != upstream: warn(f'branch {bold(pr.head)} has a different base in PR #{bold(str(pr.number))} ({bold(pr.base)}) ' f'than in machete file ({bold(upstream) if upstream else ""})') - anno += f" WRONG PR BASE or MACHETE PARENT? PR has {bold(pr.base)}" + anno += f" WRONG PR BASE or MACHETE PARENT? PR has {pr.base}" old_annotation_text, old_annotation_qualifiers_text = '', '' if LocalBranchShortName.of(pr.head) in self.__annotations: old_annotation_text = self.__annotations[LocalBranchShortName.of(pr.head)].text diff --git a/tests/test_github.py b/tests/test_github.py index a1a5a03df..c79ccc8c0 100644 --- a/tests/test_github.py +++ b/tests/test_github.py @@ -3,7 +3,7 @@ from subprocess import CalledProcessError, CompletedProcess from tempfile import mkdtemp from textwrap import dedent -from typing import Any, Dict, List, Optional +from typing import Any, Callable, Dict, List, Optional from unittest.mock import mock_open import pytest @@ -88,12 +88,12 @@ def mock_os_environ_get_github_token(self: Any, key: str, default: Optional[str] return default -def mock_shutil_which_gh(cmd: Any) -> str: - return 'path_to_gh_executable' +def mock_shutil_which_gh(path: Optional[str]) -> Callable[[Any], str]: + return lambda cmd: path -def mock_subprocess_run(*args, stdout: bytes, stderr: bytes) -> "CompletedProcess": # type: ignore[no-untyped-def, type-arg] - return CompletedProcess(args, 0, b'stdout', b'Token: ghp_mytoken_for_github_com_from_gh_cli') +def mock_subprocess_run(returncode: int, stdout: str = '', stderr: str = ''): # type: ignore[no-untyped-def] + return lambda *args, **kwargs: CompletedProcess(args, returncode, bytes(stdout, 'utf-8'), bytes(stderr, 'utf-8')) prs_per_page = 3 @@ -128,7 +128,7 @@ def mock_info(x: Any) -> Dict[str, Any]: mock_info.counter = mock_read.counter = 0 # type: ignore[attr-defined] -class TestGithub: +class TestGitHub: mock_repository_info: Dict[str, str] = {'full_name': 'testing/checkout_prs', 'html_url': 'https://github.com/tester/repo_sandbox.git'} @@ -1869,6 +1869,7 @@ def test_github_enterprise_domain(self, mocker: Any) -> None: def test_github_token_retrieval_order(self, mocker: Any) -> None: mocker.patch('os.path.isfile', mock_is_file_false) + mocker.patch('shutil.which', mock_shutil_which_gh(None)) mocker.patch('urllib.request.urlopen', MockContextManager) mocker.patch('git_machete.github.github_remote_url_patterns', mock_github_remote_url_patterns) mocker.patch('_collections_abc.Mapping.get', mock_os_environ_get_none) @@ -1930,8 +1931,14 @@ def test_get_token_from_file_in_home_directory(self, mocker: Any) -> None: def test_get_token_from_gh(self, mocker: Any) -> None: mocker.patch('os.path.isfile', mock_is_file_false) mocker.patch('_collections_abc.Mapping.get', mock_os_environ_get_none) - mocker.patch('shutil.which', mock_shutil_which_gh) - mocker.patch('subprocess.run', mock_subprocess_run) + mocker.patch('shutil.which', mock_shutil_which_gh('/path/to/gh')) + mocker.patch('subprocess.run', mock_subprocess_run(returncode=0, stdout='stdout', stderr=''' + github.com + ✓ Logged in to github.com as Foo Bar (/Users/foo_bar/.config/gh/hosts.yml) + ✓ Git operations for github.com configured to use ssh protocol. + ✓ Token: ghp_mytoken_for_github_com_from_gh_cli + ✓ Token scopes: gist, read:discussion, read:org, repo, workflow + ''')) domain = 'git.example.com' github_token = GitHubToken.for_domain(domain=domain) @@ -1954,6 +1961,7 @@ def test_get_token_from_hub(self, mocker: Any) -> None: mocker.patch('builtins.open', mock_open(read_data=dedent(config_hub_contents))) mocker.patch('os.path.isfile', mock_is_file_not_github_token) + mocker.patch('subprocess.run', mock_subprocess_run(returncode=1)) github_token = GitHubToken.for_domain(domain=domain1) assert github_token.provider == f'auth token for {domain1} from `hub` GitHub CLI'