From a504c052d8b1934a099368122a239406c33713ba Mon Sep 17 00:00:00 2001 From: Jugal Mistry <41481260+HuM4NoiD@users.noreply.github.com> Date: Wed, 18 Oct 2023 12:53:14 +0530 Subject: [PATCH] feat: Specify multiple or no package directories via CLI options for `monas init` (#15) * Add ability to specify multiple or no package dirs for 'monas init' * Remove unnecessary stdout in test_init * Add import for annotations from __future__ in commands/init.py --- src/monas/commands/init.py | 28 ++++++++++++++++++++++++++-- src/monas/project.py | 4 ++-- tests/cli/test_init.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/monas/commands/init.py b/src/monas/commands/init.py index 4c35e93..ff6f166 100644 --- a/src/monas/commands/init.py +++ b/src/monas/commands/init.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import textwrap from pathlib import Path @@ -18,13 +20,35 @@ help="The Python version to use", ) @click.option("-v", "--version", default="0.0.0", help="The version of the monorepo") -def init(*, version: str, python_version: str) -> None: +@click.option( + "-d", + "--default-package-dir", + multiple=True, + default=["packages"], + help="Default Package Directories to be created", +) +@click.option( + "-n", + "--no-package-dir", + is_flag=True, + default=False, + help="If specified, no package directories will be created", +) +def init( + *, + version: str, + python_version: str, + default_package_dir: list[str], + no_package_dir: bool, +) -> None: """Initialize the monas tool. Args: path: The path to the `pyproject.toml` file. """ - default_package_dirs = ["packages"] + default_package_dirs = list(default_package_dir) + if no_package_dir: + default_package_dirs.clear() mono_settings = { "packages": [f"{pb}/*" for pb in default_package_dirs], "version": version, diff --git a/src/monas/project.py b/src/monas/project.py index 491e4ce..3be7fc2 100644 --- a/src/monas/project.py +++ b/src/monas/project.py @@ -3,7 +3,7 @@ import textwrap from pathlib import Path from shlex import join as sh_join -from typing import Optional, Type, cast +from typing import Type, cast import tomlkit from packaging.utils import canonicalize_name @@ -179,7 +179,7 @@ def get_local_dependencies( if pkg_name not in dependency_names: continue if pkg_name == self.canonical_name: - raise ValueError(f'{self.name} cannot have a dependency on itself') + raise ValueError(f"{self.name} cannot have a dependency on itself") if pkg_name in [ld.canonical_name for ld in local_dependencies]: continue local_dependencies = [pkg, *local_dependencies] diff --git a/tests/cli/test_init.py b/tests/cli/test_init.py index 9448738..91b1271 100644 --- a/tests/cli/test_init.py +++ b/tests/cli/test_init.py @@ -22,3 +22,34 @@ def test_init_with_given_python_version(cli_run, tmp_path): python-version = "3.9" """ ) + + +def test_init_with_no_package_directories(cli_run, tmp_path): + cli_run(["init", "-n", "-p", "3.9"], cwd=tmp_path) + assert ( + tmp_path.joinpath("pyproject.toml").read_text() + == """\ +[tool.monas] +packages = [] +version = "0.0.0" +python-version = "3.9" +""" + ) + + +def test_init_with_selected_package_directories(cli_run, tmp_path): + cli_run(["init", "-p", "3.9", "-d", "packages1", "-d", "packages2"], cwd=tmp_path) + print("CLI TEST_RAN") + assert ( + tmp_path.joinpath("pyproject.toml").read_text() + == """\ +[tool.monas] +packages = ["packages1/*", "packages2/*"] +version = "0.0.0" +python-version = "3.9" +""" + ) + import os + + assert os.path.exists(os.path.join(tmp_path, "packages1")) + assert os.path.exists(os.path.join(tmp_path, "packages2"))