Skip to content

Commit

Permalink
added defaults and version flag in cli along with a cli test
Browse files Browse the repository at this point in the history
  • Loading branch information
santacodes committed Aug 13, 2024
1 parent bfb8a81 commit 0af65f1
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/test_on_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 "[email protected]"
- name: Set up uv
uses: yezz123/setup-uv@v4
with:
Expand Down
1 change: 1 addition & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ build-backend = "hatchling.build"
name = "pybamm-cookiecutter"
authors = [
{ name = "Agriya Khetarpal", email = "[email protected]" },
{ name = "Santhosh Sundaram", email = "[email protected]" },
]
maintainers = [
{ name = "PyBaMM Team", email = "[email protected]" },
Expand Down
3 changes: 1 addition & 2 deletions src/pybamm_cookiecutter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 16 additions & 0 deletions src/pybamm_cookiecutter/_version.py
Original file line number Diff line number Diff line change
@@ -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')
27 changes: 22 additions & 5 deletions src/pybamm_cookiecutter/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions tests/template_tests/test_project_generation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import pybamm_cookiecutter as m
import pytest
import os
import subprocess
import shutil

def test_version() -> None:
assert m.__version__
Expand Down Expand Up @@ -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

0 comments on commit 0af65f1

Please sign in to comment.