From 957107134ca128d033e64e066c722fdbcf0d4585 Mon Sep 17 00:00:00 2001 From: Jonathan Stoppani Date: Mon, 14 Sep 2015 14:04:57 +0200 Subject: [PATCH] Implement all missing tests --- .gitignore | 1 + HISTORY.rst | 7 ++ djclick/__init__.py | 3 +- djclick/test/conftest.py | 2 +- djclick/test/test_adapter.py | 92 ++++++++++++++++--- djclick/test/testdir/testapp2.py | 1 + .../commands/argverbositycommand.py | 8 ++ .../management/commands/ctxverbositycmd.py | 8 ++ .../testapp/management/commands/errcmd.py | 8 ++ .../testapp/management/commands/pathcmd.py | 8 ++ .../testapp/management/commands/versioncmd.py | 6 ++ tox.ini | 4 +- 12 files changed, 129 insertions(+), 19 deletions(-) create mode 100644 djclick/test/testdir/testapp2.py create mode 100644 djclick/test/testprj/testapp/management/commands/argverbositycommand.py create mode 100644 djclick/test/testprj/testapp/management/commands/ctxverbositycmd.py create mode 100644 djclick/test/testprj/testapp/management/commands/errcmd.py create mode 100644 djclick/test/testprj/testapp/management/commands/pathcmd.py create mode 100644 djclick/test/testprj/testapp/management/commands/versioncmd.py diff --git a/.gitignore b/.gitignore index 8d980ac..8d82a49 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ __pycache__ /.cache /.coverage /.tox +/.virtualenv /build /dist /docs/_build diff --git a/HISTORY.rst b/HISTORY.rst index 2515cb1..cb9e9f5 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -2,6 +2,13 @@ History ======= +0.2.0 – Unreleased +================== + +* Added a ``pass_verbosity`` decorator +* Improved test suite + + 0.1.1 – 2015-09-11 ================== diff --git a/djclick/__init__.py b/djclick/__init__.py index f8900e7..6112513 100644 --- a/djclick/__init__.py +++ b/djclick/__init__.py @@ -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'] diff --git a/djclick/test/conftest.py b/djclick/test/conftest.py index 8d5a304..eb90100 100644 --- a/djclick/test/conftest.py +++ b/djclick/test/conftest.py @@ -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 diff --git a/djclick/test/test_adapter.py b/djclick/test/test_adapter.py index 06d858a..4edaafe 100644 --- a/djclick/test/test_adapter.py +++ b/djclick/test/test_adapter.py @@ -1,5 +1,7 @@ +import os import locale import codecs +import subprocess import six @@ -7,6 +9,7 @@ import click +import django from django.core.management import get_commands, call_command from django.core.management import execute_from_command_line @@ -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' @@ -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' @@ -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' + diff --git a/djclick/test/testdir/testapp2.py b/djclick/test/testdir/testapp2.py new file mode 100644 index 0000000..80e535c --- /dev/null +++ b/djclick/test/testdir/testapp2.py @@ -0,0 +1 @@ +FLAG = 1 diff --git a/djclick/test/testprj/testapp/management/commands/argverbositycommand.py b/djclick/test/testprj/testapp/management/commands/argverbositycommand.py new file mode 100644 index 0000000..0a022aa --- /dev/null +++ b/djclick/test/testprj/testapp/management/commands/argverbositycommand.py @@ -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) diff --git a/djclick/test/testprj/testapp/management/commands/ctxverbositycmd.py b/djclick/test/testprj/testapp/management/commands/ctxverbositycmd.py new file mode 100644 index 0000000..d70be25 --- /dev/null +++ b/djclick/test/testprj/testapp/management/commands/ctxverbositycmd.py @@ -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) diff --git a/djclick/test/testprj/testapp/management/commands/errcmd.py b/djclick/test/testprj/testapp/management/commands/errcmd.py new file mode 100644 index 0000000..ee0bd6a --- /dev/null +++ b/djclick/test/testprj/testapp/management/commands/errcmd.py @@ -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') diff --git a/djclick/test/testprj/testapp/management/commands/pathcmd.py b/djclick/test/testprj/testapp/management/commands/pathcmd.py new file mode 100644 index 0000000..44cadb2 --- /dev/null +++ b/djclick/test/testprj/testapp/management/commands/pathcmd.py @@ -0,0 +1,8 @@ +import djclick as click + +import testapp2 + + +@click.command() +def command(): + click.echo(testapp2.FLAG, nl=False) diff --git a/djclick/test/testprj/testapp/management/commands/versioncmd.py b/djclick/test/testprj/testapp/management/commands/versioncmd.py new file mode 100644 index 0000000..9d371b9 --- /dev/null +++ b/djclick/test/testprj/testapp/management/commands/versioncmd.py @@ -0,0 +1,6 @@ +import djclick as click + + +@click.command(version='20.0') +def command(): + raise RuntimeError() # NOCOV diff --git a/tox.ini b/tox.ini index ec29df3..5aea55a 100644 --- a/tox.ini +++ b/tox.ini @@ -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]