Skip to content

Commit

Permalink
Fix TestGitHub failing in envs with gh present (#832)
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelLipski authored Feb 27, 2023
1 parent 84b3158 commit d5697fc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ci/checks/enforce-consistent-style-for-github.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion git_machete/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<none, is a root>"})')
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
Expand Down
24 changes: 16 additions & 8 deletions tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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'
Expand Down

0 comments on commit d5697fc

Please sign in to comment.