|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | from argparse import Namespace |
4 | | -from unittest.mock import Mock |
| 4 | +from pathlib import Path |
| 5 | +from unittest.mock import Mock, patch |
5 | 6 |
|
| 7 | +import pytest |
| 8 | + |
| 9 | +from codeflash.cli_cmds.cmd_init import init_codeflash |
| 10 | +from codeflash.cli_cmds.init_config import should_modify_pyproject_toml |
| 11 | +from codeflash.cli_cmds.init_javascript import ProjectLanguage |
6 | 12 | from codeflash.main import main |
7 | 13 |
|
8 | 14 |
|
@@ -39,3 +45,62 @@ def test_main_passes_yes_to_init_actions(monkeypatch) -> None: |
39 | 45 | main() |
40 | 46 |
|
41 | 47 | install_github_actions.assert_called_once_with(skip_confirm=True) |
| 48 | + |
| 49 | + |
| 50 | +def test_should_modify_pyproject_toml_skip_confirm_skips_reconfigure_prompt( |
| 51 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 52 | +) -> None: |
| 53 | + monkeypatch.chdir(tmp_path) |
| 54 | + (tmp_path / "src").mkdir() |
| 55 | + (tmp_path / "tests").mkdir() |
| 56 | + (tmp_path / "pyproject.toml").write_text( |
| 57 | + '[tool.codeflash]\nmodule-root = "src"\ntests-root = "tests"\ngit-remote = "upstream"\n', |
| 58 | + encoding="utf-8", |
| 59 | + ) |
| 60 | + |
| 61 | + with patch("rich.prompt.Confirm.ask", side_effect=AssertionError("Confirm.ask should not be called")): |
| 62 | + should_modify, config = should_modify_pyproject_toml(skip_confirm=True) |
| 63 | + |
| 64 | + assert should_modify is False |
| 65 | + assert config is not None |
| 66 | + assert config["git_remote"] == "upstream" |
| 67 | + |
| 68 | + |
| 69 | +def test_init_codeflash_skip_confirm_reuses_existing_python_config( |
| 70 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 71 | +) -> None: |
| 72 | + monkeypatch.chdir(tmp_path) |
| 73 | + (tmp_path / "src").mkdir() |
| 74 | + (tmp_path / "tests").mkdir() |
| 75 | + (tmp_path / "pyproject.toml").write_text( |
| 76 | + '[tool.codeflash]\nmodule-root = "src"\ntests-root = "tests"\ngit-remote = "upstream"\n', |
| 77 | + encoding="utf-8", |
| 78 | + ) |
| 79 | + |
| 80 | + install_github_app = Mock() |
| 81 | + install_github_actions = Mock() |
| 82 | + install_vscode_extension = Mock() |
| 83 | + detect_project = Mock(side_effect=AssertionError("detect_project should not be called")) |
| 84 | + write_config = Mock(side_effect=AssertionError("write_config should not be called")) |
| 85 | + exit_mock = Mock(side_effect=SystemExit(0)) |
| 86 | + |
| 87 | + monkeypatch.setattr("codeflash.cli_cmds.cmd_init.detect_project_language", Mock(return_value=ProjectLanguage.PYTHON)) |
| 88 | + monkeypatch.setattr("codeflash.cli_cmds.cmd_init.install_github_app", install_github_app) |
| 89 | + monkeypatch.setattr("codeflash.cli_cmds.cmd_init.install_github_actions", install_github_actions) |
| 90 | + monkeypatch.setattr("codeflash.cli_cmds.cmd_init.install_vscode_extension", install_vscode_extension) |
| 91 | + monkeypatch.setattr("codeflash.cli_cmds.cmd_init.console.print", Mock()) |
| 92 | + monkeypatch.setattr("codeflash.cli_cmds.cmd_init.ph", Mock()) |
| 93 | + monkeypatch.setattr("codeflash.cli_cmds.cmd_init.sys.exit", exit_mock) |
| 94 | + monkeypatch.setattr("codeflash.setup.detect_project", detect_project) |
| 95 | + monkeypatch.setattr("codeflash.setup.write_config", write_config) |
| 96 | + |
| 97 | + with pytest.raises(SystemExit) as exc_info: |
| 98 | + init_codeflash(skip_confirm=True, skip_api_key=True) |
| 99 | + |
| 100 | + assert exc_info.value.code == 0 |
| 101 | + install_github_app.assert_called_once_with("upstream") |
| 102 | + install_github_actions.assert_called_once_with(override_formatter_check=True, skip_confirm=True) |
| 103 | + install_vscode_extension.assert_called_once() |
| 104 | + detect_project.assert_not_called() |
| 105 | + write_config.assert_not_called() |
| 106 | + exit_mock.assert_called_once_with(0) |
0 commit comments