Skip to content

Commit 4e77696

Browse files
authored
Merge pull request #2 from TTWShell/feature/tests
Feature/tests
2 parents 1c44363 + 7b9e128 commit 4e77696

File tree

10 files changed

+274
-32
lines changed

10 files changed

+274
-32
lines changed

.circleci/config.yml

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,37 @@
44
#
55
version: 2
66
jobs:
7+
tox:
8+
docker:
9+
- image: circleci/python:3.6
10+
user: root
11+
working_directory: ~/repo
12+
steps:
13+
- checkout
14+
- run:
15+
name: install dependencies && tox
16+
command: |
17+
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
18+
export PATH="/root/.pyenv/bin:$PATH"
19+
eval "$(pyenv init -)"
20+
eval "$(pyenv virtualenv-init -)"
21+
pyenv install 3.7.0
22+
pyenv virtualenv -f 3.7.0 py37
23+
pyenv shell py37
24+
pip install tox
25+
tox
726
test-py36:
827
docker:
9-
# specify the version you desire here
10-
# use `-browsers` prefix for selenium tests, e.g. `3.6.1-browsers`
1128
- image: circleci/python:3.6
1229
user: root
13-
14-
# Specify service dependencies here if necessary
15-
# CircleCI maintains a library of pre-built images
16-
# documented at https://circleci.com/docs/2.0/circleci-images/
17-
# - image: circleci/postgres:9.4
18-
1930
working_directory: ~/repo
20-
2131
steps:
2232
- checkout
2333
- run:
2434
name: install dependencies
2535
command: |
26-
sudo pip install flake8 pytest pytest-cov
27-
sudo pip install --editable .
36+
pip install flake8 pytest pytest-cov
37+
pip install --editable .
2838
- run:
2939
name: use flake8 check self
3040
command: |
@@ -59,8 +69,8 @@ jobs:
5969
- run:
6070
name: install dependencies
6171
command: |
62-
sudo pip install flake8 pytest pytest-cov
63-
sudo pip install --editable .
72+
pip install flake8 pytest pytest-cov
73+
pip install --editable .
6474
- run:
6575
name: use flake8 check self
6676
command: |
@@ -91,7 +101,10 @@ workflows:
91101
version: 2
92102
test:
93103
jobs:
94-
- test-py36
95-
- test-py37
96-
97-
104+
- tox
105+
- test-py36:
106+
requires:
107+
- tox
108+
- test-py37:
109+
requires:
110+
- tox

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
__pycache__/*
66
*/__pycache__/*
77
.coverage
8+
.pytest_cache/*
9+
.tox/*
810

911
hobbit_core.egg-info/*
1012
dist/*

Dockerfile

Lines changed: 0 additions & 11 deletions
This file was deleted.

Pipfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ name = "pypi"
77
click = "*"
88
jinja2 = "*"
99
pypandoc = "*"
10+
tox = "*"
11+
pytest = "*"
12+
pytest-cov = "*"
1013

1114
[dev-packages]
1215

Pipfile.lock

Lines changed: 140 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os
2+
import shutil
3+
import functools
4+
5+
6+
class BaseTest:
7+
8+
def setup_method(self, method):
9+
print('\n{}::{}'.format(type(self).__name__, method.__name__))
10+
11+
def teardown_method(self, method):
12+
pass
13+
14+
15+
def rmdir(path):
16+
if os.path.exists(path):
17+
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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import os
2+
3+
from click.testing import CliRunner
4+
5+
from hobbit_core.hobbit import main as hobbit
6+
7+
from . import BaseTest, rmdir, chdir
8+
9+
10+
class TestHobbit(BaseTest):
11+
wkdir = os.path.abspath('hobbit-tox-test')
12+
13+
def setup_method(self, method):
14+
rmdir(self.wkdir)
15+
super().setup_method(method)
16+
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
33+
runner = CliRunner()
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={})
52+
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)