From da32bba48b5aa8e3341e258dd79bac5a417071c5 Mon Sep 17 00:00:00 2001 From: Jugal Mistry Date: Sat, 21 Oct 2023 20:49:02 +0530 Subject: [PATCH 1/2] Add explicit package entry implementation for `monas new` --- src/monas/commands/new.py | 20 ++++++++++++++++++-- src/monas/config.py | 18 +++++++++++++++++- tests/cli/test_new.py | 26 ++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/src/monas/commands/new.py b/src/monas/commands/new.py index 33e2e4d..3ee94c2 100644 --- a/src/monas/commands/new.py +++ b/src/monas/commands/new.py @@ -15,10 +15,23 @@ @click.command() @click.argument("package", required=True) @click.argument("location", required=False) +@click.option( + "-e", + "--explicit-package-entry", + is_flag=True, + default=False, + help="If specified, an explicit package entry " + + "will be added to the list of packages", +) @pass_config @pass_context def new( - ctx: click.Context, config: Config, *, package: str, location: str | None = None + ctx: click.Context, + config: Config, + *, + package: str, + location: str | None = None, + explicit_package_entry: bool, ): """Create a new under . @@ -67,4 +80,7 @@ def new( info("Creating project files") PyPackage.create(config, package_path, inputs) - config.add_package_path(parent_dir) + if explicit_package_entry: + config.add_explicit_package_path(package_path) + else: + config.add_package_path(parent_dir) diff --git a/src/monas/config.py b/src/monas/config.py index 84edf1b..114019e 100644 --- a/src/monas/config.py +++ b/src/monas/config.py @@ -80,7 +80,12 @@ def python_version(self) -> str: @property def default_package_dir(self) -> Path: - """Default directory to find or add mono-repo packages.""" + """ + Default directory to find or add mono-repo packages. + If no directory is specified, will fallback to project root path + """ + if len(self.package_paths) == 0: + return self.path first_pkg_path = self.package_paths[0] if "*" or "?" in first_pkg_path.name: return first_pkg_path.parent @@ -96,6 +101,17 @@ def add_package_path(self, path: Path) -> None: self._tool.setdefault("packages", []).append(relative_path.as_posix()) TOMLFile(self.path / "pyproject.toml").write(self._pyproject) + def add_explicit_package_path(self, package_path: Path) -> None: + relative_path = ( + package_path.relative_to(self.path) + if package_path.is_absolute() + else package_path + ) + if relative_path in self.package_paths: + return + self._tool.setdefault("packages", []).append(relative_path.as_posix()) + TOMLFile(self.path / "pyproject.toml").write(self._pyproject) + def iter_packages(self) -> Iterable[PyPackage]: """Iterate over the packages in the monorepo""" from monas.project import PyPackage diff --git a/tests/cli/test_new.py b/tests/cli/test_new.py index 98c22b8..7320e1a 100644 --- a/tests/cli/test_new.py +++ b/tests/cli/test_new.py @@ -149,3 +149,29 @@ def test_new_package_under_non_default_location(project, cli_run, python_version python-version = "{python_version}" """ ) + + +@pytest.mark.usefixtures("mock_input") +def test_new_package_with_explicit_path(project, cli_run, python_version): + """ + Create a new project with explicit path entry + """ + cli_run(["new", "-e", "first", "."], cwd=project, input="\n") + cli_run(["new", "-e", "second", "."], cwd=project, input="\n") + cli_run(["new", "-e", "third", "nested"], cwd=project, input="\n") + packages = [ + ("first", project.joinpath("first")), + ("second", project.joinpath("second")), + ("third", project.joinpath("nested/third")), + ] + for _package_name, package_path in packages: + assert package_path.is_dir() + assert ( + project.joinpath("pyproject.toml").read_text() + == f"""\ +[tool.monas] +packages = ["packages/*", "first", "second", "nested/third"] +version = "0.0.0" +python-version = "{python_version}" +""" + ) From 128dedfd917a0f103d3a250592b0ae28aa9111b6 Mon Sep 17 00:00:00 2001 From: Jugal Mistry Date: Sat, 21 Oct 2023 21:05:46 +0530 Subject: [PATCH 2/2] Add backend fixtures for test_new_package_with_explicit_path --- tests/cli/test_new.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/cli/test_new.py b/tests/cli/test_new.py index 7320e1a..0e8a5c6 100644 --- a/tests/cli/test_new.py +++ b/tests/cli/test_new.py @@ -152,6 +152,7 @@ def test_new_package_under_non_default_location(project, cli_run, python_version @pytest.mark.usefixtures("mock_input") +@pytest.mark.parametrize("backend", ["setuptools(pyproject.toml)"]) def test_new_package_with_explicit_path(project, cli_run, python_version): """ Create a new project with explicit path entry