Skip to content

Commit 2fc7ba0

Browse files
committed
Added new command test and some code refactoring
1 parent a4964c9 commit 2fc7ba0

File tree

7 files changed

+90
-9
lines changed

7 files changed

+90
-9
lines changed

ellar_cli/file_scaffolding.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def __init__(
2626
working_project_name: str,
2727
working_directory: str,
2828
scaffold_ellar_template_root_path: str,
29-
ellar_cli_service: EllarCLIService,
29+
ellar_cli_service: t.Optional[EllarCLIService] = None,
3030
) -> None:
3131
self._schema = schema
3232
self._working_project_name = working_project_name
@@ -55,7 +55,7 @@ def create_root_path(self) -> str:
5555

5656
def create_file(self, base_path: str, file_name: str, content: t.Any) -> None:
5757
with open(
58-
os.path.join(base_path, file_name.replace("ellar", "py")), mode="w"
58+
os.path.join(base_path, file_name.replace(".ellar", ".py")), mode="w"
5959
) as fw:
6060
refined_content = self._ctx.environment.from_string(content).render()
6161
fw.writelines(refined_content)

ellar_cli/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
from typer import Typer
1111
from typer.models import CommandInfo
1212

13-
from .manage_commands import create_module, create_project, runserver
13+
from .manage_commands import create_module, create_project, new_command, runserver
1414
from .service import EllarCLIService
1515

1616
__all__ = ["build_typers", "_typer", "typer_callback"]
1717

1818
_typer = Typer(name="ellar")
19-
19+
_typer.command(name="new")(new_command)
2020
_typer.command()(runserver)
2121
_typer.command(name="create-project")(create_project)
2222
_typer.command(name="create-module")(create_module)

ellar_cli/py.typed

Whitespace-only changes.

ellar_cli/schema.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class EllarPyProjectSerializer(Serializer):
1414

1515
class EllarScaffoldList(Serializer):
1616
name: str
17-
is_directory: bool
17+
is_directory: bool = False
1818
name_in_context: t.Optional[bool] = Field(default=None, alias="name-context")
1919
files: t.Optional[t.List["EllarScaffoldList"]]
2020

