Skip to content

Commit a8b83d2

Browse files
committed
added test for piccolo app new
1 parent bc8b218 commit a8b83d2

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

piccolo/apps/app/commands/new.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@
1616
)
1717

1818

19-
def new_app(app_name: str):
19+
def new_app(app_name: str, root: str = ""):
2020
print(f"Creating {app_name} app ...")
21-
if os.path.exists(app_name):
21+
22+
app_root = os.path.join(root, app_name)
23+
24+
if os.path.exists(app_root):
2225
print("Folder already exists - exiting.")
2326
sys.exit(1)
24-
os.mkdir(app_name)
27+
os.mkdir(app_root)
2528

26-
with open(os.path.join(app_name, "__init__.py"), "w"):
29+
with open(os.path.join(app_root, "__init__.py"), "w"):
2730
pass
2831

2932
templates: t.Dict[str, t.Any] = {
@@ -32,26 +35,30 @@ def new_app(app_name: str):
3235
}
3336

3437
for filename, context in templates.items():
35-
with open(os.path.join(app_name, filename), "w") as f:
38+
with open(os.path.join(app_root, filename), "w") as f:
3639
template = JINJA_ENV.get_template(filename + ".jinja")
3740
file_contents = template.render(**context)
3841
file_contents = black.format_str(
3942
file_contents, mode=black.FileMode(line_length=80)
4043
)
4144
f.write(file_contents)
4245

43-
migrations_folder_path = os.path.join(app_name, "piccolo_migrations")
46+
migrations_folder_path = os.path.join(app_root, "piccolo_migrations")
4447
os.mkdir(migrations_folder_path)
4548

4649
with open(os.path.join(migrations_folder_path, "__init__.py"), "w"):
4750
pass
4851

4952

50-
def new(app_name: str):
53+
def new(app_name: str, root: str = ""):
5154
"""
5255
Creates a new Piccolo app.
5356
5457
:param app_name:
5558
The name of the new app.
59+
:param root:
60+
Where to create the app e.g. /my/folder. By default it creates the
61+
app in the current directory.
62+
5663
"""
57-
new_app(app_name)
64+
new_app(app_name=app_name, root=root)

tests/apps/app/__init__.py

Whitespace-only changes.

tests/apps/app/commands/__init__.py

Whitespace-only changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import os
2+
import shutil
3+
from unittest import TestCase
4+
5+
from piccolo.apps.app.commands.new import new
6+
7+
8+
class TestNewApp(TestCase):
9+
def test_new(self):
10+
root = "/tmp"
11+
app_name = "my_app"
12+
13+
app_path = os.path.join(root, app_name)
14+
15+
if os.path.exists(app_path):
16+
shutil.rmtree(app_path)
17+
18+
new(app_name=app_name, root=root)
19+
20+
self.assertTrue(os.path.exists(app_path))

0 commit comments

Comments
 (0)