Skip to content

Commit c267f38

Browse files
committed
Add tests
1 parent 1a4904a commit c267f38

File tree

14 files changed

+375
-0
lines changed

14 files changed

+375
-0
lines changed

djclick/test/conftest.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import sys
2+
import os
3+
import contextlib
4+
import subprocess
5+
6+
import pytest
7+
8+
9+
@contextlib.contextmanager
10+
def set_key(dictionary, key, value):
11+
key_is_set = key in dictionary
12+
original_value = dictionary.pop(key, None)
13+
14+
dictionary[key] = value
15+
16+
try:
17+
yield
18+
finally:
19+
if key_is_set:
20+
dictionary[key] = original_value
21+
else:
22+
del dictionary[key]
23+
24+
25+
@contextlib.contextmanager
26+
def insert_value(list, index, value):
27+
list.insert(index, value)
28+
try:
29+
yield
30+
finally:
31+
if value in list:
32+
list.pop(list.index(value))
33+
34+
35+
@pytest.yield_fixture(autouse=True, scope='session')
36+
def test_project():
37+
project_dir = os.path.join(os.path.dirname(__file__), 'testprj')
38+
with insert_value(sys.path, 0, project_dir):
39+
with set_key(os.environ, 'DJANGO_SETTINGS_MODULE', 'testprj.settings'):
40+
from django.conf import settings
41+
assert 'testapp' in settings.INSTALLED_APPS
42+
43+
import django
44+
if hasattr(django, 'setup'):
45+
django.setup()
46+
47+
yield
48+
49+
50+
@pytest.fixture(scope='session')
51+
def manage():
52+
def call(*args):
53+
cmd = [
54+
os.path.join(os.path.dirname(__file__), 'testprj', 'manage.py'),
55+
] + list(args)
56+
return subprocess.check_output(cmd)
57+
58+
return call

