|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from unittest.mock import Mock, patch |
| 4 | + |
| 5 | +from codeflash.cli_cmds.init_auth import install_github_app |
| 6 | + |
| 7 | + |
| 8 | +def _echo_messages(mock: Mock) -> list[str]: |
| 9 | + return [str(call.args[0]) for call in mock.call_args_list if call.args] |
| 10 | + |
| 11 | + |
| 12 | +def _mock_repo_context(monkeypatch) -> None: |
| 13 | + git_repo = object() |
| 14 | + monkeypatch.setattr("codeflash.cli_cmds.init_auth.git.Repo", Mock(return_value=git_repo)) |
| 15 | + monkeypatch.setattr("codeflash.cli_cmds.init_auth.get_git_remotes", Mock(return_value=["origin"])) |
| 16 | + monkeypatch.setattr("codeflash.cli_cmds.init_auth.get_repo_owner_and_name", Mock(return_value=("octocat", "demo"))) |
| 17 | + |
| 18 | + |
| 19 | +def test_install_github_app_allows_user_to_skip(monkeypatch) -> None: |
| 20 | + _mock_repo_context(monkeypatch) |
| 21 | + monkeypatch.setattr("codeflash.cli_cmds.init_auth.is_github_app_installed_on_repo", Mock(return_value=False)) |
| 22 | + echo = Mock() |
| 23 | + prompt = Mock() |
| 24 | + launch = Mock() |
| 25 | + monkeypatch.setattr("codeflash.cli_cmds.init_auth.click.echo", echo) |
| 26 | + monkeypatch.setattr("codeflash.cli_cmds.init_auth.click.prompt", prompt) |
| 27 | + monkeypatch.setattr("codeflash.cli_cmds.init_auth.click.launch", launch) |
| 28 | + |
| 29 | + with patch("rich.prompt.Confirm.ask", return_value=False) as confirm_ask: |
| 30 | + install_github_app("origin") |
| 31 | + |
| 32 | + confirm_ask.assert_called_once() |
| 33 | + prompt.assert_not_called() |
| 34 | + launch.assert_not_called() |
| 35 | + assert any("Skipping Codeflash GitHub app installation for octocat/demo." in message for message in _echo_messages(echo)) |
| 36 | + |
| 37 | + |
| 38 | +def test_install_github_app_skips_on_noninteractive_abort(monkeypatch) -> None: |
| 39 | + _mock_repo_context(monkeypatch) |
| 40 | + monkeypatch.setattr("codeflash.cli_cmds.init_auth.is_github_app_installed_on_repo", Mock(return_value=False)) |
| 41 | + echo = Mock() |
| 42 | + prompt = Mock() |
| 43 | + launch = Mock() |
| 44 | + monkeypatch.setattr("codeflash.cli_cmds.init_auth.click.echo", echo) |
| 45 | + monkeypatch.setattr("codeflash.cli_cmds.init_auth.click.prompt", prompt) |
| 46 | + monkeypatch.setattr("codeflash.cli_cmds.init_auth.click.launch", launch) |
| 47 | + |
| 48 | + with patch("rich.prompt.Confirm.ask", side_effect=EOFError): |
| 49 | + install_github_app("origin") |
| 50 | + |
| 51 | + prompt.assert_not_called() |
| 52 | + launch.assert_not_called() |
| 53 | + assert any("Skipping Codeflash GitHub app installation for octocat/demo." in message for message in _echo_messages(echo)) |
| 54 | + |
| 55 | + |
| 56 | +def test_install_github_app_still_runs_install_flow_when_confirmed(monkeypatch) -> None: |
| 57 | + _mock_repo_context(monkeypatch) |
| 58 | + monkeypatch.setattr( |
| 59 | + "codeflash.cli_cmds.init_auth.is_github_app_installed_on_repo", |
| 60 | + Mock(side_effect=[False, True]), |
| 61 | + ) |
| 62 | + echo = Mock() |
| 63 | + prompt = Mock(return_value="") |
| 64 | + launch = Mock() |
| 65 | + monkeypatch.setattr("codeflash.cli_cmds.init_auth.click.echo", echo) |
| 66 | + monkeypatch.setattr("codeflash.cli_cmds.init_auth.click.prompt", prompt) |
| 67 | + monkeypatch.setattr("codeflash.cli_cmds.init_auth.click.launch", launch) |
| 68 | + |
| 69 | + with patch("rich.prompt.Confirm.ask", return_value=True) as confirm_ask: |
| 70 | + install_github_app("origin") |
| 71 | + |
| 72 | + confirm_ask.assert_called_once() |
| 73 | + launch.assert_called_once_with("https://github.com/apps/codeflash-ai/installations/select_target") |
| 74 | + assert prompt.call_count == 2 |
0 commit comments