Skip to content

Commit

Permalink
project.yaml validations on "project create"
Browse files Browse the repository at this point in the history
  • Loading branch information
Franr committed Aug 24, 2023
1 parent cb3c446 commit 38381b4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
15 changes: 14 additions & 1 deletion leverage/modules/project.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Module for managing Leverage projects.
"""
import re
from pathlib import Path
from shutil import copy2
from shutil import copytree
Expand All @@ -17,7 +18,7 @@
from leverage.logger import console
from leverage.path import get_root_path
from leverage.path import NotARepositoryError
from leverage._utils import git
from leverage._utils import git, ExitError
from leverage.container import get_docker_client
from leverage.container import TerraformContainer

Expand Down Expand Up @@ -279,6 +280,16 @@ def load_project_config():
raise Exit(1)


def validate_config(config: dict):
"""
Run a battery of validations over the config file (project.yaml).
"""
if not re.match(r"^\w+$", config["project_name"]):
raise ExitError(1, "Project name is not valid. Only alphanumerics characters are allowed.")

return True


@project.command()
def create():
"""Create the directory structure required by the project configuration and set up each account accordingly."""
Expand All @@ -295,6 +306,8 @@ def create():
logger.error("Project has already been created.")
return

validate_config(config)

# Make project structure
_copy_project_template(config=config)

Expand Down
17 changes: 17 additions & 0 deletions tests/test_modules/test_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest

from leverage._utils import ExitError
from leverage.modules.project import validate_config


def test_validate_config_happy_path():
assert validate_config({"project_name": "fine"})
assert validate_config({"project_name": "fine_with_underscores"})


def test_validate_config_errors(muted_click_context):
with pytest.raises(ExitError, match="Project name is not valid"):
validate_config({"project_name": "with spaces"})

with pytest.raises(ExitError, match="Project name is not valid"):
validate_config({"project_name": "with-hyphen"})

0 comments on commit 38381b4

Please sign in to comment.