소스 검색

Fix/#1202 - Catch cookiecutter.OutputDirExistsException and exit (#1228)

* fix: catch cookiecutter OutputDirExistsException and exit

* fix: mock the builtin.input to not read from stdin

---------

Co-authored-by: Jean-Robin <jeanrobin.medori@avaiga.com>
Đỗ Trường Giang 1 년 전
부모
커밋
643a824332
2개의 변경된 파일24개의 추가작업 그리고 2개의 파일을 삭제
  1. 7 2
      taipy/_cli/_scaffold_cli.py
  2. 17 0
      tests/templates/test_template_cli.py

+ 7 - 2
taipy/_cli/_scaffold_cli.py

@@ -12,6 +12,7 @@
 import pathlib
 import sys
 
+from cookiecutter.exceptions import OutputDirExistsException
 from cookiecutter.main import cookiecutter
 
 import taipy
@@ -45,6 +46,10 @@ class _ScaffoldCLI(_AbstractCLI):
         args = cls._parse_arguments()
         if not args:
             return
-
-        cookiecutter(cls._TEMPLATE_MAP[args.template])
+        try:
+            cookiecutter(cls._TEMPLATE_MAP[args.template])
+        except OutputDirExistsException as err:
+            error_msg = f"{str(err)}. Please remove the existing directory or provide a new folder name."
+            print(error_msg)  # noqa: T201
+            sys.exit(1)
         sys.exit(0)

+ 17 - 0
tests/templates/test_template_cli.py

@@ -9,6 +9,7 @@
 # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 # specific language governing permissions and limitations under the License.
 
+import os
 from unittest.mock import patch
 
 import pytest
@@ -29,3 +30,19 @@ def test_create_cli_with_unsupported_template(capsys):
             _entrypoint()
         _, err = capsys.readouterr()
         assert "invalid choice: 'not-a-template'" in err
+
+
+def test_create_app_on_existing_folder(tmpdir, capsys, monkeypatch):
+    os.chdir(tmpdir)
+    os.mkdir(os.path.join(tmpdir, "taipy_application"))
+
+    # Mock the click.prompt to always return the default value
+    monkeypatch.setattr("click.prompt", lambda *args, **kw: kw["default"] if "default" in kw else "")
+    monkeypatch.setattr("builtins.input", lambda *args, **kw: "")
+
+    with patch("sys.argv", ["prog", "create"]):
+        with pytest.raises(SystemExit):
+            _entrypoint()
+
+    out, _ = capsys.readouterr()
+    assert '"taipy_application" directory already exists' in out