Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions nest/cli/src/generate/generate_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ def __init__(self):
@staticmethod
def get_metadata():
config = {"db_type": None, "is_async": False, "is_cli": False}
setting_path = Path(__file__).parent.parent.parent.parent / "settings.yaml"
if setting_path.exists():
with open(setting_path, "r") as file:
file = yaml.load(file, Loader=yaml.FullLoader)
config = file["config"]
# Metadata belongs to the generated project, not the installed package.
# Search upward so commands also work from a project's src/ directory.
for directory in (Path.cwd(), *Path.cwd().parents):
setting_path = directory / "settings.yaml"
if setting_path.is_file():
with setting_path.open("r") as file:
settings = yaml.safe_load(file) or {}
config.update(settings.get("config") or {})
break
db_type = config["db_type"]
is_async = config["is_async"]
is_cli = config["is_cli"] if "is_cli" in config else False
Expand Down
2 changes: 1 addition & 1 deletion nest/cli/templates/blank_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ def generate_module(self, module_name: str, path: str = None):
self.create_module(module_name, src_path)

def generate_project(self, project_name: str):
self.create_template(self.nest_path / "settings.yaml", self.settings_file())
root = self.base_path / project_name
src_path = root / "src"
self.create_folder(root)
self.create_template(root / "settings.yaml", self.settings_file())
self.create_template(root / "main.py", self.main_file())
self.create_template(root / "README.md", self.readme_file())
self.create_template(root / "requirements.txt", self.requirements_file())
Expand Down
2 changes: 1 addition & 1 deletion nest/cli/templates/cli_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ def generate_module(self, module_name: str, path: str = None):
self.create_module(module_name, src_path)

def generate_project(self, project_name: str):
self.create_template(self.nest_path / "settings.yaml", self.settings_file())
root = self.base_path / project_name
src_path = root / "src"
self.create_folder(root)
self.create_template(root / "settings.yaml", self.settings_file())
self.create_template(root / "README.md", self.readme_file())
self.create_template(root / "requirements.txt", self.requirements_file())
self.create_folder(src_path)
Expand Down
2 changes: 1 addition & 1 deletion nest/cli/templates/orm_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,13 @@ def generate_module(self, module_name: str, path: str = None):
self.create_module(module_name, src_path)

def generate_project(self, project_name: str):
self.create_template(self.nest_path / "settings.yaml", self.settings_file())
# define paths: root, src, module
root_path = self.base_path / project_name
src_path = self.base_path / project_name / "src"

# create folders
self.create_folder(root_path)
self.create_template(root_path / "settings.yaml", self.settings_file())
self.create_folder(src_path)

# create root level files
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ test = [
test = [
"pytest>=7.0.1,<8.0.0",
"httpx>=0.27.0,<1.0.0",
"sqlalchemy>=2.0.36,<3.0.0",
"sqlalchemy[asyncio]>=2.0.36,<3.0.0",
"motor>=3.2.0,<4.0.0",
"beanie>=1.27.0,<2.0.0",
"python-dotenv>=1.0.1,<2.0.0",
Expand Down
43 changes: 43 additions & 0 deletions tests/test_cli/test_project_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from pathlib import Path

import pytest
import yaml

from nest.cli.src.generate.generate_service import GenerateService


@pytest.mark.parametrize(
"db_type,is_async,is_cli,expected",
[
(None, False, False, (None, False, False)),
("sqlite", True, False, ("sqlite", True, False)),
(None, False, True, ("", False, True)),
],
)
def test_generated_project_keeps_cli_settings_locally(
tmp_path, monkeypatch, db_type, is_async, is_cli, expected
):
monkeypatch.chdir(tmp_path)
service = GenerateService()
package_settings = Path(__file__).parents[2] / "nest" / "settings.yaml"
assert not package_settings.exists()

service.generate_app("sample", db_type, is_async, is_cli)

project = tmp_path / "sample"
assert yaml.safe_load((project / "settings.yaml").read_text())["config"]
assert not package_settings.exists()
monkeypatch.chdir(project / "src")
assert service.get_metadata() == expected


def test_project_metadata_isolated_between_projects(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
service = GenerateService()
service.generate_app("web", None, False, False)
service.generate_app("database", "sqlite", True, False)

monkeypatch.chdir(tmp_path / "web")
assert service.get_metadata() == (None, False, False)
monkeypatch.chdir(tmp_path / "database" / "src")
assert service.get_metadata() == ("sqlite", True, False)
Loading