djclick/test/test_adapter.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import locale
2+
import codecs
3+
4+
import six
5+
6+
import pytest
7+
8+
import click
9+
10+
from django.core.management import get_commands, call_command
11+
from django.core.management import execute_from_command_line
12+
13+
import djclick
14+
15+
16+
todo = pytest.mark.xfail(reason='TODO')
17+
18+
19+
@pytest.mark.skipif(not six.PY3, reason='Only necessary on Python3')
20+
def test_not_ascii():
21+
"""Make sure that the systems preferred encoding is not `ascii`.
22+
23+
Otherwise `click` is raising a RuntimeError for Python3. For a detailed
24+
description of this very problem please consult the following gist:
25+
https://gist.github.com/hackebrot/937245251887197ef542
26+
27+
This test also checks that `tox.ini` explicitly copies the according
28+
system environment variables to the test environments.
29+
"""
30+
try:
31+
preferred_encoding = locale.getpreferredencoding()
32+
fs_enc = codecs.lookup(preferred_encoding).name
33+
except Exception:
34+
fs_enc = 'ascii'
35+
assert fs_enc != 'ascii'
36+
37+
38+
def test_attributes():
39+
for attr in click.__all__:
40+
assert hasattr(djclick, attr)
41+
42+
43+
def test_command_recognized():
44+
assert 'testcmd' in get_commands()
45+
46+
47+
def test_call_cli():
48+
execute_from_command_line(['./manage.py', 'testcmd'])
49+
with pytest.raises(RuntimeError):
50+
execute_from_command_line(['./manage.py', 'testcmd', '--raise'])
51+
52+
53+
def test_call_command_args():
54+
call_command('testcmd')
55+
with pytest.raises(RuntimeError):
56+
call_command('testcmd', '--raise')
57+
58+
59+
def test_call_command_kwargs():
60+
call_command('testcmd', raise_when_called=False)
61+
with pytest.raises(RuntimeError):
62+
call_command('testcmd', raise_when_called=True)
63+
64+
65+
def test_call_command_kwargs_rename():
66+
call_command('testcmd', **{'raise': False})
67+
with pytest.raises(RuntimeError):
68+
call_command('testcmd', **{'raise': True})
69+
70+
71+
def test_call_directly():
72+
from testapp.management.commands.testcmd import command
73+
74+
command(raise_when_called=False)
75+
76+
with pytest.raises(RuntimeError):
77+
command(raise_when_called=True)
78+
79+
with pytest.raises(RuntimeError):
80+
command(**{'raise': True})
81+
82+
83+
@todo
84+
def test_django_verbosity():
85+
assert False
86+
87+
88+
@todo
89+
def test_django_pythonpath():
90+
assert False
91+
92+
93+
@todo
94+
def test_django_traceback():
95+
assert False
96+
97+
98+
@pytest.mark.xfail(six.PY3, reason='Encoding issues')
99+
def test_django_settings(manage):
100+
# The --settings switch only works from the command line (or if the django
101+
# settings where not setup before... this means that we have to call it
102+
# in a subprocess.
103+
cmd = 'settingscmd'
104+
assert manage(cmd) == 'default'
105+
assert manage(cmd, '--settings', 'testprj.settings') == 'default'
106+
assert manage(cmd, '--settings', 'testprj.settings_alt') == 'alternative'
107+
108+
109+
def test_django_color(capsys):
110+
call_command('colorcmd')
111+
out, err = capsys.readouterr()
112+
# Not passing a --color/--no-color flag defaults to autodetection. As the
113+
# command is run through the test suite, the autodetection defaults to
114+
# --no-color
115+
assert out == 'stdout'
116+
assert err == 'stderr'
117+
118+
call_command('colorcmd', '--color')
119+
out, err = capsys.readouterr()
120+
assert out == click.style('stdout', fg='blue')
121+
assert err == click.style('stderr', bg='red')
122+
123+
call_command('colorcmd', '--no-color')
124+
out, err = capsys.readouterr()
125+
assert out == 'stdout'
126+
assert err == 'stderr'
127+
128+
129+
@pytest.mark.xfail(six.PY3, reason='Encoding issues')
130+
def test_django_help(manage):
131+
# The -h/--help switches cause the program to exit. Invoking the command
132+
# through execute_from_command_line would cause the test suit to exit as
133+
# well... this means that we have to call it in a subprocess.
134+
helps = [
135+
manage('helpcmd', '-h'),
136+
manage('helpcmd', '--help'),
137+
manage('help', 'helpcmd'),
138+
]
139+
assert len(set(helps)) == 1
140+
141+
help_text = helps[0]
142+
assert 'HELP_CALLED' not in help_text
143+
assert help_text.startswith('Usage: manage.py helpcmd ')
144+
145+
146+
@todo
147+
def test_django_version():
148+
assert False

djclick/test/testprj/manage.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env python
2+
import os
3+
import sys
4+
5+
if __name__ == "__main__":
6+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testprj.settings")
7+
8+
from django.core.management import execute_from_command_line
9+
10+
execute_from_command_line(sys.argv)

djclick/test/testprj/testapp/__init__.py

Whitespace-only changes.

djclick/test/testprj/testapp/management/__init__.py

Whitespace-only changes.

djclick/test/testprj/testapp/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import djclick as click
2+
3+
4+
@click.command()
5+
def command():
6+
click.secho('stdout', fg='blue', nl=False)
7+
click.secho('stderr', bg='red', err=True, nl=False)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import djclick as click
2+
3+
4+
@click.command()
5+
def command():
6+
# Just print some things which shall not be found in the output
7+
click.echo('HELP_CALLED')
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import djclick as click
2+
3+
from django.conf import settings
4+
5+
6+
@click.command()
7+
def command():
8+
click.echo(settings.SETTINGS_NAME, nl=False)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import djclick as click
2+
3+
4+
@click.command()
5+
@click.option('--raise', 'raise_when_called', is_flag=True)
6+
def command(raise_when_called):
7+
if raise_when_called:
8+
raise RuntimeError()

0 commit comments

Comments
 (0)