Skip to content

Commit a126983

Browse files
committed
finished startproject test
1 parent 0ffccda commit a126983

File tree

4 files changed

+61
-8
lines changed

4 files changed

+61
-8
lines changed

hobbit_core/hobbit/bootstrap.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def cli(ctx, force):
1717

1818
@cli.command()
1919
@click.option('-n', '--name', help='Name of project.', required=True)
20-
@click.option('-d', '--dist', type=click.Path(), default=os.getcwd(),
20+
@click.option('-d', '--dist', type=click.Path(), required=False,
2121
help='Dir for new project.')
2222
@click.option('-t', '--template', type=click.Choice(['shire']),
2323
default='shire', help='Template name.')
@@ -27,6 +27,7 @@ def cli(ctx, force):
2727
def startproject(ctx, name, dist, template, force):
2828
"""Create a new flask project, render from different template.
2929
"""
30+
dist = os.getcwd() if dist is None else dist
3031
ctx.obj['FORCE'] = force
3132
ctx.obj['JINJIA_CONTEXT'] = {
3233
'project_name': name,

hobbit_core/hobbit/handlers/bootstrap.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
@contextmanager
1212
def chdir(dist):
1313
cwd = os.getcwd()
14-
echo('mkdir {}', (dist, ))
14+
echo('mkdir\t{}', (dist, ))
1515
os.makedirs(dist, exist_ok=True)
1616
os.chdir(dist)
1717
yield dist
@@ -46,7 +46,7 @@ def render_file(ctx, dist, fn, data):
4646
echo('exists {}, ignore ...', (target, ))
4747
return
4848

49-
echo('render {} ...', (target, ))
49+
echo('render\t{} ...', (target, ))
5050

5151
with open(fn, 'w') as wf:
5252
wf.write(data)

tests/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
import os
22
import shutil
3+
import functools
34

45

56
class BaseTest:
67

78
def setup_method(self, method):
89
print('\n{}::{}'.format(type(self).__name__, method.__name__))
910

11+
def teardown_method(self, method):
12+
pass
13+
1014

1115
def rmdir(path):
1216
if os.path.exists(path):
1317
shutil.rmtree(path)
18+
19+
20+
def chdir(path):
21+
def wrapper(func):
22+
@functools.wraps(func)
23+
def inner(*args, **kwargs):
24+
cwd = os.getcwd()
25+
os.makedirs(path, exist_ok=True)
26+
os.chdir(path)
27+
func(*args, **kwargs)
28+
os.chdir(cwd)
29+
return inner
30+
return wrapper

tests/test_hobbit.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,54 @@
1+
import os
12

23
from click.testing import CliRunner
34

4-
from hobbit_core.hobbit import main
5+
from hobbit_core.hobbit import main as hobbit
56

6-
from . import BaseTest, rmdir
7+
from . import BaseTest, rmdir, chdir
78

89

910
class TestHobbit(BaseTest):
10-
wkdir = '/tmp/hobbit-tox-test'
11+
wkdir = os.path.abspath('hobbit-tox-test')
1112

1213
def setup_method(self, method):
1314
rmdir(self.wkdir)
1415
super().setup_method(method)
1516

16-
def test_startproject(self):
17+
def teardown_method(self, method):
18+
rmdir(self.wkdir)
19+
super().teardown_method(method)
20+
21+
def test_hobbit_cmd(self):
22+
runner = CliRunner()
23+
24+
result = runner.invoke(hobbit)
25+
assert result.exit_code == 0
26+
27+
result = runner.invoke(hobbit, ['doesnotexistcmd'], obj={})
28+
assert 'Error: cmd not exist: doesnotexistcmd' in result.output
29+
30+
@chdir(wkdir)
31+
def test_startproject_cmd_nodist(self):
32+
assert os.getcwd() == self.wkdir
1733
runner = CliRunner()
18-
result = runner.invoke(main)
34+
35+
result = runner.invoke(hobbit, ['--echo', 'startproject'], obj={})
36+
assert result.exit_code == 2
37+
assert 'Error: Missing option "-n" / "--name".' in result.output
38+
39+
result = runner.invoke(
40+
hobbit, ['--echo', 'startproject', '-n', 'haha', '-f'], obj={})
41+
assert result.exit_code == 0
42+
assert 'mkdir\t{}'.format(self.wkdir) in result.output
43+
assert 'render\t{}'.format(self.wkdir) in result.output
44+
45+
def test_startproject_cmd_dist(self):
46+
runner = CliRunner()
47+
48+
result = runner.invoke(
49+
hobbit,
50+
['--echo', 'startproject', '-n', 'haha', '-f', '-d', self.wkdir],
51+
obj={})
1952
assert result.exit_code == 0
53+
assert 'mkdir\t{}'.format(self.wkdir) in result.output
54+
assert 'render\t{}'.format(self.wkdir) in result.output

0 commit comments

Comments
 (0)