Skip to content

Commit

Permalink
Implement all missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GaretJax committed Sep 14, 2015
1 parent e4240e1 commit 9571071
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ __pycache__
/.cache
/.coverage
/.tox
/.virtualenv
/build
/dist
/docs/_build
7 changes: 7 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
History
=======

0.2.0 – Unreleased
==================

* Added a ``pass_verbosity`` decorator
* Improved test suite


0.1.1 – 2015-09-11
==================

Expand Down
3 changes: 2 additions & 1 deletion djclick/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import click
from click import * # NOQA
from .adapter import CommandRegistrator as command # NOQA
from .adapter import GroupRegistrator as group, pass_verbosity # NOQA


__version__ = '0.1.1'
__url__ = 'https://github.com/GaretJax/django-click'
__all__ = click.__all__
__all__ = click.__all__ + ['pass_verbosity']
2 changes: 1 addition & 1 deletion djclick/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ def call(*args):
sys.executable,
os.path.join(os.path.dirname(__file__), 'testprj', 'manage.py'),
] + list(args)
return subprocess.check_output(cmd)
return subprocess.check_output(cmd, stderr=subprocess.STDOUT)

return call
92 changes: 77 additions & 15 deletions djclick/test/test_adapter.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import os
import locale
import codecs
import subprocess

import six

import pytest

import click

import django
from django.core.management import get_commands, call_command
from django.core.management import execute_from_command_line

Expand Down Expand Up @@ -81,24 +84,77 @@ def test_call_directly():
command(**{'raise': True})


@todo
def test_django_verbosity():
assert False
def test_django_verbosity(capsys):
# Make sure any command can be called, even if it does not explictly
# accept the --verbosity option
with pytest.raises(RuntimeError):
execute_from_command_line([
'./manage.py', 'testcmd', '--raise', '--verbosity', '1'])

# Default
execute_from_command_line([
'./manage.py', 'ctxverbositycmd'])
out, err = capsys.readouterr()
assert out == '1'

# Explicit
execute_from_command_line([
'./manage.py', 'ctxverbositycmd', '--verbosity', '2'])
out, err = capsys.readouterr()
assert out == '2'

# Invalid
with pytest.raises(click.BadParameter):
execute_from_command_line([
'./manage.py', 'ctxverbositycmd', '--verbosity', '4'])

# Default (option)
execute_from_command_line([
'./manage.py', 'argverbositycommand'])
out, err = capsys.readouterr()
assert out == '1'

@todo
def test_django_pythonpath():
assert False
# Explicit (option)
execute_from_command_line([
'./manage.py', 'argverbositycommand', '--verbosity', '2'])
out, err = capsys.readouterr()
assert out == '2'


@todo
def test_django_traceback():
assert False
def test_django_pythonpath(manage):
with pytest.raises(subprocess.CalledProcessError):
manage('pathcmd')

manage('pathcmd', '--pythonpath',
os.path.join(os.path.dirname(__file__), 'testdir')) == b'1'


def test_django_traceback(manage):
try:
manage('errcmd')
except subprocess.CalledProcessError as e:
assert e.output == b'CommandError: Raised error description\n'
assert e.returncode == 1
else:
assert False

try:
manage('errcmd', '--traceback')
except subprocess.CalledProcessError as e:
lines = e.output.splitlines()
assert lines[0] == b'Traceback (most recent call last):'
for line in lines[1:-1]:
assert line.startswith(b' ')
assert lines[-1] == (b'django.core.management.base.CommandError: '
b'Raised error description')
assert e.returncode == 1
else:
assert False


def test_django_settings(manage):
# The --settings switch only works from the command line (or if the django
# settings where not setup before... this means that we have to call it
# settings where not setup before)... this means that we have to call it
# in a subprocess.
cmd = 'settingscmd'
assert manage(cmd) == b'default'
Expand All @@ -110,8 +166,8 @@ def test_django_color(capsys):
call_command('colorcmd')
out, err = capsys.readouterr()
# Not passing a --color/--no-color flag defaults to autodetection. As the
# command is run through the test suite, the autodetection defaults to
# --no-color
# command is run through the test suite, the autodetection defaults to not
# colorizing the output.
assert out == 'stdout'
assert err == 'stderr'

Expand Down Expand Up @@ -142,6 +198,12 @@ def test_django_help(manage):
assert help_text.startswith(b'Usage: manage.py helpcmd ')


@todo
def test_django_version():
assert False
def test_django_version(manage):
django_version = django.get_version().encode('ascii') + b'\n'
if django.VERSION < (1, 8):
prefix = django_version
else:
prefix = b''
assert manage('testcmd', '--version') == prefix + django_version
assert manage('versioncmd', '--version') == prefix + b'20.0\n'

1 change: 1 addition & 0 deletions djclick/test/testdir/testapp2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FLAG = 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import djclick as click


@click.command()
@click.pass_verbosity
def command(verbosity):
assert isinstance(verbosity, int)
click.echo(verbosity, nl=False)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import djclick as click


@click.command()
@click.pass_context
def command(ctx):
assert isinstance(ctx.verbosity, int)
click.echo(ctx.verbosity, nl=False)
8 changes: 8 additions & 0 deletions djclick/test/testprj/testapp/management/commands/errcmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.core.management import CommandError

import djclick as click


@click.command(version='20.0')
def command():
raise CommandError('Raised error description')
8 changes: 8 additions & 0 deletions djclick/test/testprj/testapp/management/commands/pathcmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import djclick as click

import testapp2


@click.command()
def command():
click.echo(testapp2.FLAG, nl=False)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import djclick as click


@click.command(version='20.0')
def command():
raise RuntimeError() # NOCOV
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[tox]
# Having the .tox directory in the project directory slows down the
# `pip install -e .` step required by `usedevelop = true` considerably.
# By moving it out of the way, we trim test execution time by 80%.
# By moving it out of the way (~500MB), we trim test execution time by > 80%.
toxworkdir = {homedir}/.toxenvs/django-click
envlist =
coverage_erase,
{py27,py34,pypy19}-{dj15,dj16,dj17,dj18},
{py27,pypy19}-dj14,
flake8
flake8,
coverage_report

[testenv]
Expand Down

0 comments on commit 9571071

Please sign in to comment.