Skip to content

Commit

Permalink
[BB-212] Project name validation (#215)
Browse files Browse the repository at this point in the history
* project.yaml validations on "project create"

* adapt test project name

* lower case letters, numbers and hypens, 25 chars max

* clarify error

* adapting integration test

* Update leverage/modules/project.py

Co-authored-by: Angelo Fenoglio <[email protected]>

* Update leverage/modules/project.py

Co-authored-by: Angelo Fenoglio <[email protected]>

* Update leverage/modules/project.py

---------

Co-authored-by: Angelo Fenoglio <[email protected]>
  • Loading branch information
Franr and angelofenoglio authored Sep 11, 2023
1 parent 5cbbb4f commit 48add55
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests-unit-and-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
- name: Set project file and create
run: |
printf "[INFO] Setting Project file\n"
sed 's/<project name>/The Adam Project/' -i project.yaml
sed 's/<project name>/the-adam-project/' -i project.yaml
sed 's/<short project name>/bb/' -i project.yaml
sed 's/<management email address>/bb@domainmgmt/' -i project.yaml
sed 's/<security email address>/bb@domainsec/' -i project.yaml
Expand Down
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"^[a-z0-9]([a-z0-9]|-){1,23}[a-z0-9]$", config["project_name"]):
raise ExitError(1, "Project name is not valid. Only lowercase alphanumeric characters and hyphens 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
25 changes: 25 additions & 0 deletions tests/test_modules/test_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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": "fine123"})
assert validate_config({"project_name": "123fine"})
assert validate_config({"project_name": "hyphens-are-allowed"})


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": "underscores_not_allowed"})

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

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

0 comments on commit 48add55

Please sign in to comment.