From 0af65f16f35e641913b2b2ad5bf71a689c54208c Mon Sep 17 00:00:00 2001 From: Santhosh Sundaram Date: Tue, 13 Aug 2024 18:20:27 +0530 Subject: [PATCH] added defaults and version flag in cli along with a cli test --- .github/workflows/test_on_push.yml | 5 ++++ noxfile.py | 1 + pyproject.toml | 1 + src/pybamm_cookiecutter/__init__.py | 3 +-- src/pybamm_cookiecutter/_version.py | 16 +++++++++++ src/pybamm_cookiecutter/cli.py | 27 +++++++++++++++---- .../template_tests/test_project_generation.py | 12 +++++++++ 7 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 src/pybamm_cookiecutter/_version.py diff --git a/.github/workflows/test_on_push.yml b/.github/workflows/test_on_push.yml index 6d4b949..8105c05 100644 --- a/.github/workflows/test_on_push.yml +++ b/.github/workflows/test_on_push.yml @@ -47,6 +47,11 @@ jobs: with: python-version: ${{ matrix.python-version }} + - name: Set up Git + run: | + git config --global user.name "pybamm user" + git config --global user.email "pybammuser@pybamm.org" + - name: Set up uv uses: yezz123/setup-uv@v4 with: diff --git a/noxfile.py b/noxfile.py index 721d245..610d17c 100644 --- a/noxfile.py +++ b/noxfile.py @@ -39,6 +39,7 @@ def install_and_run_tests(session, test_dir): """Install dependencies and run tests in the specified directory.""" session.install("setuptools", silent=False) session.install("-e", ".[dev]", silent=False) + session.run("pipx", "install", ".", "--force", silent=False) session.run("pytest", test_dir) @nox.session(name="template-tests") diff --git a/pyproject.toml b/pyproject.toml index 2fa2f4d..7abf61d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,6 +6,7 @@ build-backend = "hatchling.build" name = "pybamm-cookiecutter" authors = [ { name = "Agriya Khetarpal", email = "agriyakhetarpal@outlook.com" }, + { name = "Santhosh Sundaram", email = "santhoshsundaram9650@gmail.com" }, ] maintainers = [ { name = "PyBaMM Team", email = "pybamm@pybamm.org" }, diff --git a/src/pybamm_cookiecutter/__init__.py b/src/pybamm_cookiecutter/__init__.py index d32f61c..aca1fa6 100644 --- a/src/pybamm_cookiecutter/__init__.py +++ b/src/pybamm_cookiecutter/__init__.py @@ -6,9 +6,8 @@ from __future__ import annotations from pybamm_cookiecutter.cli import pybamm_cookiecutter_cli -import importlib.metadata +from pybamm_cookiecutter._version import __version__ -__version__ = importlib.metadata.version("pybamm-cookiecutter") __all__ : list[str] = [ "__version__", "pybamm_cookiecutter_cli", diff --git a/src/pybamm_cookiecutter/_version.py b/src/pybamm_cookiecutter/_version.py new file mode 100644 index 0000000..5db6891 --- /dev/null +++ b/src/pybamm_cookiecutter/_version.py @@ -0,0 +1,16 @@ +# file generated by setuptools_scm +# don't change, don't track in version control +TYPE_CHECKING = False +if TYPE_CHECKING: + from typing import Tuple, Union + VERSION_TUPLE = Tuple[Union[int, str], ...] +else: + VERSION_TUPLE = object + +version: str +__version__: str +__version_tuple__: VERSION_TUPLE +version_tuple: VERSION_TUPLE + +__version__ = version = '0.1.dev142+g0fc714a.d20240813' +__version_tuple__ = version_tuple = (0, 1, 'dev142', 'g0fc714a.d20240813') diff --git a/src/pybamm_cookiecutter/cli.py b/src/pybamm_cookiecutter/cli.py index 5da7668..2891d32 100644 --- a/src/pybamm_cookiecutter/cli.py +++ b/src/pybamm_cookiecutter/cli.py @@ -24,16 +24,33 @@ def pybamm_cookiecutter_cli(): """ try: parser = argparse.ArgumentParser(description = "A copier template generator for PyBaMM based projects") - parser.add_argument("--path", type = str, required = False, default = os.getcwd(), - help = "The destination path for project generation. The default is the current working directory") - + parser.add_argument( + "--path", type = str, + required = False, + default = os.getcwd(), + help = "The destination path for project generation. The default is the current working directory" + ) + + from pybamm_cookiecutter import __version__ as version + parser.add_argument( + '--version', + action='version', + version=f'PyBaMM Cookiecutter CLI Version - {version}' + ) + + parser.add_argument( + "--defaults", + action="store_true", + help="Whether to use default options for generating the template" + ) args = parser.parse_args() destination_path = Path(args.path) - copier.run_copy(src_path = TEMPLATE, dst_path = destination_path, unsafe=True) + with copier.Worker(src_path = TEMPLATE, dst_path = destination_path, unsafe = True, defaults = args.defaults) as worker: + worker.run_copy() except KeyboardInterrupt: - print("Execution stopped by the user") + print(Fore.RED + "Execution stopped by the user" + Fore.RESET) except Exception as error: print(Fore.RED + "Error caused by an exception: " + Fore.RESET, error) print(Fore.CYAN + "If you are unsure what the error is, feel free to open an issue at" + Fore.YELLOW +" - https://github.com/pybamm-team/pybamm-cookiecutter/issues" + Fore.RESET) diff --git a/tests/template_tests/test_project_generation.py b/tests/template_tests/test_project_generation.py index 5c13c7b..8bcec51 100644 --- a/tests/template_tests/test_project_generation.py +++ b/tests/template_tests/test_project_generation.py @@ -1,5 +1,8 @@ import pybamm_cookiecutter as m import pytest +import os +import subprocess +import shutil def test_version() -> None: assert m.__version__ @@ -36,3 +39,12 @@ def test_template_with_extra_answers(copie): # codespell:ignore copie assert result.project_dir.is_dir(), f"Project directory {result.project_dir} not found" with open(result.project_dir / extra_context["project_name"] / "README.md") as f: assert f.readline() == f"# {extra_context['project_name']}\n", f"{f.readline()} is not the same as {extra_context['project_name']}\n" + +def test_cli(): + """ + Testing if the CLI works and returns a successful exit code on execution + """ + os.mkdir("testcli") + return_code = subprocess.run(["pybamm-cookiecutter", "--defaults"], cwd = "./testcli") + shutil.rmtree("testcli") + assert return_code