@@ -34,7 +34,6 @@ def schema_example(cls) -> "EllarScaffoldSchema":
3434
dict(name="sample.ellar", is_directory=False),
3535
dict(
3636
name="sample",
37-
is_directory=False,
3837
files=[
3938
dict(
4039
dict(name="sample.ellar", is_directory=False),

tests/test_build_typers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ def test_help_command(cli_runner):
6060
assert result.returncode == 0
6161
assert (
6262
result.stdout
63-
== b"Usage: Ellar, Python Web framework [OPTIONS] COMMAND [ARGS]...\n\nOptions:\n -p, --project TEXT Run Specific Command on a specific project\n --install-completion [bash|zsh|fish|powershell|pwsh]\n Install completion for the specified shell.\n --show-completion [bash|zsh|fish|powershell|pwsh]\n Show completion for the specified shell, to\n copy it or customize the installation.\n --help Show this message and exit.\n\nCommands:\n create-module - Scaffolds Ellar Application Module -\n create-project - Scaffolds Ellar Application -\n db\n runserver - Starts Uvicorn Server -\n say-hi\n whatever-you-want Whatever you want\n"
63+
== b"Usage: Ellar, Python Web framework [OPTIONS] COMMAND [ARGS]...\n\nOptions:\n -p, --project TEXT Run Specific Command on a specific project\n --install-completion [bash|zsh|fish|powershell|pwsh]\n Install completion for the specified shell.\n --show-completion [bash|zsh|fish|powershell|pwsh]\n Show completion for the specified shell, to\n copy it or customize the installation.\n --help Show this message and exit.\n\nCommands:\n create-module - Scaffolds Ellar Application Module -\n create-project - Scaffolds Ellar Application -\n db\n new - Runs a complete Ellar project scaffold and creates...\n runserver - Starts Uvicorn Server -\n say-hi\n whatever-you-want Whatever you want\n"
6464
)
6565
result = subprocess.run(
6666
["ellar", "-p", "example_project", "--help"], stdout=subprocess.PIPE
6767
)
6868
assert result.returncode == 0
6969
assert (
7070
result.stdout
71-
== b"Usage: Ellar, Python Web framework [OPTIONS] COMMAND [ARGS]...\n\nOptions:\n -p, --project TEXT Run Specific Command on a specific project\n --install-completion [bash|zsh|fish|powershell|pwsh]\n Install completion for the specified shell.\n --show-completion [bash|zsh|fish|powershell|pwsh]\n Show completion for the specified shell, to\n copy it or customize the installation.\n --help Show this message and exit.\n\nCommands:\n create-module - Scaffolds Ellar Application Module -\n create-project - Scaffolds Ellar Application -\n db\n runserver - Starts Uvicorn Server -\n say-hi\n whatever-you-want Whatever you want\n"
71+
== b"Usage: Ellar, Python Web framework [OPTIONS] COMMAND [ARGS]...\n\nOptions:\n -p, --project TEXT Run Specific Command on a specific project\n --install-completion [bash|zsh|fish|powershell|pwsh]\n Install completion for the specified shell.\n --show-completion [bash|zsh|fish|powershell|pwsh]\n Show completion for the specified shell, to\n copy it or customize the installation.\n --help Show this message and exit.\n\nCommands:\n create-module - Scaffolds Ellar Application Module -\n create-project - Scaffolds Ellar Application -\n db\n new - Runs a complete Ellar project scaffold and creates...\n runserver - Starts Uvicorn Server -\n say-hi\n whatever-you-want Whatever you want\n"
7272
)
7373

7474
result = subprocess.run(
@@ -77,5 +77,5 @@ def test_help_command(cli_runner):
7777
assert result.returncode == 0
7878
assert (
7979
result.stdout
80-
== b"Usage: Ellar, Python Web framework [OPTIONS] COMMAND [ARGS]...\n\nOptions:\n -p, --project TEXT Run Specific Command on a specific project\n --install-completion [bash|zsh|fish|powershell|pwsh]\n Install completion for the specified shell.\n --show-completion [bash|zsh|fish|powershell|pwsh]\n Show completion for the specified shell, to\n copy it or customize the installation.\n --help Show this message and exit.\n\nCommands:\n create-module - Scaffolds Ellar Application Module -\n create-project - Scaffolds Ellar Application -\n db\n project-2-command Project 2 Custom Command\n runserver - Starts Uvicorn Server -\n say-hi\n whatever-you-want Whatever you want\n"
80+
== b"Usage: Ellar, Python Web framework [OPTIONS] COMMAND [ARGS]...\n\nOptions:\n -p, --project TEXT Run Specific Command on a specific project\n --install-completion [bash|zsh|fish|powershell|pwsh]\n Install completion for the specified shell.\n --show-completion [bash|zsh|fish|powershell|pwsh]\n Show completion for the specified shell, to\n copy it or customize the installation.\n --help Show this message and exit.\n\nCommands:\n create-module - Scaffolds Ellar Application Module -\n create-project - Scaffolds Ellar Application -\n db\n new - Runs a complete Ellar project scaffold and creates...\n project-2-command Project 2 Custom Command\n runserver - Starts Uvicorn Server -\n say-hi\n whatever-you-want Whatever you want\n"
8181
)

tests/test_ellar_commands/test_create_project_command.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ def test_create_project_fails_for_existing_module_project_name(
4646
)
4747

4848

49+
def test_create_project_works_for_existing_folder_with_same_project_name(
50+
tmpdir, cli_runner, write_empty_py_project
51+
):
52+
module_name = "new_project_module_2"
53+
os.makedirs(os.path.join(tmpdir, module_name), exist_ok=True)
54+
55+
result = cli_runner.invoke_ellar_command(["create-project", module_name])
56+
assert result.exit_code == 0
57+
assert result.output == (
58+
"`new_project_module_2` project scaffold completed. To start your server, run the command below\n"
59+
"ellar --project new_project_module_2 runserver --reload\n"
60+
"Happy coding!\n"
61+
)
62+
63+
4964
def test_create_project_command_works(tmpdir, process_runner, write_empty_py_project):
5065
result = process_runner(["ellar", "create-project", "ellar_project"])
5166
assert result.returncode == 0
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import os
2+
3+
from ellar_cli.manage_commands.new import NewTemplateScaffold
4+
from ellar_cli.schema import EllarScaffoldSchema
5+
from ellar_cli.service import EllarCLIService
6+
7+
8+
def test_new_command_works(tmpdir, process_runner):
9+
result = process_runner(["ellar", "new", "ellar-project-new"])
10+
assert result.returncode == 0
11+
assert result.stdout.decode("utf8") == (
12+
"`ellar-project-new` project created successfully.\n"
13+
"- cd ellar-project-new\n"
14+
"To start your server, run the command below\n"
15+
"- ellar --project ellar_project_new runserver --reload\n"
16+
"Happy coding!\n"
17+
)
18+
os.chdir(os.path.join(tmpdir, "ellar-project-new"))
19+
ellar_cli_service = EllarCLIService.import_project_meta()
20+
assert ellar_cli_service._meta.dict() == {
21+
"project_name": "ellar_project_new",
22+
"application": "ellar_project_new.server:application",
23+
"config": "ellar_project_new.config:DevelopmentConfig",
24+
"root_module": "ellar_project_new.root_module:ApplicationModule",
25+
"apps_module": "ellar_project_new.apps",
26+
}
27+
28+
29+
def test_new_command_fails_for_existing_folder_name(tmp_path, process_runner):
30+
os.makedirs(tmp_path / "ellar-project-exist", exist_ok=True)
31+
result = process_runner(["ellar", "new", "ellar-project-exist"])
32+
assert result.returncode == 1
33+
assert result.stderr.decode("utf8") == (
34+
"Error: A folder with same name exist 'ellar-project-exist' \n"
35+
)
36+
37+
38+
def test_new_command_fails_for_invalid_project_name(tmp_path, process_runner):
39+
result = process_runner(["ellar", "new", "ellar-project-exist&^"])
40+
assert result.returncode == 1
41+
assert result.stderr.decode("utf8") == (
42+
"Error: 'ellar-project-exist&^' is not a valid project-name. "
43+
"Please make sure the project-name is a valid identifier.\n"
44+
)
45+
46+
47+
def test_new_template_scaffold_get_project_name():
48+
init_template_scaffold = NewTemplateScaffold(
49+
schema=EllarScaffoldSchema.schema_example(),
50+
working_directory=os.getcwd(),
51+
scaffold_ellar_template_root_path="",
52+
working_project_name="folder-name".lower(),
53+
)
54+
assert init_template_scaffold.get_project_name() == "folder_name"
55+
init_template_scaffold._project_name = "folder-name!;-:*"
56+
assert init_template_scaffold.get_project_name() == "folder_name_____"
57+
assert init_template_scaffold.get_project_name().isidentifier()
58+
59+
60+
def test_new_template_scaffold_get_project_cwd():
61+
init_template_scaffold = NewTemplateScaffold(
62+
schema=EllarScaffoldSchema.schema_example(),
63+
working_directory="working-directory",
64+
scaffold_ellar_template_root_path="",
65+
working_project_name="folder-name".lower(),
66+
)
67+
assert init_template_scaffold.get_project_cwd() == "working-directory/folder-name"

0 commit comments

Comments
 (0)